24 private bool $withOwnReactions;
26 public function __construct(array $messageIds,
bool $withOwnReactions =
true)
28 parent::__construct();
29 $this->withOwnReactions = $withOwnReactions;
31 if (empty($messageIds))
36 $this->fillCounters($messageIds)->fillUsers()->fillOwnReactions();
39 public static function initFromArray(array $reactionArray,
bool $withOwnReactions =
true): self
41 $reactions =
new static([], $withOwnReactions);
43 foreach ($reactionArray as $reaction)
45 $reactions->addReactionMessage($reaction);
62 return $this[$messageId];
65 private function getMessageIds(): array
69 foreach ($this as $reactionMessage)
71 $messageIds[] = $reactionMessage->getMessageId();
77 private function getMessageIdsToFillOwnReaction(): array
81 foreach ($this as $reactionMessage)
83 if ($reactionMessage->needToFillOwnReactions())
85 $messageIds[] = $reactionMessage->getMessageId();
100 if (!isset($option[
'WITHOUT_OWN_REACTIONS']))
102 $option[
'WITHOUT_OWN_REACTIONS'] = !$this->withOwnReactions;
105 foreach ($this as $reactionMessage)
107 if ($reactionMessage->haveReactions())
109 $rest[] = $reactionMessage->toRestFormat($option);
120 foreach ($this as $reactionMessage)
122 foreach ($reactionMessage->getUserIds() as $userId)
124 $userIds[$userId] = $userId;
136 private function fillCounters(array $messageIds): self
138 $result = ReactionTable::query()
139 ->setSelect([
'MESSAGE_ID',
'REACTION',
'COUNT'])
140 ->whereIn(
'MESSAGE_ID', $messageIds)
144 foreach ($result as $row)
146 $this->
getReactionMessage((
int)$row[
'MESSAGE_ID'])->addCounter($row[
'REACTION'], (
int)$row[
'COUNT']);
152 private function fillUsers(): self
154 $messagesNeedFillUsers = [];
156 foreach ($this as $reactionMessage)
158 if ($reactionMessage->haveDisplayedUsers())
160 $messagesNeedFillUsers[] = $reactionMessage->getMessageId();
164 if (empty($messagesNeedFillUsers))
169 $derivedQuery = ReactionTable::query()
170 ->setSelect([
'MESSAGE_ID',
'REACTION'])
171 ->setGroup([
'MESSAGE_ID',
'REACTION'])
172 ->whereIn(
'MESSAGE_ID', $messagesNeedFillUsers)
175 $entity = ORM\Entity::getInstanceByQuery($derivedQuery);
177 $result = ReactionTable::query()
178 ->setSelect([
'MESSAGE_ID',
'REACTION',
'USER_ID'])
179 ->registerRuntimeField(
180 (
new ORM\Fields\Relations\Reference(
183 ORM\Query\Join::on(
'this.MESSAGE_ID',
'ref.MESSAGE_ID')
184 ->whereColumn(
'this.REACTION',
'ref.REACTION')
185 ))->configureJoinType(ORM\Query\Join::TYPE_INNER)
190 while ($row = $result->fetch())
192 $reactionMessage = $this->
getReactionMessage((
int)$row[
'MESSAGE_ID'])->addUsers($row[
'REACTION'], [(
int)$row[
'USER_ID']]);
193 if ($this->withOwnReactions && $this->getContext()->getUserId() == (
int)$row[
'USER_ID'])
195 $reactionMessage->addOwnReaction($row[
'REACTION']);
202 private function fillOwnReactions(): self
204 if (!$this->withOwnReactions)
209 $messageIds = $this->getMessageIdsToFillOwnReaction();
211 if (empty($messageIds))
216 $result = ReactionTable::query()
217 ->setSelect([
'MESSAGE_ID',
'REACTION'])
218 ->whereIn(
'MESSAGE_ID', $messageIds)
219 ->where(
'USER_ID', $this->
getContext()->getUserId())
223 foreach ($result as $row)