From 8856cfcd8e5c0a5a43706b9852774348105a9c73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20D=C3=BCsterhus?= Date: Sun, 1 Nov 2020 12:53:26 +0100 Subject: [PATCH] Add MessageAction::pushAttachment() --- .../lib/data/message/MessageAction.class.php | 52 ++++++++++++++++++- 1 file changed, 50 insertions(+), 2 deletions(-) diff --git a/files/lib/data/message/MessageAction.class.php b/files/lib/data/message/MessageAction.class.php index 156c40a..d5e9b93 100644 --- a/files/lib/data/message/MessageAction.class.php +++ b/files/lib/data/message/MessageAction.class.php @@ -1,11 +1,11 @@ validate($this->parameters['parameters'], $room); $processor->execute($this->parameters['parameters'], $room); } + + /** + * Validates parameters and permissions. + */ + public function validatePushAttachment() { + $this->readInteger('roomID'); + + $room = RoomCache::getInstance()->getRoom($this->parameters['roomID']); + if ($room === null) throw new UserInputException('roomID'); + if (!$room->canSee($user = null, $reason)) throw $reason; + $user = new \chat\data\user\User(WCF::getUser()); + if (!$user->isInRoom($room)) throw new PermissionDeniedException(); + + $this->readString('tmpHash'); + } + + /** + * Pushes a new attachment into the given room. + */ + public function pushAttachment() { + $objectTypeID = ObjectTypeCache::getInstance()->getObjectTypeIDByName('be.bastelstu.chat.messageType', 'be.bastelstu.chat.messageType.attachment'); + if (!$objectTypeID) throw new \LogicException('Missing object type'); + + $room = RoomCache::getInstance()->getRoom($this->parameters['roomID']); + if ($room === null) throw new UserInputException('roomID'); + + $attachmentHandler = new AttachmentHandler('be.bastelstu.chat.message', 0, $this->parameters['tmpHash'], $room->roomID); + $attachments = $attachmentHandler->getAttachmentList(); + $attachmentIDs = []; + foreach ($attachments as $attachment) { + $attachmentIDs[] = $attachment->attachmentID; + } + + /** @var Message $message */ + $message = (new MessageAction([ ], 'create', [ 'data' => [ 'roomID' => $room->roomID + , 'userID' => WCF::getUser()->userID + , 'username' => WCF::getUser()->username + , 'time' => TIME_NOW + , 'objectTypeID' => $objectTypeID + , 'payload' => serialize([ 'attachmentIDs' => $attachmentIDs ]) + ] + ] + ) + )->executeAction()['returnValues']; + + $attachmentHandler->updateObjectID($message->messageID); + } }