Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
controller.php
1<?php
2
4
5use Bitrix\AI\Context;
6use Bitrix\AI\Engine;
12
13final class Controller
14{
15 private static array $listMessages = [];
16 private static int $blogAuthorId = 0;
17 private const LIMIT = 20;
18
19 public static function onContextGetMessages(Event $event): array
20 {
21 $moduleId = $event->getParameter('module');
22 $contextId = $event->getParameter('id');
23 $contextParameters = $event->getParameter('params');
24 $nextStep = $event->getParameter('next_step');
25
26 $isCommentContext = (
27 $moduleId === 'socialnetwork'
28 && str_starts_with($contextId, 'sonet_comment_')
29 );
30 if ($isCommentContext)
31 {
32 $messages = [];
33
34 $xmlId = is_string($contextParameters['xmlId'] ?? null) ? $contextParameters['xmlId'] : null;
35 if (!$xmlId)
36 {
37 return ['messages' => []];
38 }
39
40 if (isset(self::$listMessages[$xmlId]))
41 {
42 return ['messages' => self::$listMessages[$xmlId]];
43 }
44
45 if (str_starts_with($xmlId, 'BLOG_') && Loader::includeModule('blog'))
46 {
47 $postId = (int) mb_substr($xmlId, 5);
48 $postMessages = self::getPostContext($postId);
49 foreach ($postMessages as $postMessage)
50 {
51 $messages[] = ['content' => $postMessage];
52 }
53 }
54 else if (
55 str_starts_with($xmlId, 'TASK_')
56 && Loader::includeModule('tasks')
57 && Loader::includeModule('forum')
58 )
59 {
60 $postMessages = self::getTaskContext($xmlId);
61 foreach ($postMessages as $postMessage)
62 {
63 $messages[] = ['content' => $postMessage];
64 }
65 }
66
67 $messages[0] = self::modifyOriginalMessage($messages[0] ?? []);
68
69 if ($messages)
70 {
71 self::$listMessages[$xmlId] = $messages;
72 }
73
74 return ['messages' => $messages];
75 }
76
77 return ['messages' => []];
78 }
79
80 private static function getPostContext(int $postId): array
81 {
82 $messages = [];
83
84 $textParser = new \CTextParser();
85
86 $post = \CBlogPost::getByID($postId);
87 if ($post)
88 {
89 self::setBlogAuthorId((int)$post['AUTHOR_ID']);
90
91 $messages[] = $textParser->clearAllTags($post['DETAIL_TEXT']);
92
93 $comments = self::getLastComments($postId);
94
95 $messages = array_merge($messages, $comments);
96 }
97
98 return $messages;
99 }
100
101 private static function getTaskContext(string $xmlId): array
102 {
103 $taskId = (int) mb_substr($xmlId, 5);
104
105 $textParser = new \CTextParser();
106
107 $messages = [];
108
109 $task = \Bitrix\Tasks\Internals\Registry\TaskRegistry::getInstance()->getObject($taskId);
110 self::setBlogAuthorId($task->getCreatedBy());
111 $messages[] = $textParser->clearAllTags($task->getDescription());
112
113 $liveFeedEntity = \Bitrix\Socialnetwork\Livefeed\Provider::init([
114 'ENTITY_TYPE' => \Bitrix\Socialnetwork\Livefeed\Provider::DATA_ENTITY_TYPE_TASKS_TASK,
115 'ENTITY_ID' => $taskId,
116 ]);
117 if ($liveFeedEntity)
118 {
119 $logId = (int) $liveFeedEntity->getLogId();
120 if ($logId)
121 {
122 $comments = self::getForumComments($xmlId);
123
124 $messages = array_merge($messages, array_reverse($comments));
125 }
126 }
127
128 return $messages;
129 }
130
131 private static function getForumComments(string $xmlId): array
132 {
133 $textParser = new \CTextParser();
134
135 $comments = [];
136
137 $query = MessageTable::query();
138 $query
139 ->setSelect(['ID', 'POST_MESSAGE'])
140 ->where('XML_ID', $xmlId)
141 ->whereNull('SERVICE_TYPE')
142 ->whereNull('PARAM1')
143 ->setOrder(['POST_DATE' => 'desc'])
144 ->setLimit(self::LIMIT);
145
146 $postMessages = $query->exec()->fetchCollection();
147 foreach ($postMessages as $postMessage)
148 {
149 $comments[] = $textParser->clearAllTags($postMessage->getPostMessage());
150 }
151
152 return $comments;
153 }
154
155 private static function getLastComments(int $postId): array
156 {
157 $textParser = new \CTextParser();
158
159 $comments = [];
160
161 $queryCommentObject = \CBlogComment::getList(
162 ['ID' => 'DESC'],
163 [
164 'PUBLISH_STATUS' => BLOG_PUBLISH_STATUS_PUBLISH,
165 'POST_ID' => $postId,
166 '!=POST_TEXT' => \Bitrix\Socialnetwork\CommentAux\TaskInfo::POST_TEXT,
167 ],
168 false,
169 [
170 'nTopCount' => self::LIMIT
171 ],
172 ['POST_TEXT']
173 );
174 while ($commentData = $queryCommentObject->fetch())
175 {
176 $comments[] = $textParser->clearAllTags($commentData['POST_TEXT']);
177 }
178
179 return $comments;
180 }
181
182 private static function modifyOriginalMessage(array $message): array
183 {
184 $message['is_original_message'] = true;
185
186 if (self::$blogAuthorId)
187 {
188 $author = new Author(self::$blogAuthorId);
189 $message['meta'] = $author->toMeta();
190 }
191
192 return $message;
193 }
194
195 private static function setBlogAuthorId(int $userId): void
196 {
197 self::$blogAuthorId = $userId;
198 }
199}
getParameter($key)
Definition event.php:80