Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
FavoriteService.php
1<?php
2
4
7use Bitrix\Im\V2\Common\ContextCustomer;
14
16{
17 use ContextCustomer;
18
19 public const ADD_FAVORITE_MESSAGE_EVENT = 'messageFavoriteAdd';
20 public const DELETE_FAVORITE_MESSAGE_EVENT = 'messageFavoriteDelete';
21
22 public function getCount(int $chatId): int
23 {
24 $filter = Query::filter()
25 ->where('CHAT_ID', $chatId)
26 ->where('AUTHOR_ID', $this->getContext()->getUserId())
27 ;
28
29 return LinkFavoriteTable::getCount($filter);
30 }
31
32 public function markMessageAsFavorite(Message $message): Result
33 {
34 $result = new Result();
35
36 $favoriteMessage = FavoriteItem::createFromMessage($message, $this->getContext());
37 $saveResult = $this->saveFavoriteMessage($favoriteMessage);
38
39 if (!$saveResult->isSuccess())
40 {
41 return $result->addErrors($saveResult->getErrors());
42 }
43
44 $this->saveInParam($message);
45
46 $pushRecipient = ['RECIPIENT' => [$this->getContext()->getUserId()]];
47
49 ->setContext($this->context)
50 ->sendFull($favoriteMessage, static::ADD_FAVORITE_MESSAGE_EVENT, $pushRecipient)
51 ;
52
53 return $result;
54 }
55
56 public function unmarkMessageAsFavorite(Message $message): Result
57 {
58 $result = new Result();
59
60 $favoriteMessage = FavoriteItem::getByMessageAndUserId($message, $this->getContext()->getUserId());
61
62 if ($favoriteMessage === null)
63 {
64 return $result;
65 }
66
67 $deleteResult = $favoriteMessage->delete();
68
69 if (!$deleteResult->isSuccess())
70 {
71 return $result->addErrors($deleteResult->getErrors());
72 }
73
74 $deleteParamResult = $this->deleteFromParam($message);
75
76 if (!$deleteParamResult->isSuccess())
77 {
78 return $result->addErrors($deleteParamResult->getErrors());
79 }
80
81 $pushRecipient = ['RECIPIENT' => [$this->getContext()->getUserId()]];
82
84 ->setContext($this->context)
85 ->sendIdOnly($favoriteMessage, static::DELETE_FAVORITE_MESSAGE_EVENT, $pushRecipient)
86 ;
87
88 return $result;
89 }
90
91 public function unmarkMessageAsFavoriteForAll(Message $message): Result
92 {
93 $result = new Result();
94
95 $favoriteMessages = FavoriteCollection::getByMessage($message);
96
97 if ($favoriteMessages === null || $favoriteMessages->count() === 0)
98 {
99 return $result;
100 }
101
102 $deleteResult = $favoriteMessages->delete();
103
104 if (!$deleteResult->isSuccess())
105 {
106 return $result->addErrors($deleteResult->getErrors());
107 }
108
110 foreach ($favoriteMessages as $favoriteMessage)
111 {
112 $pushRecipient = ['RECIPIENT' => [$favoriteMessage->getAuthorId()]];
114 ->setContext($this->context)
115 ->sendIdOnly($favoriteMessage,static::DELETE_FAVORITE_MESSAGE_EVENT, $pushRecipient)
116 ;
117 }
118
119 return $result;
120 }
121
122 protected function saveFavoriteMessage(FavoriteItem $favoriteMessage): Result
123 {
124 try
125 {
126 return $favoriteMessage->save();
127 }
128 catch (\Bitrix\Main\SystemException $exception)
129 {
130 return (new Result())->addError(new MessageError(MessageError::MESSAGE_IS_ALREADY_FAVORITE));
131 }
132 }
133
134 protected function saveInParam(Message $message): Result
135 {
136 $favoriteListParam = $message->getParams()->get(Params::FAVORITE)->getValue() ?: [];
137 $userId = $this->getContext()->getUserId();
138 if (!in_array($userId, $favoriteListParam, true))
139 {
140 $message->getParams()->get(Params::FAVORITE)->addValue($userId);
141 $message->getParams()->save();
142 \CIMMessageParam::SendPull($message->getMessageId(), [Params::FAVORITE]);
143 }
144
145 return new Result();
146 }
147
148 protected function deleteFromParam(Message $message): Result
149 {
150 $favoriteListParam = $message->getParams()->get(Params::FAVORITE)->getValue() ?: [];
151 $userId = $this->getContext()->getUserId();
152 if (in_array($userId, $favoriteListParam, true))
153 {
154 $message->getParams()->get(Params::FAVORITE)->unsetValue($userId);
155 $message->getParams()->save();
156 \CIMMessageParam::SendPull($message->getMessageId(), [Params::FAVORITE]);
157 }
158
159 return new Result();
160 }
161}
getParams(bool $disallowLazyLoad=false)
Definition Message.php:313