]> git.mxchange.org Git - friendica.git/blob - src/Model/Group.php
Merge pull request #12024 from annando/no-boot-src-1
[friendica.git] / src / Model / Group.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, 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\Core\Session;
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 Group
39 {
40         const FOLLOWERS = '~';
41         const MUTUALS = '&';
42
43         /**
44          * Fetches group record by user id and maybe includes deleted groups as well
45          *
46          * @param int  $uid User id to fetch group(s) for
47          * @param bool $includesDeleted Whether deleted groups 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 group id is found in database
63          *
64          * @param int $group_id Groupd it
65          * @param int $uid Optional user id
66          * @return bool
67          * @throws \Exception
68          */
69         public static function exists(int $group_id, int $uid = null): bool
70         {
71                 $condition = ['id' => $group_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 group
84          *
85          * Note: If we found a deleted group with the same name, we restore it
86          *
87          * @param int    $uid User id to create group for
88          * @param string $name Name of group
89          * @return int|boolean Id of newly created group 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 group which we once deleted
100                                 // all the old members are gone, but the group remains so we don't break any security
101                                 // access lists. What we're doing here is reviving the dead group, but old content which
102                                 // was restricted to this group may now be seen by the new group members.
103                                 $group = DBA::selectFirst('group', ['deleted'], ['id' => $gid]);
104                                 if (DBA::isResult($group) && $group['deleted']) {
105                                         DBA::update('group', ['deleted' => 0], ['id' => $gid]);
106                                         DI::sysmsg()->addNotice(DI::l10n()->t('A deleted group with this name was revived. Existing item permissions <strong>may</strong> apply to this group and any future members. If this is not what you intended, please create another group 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 group information.
121          *
122          * @param int    $id   Group ID
123          * @param string $name Group 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 group ids a contact belongs to
135          *
136          * @param int $cid Contact id
137          * @return array Group 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                 $groupIds = [];
148
149                 $stmt = DBA::select('group_member', ['gid'], ['contact-id' => $cid]);
150                 while ($group = DBA::fetch($stmt)) {
151                         $groupIds[] = $group['gid'];
152                 }
153                 DBA::close($stmt);
154
155                 // Meta-groups
156                 if ($contact['rel'] == Contact::FOLLOWER || $contact['rel'] == Contact::FRIEND) {
157                         $groupIds[] = self::FOLLOWERS;
158                 }
159
160                 if ($contact['rel'] == Contact::FRIEND) {
161                         $groupIds[] = self::MUTUALS;
162                 }
163
164                 return $groupIds;
165         }
166
167         /**
168          * count unread group items
169          *
170          * Count unread items of each groups of the local user
171          *
172          * @return array
173          *    'id' => group id
174          *    'name' => group name
175          *    'count' => counted unseen group items
176          * @throws \Exception
177          */
178         public static function countUnseen()
179         {
180                 $stmt = DBA::p("SELECT `group`.`id`, `group`.`name`,
181                                 (SELECT COUNT(*) FROM `post-user`
182                                         WHERE `uid` = ?
183                                         AND `unseen`
184                                         AND `contact-id` IN
185                                                 (SELECT `contact-id`
186                                                 FROM `group_member`
187                                                 WHERE `group_member`.`gid` = `group`.`id`)
188                                         ) AS `count`
189                                 FROM `group`
190                                 WHERE `group`.`uid` = ?;",
191                         Session::getLocalUser(),
192                         Session::getLocalUser()
193                 );
194
195                 return DBA::toArray($stmt);
196         }
197
198         /**
199          * Get the group id for a user/name couple
200          *
201          * Returns false if no group has been found.
202          *
203          * @param int    $uid User id
204          * @param string $name Group name
205          * @return int|boolean Groups' 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                 $group = DBA::selectFirst('group', ['id'], ['uid' => $uid, 'name' => $name]);
215                 if (DBA::isResult($group)) {
216                         return $group['id'];
217                 }
218
219                 return false;
220         }
221
222         /**
223          * Mark a group 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                 $group = DBA::selectFirst('group', ['uid'], ['id' => $gid]);
236                 if (!DBA::isResult($group)) {
237                         return false;
238                 }
239
240                 // remove group from default posting lists
241                 $user = DBA::selectFirst('user', ['def_gid', 'allow_gid', 'deny_gid'], ['uid' => $group['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' => $group['uid']]);
260                         }
261                 }
262
263                 // remove all members
264                 DBA::delete('group_member', ['gid' => $gid]);
265
266                 // remove group
267                 $return = DBA::update('group', ['deleted' => 1], ['id' => $gid]);
268
269                 return $return;
270         }
271
272         /**
273          * Adds a contact to a group
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                 $group = DBA::selectFirst('group', ['uid'], ['id' => $gid]);
288                 if (empty($group)) {
289                         throw new HTTPException\NotFoundException('Group not found.');
290                 }
291
292                 $cdata = Contact::getPublicAndUserContactID($cid, $group['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 group
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                 $group = DBA::selectFirst('group', ['uid'], ['id' => $gid]);
316                 if (empty($group)) {
317                         throw new HTTPException\NotFoundException('Group not found.');
318                 }
319
320                 $cdata = Contact::getPublicAndUserContactID($cid, $group['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 group
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                 $group = DBA::selectFirst('group', ['uid'], ['id' => $gid]);
344                 if (empty($group)) {
345                         throw new HTTPException\NotFoundException('Group not found.');
346                 }
347
348                 foreach ($contacts as $cid) {
349                         $cdata = Contact::getPublicAndUserContactID($cid, $group['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 group
360          *
361          * @param int $gid Group 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                 $group = DBA::selectFirst('group', ['uid'], ['id' => $gid]);
374                 if (empty($group)) {
375                         throw new HTTPException\NotFoundException('Group not found.');
376                 }
377
378                 $contactIds = [];
379
380                 foreach ($contacts as $cid) {
381                         $cdata = Contact::getPublicAndUserContactID($cid, $group['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 group id list
395          *
396          * @param int     $uid User id
397          * @param array   $group_ids Groups ids
398          * @param boolean $check_dead Whether check "dead" records (?)
399          * @return array
400          * @throws \Exception
401          */
402         public static function expand(int $uid, array $group_ids, bool $check_dead = false): array
403         {
404                 if (!is_array($group_ids) || !count($group_ids)) {
405                         return [];
406                 }
407
408                 $return = [];
409                 $pubmail = false;
410                 $networks = Protocol::SUPPORT_PRIVATE;
411
412                 $mailacct = DBA::selectFirst('mailacct', ['pubmail'], ['`uid` = ? AND `server` != ""', $uid]);
413                 if (DBA::isResult($mailacct)) {
414                         $pubmail = $mailacct['pubmail'];
415                 }
416
417                 if (!$pubmail) {
418                         $networks = array_diff($networks, [Protocol::MAIL]);
419                 }
420
421                 $key = array_search(self::FOLLOWERS, $group_ids);
422                 if ($key !== false) {
423                         $followers = Contact::selectToArray(['id'], [
424                                 'uid' => $uid,
425                                 'rel' => [Contact::FOLLOWER, Contact::FRIEND],
426                                 'network' => $networks,
427                                 'contact-type' => [Contact::TYPE_UNKNOWN, Contact::TYPE_PERSON],
428                                 'archive' => false,
429                                 'pending' => false,
430                                 'blocked' => false,
431                         ]);
432
433                         foreach ($followers as $follower) {
434                                 $return[] = $follower['id'];
435                         }
436
437                         unset($group_ids[$key]);
438                 }
439
440                 $key = array_search(self::MUTUALS, $group_ids);
441                 if ($key !== false) {
442                         $mutuals = Contact::selectToArray(['id'], [
443                                 'uid' => $uid,
444                                 'rel' => [Contact::FRIEND],
445                                 'network' => $networks,
446                                 'contact-type' => [Contact::TYPE_UNKNOWN, Contact::TYPE_PERSON],
447                                 'archive' => false,
448                                 'pending' => false,
449                                 'blocked' => false,
450                         ]);
451
452                         foreach ($mutuals as $mutual) {
453                                 $return[] = $mutual['id'];
454                         }
455
456                         unset($group_ids[$key]);
457                 }
458
459                 $stmt = DBA::select('group_member', ['contact-id'], ['gid' => $group_ids]);
460                 while ($group_member = DBA::fetch($stmt)) {
461                         $return[] = $group_member['contact-id'];
462                 }
463                 DBA::close($stmt);
464
465                 if ($check_dead) {
466                         $return = Contact::pruneUnavailable($return);
467                 }
468
469                 return $return;
470         }
471
472         /**
473          * Returns a templated group selection list
474          *
475          * @param int    $uid User id
476          * @param int    $gid   An optional pre-selected group
477          * @param string $label An optional label of the list
478          * @return string
479          * @throws \Exception
480          */
481         public static function displayGroupSelection(int $uid, int $gid = 0, string $label = ''): string
482         {
483                 $display_groups = [
484                         [
485                                 'name' => '',
486                                 'id' => '0',
487                                 'selected' => ''
488                         ]
489                 ];
490
491                 $stmt = DBA::select('group', [], ['deleted' => false, 'uid' => $uid, 'cid' => null], ['order' => ['name']]);
492                 while ($group = DBA::fetch($stmt)) {
493                         $display_groups[] = [
494                                 'name' => $group['name'],
495                                 'id' => $group['id'],
496                                 'selected' => $gid == $group['id'] ? 'true' : ''
497                         ];
498                 }
499                 DBA::close($stmt);
500
501                 Logger::info('Got groups', $display_groups);
502
503                 if ($label == '') {
504                         $label = DI::l10n()->t('Default privacy group for new contacts');
505                 }
506
507                 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('group_selection.tpl'), [
508                         '$label' => $label,
509                         '$groups' => $display_groups
510                 ]);
511                 return $o;
512         }
513
514         /**
515          * Create group sidebar widget
516          *
517          * @param string $every
518          * @param string $each
519          * @param string $editmode
520          *    'standard' => include link 'Edit groups'
521          *    'extended' => include link 'Create new group'
522          *    'full' => include link 'Create new group' and provide for each group a link to edit this group
523          * @param string|int $group_id Distinct group id or 'everyone'
524          * @param int    $cid Contact id
525          * @return string Sidebar widget HTML code
526          * @throws \Exception
527          */
528         public static function sidebarWidget(string $every = 'contact', string $each = 'group', string $editmode = 'standard', $group_id = '', int $cid = 0)
529         {
530                 if (!Session::getLocalUser()) {
531                         return '';
532                 }
533
534                 $display_groups = [
535                         [
536                                 'text' => DI::l10n()->t('Everybody'),
537                                 'id' => 0,
538                                 'selected' => (($group_id === 'everyone') ? 'group-selected' : ''),
539                                 'href' => $every,
540                         ]
541                 ];
542
543                 $member_of = [];
544                 if ($cid) {
545                         $member_of = self::getIdsByContactId($cid);
546                 }
547
548                 $stmt = DBA::select('group', [], ['deleted' => false, 'uid' => Session::getLocalUser(), 'cid' => null], ['order' => ['name']]);
549                 while ($group = DBA::fetch($stmt)) {
550                         $selected = (($group_id == $group['id']) ? ' group-selected' : '');
551
552                         if ($editmode == 'full') {
553                                 $groupedit = [
554                                         'href' => 'group/' . $group['id'],
555                                         'title' => DI::l10n()->t('edit'),
556                                 ];
557                         } else {
558                                 $groupedit = null;
559                         }
560
561                         if ($each == 'group') {
562                                 $count = DBA::count('group_member', ['gid' => $group['id']]);
563                                 $group_name = sprintf('%s (%d)', $group['name'], $count);
564                         } else {
565                                 $group_name = $group['name'];
566                         }
567
568                         $display_groups[] = [
569                                 'id'   => $group['id'],
570                                 'cid'  => $cid,
571                                 'text' => $group_name,
572                                 'href' => $each . '/' . $group['id'],
573                                 'edit' => $groupedit,
574                                 'selected' => $selected,
575                                 'ismember' => in_array($group['id'], $member_of),
576                         ];
577                 }
578                 DBA::close($stmt);
579
580                 // Don't show the groups on the network page when there is only one
581                 if ((count($display_groups) <= 2) && ($each == 'network')) {
582                         return '';
583                 }
584
585                 $tpl = Renderer::getMarkupTemplate('group_side.tpl');
586                 $o = Renderer::replaceMacros($tpl, [
587                         '$add' => DI::l10n()->t('add'),
588                         '$title' => DI::l10n()->t('Groups'),
589                         '$groups' => $display_groups,
590                         'newgroup' => $editmode == 'extended' || $editmode == 'full' ? 1 : '',
591                         'grouppage' => 'group/',
592                         '$edittext' => DI::l10n()->t('Edit group'),
593                         '$ungrouped' => $every === 'contact' ? DI::l10n()->t('Contacts not in any group') : '',
594                         '$ungrouped_selected' => (($group_id === 'none') ? 'group-selected' : ''),
595                         '$createtext' => DI::l10n()->t('Create a new group'),
596                         '$creategroup' => DI::l10n()->t('Group Name: '),
597                         '$editgroupstext' => DI::l10n()->t('Edit groups'),
598                         '$form_security_token' => BaseModule::getFormSecurityToken('group_edit'),
599                 ]);
600
601                 return $o;
602         }
603
604         /**
605          * Fetch the group id for the given contact id
606          *
607          * @param integer $id Contact ID
608          * @return integer Group IO
609          */
610         public static function getIdForForum(int $id): int
611         {
612                 Logger::info('Get id for forum id', ['id' => $id]);
613                 $contact = Contact::getById($id, ['uid', 'name', 'contact-type', 'manually-approve']);
614                 if (empty($contact) || ($contact['contact-type'] != Contact::TYPE_COMMUNITY) || !$contact['manually-approve']) {
615                         return 0;
616                 }
617
618                 $group = DBA::selectFirst('group', ['id'], ['uid' => $contact['uid'], 'cid' => $id]);
619                 if (empty($group)) {
620                         $fields = [
621                                 'uid'  => $contact['uid'],
622                                 'name' => $contact['name'],
623                                 'cid'  => $id,
624                         ];
625                         DBA::insert('group', $fields);
626                         $gid = DBA::lastInsertId();
627                 } else {
628                         $gid = $group['id'];
629                 }
630
631                 return $gid;
632         }
633
634         /**
635          * Fetch the followers of a given contact id and store them as group members
636          *
637          * @param integer $id Contact ID
638          * @return void
639          */
640         public static function updateMembersForForum(int $id)
641         {
642                 Logger::info('Update forum members', ['id' => $id]);
643
644                 $contact = Contact::getById($id, ['uid', 'url']);
645                 if (empty($contact)) {
646                         return;
647                 }
648
649                 $apcontact = APContact::getByURL($contact['url']);
650                 if (empty($apcontact['followers'])) {
651                         return;
652                 }
653
654                 $gid = self::getIdForForum($id);
655                 if (empty($gid)) {
656                         return;
657                 }
658
659                 $group_members = DBA::selectToArray('group_member', ['contact-id'], ['gid' => $gid]);
660                 if (!empty($group_members)) {
661                         $current = array_unique(array_column($group_members, 'contact-id'));
662                 } else {
663                         $current = [];
664                 }
665
666                 foreach (ActivityPub::fetchItems($apcontact['followers'], $contact['uid']) as $follower) {
667                         $id = Contact::getIdForURL($follower);
668                         if (!in_array($id, $current)) {
669                                 DBA::insert('group_member', ['gid' => $gid, 'contact-id' => $id]);
670                         } else {
671                                 $key = array_search($id, $current);
672                                 unset($current[$key]);
673                         }
674                 }
675
676                 DBA::delete('group_member', ['gid' => $gid, 'contact-id' => $current]);
677                 Logger::info('Updated forum members', ['id' => $id, 'count' => DBA::count('group_member', ['gid' => $gid])]);
678         }
679 }