]> git.mxchange.org Git - friendica.git/blob - src/Model/Circle.php
Merge pull request #13238 from annando/issue-13221
[friendica.git] / src / Model / Circle.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Model;
23
24 use Friendica\BaseModule;
25 use Friendica\Core\Logger;
26 use Friendica\Core\Protocol;
27 use Friendica\Core\Renderer;
28 use Friendica\Database\Database;
29 use Friendica\Database\DBA;
30 use Friendica\DI;
31 use Friendica\Network\HTTPException;
32 use Friendica\Protocol\ActivityPub;
33
34 /**
35  * functions for interacting with the group database table
36  */
37 class Circle
38 {
39         const FOLLOWERS = '~';
40         const MUTUALS = '&';
41
42         /**
43          * Fetches circle record by user id and maybe includes deleted circles as well
44          *
45          * @param int  $uid User id to fetch circle(s) for
46          * @param bool $includesDeleted Whether deleted circles should be included
47          * @return array|bool Array on success, bool on error
48          */
49         public static function getByUserId(int $uid, bool $includesDeleted = false)
50         {
51                 $conditions = ['uid' => $uid, 'cid' => null];
52
53                 if (!$includesDeleted) {
54                         $conditions['deleted'] = false;
55                 }
56
57                 return DBA::selectToArray('group', [], $conditions);
58         }
59
60         /**
61          * Checks whether given circle id is found in database
62          *
63          * @param int $circle_id Circle id
64          * @param int $uid Optional user id
65          * @return bool
66          * @throws \Exception
67          */
68         public static function exists(int $circle_id, int $uid = null): bool
69         {
70                 $condition = ['id' => $circle_id, 'deleted' => false];
71
72                 if (!is_null($uid)) {
73                         $condition = [
74                                 'uid' => $uid
75                         ];
76                 }
77
78                 return DBA::exists('group', $condition);
79         }
80
81         /**
82          * Create a new contact circle
83          *
84          * Note: If we found a deleted circle with the same name, we restore it
85          *
86          * @param int    $uid User id to create circle for
87          * @param string $name Name of circle
88          * @return int|boolean Id of newly created circle or false on error
89          * @throws \Exception
90          */
91         public static function create(int $uid, string $name)
92         {
93                 $return = false;
94                 if (!empty($uid) && !empty($name)) {
95                         $gid = self::getIdByName($uid, $name); // check for dupes
96                         if ($gid !== false) {
97                                 // This could be a problem.
98                                 // Let's assume we've just created a circle which we once deleted
99                                 // all the old members are gone, but the circle remains, so we don't break any security
100                                 // access lists. What we're doing here is reviving the dead circle, but old content which
101                                 // was restricted to this circle may now be seen by the new circle members.
102                                 $circle = DBA::selectFirst('group', ['deleted'], ['id' => $gid]);
103                                 if (DBA::isResult($circle) && $circle['deleted']) {
104                                         DBA::update('group', ['deleted' => 0], ['id' => $gid]);
105                                         DI::sysmsg()->addNotice(DI::l10n()->t('A deleted circle with this name was revived. Existing item permissions <strong>may</strong> apply to this circle and any future members. If this is not what you intended, please create another circle with a different name.'));
106                                 }
107                                 return true;
108                         }
109
110                         $return = DBA::insert('group', ['uid' => $uid, 'name' => $name]);
111                         if ($return) {
112                                 $return = DBA::lastInsertId();
113                         }
114                 }
115                 return $return;
116         }
117
118         /**
119          * Update circle information.
120          *
121          * @param int    $id   Circle ID
122          * @param string $name Circle name
123          *
124          * @return bool Was the update successful?
125          * @throws \Exception
126          */
127         public static function update(int $id, string $name): bool
128         {
129                 return DBA::update('group', ['name' => $name], ['id' => $id]);
130         }
131
132         /**
133          * Get a list of circle ids a contact belongs to
134          *
135          * @param int $cid Contact id
136          * @return array Circle ids
137          * @throws \Exception
138          */
139         public static function getIdsByContactId(int $cid): array
140         {
141                 $contact = Contact::getById($cid, ['rel']);
142                 if (!$contact) {
143                         return [];
144                 }
145
146                 $circleIds = [];
147
148                 $stmt = DBA::select('group_member', ['gid'], ['contact-id' => $cid]);
149                 while ($circle = DBA::fetch($stmt)) {
150                         $circleIds[] = $circle['gid'];
151                 }
152                 DBA::close($stmt);
153
154                 // Meta-circles
155                 if ($contact['rel'] == Contact::FOLLOWER || $contact['rel'] == Contact::FRIEND) {
156                         $circleIds[] = self::FOLLOWERS;
157                 }
158
159                 if ($contact['rel'] == Contact::FRIEND) {
160                         $circleIds[] = self::MUTUALS;
161                 }
162
163                 return $circleIds;
164         }
165
166         /**
167          * count unread circle items
168          *
169          * Count unread items of each circle of the local user
170          *
171          * @return array
172          *    'id' => circle id
173          *    'name' => circle name
174          *    'count' => counted unseen circle items
175          * @throws \Exception
176          */
177         public static function countUnseen()
178         {
179                 $stmt = DBA::p("SELECT `circle`.`id`, `circle`.`name`,
180                                 (SELECT COUNT(*) FROM `post-user-view`
181                                         WHERE `uid` = ?
182                                         AND `unseen`
183                                         AND `contact-id` IN
184                                                 (SELECT `contact-id`
185                                                 FROM `group_member` AS `circle_member`
186                                                 WHERE `circle_member`.`gid` = `circle`.`id`)
187                                         ) AS `count`
188                                 FROM `group` AS `circle`
189                                 WHERE `circle`.`uid` = ?;",
190                         DI::userSession()->getLocalUserId(),
191                         DI::userSession()->getLocalUserId()
192                 );
193
194                 return DBA::toArray($stmt);
195         }
196
197         /**
198          * Get the circle id for a user/name couple
199          *
200          * Returns false if no circle has been found.
201          *
202          * @param int    $uid User id
203          * @param string $name Circle name
204          * @return int|boolean Circle's id number or false on error
205          * @throws \Exception
206          */
207         public static function getIdByName(int $uid, string $name)
208         {
209                 if (!$uid || !strlen($name)) {
210                         return false;
211                 }
212
213                 $circle = DBA::selectFirst('group', ['id'], ['uid' => $uid, 'name' => $name]);
214                 if (DBA::isResult($circle)) {
215                         return $circle['id'];
216                 }
217
218                 return false;
219         }
220
221         /**
222          * Mark a circle as deleted
223          *
224          * @param int $gid
225          * @return boolean
226          * @throws \Exception
227          */
228         public static function remove(int $gid): bool
229         {
230                 if (!$gid) {
231                         return false;
232                 }
233
234                 $circle = DBA::selectFirst('group', ['uid'], ['id' => $gid]);
235                 if (!DBA::isResult($circle)) {
236                         return false;
237                 }
238
239                 // remove circle from default posting lists
240                 $user = DBA::selectFirst('user', ['def_gid', 'allow_gid', 'deny_gid'], ['uid' => $circle['uid']]);
241                 if (DBA::isResult($user)) {
242                         $change = false;
243
244                         if ($user['def_gid'] == $gid) {
245                                 $user['def_gid'] = 0;
246                                 $change = true;
247                         }
248                         if (strpos($user['allow_gid'], '<' . $gid . '>') !== false) {
249                                 $user['allow_gid'] = str_replace('<' . $gid . '>', '', $user['allow_gid']);
250                                 $change = true;
251                         }
252                         if (strpos($user['deny_gid'], '<' . $gid . '>') !== false) {
253                                 $user['deny_gid'] = str_replace('<' . $gid . '>', '', $user['deny_gid']);
254                                 $change = true;
255                         }
256
257                         if ($change) {
258                                 DBA::update('user', $user, ['uid' => $circle['uid']]);
259                         }
260                 }
261
262                 // remove all members
263                 DBA::delete('group_member', ['gid' => $gid]);
264
265                 // remove circle
266                 $return = DBA::update('group', ['deleted' => 1], ['id' => $gid]);
267
268                 return $return;
269         }
270
271         /**
272          * Adds a contact to a circle
273          *
274          * @param int $gid
275          * @param int $cid
276          * @return boolean
277          * @throws \Exception
278          */
279         public static function addMember(int $gid, int $cid): bool
280         {
281                 if (!$gid || !$cid) {
282                         return false;
283                 }
284
285                 // @TODO Backward compatibility with user contacts, remove by version 2022.03
286                 $circle = DBA::selectFirst('group', ['uid'], ['id' => $gid]);
287                 if (empty($circle)) {
288                         throw new HTTPException\NotFoundException('Circle not found.');
289                 }
290
291                 $cdata = Contact::getPublicAndUserContactID($cid, $circle['uid']);
292                 if (empty($cdata['user'])) {
293                         throw new HTTPException\NotFoundException('Invalid contact.');
294                 }
295
296                 return DBA::insert('group_member', ['gid' => $gid, 'contact-id' => $cdata['user']], Database::INSERT_IGNORE);
297         }
298
299         /**
300          * Removes a contact from a circle
301          *
302          * @param int $gid
303          * @param int $cid
304          * @return boolean
305          * @throws \Exception
306          */
307         public static function removeMember(int $gid, int $cid): bool
308         {
309                 if (!$gid || !$cid) {
310                         return false;
311                 }
312
313                 // @TODO Backward compatibility with user contacts, remove by version 2022.03
314                 $circle = DBA::selectFirst('group', ['uid'], ['id' => $gid]);
315                 if (empty($circle)) {
316                         throw new HTTPException\NotFoundException('Circle not found.');
317                 }
318
319                 $cdata = Contact::getPublicAndUserContactID($cid, $circle['uid']);
320                 if (empty($cdata['user'])) {
321                         throw new HTTPException\NotFoundException('Invalid contact.');
322                 }
323
324                 return DBA::delete('group_member', ['gid' => $gid, 'contact-id' => $cid]);
325         }
326
327         /**
328          * Adds contacts to a circle
329          *
330          * @param int $gid
331          * @param array $contacts Array with contact ids
332          * @return void
333          * @throws \Exception
334          */
335         public static function addMembers(int $gid, array $contacts)
336         {
337                 if (!$gid || !$contacts) {
338                         return;
339                 }
340
341                 // @TODO Backward compatibility with user contacts, remove by version 2022.03
342                 $circle = DBA::selectFirst('group', ['uid'], ['id' => $gid]);
343                 if (empty($circle)) {
344                         throw new HTTPException\NotFoundException('Circle not found.');
345                 }
346
347                 foreach ($contacts as $cid) {
348                         $cdata = Contact::getPublicAndUserContactID($cid, $circle['uid']);
349                         if (empty($cdata['user'])) {
350                                 throw new HTTPException\NotFoundException('Invalid contact.');
351                         }
352
353                         DBA::insert('group_member', ['gid' => $gid, 'contact-id' => $cdata['user']], Database::INSERT_IGNORE);
354                 }
355         }
356
357         /**
358          * Removes contacts from a circle
359          *
360          * @param int $gid Circle id
361          * @param array $contacts Contact ids
362          * @return bool
363          * @throws \Exception
364          */
365         public static function removeMembers(int $gid, array $contacts)
366         {
367                 if (!$gid || !$contacts) {
368                         return false;
369                 }
370
371                 // @TODO Backward compatibility with user contacts, remove by version 2022.03
372                 $circle = DBA::selectFirst('group', ['uid'], ['id' => $gid]);
373                 if (empty($circle)) {
374                         throw new HTTPException\NotFoundException('Circle not found.');
375                 }
376
377                 $contactIds = [];
378
379                 foreach ($contacts as $cid) {
380                         $cdata = Contact::getPublicAndUserContactID($cid, $circle['uid']);
381                         if (empty($cdata['user'])) {
382                                 throw new HTTPException\NotFoundException('Invalid contact.');
383                         }
384
385                         $contactIds[] = $cdata['user'];
386                 }
387
388                 // Return status of deletion
389                 return DBA::delete('group_member', ['gid' => $gid, 'contact-id' => $contactIds]);
390         }
391
392         /**
393          * Returns the combined list of contact ids from a circle id list
394          *
395          * @param int     $uid              User id
396          * @param array   $circle_ids        Circles ids
397          * @param boolean $check_dead       Whether check "dead" records (?)
398          * @param boolean $expand_followers Expand the list of followers
399          * @return array
400          * @throws \Exception
401          */
402         public static function expand(int $uid, array $circle_ids, bool $check_dead = false, bool $expand_followers = true): array
403         {
404                 if (!is_array($circle_ids) || !count($circle_ids)) {
405                         return [];
406                 }
407
408                 $return               = [];
409                 $pubmail              = false;
410                 $followers_collection = false;
411                 $networks             = Protocol::SUPPORT_PRIVATE;
412
413                 $mailacct = DBA::selectFirst('mailacct', ['pubmail'], ['`uid` = ? AND `server` != ""', $uid]);
414                 if (DBA::isResult($mailacct)) {
415                         $pubmail = $mailacct['pubmail'];
416                 }
417
418                 if (!$pubmail) {
419                         $networks = array_diff($networks, [Protocol::MAIL]);
420                 }
421
422                 $key = array_search(self::FOLLOWERS, $circle_ids);
423                 if ($key !== false) {
424                         if ($expand_followers) {
425                                 $followers = Contact::selectToArray(['id'], [
426                                         'uid' => $uid,
427                                         'rel' => [Contact::FOLLOWER, Contact::FRIEND],
428                                         'network' => $networks,
429                                         'contact-type' => [Contact::TYPE_UNKNOWN, Contact::TYPE_PERSON],
430                                         'archive' => false,
431                                         'pending' => false,
432                                         'blocked' => false,
433                                 ]);
434
435                                 foreach ($followers as $follower) {
436                                         $return[] = $follower['id'];
437                                 }
438                         } else {
439                                 $followers_collection = true;
440                         }
441                         unset($circle_ids[$key]);
442                 }
443
444                 $key = array_search(self::MUTUALS, $circle_ids);
445                 if ($key !== false) {
446                         $mutuals = Contact::selectToArray(['id'], [
447                                 'uid' => $uid,
448                                 'rel' => [Contact::FRIEND],
449                                 'network' => $networks,
450                                 'contact-type' => [Contact::TYPE_UNKNOWN, Contact::TYPE_PERSON],
451                                 'archive' => false,
452                                 'pending' => false,
453                                 'blocked' => false,
454                         ]);
455
456                         foreach ($mutuals as $mutual) {
457                                 $return[] = $mutual['id'];
458                         }
459
460                         unset($circle_ids[$key]);
461                 }
462
463                 $stmt = DBA::select('group_member', ['contact-id'], ['gid' => $circle_ids]);
464                 while ($circle_member = DBA::fetch($stmt)) {
465                         $return[] = $circle_member['contact-id'];
466                 }
467                 DBA::close($stmt);
468
469                 if ($check_dead) {
470                         $return = Contact::pruneUnavailable($return);
471                 }
472
473                 if ($followers_collection) {
474                         $return[] = -1;
475                 }
476
477                 return $return;
478         }
479
480         /**
481          * Returns a templated circle selection list
482          *
483          * @param int    $uid User id
484          * @param int    $gid   A pre-selected circle
485          * @param string $id    The id of the option group
486          * @param string $label The label of the option group
487          * @return string
488          * @throws \Exception
489          */
490         public static function getSelectorHTML(int $uid, int $gid, string $id, string $label): string
491         {
492                 $display_circles = [
493                         [
494                                 'name' => '',
495                                 'id' => '0',
496                                 'selected' => ''
497                         ]
498                 ];
499
500                 $stmt = DBA::select('group', [], ['deleted' => false, 'uid' => $uid, 'cid' => null], ['order' => ['name']]);
501                 while ($circle = DBA::fetch($stmt)) {
502                         $display_circles[] = [
503                                 'name' => $circle['name'],
504                                 'id' => $circle['id'],
505                                 'selected' => $gid == $circle['id'] ? 'true' : ''
506                         ];
507                 }
508                 DBA::close($stmt);
509
510                 Logger::info('Got circles', $display_circles);
511
512                 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('circle_selection.tpl'), [
513                         '$id' => $id,
514                         '$label' => $label,
515                         '$circles' => $display_circles
516                 ]);
517                 return $o;
518         }
519
520         /**
521          * Create circle sidebar widget
522          *
523          * @param string $every
524          * @param string $each
525          * @param string $editmode
526          *    'standard' => include link 'Edit circles'
527          *    'extended' => include link 'Create new circle'
528          *    'full' => include link 'Create new circle' and provide for each circle a link to edit this circle
529          * @param string|int $circle_id Distinct circle id or 'everyone'
530          * @param int    $cid Contact id
531          * @return string Sidebar widget HTML code
532          * @throws \Exception
533          */
534         public static function sidebarWidget(string $every = 'contact', string $each = 'circle', string $editmode = 'standard', $circle_id = '', int $cid = 0)
535         {
536                 if (!DI::userSession()->getLocalUserId()) {
537                         return '';
538                 }
539
540                 $display_circles = [
541                         [
542                                 'text' => DI::l10n()->t('Everybody'),
543                                 'id' => 0,
544                                 'selected' => (($circle_id === 'everyone') ? 'circle-selected' : ''),
545                                 'href' => $every,
546                         ]
547                 ];
548
549                 $member_of = [];
550                 if ($cid) {
551                         $member_of = self::getIdsByContactId($cid);
552                 }
553
554                 $stmt = DBA::select('group', [], ['deleted' => false, 'uid' => DI::userSession()->getLocalUserId(), 'cid' => null], ['order' => ['name']]);
555                 while ($circle = DBA::fetch($stmt)) {
556                         $selected = (($circle_id == $circle['id']) ? ' circle-selected' : '');
557
558                         if ($editmode == 'full') {
559                                 $circleedit = [
560                                         'href' => 'circle/' . $circle['id'],
561                                         'title' => DI::l10n()->t('edit'),
562                                 ];
563                         } else {
564                                 $circleedit = null;
565                         }
566
567                         if ($each == 'circle') {
568                                 $count = DBA::count('group_member', ['gid' => $circle['id']]);
569                                 $circle_name = sprintf('%s (%d)', $circle['name'], $count);
570                         } else {
571                                 $circle_name = $circle['name'];
572                         }
573
574                         $display_circles[] = [
575                                 'id'   => $circle['id'],
576                                 'cid'  => $cid,
577                                 'text' => $circle_name,
578                                 'href' => $each . '/' . $circle['id'],
579                                 'edit' => $circleedit,
580                                 'selected' => $selected,
581                                 'ismember' => in_array($circle['id'], $member_of),
582                         ];
583                 }
584                 DBA::close($stmt);
585
586                 // Don't show the circles on the network page when there is only one
587                 if ((count($display_circles) <= 2) && ($each == 'network')) {
588                         return '';
589                 }
590
591                 $tpl = Renderer::getMarkupTemplate('circle_side.tpl');
592                 $o = Renderer::replaceMacros($tpl, [
593                         '$add' => DI::l10n()->t('add'),
594                         '$title' => DI::l10n()->t('Circles'),
595                         '$circles' => $display_circles,
596                         '$new_circle' => $editmode == 'extended' || $editmode == 'full' ? 1 : '',
597                         '$circle_page' => 'circle/',
598                         '$edittext' => DI::l10n()->t('Edit circle'),
599                         '$uncircled' => $every === 'contact' ? DI::l10n()->t('Contacts not in any circle') : '',
600                         '$uncircled_selected' => (($circle_id === 'none') ? 'circle-selected' : ''),
601                         '$createtext' => DI::l10n()->t('Create a new circle'),
602                         '$create_circle' => DI::l10n()->t('Circle Name: '),
603                         '$edit_circles_text' => DI::l10n()->t('Edit circles'),
604                         '$form_security_token' => BaseModule::getFormSecurityToken('circle_edit'),
605                 ]);
606
607                 return $o;
608         }
609
610         /**
611          * Fetch the circle id for the given contact id
612          *
613          * @param integer $id Contact ID
614          * @return integer Circle ID
615          */
616         public static function getIdForGroup(int $id): int
617         {
618                 Logger::info('Get id for group id', ['id' => $id]);
619                 $contact = Contact::getById($id, ['uid', 'name', 'contact-type', 'manually-approve']);
620                 if (empty($contact) || ($contact['contact-type'] != Contact::TYPE_COMMUNITY) || !$contact['manually-approve']) {
621                         return 0;
622                 }
623
624                 $circle = DBA::selectFirst('group', ['id'], ['uid' => $contact['uid'], 'cid' => $id]);
625                 if (empty($circle)) {
626                         $fields = [
627                                 'uid'  => $contact['uid'],
628                                 'name' => $contact['name'],
629                                 'cid'  => $id,
630                         ];
631                         DBA::insert('group', $fields);
632                         $gid = DBA::lastInsertId();
633                 } else {
634                         $gid = $circle['id'];
635                 }
636
637                 return $gid;
638         }
639
640         /**
641          * Fetch the followers of a given contact id and store them as circle members
642          *
643          * @param integer $id Contact ID
644          * @return void
645          */
646         public static function updateMembersForGroup(int $id)
647         {
648                 Logger::info('Update group members', ['id' => $id]);
649
650                 $contact = Contact::getById($id, ['uid', 'url']);
651                 if (empty($contact)) {
652                         return;
653                 }
654
655                 $apcontact = APContact::getByURL($contact['url']);
656                 if (empty($apcontact['followers'])) {
657                         return;
658                 }
659
660                 $gid = self::getIdForGroup($id);
661                 if (empty($gid)) {
662                         return;
663                 }
664
665                 $circle_members = DBA::selectToArray('group_member', ['contact-id'], ['gid' => $gid]);
666                 if (!empty($circle_members)) {
667                         $current = array_unique(array_column($circle_members, 'contact-id'));
668                 } else {
669                         $current = [];
670                 }
671
672                 foreach (ActivityPub::fetchItems($apcontact['followers'], $contact['uid']) as $follower) {
673                         $id = Contact::getIdForURL($follower);
674                         if (!in_array($id, $current)) {
675                                 DBA::insert('group_member', ['gid' => $gid, 'contact-id' => $id]);
676                         } else {
677                                 $key = array_search($id, $current);
678                                 unset($current[$key]);
679                         }
680                 }
681
682                 DBA::delete('group_member', ['gid' => $gid, 'contact-id' => $current]);
683                 Logger::info('Updated group members', ['id' => $id, 'count' => DBA::count('group_member', ['gid' => $gid])]);
684         }
685 }