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