1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Talk\Service;
use OCA\Talk\Exceptions\CannotReachRemoteException;
use OCA\Talk\Model\Message;
use OCA\Talk\Model\ProxyCacheMessage;
use OCA\Talk\Model\ProxyCacheMessageMapper;
use OCA\Talk\Participant;
use OCA\Talk\ResponseDefinitions;
use OCA\Talk\Room;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Http;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\DB\Exception as DBException;
use Psr\Log\LoggerInterface;
/**
* @psalm-import-type TalkChatMessageWithParent from ResponseDefinitions
*/
class ProxyCacheMessageService {
public function __construct(
protected ProxyCacheMessageMapper $mapper,
protected LoggerInterface $logger,
protected ITimeFactory $timeFactory,
) {
}
/**
* @throws DoesNotExistException
*/
public function findByRemote(string $remoteServerUrl, string $remoteToken, int $remoteMessageId): ProxyCacheMessage {
return $this->mapper->findByRemote($remoteServerUrl, $remoteToken, $remoteMessageId);
}
public function delete(ProxyCacheMessage $message): ProxyCacheMessage {
return $this->mapper->delete($message);
}
public function deleteExpiredMessages(): void {
$this->mapper->deleteExpiredMessages($this->timeFactory->getDateTime());
}
/**
* @throws \InvalidArgumentException
* @throws CannotReachRemoteException
*/
public function syncRemoteMessage(Room $room, Participant $participant, int $messageId): ProxyCacheMessage {
if (!$room->isFederatedConversation()) {
throw new \InvalidArgumentException('room');
}
/** @var \OCA\Talk\Federation\Proxy\TalkV1\Controller\ChatController $proxy */
$proxy = \OCP\Server::get(\OCA\Talk\Federation\Proxy\TalkV1\Controller\ChatController::class);
$ocsResponse = $proxy->getMessageContext($room, $participant, $messageId, 1);
if ($ocsResponse->getStatus() !== Http::STATUS_OK || !isset($ocsResponse->getData()[0])) {
throw new \InvalidArgumentException('message');
}
/** @var TalkChatMessageWithParent $messageData */
$messageData = $ocsResponse->getData()[0];
try {
$proxy = $this->mapper->findByRemote($room->getRemoteServer(), $room->getRemoteToken(), $messageId);
} catch (DoesNotExistException) {
$proxy = new ProxyCacheMessage();
}
$proxy->setLocalToken($room->getToken());
$proxy->setRemoteServerUrl($room->getRemoteServer());
$proxy->setRemoteToken($room->getRemoteToken());
$proxy->setRemoteMessageId($messageData['id']);
$proxy->setActorType($messageData['actorType']);
$proxy->setActorId($messageData['actorId']);
$proxy->setActorDisplayName($messageData['actorDisplayName']);
$proxy->setMessageType($messageData['messageType']);
$proxy->setSystemMessage($messageData['systemMessage']);
if ($messageData['expirationTimestamp']) {
$proxy->setExpirationDatetime(new \DateTime('@' . $messageData['expirationTimestamp']));
}
$proxy->setCreationDatetime(new \DateTime('@' . $messageData['timestamp']));
$proxy->setMessage($messageData['message']);
$proxy->setMessageParameters(json_encode($messageData['messageParameters']));
$metaData = [];
if (!empty($messageData['lastEditActorType']) && !empty($messageData['lastEditActorId'])) {
$metaData[Message::METADATA_LAST_EDITED_BY_TYPE] = $messageData['lastEditActorType'];
$metaData[Message::METADATA_LAST_EDITED_BY_ID] = $messageData['lastEditActorId'];
}
if (!empty($messageData['lastEditTimestamp'])) {
$metaData[Message::METADATA_LAST_EDITED_TIME] = $messageData['lastEditTimestamp'];
}
if (!empty($messageData['silent'])) {
$metaData[Message::METADATA_SILENT] = $messageData['silent'];
}
$proxy->setMetaData(json_encode($metaData));
if ($proxy->getId() !== null) {
$this->mapper->update($proxy);
return $proxy;
}
try {
$this->mapper->insert($proxy);
} catch (DBException $e) {
// DBException::REASON_UNIQUE_CONSTRAINT_VIOLATION happens when
// multiple users are in the same conversation. We are therefore
// informed multiple times about the same remote message.
if ($e->getReason() !== DBException::REASON_UNIQUE_CONSTRAINT_VIOLATION) {
$this->logger->error('Error saving proxy cache message failed: ' . $e->getMessage(), ['exception' => $e]);
throw $e;
}
$proxy = $this->mapper->findByRemote(
$room->getRemoteServer(),
$room->getRemoteToken(),
$messageData['id'],
);
}
return $proxy;
}
}
|