1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
WorkflowCommentService.php
См. документацию.
1<?php
2
3namespace Bitrix\Bizproc\Api\Service;
4
5use Bitrix\Bizproc\Api\Request\WorkflowCommentService\CommentRequest;
6use Bitrix\Bizproc\Api\Request\WorkflowCommentService\MarkAsReadRequest;
7use Bitrix\Bizproc\Api\Request\WorkflowCommentService\AddSystemCommentRequest;
8use Bitrix\Bizproc\Api\Response\WorkflowCommentService\AddSystemCommentResponse;
9use Bitrix\Bizproc\Integration\Push\CommentPush;
10use Bitrix\Bizproc\Integration\Push\Dto\UserCounter;
11use Bitrix\Bizproc\Workflow\Entity\WorkflowStateTable;
12use Bitrix\Bizproc\Workflow\Entity\WorkflowUserTable;
13use Bitrix\Bizproc\Workflow\Entity\WorkflowUserCommentTable;
14use Bitrix\Bizproc\Workflow\WorkflowUserCounters;
15use Bitrix\Main\Error;
16use Bitrix\Main\Loader;
17use Bitrix\Forum;
18
20{
22 {
23 if (!$this->isCorrectRequest($comment))
24 {
25 return;
26 }
27
28 $toIncrement = $this->getRecipientsByComment($comment);
29 if ($toIncrement)
30 {
31 WorkflowUserCommentTable::incrementUnreadCounter($comment->workflowId, $toIncrement);
32 $this->incrementUsersCounters($toIncrement);
33 $this->pushCounters($comment->workflowId, $toIncrement);
34
35 $documentId = \CBPStateService::getStateDocumentId($comment->workflowId); //TODO using events mechanism
36 $documentService = \CBPRuntime::getRuntime()->getDocumentService();
37 $documentService->onWorkflowCommentAdded($documentId, $comment->workflowId, $comment->authorId);
38 }
39 }
40
42 {
43 if (!$this->isCorrectRequest($comment))
44 {
45 return;
46 }
47
48 $userIds = WorkflowUserCommentTable::decrementUnreadCounterByDate($comment->workflowId, $comment->created);
49 $this->decrementUsersCounters($userIds);
50 $this->pushCounters($comment->workflowId, $userIds);
51
52 $documentId = \CBPStateService::getStateDocumentId($comment->workflowId); //TODO using events mechanism
53 $documentService = \CBPRuntime::getRuntime()->getDocumentService();
54 $documentService->onWorkflowCommentDeleted($documentId, $comment->workflowId, $comment->authorId);
55 }
56
57 public function markAsRead(MarkAsReadRequest $markRead): void
58 {
59 $filter = [
60 '=WORKFLOW_ID' => $markRead->workflowId,
61 '=USER_ID' => $markRead->userId,
62 ];
63
64 $hasUnread = (bool)WorkflowUserCommentTable::query()->setFilter($filter)->fetch();
65
66 if ($hasUnread)
67 {
68 $documentId = \CBPStateService::getStateDocumentId($markRead->workflowId); //TODO using events mechanism
69 $documentService = \CBPRuntime::getRuntime()->getDocumentService();
70 $documentService->onWorkflowAllCommentViewed($documentId, $markRead->workflowId, $markRead->userId);
71
72 WorkflowUserCommentTable::delete([
73 'WORKFLOW_ID' => $markRead->workflowId,
74 'USER_ID' => $markRead->userId,
75 ]);
76 $this->updateUserCounters($markRead->userId);
77 $this->pushCounters($markRead->workflowId, [$markRead->userId]);
78 }
79 }
80
82 {
84 if (!Loader::includeModule('forum'))
85 {
86 $response->addError(new Error('no forum here')); // TODO
87
88 return $response;
89 }
90
91 $workflowIdInt = \CBPStateService::getWorkflowIntegerId($comment->workflowId);
92
93 $feed = new Forum\Comments\Feed(
94 \CBPHelper::getForumId(),
95 [
96 'type' => 'WF',
97 'id' => $workflowIdInt,
98 'xml_id' => 'WF_' . $comment->workflowId,
99 ],
100 $comment->authorId,
101 );
102
103 if (!$feed->addServiceComment([
104 'POST_MESSAGE' => $comment->message,
105 ]))
106 {
107 $response->addErrors($feed->getErrors());
108 }
109 else
110 {
111 WorkflowUserCommentTable::incrementUnreadCounter(
112 $comment->workflowId,
113 [$comment->authorId],
114 WorkflowUserCommentTable::COMMENT_TYPE_SYSTEM
115 );
116 $this->incrementUsersCounters([$comment->authorId]);
117 $this->pushCounters($comment->workflowId, [$comment->authorId]);
118 }
119
120 return $response;
121 }
122
123 private function getRecipientsByComment(CommentRequest $comment): array
124 {
125 $workflowUsers = $this->getWorkflowUsers($comment->workflowId);
126 unset($workflowUsers[$comment->authorId]);
127
128 if (!$workflowUsers)
129 {
130 return [];
131 }
132
133 $mentions = array_map(static fn($userId) => (int)$userId, $comment->mentionUserIds);
134 $directly = array_intersect($mentions, array_keys($workflowUsers));
135
136 if ($directly)
137 {
138 return $directly;
139 }
140
141 //send to active users
142 $activeUsers = array_filter(
143 $workflowUsers,
144 static fn ($user) => $user['TASK_STATUS'] === WorkflowUserTable::TASK_STATUS_ACTIVE,
145 );
146
147 if ($activeUsers)
148 {
149 return array_keys($activeUsers);
150 }
151
152 $author = array_filter(
153 $workflowUsers,
154 static fn ($user) => $user['IS_AUTHOR'] === 1,
155 );
156
157 return array_keys($author);
158 }
159
160 private function getWorkflowUsers(string $workflowId): array
161 {
162 $result = WorkflowUserTable::getList([
163 'select' => ['USER_ID', 'IS_AUTHOR', 'TASK_STATUS'],
164 'filter' => ['=WORKFLOW_ID' => $workflowId],
165 ]);
166
167 $users = [];
168
169 while ($row = $result->fetch())
170 {
171 $users[(int)$row['USER_ID']] = [
172 'IS_AUTHOR' => (int)$row['IS_AUTHOR'],
173 'TASK_STATUS' => (int)$row['TASK_STATUS'],
174 ];
175 }
176
177 return $users;
178 }
179
180 private function isCorrectRequest(CommentRequest $comment): bool
181 {
182 return (
183 $comment->workflowId
184 && $comment->authorId
185 && WorkflowStateTable::exists($comment->workflowId)
186 );
187 }
188
189 private function pushCounters(string $workflowId, array $touchUserIds): void
190 {
191 $userIds = WorkflowUserTable::getUserIdsByWorkflowId($workflowId);
192 $rows = WorkflowUserCommentTable::query()
193 ->setSelect(['USER_ID', 'UNREAD_CNT'])
194 ->where('WORKFLOW_ID', $workflowId)
195 ->fetchAll();
196
197 $values = array_combine(
198 array_column($rows, 'USER_ID'),
199 array_column($rows, 'UNREAD_CNT')
200 );
201
202 $all = 0;
203 if (Loader::includeModule('forum'))
204 {
205 $topic = \CForumTopic::getList([], ['XML_ID' => 'WF_' . $workflowId])->fetch() ?: [];
206 $all = (int)($topic['POSTS'] ?? 0);
207 }
208
209 foreach ($userIds as $userId)
210 {
211 CommentPush::pushCounter(
212 $workflowId,
213 $userId,
214 new UserCounter(
215 $all,
216 $values[$userId] ?? 0,
217 WorkflowUserCommentTable::getCountUserUnread($userId),
218 ),
219 );
220 }
221
222 WorkflowUserTable::touchWorkflowUsers($workflowId, $touchUserIds);
223 }
224
225 private function incrementUsersCounters(array $userIds): void
226 {
227 foreach ($userIds as $userId)
228 {
229 $userCounters = new WorkflowUserCounters($userId);
230 $userCounters->incrementComment();
231 }
232 }
233
234 private function decrementUsersCounters(array $userIds): void
235 {
236 foreach ($userIds as $userId)
237 {
238 $userCounters = new WorkflowUserCounters($userId);
239 $userCounters->decrementComment();
240 }
241 }
242
243 private function updateUserCounters(int $userId): void
244 {
245 $userCounters = new WorkflowUserCounters($userId);
246 $userCounters->setComment(WorkflowUserCommentTable::getCountUserUnread($userId));
247 }
248}
if(!is_object($USER)||! $USER->IsAuthorized()) $userId
Определения check_mail.php:18
addSystemComment(AddSystemCommentRequest $comment)
Определения WorkflowCommentService.php:81
unRegisterComment(CommentRequest $comment)
Определения WorkflowCommentService.php:41
markAsRead(MarkAsReadRequest $markRead)
Определения WorkflowCommentService.php:57
registerComment(CommentRequest $comment)
Определения WorkflowCommentService.php:21
Определения error.php:15
static getStateDocumentId($workflowId)
Определения stateservice.php:75
static getWorkflowIntegerId($workflowId)
Определения stateservice.php:404
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
$filter
Определения iblock_catalog_list.php:54
$user
Определения mysql_to_pgsql.php:33
$comment
Определения template.php:15
$response
Определения result.php:21
$rows
Определения options.php:264