]> git.mxchange.org Git - friendica.git/blob - src/Model/Circle.php
Use the owner, not the author
[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          * @return array
173          *    'id' => circle id
174          *    'name' => circle name
175          *    'count' => counted unseen circle items
176          * @throws \Exception
177          */
178         public static function countUnseen()
179         {
180                 $stmt = DBA::p("SELECT `circle`.`id`, `circle`.`name`,
181                                 (SELECT COUNT(*) FROM `post-user-view`
182                                         WHERE `uid` = ?
183                                         AND `unseen`
184                                         AND `contact-id` IN
185                                                 (SELECT `contact-id`
186                                                 FROM `group_member` AS `circle_member`
187                                                 WHERE `circle_member`.`gid` = `circle`.`id`)
188                                         ) AS `count`
189                                 FROM `group` AS `circle`
190                                 WHERE `circle`.`uid` = ?;",
191                         DI::userSession()->getLocalUserId(),
192                         DI::userSession()->getLocalUserId()
193                 );
194
195                 return DBA::toArray($stmt);
196         }
197
198         /**
199          * Get the circle id for a user/name couple
200          *
201          * Returns false if no circle has been found.
202          *
203          * @param int    $uid User id
204          * @param string $name Circle name
205          * @return int|boolean Circle's id number or false on error
206          * @throws \Exception
207          */
208         public static function getIdByName(int $uid, string $name)
209         {
210                 if (!$uid || !strlen($name)) {
211                         return false;
212                 }
213
214                 $circle = DBA::selectFirst('group', ['id'], ['uid' => $uid, 'name' => $name]);
215                 if (DBA::isResult($circle)) {
216                         return $circle['id'];
217                 }
218
219                 return false;
220         }
221
222         /**
223          * Mark a circle as deleted
224          *
225          * @param int $gid
226          * @return boolean
227          * @throws \Exception
228          */
229         public static function remove(int $gid): bool
230         {
231                 if (!$gid) {
232                         return false;
233                 }
234
235                 $circle = DBA::selectFirst('group', ['uid'], ['id' => $gid]);
236                 if (!DBA::isResult($circle)) {
237                         return false;
238                 }
239
240                 // remove circle from default posting lists
241                 $user = DBA::selectFirst('user', ['def_gid', 'allow_gid', 'deny_gid'], ['uid' => $circle['uid']]);
242                 if (DBA::isResult($user)) {
243                         $change = false;
244
245                         if ($user['def_gid'] == $gid) {
246                                 $user['def_gid'] = 0;
247                                 $change = true;
248                         }
249                         if (strpos($user['allow_gid'], '<' . $gid . '>') !== false) {
250                                 $user['allow_gid'] = str_replace('<' . $gid . '>', '', $user['allow_gid']);
251                                 $change = true;
252                         }
253                         if (strpos($user['deny_gid'], '<' . $gid . '>') !== false) {
254                                 $user['deny_gid'] = str_replace('<' . $gid . '>', '', $user['deny_gid']);
255                                 $change = true;
256                         }
257
258                         if ($change) {
259                                 DBA::update('user', $user, ['uid' => $circle['uid']]);
260                         }
261                 }
262
263                 // remove all members
264                 DBA::delete('group_member', ['gid' => $gid]);
265
266                 // remove circle
267                 $return = DBA::update('group', ['deleted' => 1], ['id' => $gid]);
268
269                 return $return;
270         }
271
272         /**
273          * Adds a contact to a circle
274          *
275          * @param int $gid
276          * @param int $cid
277          * @return boolean
278          * @throws \Exception
279          */
280         public static function addMember(int $gid, int $cid): bool
281         {
282                 if (!$gid || !$cid) {
283                         return false;
284                 }
285
286                 // @TODO Backward compatibility with user contacts, remove by version 2022.03
287                 $circle = DBA::selectFirst('group', ['uid'], ['id' => $gid]);
288                 if (empty($circle)) {
289                         throw new HTTPException\NotFoundException('Circle not found.');
290                 }
291
292                 $cdata = Contact::getPublicAndUserContactID($cid, $circle['uid']);
293                 if (empty($cdata['user'])) {
294                         throw new HTTPException\NotFoundException('Invalid contact.');
295                 }
296
297                 return DBA::insert('group_member', ['gid' => $gid, 'contact-id' => $cdata['user']], Database::INSERT_IGNORE);
298         }
299
300         /**
301          * Removes a contact from a circle
302          *
303          * @param int $gid
304          * @param int $cid
305          * @return boolean
306          * @throws \Exception
307          */
308         public static function removeMember(int $gid, int $cid): bool
309         {
310                 if (!$gid || !$cid) {
311                         return false;
312                 }
313
314                 // @TODO Backward compatibility with user contacts, remove by version 2022.03
315                 $circle = DBA::selectFirst('group', ['uid'], ['id' => $gid]);
316                 if (empty($circle)) {
317                         throw new HTTPException\NotFoundException('Circle not found.');
318                 }
319
320                 $cdata = Contact::getPublicAndUserContactID($cid, $circle['uid']);
321                 if (empty($cdata['user'])) {
322                         throw new HTTPException\NotFoundException('Invalid contact.');
323                 }
324
325                 return DBA::delete('group_member', ['gid' => $gid, 'contact-id' => $cid]);
326         }
327
328         /**
329          * Adds contacts to a circle
330          *
331          * @param int $gid
332          * @param array $contacts Array with contact ids
333          * @return void
334          * @throws \Exception
335          */
336         public static function addMembers(int $gid, array $contacts)
337         {
338                 if (!$gid || !$contacts) {
339                         return;
340                 }
341
342                 // @TODO Backward compatibility with user contacts, remove by version 2022.03
343                 $circle = DBA::selectFirst('group', ['uid'], ['id' => $gid]);
344                 if (empty($circle)) {
345                         throw new HTTPException\NotFoundException('Circle not found.');
346                 }
347
348                 foreach ($contacts as $cid) {
349                         $cdata = Contact::getPublicAndUserContactID($cid, $circle['uid']);
350                         if (empty($cdata['user'])) {
351                                 throw new HTTPException\NotFoundException('Invalid contact.');
352                         }
353
354                         DBA::insert('group_member', ['gid' => $gid, 'contact-id' => $cdata['user']], Database::INSERT_IGNORE);
355                 }
356         }
357
358         /**
359          * Removes contacts from a circle
360          *
361          * @param int $gid Circle id
362          * @param array $contacts Contact ids
363          * @return bool
364          * @throws \Exception
365          */
366         public static function removeMembers(int $gid, array $contacts)
367         {
368                 if (!$gid || !$contacts) {
369                         return false;
370                 }
371
372                 // @TODO Backward compatibility with user contacts, remove by version 2022.03
373                 $circle = DBA::selectFirst('group', ['uid'], ['id' => $gid]);
374                 if (empty($circle)) {
375                         throw new HTTPException\NotFoundException('Circle not found.');
376                 }
377
378                 $contactIds = [];
379
380                 foreach ($contacts as $cid) {
381                         $cdata = Contact::getPublicAndUserContactID($cid, $circle['uid']);
382                         if (empty($cdata['user'])) {
383                                 throw new HTTPException\NotFoundException('Invalid contact.');
384                         }
385
386                         $contactIds[] = $cdata['user'];
387                 }
388
389                 // Return status of deletion
390                 return DBA::delete('group_member', ['gid' => $gid, 'contact-id' => $contactIds]);
391         }
392
393         /**
394          * Returns the combined list of contact ids from a circle id list
395          *
396          * @param int     $uid              User id
397          * @param array   $circle_ids        Circles ids
398          * @param boolean $check_dead       Whether check "dead" records (?)
399          * @param boolean $expand_followers Expand the list of followers
400          * @return array
401          * @throws \Exception
402          */
403         public static function expand(int $uid, array $circle_ids, bool $check_dead = false, bool $expand_followers = true): array
404         {
405                 if (!is_array($circle_ids) || !count($circle_ids)) {
406                         return [];
407                 }
408
409                 $return               = [];
410                 $pubmail              = false;
411                 $followers_collection = false;
412                 $networks             = Protocol::SUPPORT_PRIVATE;
413
414                 $mailacct = DBA::selectFirst('mailacct', ['pubmail'], ['`uid` = ? AND `server` != ""', $uid]);
415                 if (DBA::isResult($mailacct)) {
416                         $pubmail = $mailacct['pubmail'];
417                 }
418
419                 if (!$pubmail) {
420                         $networks = array_diff($networks, [Protocol::MAIL]);
421                 }
422
423                 $key = array_search(self::FOLLOWERS, $circle_ids);
424                 if ($key !== false) {
425                         if ($expand_followers) {
426                                 $followers = Contact::selectToArray(['id'], [
427                                         'uid' => $uid,
428                                         'rel' => [Contact::FOLLOWER, Contact::FRIEND],
429                                         'network' => $networks,
430                                         'contact-type' => [Contact::TYPE_UNKNOWN, Contact::TYPE_PERSON],
431                                         'archive' => false,
432                                         'pending' => false,
433                                         'blocked' => false,
434                                 ]);
435
436                                 foreach ($followers as $follower) {
437                                         $return[] = $follower['id'];
438                                 }
439                         } else {
440                                 $followers_collection = true;
441                         }
442                         unset($circle_ids[$key]);
443                 }
444
445                 $key = array_search(self::MUTUALS, $circle_ids);
446                 if ($key !== false) {
447                         $mutuals = Contact::selectToArray(['id'], [
448                                 'uid' => $uid,
449                                 'rel' => [Contact::FRIEND],
450                                 'network' => $networks,
451                                 'contact-type' => [Contact::TYPE_UNKNOWN, Contact::TYPE_PERSON],
452                                 'archive' => false,
453                                 'pending' => false,
454                                 'blocked' => false,
455                         ]);
456
457                         foreach ($mutuals as $mutual) {
458                                 $return[] = $mutual['id'];
459                         }
460
461                         unset($circle_ids[$key]);
462                 }
463
464                 $stmt = DBA::select('group_member', ['contact-id'], ['gid' => $circle_ids]);
465                 while ($circle_member = DBA::fetch($stmt)) {
466                         $return[] = $circle_member['contact-id'];
467                 }
468                 DBA::close($stmt);
469
470                 if ($check_dead) {
471                         $return = Contact::pruneUnavailable($return);
472                 }
473
474                 if ($followers_collection) {
475                         $return[] = -1;
476                 }
477
478                 return $return;
479         }
480
481         /**
482          * Returns a templated circle selection list
483          *
484          * @param int    $uid User id
485          * @param int    $gid   A pre-selected circle
486          * @param string $id    The id of the option group
487          * @param string $label The label of the option group
488          * @return string
489          * @throws \Exception
490          */
491         public static function getSelectorHTML(int $uid, int $gid, string $id, string $label): string
492         {
493                 $display_circles = [
494                         [
495                                 'name' => '',
496                                 'id' => '0',
497                                 'selected' => ''
498                         ]
499                 ];
500
501                 $stmt = DBA::select('group', [], ['deleted' => false, 'uid' => $uid, 'cid' => null], ['order' => ['name']]);
502                 while ($circle = DBA::fetch($stmt)) {
503                         $display_circles[] = [
504                                 'name' => $circle['name'],
505                                 'id' => $circle['id'],
506                                 'selected' => $gid == $circle['id'] ? 'true' : ''
507                         ];
508                 }
509                 DBA::close($stmt);
510
511                 Logger::info('Got circles', $display_circles);
512
513                 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('circle_selection.tpl'), [
514                         '$id' => $id,
515                         '$label' => $label,
516                         '$circles' => $display_circles
517                 ]);
518                 return $o;
519         }
520
521         /**
522          * Create circle sidebar widget
523          *
524          * @param string $every
525          * @param string $each
526          * @param string $editmode
527          *    'standard' => include link 'Edit circles'
528          *    'extended' => include link 'Create new circle'
529          *    'full' => include link 'Create new circle' and provide for each circle a link to edit this circle
530          * @param string|int $circle_id Distinct circle id or 'everyone'
531          * @param int    $cid Contact id
532          * @return string Sidebar widget HTML code
533          * @throws \Exception
534          */
535         public static function sidebarWidget(string $every = 'contact', string $each = 'circle', string $editmode = 'standard', $circle_id = '', int $cid = 0)
536         {
537                 if (!DI::userSession()->getLocalUserId()) {
538                         return '';
539                 }
540
541                 $display_circles = [
542                         [
543                                 'text' => DI::l10n()->t('Everybody'),
544                                 'id' => 0,
545                                 'selected' => (($circle_id === 'everyone') ? 'circle-selected' : ''),
546                                 'href' => $every,
547                         ]
548                 ];
549
550                 $member_of = [];
551                 if ($cid) {
552                         $member_of = self::getIdsByContactId($cid);
553                 }
554
555                 $stmt = DBA::select('group', [], ['deleted' => false, 'uid' => DI::userSession()->getLocalUserId(), 'cid' => null], ['order' => ['name']]);
556                 while ($circle = DBA::fetch($stmt)) {
557                         $selected = (($circle_id == $circle['id']) ? ' circle-selected' : '');
558
559                         if ($editmode == 'full') {
560                                 $circleedit = [
561                                         'href' => 'circle/' . $circle['id'],
562                                         'title' => DI::l10n()->t('edit'),
563                                 ];
564                         } else {
565                                 $circleedit = null;
566                         }
567
568                         if ($each == 'circle') {
569                                 $networks = Widget::unavailableNetworks();
570                                 $sql_values = array_merge([$circle['id']], $networks);
571                                 $condition = ["`circle-id` = ? AND NOT `contact-network` IN (" . substr(str_repeat("?, ", count($networks)), 0, -2) . ")"];
572                                 $condition = array_merge($condition, $sql_values);
573
574                                 $count = DBA::count('circle-member-view', $condition);
575                                 $circle_name = sprintf('%s (%d)', $circle['name'], $count);
576                         } else {
577                                 $circle_name = $circle['name'];
578                         }
579
580                         $display_circles[] = [
581                                 'id'   => $circle['id'],
582                                 'cid'  => $cid,
583                                 'text' => $circle_name,
584                                 'href' => $each . '/' . $circle['id'],
585                                 'edit' => $circleedit,
586                                 'selected' => $selected,
587                                 'ismember' => in_array($circle['id'], $member_of),
588                         ];
589                 }
590                 DBA::close($stmt);
591
592                 // Don't show the circles on the network page when there is only one
593                 if ((count($display_circles) <= 2) && ($each == 'network')) {
594                         return '';
595                 }
596
597                 $tpl = Renderer::getMarkupTemplate('circle_side.tpl');
598                 $o = Renderer::replaceMacros($tpl, [
599                         '$add' => DI::l10n()->t('add'),
600                         '$title' => DI::l10n()->t('Circles'),
601                         '$circles' => $display_circles,
602                         '$new_circle' => $editmode == 'extended' || $editmode == 'full' ? 1 : '',
603                         '$circle_page' => 'circle/',
604                         '$edittext' => DI::l10n()->t('Edit circle'),
605                         '$uncircled' => $every === 'contact' ? DI::l10n()->t('Contacts not in any circle') : '',
606                         '$uncircled_selected' => (($circle_id === 'none') ? 'circle-selected' : ''),
607                         '$createtext' => DI::l10n()->t('Create a new circle'),
608                         '$create_circle' => DI::l10n()->t('Circle Name: '),
609                         '$edit_circles_text' => DI::l10n()->t('Edit circles'),
610                         '$form_security_token' => BaseModule::getFormSecurityToken('circle_edit'),
611                 ]);
612
613                 return $o;
614         }
615
616         /**
617          * Fetch the circle id for the given contact id
618          *
619          * @param integer $id Contact ID
620          * @return integer Circle ID
621          */
622         public static function getIdForGroup(int $id): int
623         {
624                 Logger::info('Get id for group id', ['id' => $id]);
625                 $contact = Contact::getById($id, ['uid', 'name', 'contact-type', 'manually-approve']);
626                 if (empty($contact) || ($contact['contact-type'] != Contact::TYPE_COMMUNITY) || !$contact['manually-approve']) {
627                         return 0;
628                 }
629
630                 $circle = DBA::selectFirst('group', ['id'], ['uid' => $contact['uid'], 'cid' => $id]);
631                 if (empty($circle)) {
632                         $fields = [
633                                 'uid'  => $contact['uid'],
634                                 'name' => $contact['name'],
635                                 'cid'  => $id,
636                         ];
637                         DBA::insert('group', $fields);
638                         $gid = DBA::lastInsertId();
639                 } else {
640                         $gid = $circle['id'];
641                 }
642
643                 return $gid;
644         }
645
646         /**
647          * Fetch the followers of a given contact id and store them as circle members
648          *
649          * @param integer $id Contact ID
650          * @return void
651          */
652         public static function updateMembersForGroup(int $id)
653         {
654                 Logger::info('Update group members', ['id' => $id]);
655
656                 $contact = Contact::getById($id, ['uid', 'url']);
657                 if (empty($contact)) {
658                         return;
659                 }
660
661                 $apcontact = APContact::getByURL($contact['url']);
662                 if (empty($apcontact['followers'])) {
663                         return;
664                 }
665
666                 $gid = self::getIdForGroup($id);
667                 if (empty($gid)) {
668                         return;
669                 }
670
671                 $circle_members = DBA::selectToArray('group_member', ['contact-id'], ['gid' => $gid]);
672                 if (!empty($circle_members)) {
673                         $current = array_unique(array_column($circle_members, 'contact-id'));
674                 } else {
675                         $current = [];
676                 }
677
678                 foreach (ActivityPub::fetchItems($apcontact['followers'], $contact['uid']) as $follower) {
679                         $id = Contact::getIdForURL($follower);
680                         if (!in_array($id, $current)) {
681                                 DBA::insert('group_member', ['gid' => $gid, 'contact-id' => $id]);
682                         } else {
683                                 $key = array_search($id, $current);
684                                 unset($current[$key]);
685                         }
686                 }
687
688                 DBA::delete('group_member', ['gid' => $gid, 'contact-id' => $current]);
689                 Logger::info('Updated group members', ['id' => $id, 'count' => DBA::count('group_member', ['gid' => $gid])]);
690         }
691 }