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