88
99namespace OCA \Talk \Chat \Parser ;
1010
11+ use OCA \Circles \CirclesManager ;
1112use OCA \Talk \Chat \ChatManager ;
1213use OCA \Talk \Events \MessageParseEvent ;
1314use OCA \Talk \Exceptions \ParticipantNotFoundException ;
1718use OCA \Talk \Room ;
1819use OCA \Talk \Service \AvatarService ;
1920use OCA \Talk \Service \ParticipantService ;
21+ use OCP \App \IAppManager ;
2022use OCP \Comments \ICommentsManager ;
2123use OCP \EventDispatcher \Event ;
2224use OCP \EventDispatcher \IEventListener ;
2527use OCP \IGroupManager ;
2628use OCP \IL10N ;
2729use OCP \IUserManager ;
30+ use OCP \Server ;
2831
2932/**
3033 * Helper class to get a rich message from a plain text message.
3134 * @template-implements IEventListener<Event>
3235 */
3336class UserMention implements IEventListener {
37+ /** @var array<string, string> */
38+ protected array $ circleNames = [];
39+ /** @var array<string, string> */
40+ protected array $ circleLinks = [];
3441
3542 public function __construct (
43+ protected IAppManager $ appManager ,
3644 protected ICommentsManager $ commentsManager ,
3745 protected IUserManager $ userManager ,
3846 protected IGroupManager $ groupManager ,
@@ -117,7 +125,7 @@ protected function parseMessage(Message $chatMessage): void {
117125 $ mention ['type ' ] === 'email ' ||
118126 $ mention ['type ' ] === 'group ' ||
119127 // $mention['type'] === 'federated_group' ||
120- // $mention['type'] === 'team' ||
128+ $ mention ['type ' ] === 'team ' ||
121129 // $mention['type'] === 'federated_team' ||
122130 $ mention ['type ' ] === 'federated_user ' ) {
123131 $ search = $ mention ['type ' ] . '/ ' . $ mention ['id ' ];
@@ -135,7 +143,7 @@ protected function parseMessage(Message $chatMessage): void {
135143 && !str_starts_with ($ search , 'email/ ' )
136144 && !str_starts_with ($ search , 'group/ ' )
137145 // && !str_starts_with($search, 'federated_group/')
138- // && !str_starts_with($search, 'team/')
146+ && !str_starts_with ($ search , 'team/ ' )
139147 // && !str_starts_with($search, 'federated_team/')
140148 && !str_starts_with ($ search , 'federated_user/ ' )) {
141149 $ message = str_replace ('@ ' . $ search , '{ ' . $ mentionParameterId . '} ' , $ message );
@@ -213,6 +221,8 @@ protected function parseMessage(Message $chatMessage): void {
213221 'id ' => $ mention ['id ' ],
214222 'name ' => $ displayName ,
215223 ];
224+ } elseif ($ mention ['type ' ] === 'team ' ) {
225+ $ messageParameters [$ mentionParameterId ] = $ this ->getCircle ($ mention ['id ' ]);
216226 } else {
217227 try {
218228 $ displayName = $ this ->commentsManager ->resolveDisplayName ($ mention ['type ' ], $ mention ['id ' ]);
@@ -256,4 +266,47 @@ protected function getRoomType(Room $room): string {
256266 throw new \InvalidArgumentException ('Unknown room type ' );
257267 }
258268 }
269+
270+ protected function getCircle (string $ circleId ): array {
271+ if (!$ this ->appManager ->isEnabledForUser ('circles ' )) {
272+ return [
273+ 'type ' => 'highlight ' ,
274+ 'id ' => $ circleId ,
275+ 'name ' => $ circleId ,
276+ ];
277+ }
278+
279+ if (!isset ($ this ->circleNames [$ circleId ])) {
280+ $ this ->loadCircleDetails ($ circleId );
281+ }
282+
283+ if (!isset ($ this ->circleNames [$ circleId ])) {
284+ return [
285+ 'type ' => 'highlight ' ,
286+ 'id ' => $ circleId ,
287+ 'name ' => $ circleId ,
288+ ];
289+ }
290+
291+ return [
292+ 'type ' => 'circle ' ,
293+ 'id ' => $ circleId ,
294+ 'name ' => $ this ->circleNames [$ circleId ],
295+ 'link ' => $ this ->circleLinks [$ circleId ],
296+ ];
297+ }
298+
299+ protected function loadCircleDetails (string $ circleId ): void {
300+ try {
301+ $ circlesManager = Server::get (CirclesManager::class);
302+ $ circlesManager ->startSuperSession ();
303+ $ circle = $ circlesManager ->getCircle ($ circleId );
304+
305+ $ this ->circleNames [$ circleId ] = $ circle ->getDisplayName ();
306+ $ this ->circleLinks [$ circleId ] = $ circle ->getUrl ();
307+ } catch (\Exception ) {
308+ } finally {
309+ $ circlesManager ?->stopSession();
310+ }
311+ }
259312}
0 commit comments