Apply PSR-12 code style (#145)
[GitHub/WoltLab/com.woltlab.wcf.conversation.git] / files / lib / system / attachment / ConversationMessageAttachmentObjectType.class.php
CommitLineData
ce07ae0c 1<?php
fea86294 2
ce07ae0c 3namespace wcf\system\attachment;
fea86294
TD
4
5use wcf\data\conversation\Conversation;
c5a8bd8c 6use wcf\data\conversation\message\ConversationMessage;
a0c1a541 7use wcf\data\conversation\message\ConversationMessageList;
ce07ae0c 8use wcf\system\WCF;
f7b9b1f3 9use wcf\util\ArrayUtil;
ce07ae0c
MW
10
11/**
12 * Attachment object type implementation for conversations.
fea86294
TD
13 *
14 * @author Marcel Werk
15 * @copyright 2001-2019 WoltLab GmbH
16 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
17 * @package WoltLabSuite\Core\System\Attachment
18 *
19 * @method ConversationMessage getObject($objectID)
ce07ae0c 20 */
fea86294
TD
21class ConversationMessageAttachmentObjectType extends AbstractAttachmentObjectType
22{
23 /** @noinspection PhpMissingParentCallCommonInspection */
24
25 /**
26 * @inheritDoc
27 */
28 public function getMaxSize()
29 {
30 return WCF::getSession()->getPermission('user.conversation.maxAttachmentSize');
31 }
32
33 /** @noinspection PhpMissingParentCallCommonInspection */
34
35 /**
36 * @inheritDoc
37 */
38 public function getAllowedExtensions()
39 {
40 return ArrayUtil::trim(\explode(
41 "\n",
42 WCF::getSession()->getPermission('user.conversation.allowedAttachmentExtensions')
43 ));
44 }
45
46 /** @noinspection PhpMissingParentCallCommonInspection */
47
48 /**
49 * @inheritDoc
50 */
51 public function getMaxCount()
52 {
53 return WCF::getSession()->getPermission('user.conversation.maxAttachmentCount');
54 }
55
56 /**
57 * @inheritDoc
58 */
59 public function canDownload($objectID)
60 {
61 if ($objectID) {
62 $message = new ConversationMessage($objectID);
63 $conversation = Conversation::getUserConversation($message->conversationID, WCF::getUser()->userID);
64 if ($conversation->canRead()) {
65 return true;
66 }
67 }
68
69 return false;
70 }
71
72 /**
73 * @inheritDoc
74 */
75 public function canUpload($objectID, $parentObjectID = 0)
76 {
77 if (!WCF::getSession()->getPermission('user.conversation.canUploadAttachment')) {
78 return false;
79 }
80
81 if ($objectID) {
82 $message = new ConversationMessage($objectID);
83 if ($message->userID == WCF::getUser()->userID) {
84 return true;
85 }
86
87 return false;
88 }
89
90 return true;
91 }
92
93 /**
94 * @inheritDoc
95 */
96 public function canDelete($objectID)
97 {
98 if ($objectID) {
99 $message = new ConversationMessage($objectID);
100 if ($message->userID == WCF::getUser()->userID) {
101 return true;
102 }
103 }
104
105 return false;
106 }
107
108 /**
109 * @inheritDoc
110 */
111 public function cacheObjects(array $objectIDs)
112 {
113 $messageList = new ConversationMessageList();
114 $messageList->setObjectIDs($objectIDs);
115 $messageList->readObjects();
116 $conversationIDs = [];
117 foreach ($messageList as $message) {
118 $conversationIDs[] = $message->conversationID;
119 }
120 if (!empty($conversationIDs)) {
121 $conversations = Conversation::getUserConversations($conversationIDs, WCF::getUser()->userID);
122 foreach ($messageList as $message) {
123 if (isset($conversations[$message->conversationID])) {
124 $message->setConversation($conversations[$message->conversationID]);
125 }
126 }
127 }
128
129 foreach ($messageList->getObjects() as $objectID => $object) {
130 $this->cachedObjects[$objectID] = $object;
131 }
132 }
133
134 /** @noinspection PhpMissingParentCallCommonInspection */
135
136 /**
137 * @inheritDoc
138 */
139 public function setPermissions(array $attachments)
140 {
141 $messageIDs = [];
142 foreach ($attachments as $attachment) {
143 // set default permissions
144 $attachment->setPermissions([
145 'canDownload' => false,
146 'canViewPreview' => false,
147 ]);
148
149 if ($this->getObject($attachment->objectID) === null) {
150 $messageIDs[] = $attachment->objectID;
151 }
152 }
153
154 if (!empty($messageIDs)) {
155 $this->cacheObjects($messageIDs);
156 }
157
158 foreach ($attachments as $attachment) {
159 if (($message = $this->getObject($attachment->objectID)) !== null) {
160 if (!$message->getConversation()->canRead()) {
161 continue;
162 }
163
164 $attachment->setPermissions([
165 'canDownload' => true,
166 'canViewPreview' => true,
167 ]);
168 } elseif ($attachment->tmpHash != '' && $attachment->userID == WCF::getUser()->userID) {
169 $attachment->setPermissions([
170 'canDownload' => true,
171 'canViewPreview' => true,
172 ]);
173 }
174 }
175 }
ce07ae0c 176}