Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/capabilities.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,4 @@ title: Capabilities
* `chat-unread` - Whether the API to mark a conversation as unread is available
* `reactions` - Api reactions to chat message
* `rich-object-list-media` - When the API to get the chat messages for shared media is available
* `rich-object-delete` - When the API allows to delete chat messages which are file or rich object shares
2 changes: 1 addition & 1 deletion docs/chat.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ See [OCP\RichObjectStrings\Definitions](https://github.com/nextcloud/server/blob

## Deleting a chat message

* Required capability: `delete-messages`
* Required capability: `delete-messages` - `rich-object-delete` indicates if shared objects can be deleted from the chat
* Method: `DELETE`
* Endpoint: `/chat/{token}/{messageId}`

Expand Down
1 change: 1 addition & 0 deletions lib/Capabilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ public function getCapabilities(): array {
'notification-calls',
'conversation-permissions',
'rich-object-list-media',
'rich-object-delete',
],
'config' => [
'attachments' => [
Expand Down
65 changes: 57 additions & 8 deletions lib/Chat/ChatManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@
use OCP\IDBConnection;
use OCP\IUser;
use OCP\Notification\IManager as INotificationManager;
use OCP\Share\Exceptions\ShareNotFound;
use OCP\Share\IManager;
use OCP\Share\IShare;

/**
* Basic polling chat manager.
Expand Down Expand Up @@ -74,6 +77,8 @@ class ChatManager {
private $connection;
/** @var INotificationManager */
private $notificationManager;
/** @var IManager */
private $shareManager;
/** @var RoomShareProvider */
private $shareProvider;
/** @var ParticipantService */
Expand All @@ -91,6 +96,7 @@ public function __construct(CommentsManager $commentsManager,
IEventDispatcher $dispatcher,
IDBConnection $connection,
INotificationManager $notificationManager,
IManager $shareManager,
RoomShareProvider $shareProvider,
ParticipantService $participantService,
Notifier $notifier,
Expand All @@ -100,6 +106,7 @@ public function __construct(CommentsManager $commentsManager,
$this->dispatcher = $dispatcher;
$this->connection = $connection;
$this->notificationManager = $notificationManager;
$this->shareManager = $shareManager;
$this->shareProvider = $shareProvider;
$this->participantService = $participantService;
$this->notifier = $notifier;
Expand Down Expand Up @@ -287,12 +294,54 @@ public function sendMessage(Room $chat, Participant $participant, string $actorT
return $comment;
}

public function deleteMessage(Room $chat, int $messageId, string $actorType, string $actorId, \DateTime $deletionTime): IComment {
$comment = $this->getComment($chat, (string) $messageId);
/**
* @param Room $room
* @param Participant $participant
* @param array $messageData
* @throws ShareNotFound
*/
public function unshareFileOnMessageDelete(Room $room, Participant $participant, array $messageData): void {
if (!isset($messageData['message'], $messageData['parameters']['share']) || $messageData['message'] !== 'file_shared') {
// Not a file share
return;
}

$share = $this->shareManager->getShareById('ocRoomShare:' . $messageData['parameters']['share']);

if ($share->getShareType() !== IShare::TYPE_ROOM || $share->getSharedWith() !== $room->getToken()) {
// Share does not match the correct room
throw new ShareNotFound();
}

$attendee = $participant->getAttendee();

if (!$participant->hasModeratorPermissions() &&
!($attendee->getActorType() === Attendee::ACTOR_USERS && $attendee->getActorId() === $share->getShareOwner())) {
// Only moderators or the share owner can delete the share
return;
}

$this->shareManager->deleteShare($share);
}

/**
* @param Room $chat
* @param IComment $comment
* @param Participant $participant
* @param \DateTime $deletionTime
* @return IComment
* @throws ShareNotFound
*/
public function deleteMessage(Room $chat, IComment $comment, Participant $participant, \DateTime $deletionTime): IComment {
if ($comment->getVerb() === 'object_shared') {
$messageData = json_decode($comment->getMessage(), true);
$this->unshareFileOnMessageDelete($chat, $participant, $messageData);
}

$comment->setMessage(
json_encode([
'deleted_by_type' => $actorType,
'deleted_by_id' => $actorId,
'deleted_by_type' => $participant->getAttendee()->getActorType(),
'deleted_by_id' => $participant->getAttendee()->getActorId(),
'deleted_on' => $deletionTime->getTimestamp(),
])
);
Expand All @@ -301,13 +350,13 @@ public function deleteMessage(Room $chat, int $messageId, string $actorType, str

return $this->addSystemMessage(
$chat,
$actorType,
$actorId,
json_encode(['message' => 'message_deleted', 'parameters' => ['message' => $messageId]]),
$participant->getAttendee()->getActorType(),
$participant->getAttendee()->getActorId(),
json_encode(['message' => 'message_deleted', 'parameters' => ['message' => $comment->getId()]]),
$this->timeFactory->getDateTime(),
false,
null,
$messageId
(int) $comment->getId()
);
}

Expand Down
22 changes: 13 additions & 9 deletions lib/Controller/ChatController.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
use OCP\RichObjectStrings\InvalidObjectExeption;
use OCP\RichObjectStrings\IValidator;
use OCP\Security\ITrustedDomainHelper;
use OCP\Share\Exceptions\ShareNotFound;
use OCP\User\Events\UserLiveStatusEvent;
use OCP\UserStatus\IManager as IUserStatusManager;
use OCP\UserStatus\IUserStatus;
Expand Down Expand Up @@ -581,8 +582,8 @@ public function deleteMessage(int $messageId): DataResponse {
return new DataResponse([], Http::STATUS_FORBIDDEN);
}

if ($message->getVerb() !== 'comment') {
// System message or file share (since the message is not parsed, it has type "system")
if ($message->getVerb() !== 'comment' && $message->getVerb() !== 'object_shared') {
// System message (since the message is not parsed, it has type "system")
return new DataResponse([], Http::STATUS_METHOD_NOT_ALLOWED);
}

Expand All @@ -593,13 +594,16 @@ public function deleteMessage(int $messageId): DataResponse {
return new DataResponse([], Http::STATUS_BAD_REQUEST);
}

$systemMessageComment = $this->chatManager->deleteMessage(
$this->room,
$messageId,
$attendee->getActorType(),
$attendee->getActorId(),
$this->timeFactory->getDateTime()
);
try {
$systemMessageComment = $this->chatManager->deleteMessage(
$this->room,
$message,
$this->participant,
$this->timeFactory->getDateTime()
);
} catch (ShareNotFound $e) {
return new DataResponse([], Http::STATUS_NOT_FOUND);
}

$systemMessage = $this->messageParser->createMessage($this->room, $this->participant, $systemMessageComment, $this->l);
$this->messageParser->parseMessage($systemMessage);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,10 +276,10 @@ describe('MessageButtonsBar.vue', () => {
testDeleteMessageVisible(false)
})

test('hides delete action for file messages', () => {
test('show delete action for file messages', () => {
messageProps.message = '{file}'
messageProps.messageParameters.file = {}
testDeleteMessageVisible(false)
testDeleteMessageVisible(true)
})

test('hides delete action on other people messages for non-moderators', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,14 +304,9 @@ export default {
return false
}

const isObjectShare = this.message === '{object}'
&& this.messageParameters?.object

return (moment(this.timestamp * 1000).add(6, 'h')) > moment()
&& this.messageType === 'comment'
&& (this.messageType === 'comment' || this.messageType === 'voice-message')
&& !this.isDeleting
&& !this.isFileShare
&& !isObjectShare
&& (this.isMyMsg
|| (this.conversation.type !== CONVERSATION.TYPE.ONE_TO_ONE
&& (this.participant.participantType === PARTICIPANT.TYPE.OWNER
Expand Down
3 changes: 3 additions & 0 deletions tests/integration/features/bootstrap/FeatureContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -1630,6 +1630,9 @@ protected function compareDataResponse(TableNode $formData = null) {
// replies; this is needed to get special messages not explicitly
// sent like those for shared files.
self::$messages[$message['message']] = $message['id'];
if ($message['message'] === '{file}' && isset($message['messageParameters']['file']['name'])) {
self::$messages['shared::file::' . $message['messageParameters']['file']['name']] = $message['id'];
}
}

if ($formData === null) {
Expand Down
25 changes: 25 additions & 0 deletions tests/integration/features/chat/file-share.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Feature: chat/public
Background:
Given user "participant1" exists

Scenario: Share a file to a chat
Given user "participant1" creates room "public room" (v4)
| roomType | 3 |
| roomName | room |
When user "participant1" shares "welcome.txt" with room "public room"
Then user "participant1" sees the following messages in room "public room" with 200
| room | actorType | actorId | actorDisplayName | message | messageParameters |
| public room | users | participant1 | participant1-displayname | {file} | "IGNORE" |

Scenario: Delete share a file message from a chat
Given user "participant1" creates room "public room" (v4)
| roomType | 3 |
| roomName | room |
When user "participant1" shares "welcome.txt" with room "public room"
Then user "participant1" sees the following messages in room "public room" with 200
| room | actorType | actorId | actorDisplayName | message | messageParameters |
| public room | users | participant1 | participant1-displayname | {file} | "IGNORE" |
And user "participant1" deletes message "shared::file::welcome.txt" from room "public room" with 200
Then user "participant1" sees the following messages in room "public room" with 200
| room | actorType | actorId | actorDisplayName | message | messageParameters |
| public room | users | participant1 | participant1-displayname | Message deleted by you | {"actor":{"type":"user","id":"participant1","name":"participant1-displayname"}} |
10 changes: 10 additions & 0 deletions tests/integration/features/chat/rich-object-share.feature
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ Feature: chat/public
| room | actorType | actorId | actorDisplayName | message | messageParameters |
| public room | users | participant1 | participant1-displayname | {object} | {"actor":{"type":"user","id":"participant1","name":"participant1-displayname"},"object":{"name":"Another room","call-type":"group","type":"call","id":"R4nd0mT0k3n"}} |

Scenario: Delete a rich object from a chat
Given user "participant1" creates room "public room" (v4)
| roomType | 3 |
| roomName | room |
When user "participant1" shares rich-object "call" "R4nd0mT0k3n" '{"name":"Another room","call-type":"group"}' to room "public room" with 201 (v1)
And user "participant1" deletes message "shared::call::R4nd0mT0k3n" from room "public room" with 200
Then user "participant1" sees the following messages in room "public room" with 200
| room | actorType | actorId | actorDisplayName | message | messageParameters | parentMessage |
| public room | users | participant1 | participant1-displayname | Message deleted by you | {"actor":{"type":"user","id":"participant1","name":"participant1-displayname"}} | |

Scenario: Share an invalid rich object to a chat
Given user "participant1" creates room "public room" (v4)
| roomType | 3 |
Expand Down
1 change: 1 addition & 0 deletions tests/php/CapabilitiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ public function setUp(): void {
'notification-calls',
'conversation-permissions',
'rich-object-list-media',
'rich-object-delete',
'reactions',
];
}
Expand Down
Loading