25 $context->setUser($userId);
26 $this->setContext($context);
32 $insertFields = $this->prepareInsertFields($messages);
33 MessageViewedTable::multiplyInsertWithoutDuplicate($insertFields, [
'DEADLOCK_SAFE' =>
true]);
39 $includeBound =
false;
40 if ($lowerBound ===
null)
42 $lowerBound = $message->
getChat()->getStartId($this->getContext()->getUserId());
46 $messageIds = $this->getLastMessageIdsBetween($message, $lowerBound, $includeBound);
48 $userId = $this->getContext()->getUserId();
52 foreach ($messageIds as $messageId)
57 'MESSAGE_ID' => $messageId,
58 'DATE_CREATE' => $dateViewed,
62 MessageViewedTable::multiplyInsertWithoutDuplicate($insertFields, [
'DEADLOCK_SAFE' =>
true]);
64 return (
new Result())->setResult([
'VIEWED_MESSAGES' => $messageIds]);
69 $result = MessageViewedTable::query()
70 ->setSelect([
'LAST_VIEWED' =>
new ExpressionField(
'LAST_VIEWED',
'MAX(%s)', [
'MESSAGE_ID'])])
71 ->where(
'CHAT_ID', $chatId)
72 ->where(
'USER_ID', $this->getContext()->getUserId())
76 return ($result && isset($result[
'LAST_VIEWED'])) ? (int)$result[
'LAST_VIEWED'] :
null;
81 $result = MessageViewedTable::query()
82 ->setSelect([
'DATE_CREATE'])
83 ->where(
'USER_ID', $this->getContext()->getUserId())
84 ->where(
'MESSAGE_ID', $messageId)
88 return $result ? $result[
'DATE_CREATE'] :
null;
98 $result = MessageViewedTable::query()
99 ->setSelect([
'DATE_CREATE',
'USER_ID'])
100 ->whereIn(
'USER_ID', $userIds)
101 ->where(
'MESSAGE_ID', $messageId)
105 $dateViewedByUsers = [];
107 foreach ($result as $row)
109 $dateViewedByUsers[(int)$row[
'USER_ID']] = $row[
'DATE_CREATE'];
112 return $dateViewedByUsers;
117 $isMessageRead = MessageViewedTable::query()
118 ->setSelect([
'MESSAGE_ID'])
119 ->where(
'MESSAGE_ID', $messageId)
124 return $isMessageRead ? \IM_MESSAGE_STATUS_DELIVERED : \IM_MESSAGE_STATUS_RECEIVED;
129 $query = MessageViewedTable::query()
130 ->setSelect([
'USER_ID'])
131 ->where(
'MESSAGE_ID', $messageId)
132 ->setOrder([
'ID' =>
'ASC'])
137 $query->setLimit($limit);
141 $query->setOffset($offset);
144 $viewedMessages = $query->fetchCollection();
147 foreach ($viewedMessages as $viewedMessage)
149 $userId = $viewedMessage->getUserId();
150 $viewersIds[$userId] = $userId;
158 if (empty($messageIds))
163 $viewedMessageResult = MessageViewedTable::query()
164 ->setSelect([
'MESSAGE_ID'])
166 ->whereIn(
'MESSAGE_ID', $messageIds)
170 $deliveredMessages = [];
172 while ($row = $viewedMessageResult->fetch())
174 $deliveredMessages[(int)$row[
'MESSAGE_ID']] = \IM_MESSAGE_STATUS_DELIVERED;
177 $messageStatuses = [];
179 foreach ($messageIds as $messageId)
181 $messageStatuses[$messageId] = $deliveredMessages[$messageId] ?? \IM_MESSAGE_STATUS_RECEIVED;
184 return $messageStatuses;
189 $userId = $this->getContext()->getUserId();
190 MessageViewedTable::deleteByFilter([
'>=MESSAGE_ID' => $message->
getMessageId(),
'=CHAT_ID' => $message->
getChatId(),
'=USER_ID' => $userId]);
195 if (empty($messageIds))
200 MessageViewedTable::deleteByFilter([
'=MESSAGE_ID' => $messageIds,
'=CHAT_ID' => $chatId,
'=USER_ID' => $this->getContext()->getUserId()]);
205 MessageViewedTable::deleteByFilter([
'=MESSAGE_ID' => $messageId]);
210 MessageViewedTable::deleteByFilter([
'=MESSAGE_ID' => $messageIds]);
215 MessageViewedTable::deleteByFilter([
'=CHAT_ID' => $chatId,
'=USER_ID' => $this->getContext()->getUserId()]);
221 $userId = $this->getContext()->getUserId();
224 foreach ($messages as $message)
226 if ($message->getAuthorId() === $userId && $message->getChat()->getType() !== \IM_MESSAGE_SYSTEM)
231 'USER_ID' => $userId,
232 'CHAT_ID' => $message->getChatId(),
233 'MESSAGE_ID' => $message->getMessageId(),
234 'DATE_CREATE' => $dateCreate,
238 return $insertFields;
241 private function getLastMessageIdsBetween(Message $message,
int $lowerBound,
bool $includeBound): array
243 $operator = $includeBound ?
'>=' :
'>';
245 $query = MessageTable::query()
247 ->where(
'CHAT_ID', $message->getChatId())
248 ->where(
'ID',
'<=', $message->getMessageId())
249 ->where(
'ID', $operator, $lowerBound)
250 ->setOrder([
'DATE_CREATE' =>
'DESC',
'ID' =>
'DESC'])
253 if ($message->getChat()->getType() !== \IM_MESSAGE_SYSTEM)
255 $query->whereNot(
'AUTHOR_ID', $this->getContext()->getUserId());
258 return $query->fetchCollection()->getIdList();