3 * @copyright Copyright (C) 2010-2023, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Model;
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;
32 use Friendica\Network\HTTPException;
33 use Friendica\Protocol\ActivityPub;
36 * functions for interacting with the group database table
40 const FOLLOWERS = '~';
44 * Fetches circle record by user id and maybe includes deleted circles as well
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
50 public static function getByUserId(int $uid, bool $includesDeleted = false)
52 $conditions = ['uid' => $uid, 'cid' => null];
54 if (!$includesDeleted) {
55 $conditions['deleted'] = false;
58 return DBA::selectToArray('group', [], $conditions);
62 * Checks whether given circle id is found in database
64 * @param int $circle_id Circle id
65 * @param int $uid Optional user id
69 public static function exists(int $circle_id, int $uid = null): bool
71 $condition = ['id' => $circle_id, 'deleted' => false];
79 return DBA::exists('group', $condition);
83 * Create a new contact circle
85 * Note: If we found a deleted circle with the same name, we restore it
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
92 public static function create(int $uid, string $name)
95 if (!empty($uid) && !empty($name)) {
96 $gid = self::getIdByName($uid, $name); // check for dupes
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.'));
111 $return = DBA::insert('group', ['uid' => $uid, 'name' => $name]);
113 $return = DBA::lastInsertId();
120 * Update circle information.
122 * @param int $id Circle ID
123 * @param string $name Circle name
125 * @return bool Was the update successful?
128 public static function update(int $id, string $name): bool
130 return DBA::update('group', ['name' => $name], ['id' => $id]);
134 * Get a list of circle ids a contact belongs to
136 * @param int $cid Contact id
137 * @return array Circle ids
140 public static function getIdsByContactId(int $cid): array
142 $contact = Contact::getById($cid, ['rel']);
149 $stmt = DBA::select('group_member', ['gid'], ['contact-id' => $cid]);
150 while ($circle = DBA::fetch($stmt)) {
151 $circleIds[] = $circle['gid'];
156 if ($contact['rel'] == Contact::FOLLOWER || $contact['rel'] == Contact::FRIEND) {
157 $circleIds[] = self::FOLLOWERS;
160 if ($contact['rel'] == Contact::FRIEND) {
161 $circleIds[] = self::MUTUALS;
168 * count unread circle items
170 * Count unread items of each circle of the local user
175 * 'name' => circle name
176 * 'count' => counted unseen circle items
179 public static function countUnseen(int $uid)
181 $stmt = DBA::p("SELECT `circle`.`id`, `circle`.`name`,
182 (SELECT COUNT(*) FROM `post-user`
187 FROM `group_member` AS `circle_member`
188 WHERE `circle_member`.`gid` = `circle`.`id`)
190 FROM `group` AS `circle`
191 WHERE `circle`.`uid` = ?;",
196 return DBA::toArray($stmt);
200 * Get the circle id for a user/name couple
202 * Returns false if no circle has been found.
204 * @param int $uid User id
205 * @param string $name Circle name
206 * @return int|boolean Circle's id number or false on error
209 public static function getIdByName(int $uid, string $name)
211 if (!$uid || !strlen($name)) {
215 $circle = DBA::selectFirst('group', ['id'], ['uid' => $uid, 'name' => $name]);
216 if (DBA::isResult($circle)) {
217 return $circle['id'];
224 * Mark a circle as deleted
230 public static function remove(int $gid): bool
236 $circle = DBA::selectFirst('group', ['uid'], ['id' => $gid]);
237 if (!DBA::isResult($circle)) {
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)) {
246 if ($user['def_gid'] == $gid) {
247 $user['def_gid'] = 0;
250 if (strpos($user['allow_gid'], '<' . $gid . '>') !== false) {
251 $user['allow_gid'] = str_replace('<' . $gid . '>', '', $user['allow_gid']);
254 if (strpos($user['deny_gid'], '<' . $gid . '>') !== false) {
255 $user['deny_gid'] = str_replace('<' . $gid . '>', '', $user['deny_gid']);
260 DBA::update('user', $user, ['uid' => $circle['uid']]);
264 // remove all members
265 DBA::delete('group_member', ['gid' => $gid]);
268 $return = DBA::update('group', ['deleted' => 1], ['id' => $gid]);
274 * Adds a contact to a circle
281 public static function addMember(int $gid, int $cid): bool
283 if (!$gid || !$cid) {
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.');
293 $cdata = Contact::getPublicAndUserContactID($cid, $circle['uid']);
294 if (empty($cdata['user'])) {
295 throw new HTTPException\NotFoundException('Invalid contact.');
298 return DBA::insert('group_member', ['gid' => $gid, 'contact-id' => $cdata['user']], Database::INSERT_IGNORE);
302 * Removes a contact from a circle
309 public static function removeMember(int $gid, int $cid): bool
311 if (!$gid || !$cid) {
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.');
321 $cdata = Contact::getPublicAndUserContactID($cid, $circle['uid']);
322 if (empty($cdata['user'])) {
323 throw new HTTPException\NotFoundException('Invalid contact.');
326 return DBA::delete('group_member', ['gid' => $gid, 'contact-id' => $cid]);
330 * Adds contacts to a circle
333 * @param array $contacts Array with contact ids
337 public static function addMembers(int $gid, array $contacts)
339 if (!$gid || !$contacts) {
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.');
349 foreach ($contacts as $cid) {
350 $cdata = Contact::getPublicAndUserContactID($cid, $circle['uid']);
351 if (empty($cdata['user'])) {
352 throw new HTTPException\NotFoundException('Invalid contact.');
355 DBA::insert('group_member', ['gid' => $gid, 'contact-id' => $cdata['user']], Database::INSERT_IGNORE);
360 * Removes contacts from a circle
362 * @param int $gid Circle id
363 * @param array $contacts Contact ids
367 public static function removeMembers(int $gid, array $contacts)
369 if (!$gid || !$contacts) {
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.');
381 foreach ($contacts as $cid) {
382 $cdata = Contact::getPublicAndUserContactID($cid, $circle['uid']);
383 if (empty($cdata['user'])) {
384 throw new HTTPException\NotFoundException('Invalid contact.');
387 $contactIds[] = $cdata['user'];
390 // Return status of deletion
391 return DBA::delete('group_member', ['gid' => $gid, 'contact-id' => $contactIds]);
395 * Returns the combined list of contact ids from a circle id list
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
404 public static function expand(int $uid, array $circle_ids, bool $check_dead = false, bool $expand_followers = true): array
406 if (!is_array($circle_ids) || !count($circle_ids)) {
412 $followers_collection = false;
413 $networks = Protocol::SUPPORT_PRIVATE;
415 $mailacct = DBA::selectFirst('mailacct', ['pubmail'], ['`uid` = ? AND `server` != ""', $uid]);
416 if (DBA::isResult($mailacct)) {
417 $pubmail = $mailacct['pubmail'];
421 $networks = array_diff($networks, [Protocol::MAIL]);
424 $key = array_search(self::FOLLOWERS, $circle_ids);
425 if ($key !== false) {
426 if ($expand_followers) {
427 $followers = Contact::selectToArray(['id'], [
429 'rel' => [Contact::FOLLOWER, Contact::FRIEND],
430 'network' => $networks,
431 'contact-type' => [Contact::TYPE_UNKNOWN, Contact::TYPE_PERSON],
437 foreach ($followers as $follower) {
438 $return[] = $follower['id'];
441 $followers_collection = true;
443 unset($circle_ids[$key]);
446 $key = array_search(self::MUTUALS, $circle_ids);
447 if ($key !== false) {
448 $mutuals = Contact::selectToArray(['id'], [
450 'rel' => [Contact::FRIEND],
451 'network' => $networks,
452 'contact-type' => [Contact::TYPE_UNKNOWN, Contact::TYPE_PERSON],
458 foreach ($mutuals as $mutual) {
459 $return[] = $mutual['id'];
462 unset($circle_ids[$key]);
465 $stmt = DBA::select('group_member', ['contact-id'], ['gid' => $circle_ids]);
466 while ($circle_member = DBA::fetch($stmt)) {
467 $return[] = $circle_member['contact-id'];
472 $return = Contact::pruneUnavailable($return);
475 if ($followers_collection) {
483 * Returns a templated circle selection list
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
492 public static function getSelectorHTML(int $uid, int $gid, string $id, string $label): string
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' : ''
512 Logger::info('Got circles', $display_circles);
514 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('circle_selection.tpl'), [
517 '$circles' => $display_circles
523 * Create circle sidebar widget
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
536 public static function sidebarWidget(string $every = 'contact', string $each = 'circle', string $editmode = 'standard', $circle_id = '', int $cid = 0)
538 if (!DI::userSession()->getLocalUserId()) {
544 'text' => DI::l10n()->t('Everybody'),
546 'selected' => (($circle_id === 'everyone') ? 'circle-selected' : ''),
553 $member_of = self::getIdsByContactId($cid);
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' : '');
560 if ($editmode == 'full') {
562 'href' => 'circle/' . $circle['id'],
563 'title' => DI::l10n()->t('edit'),
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);
575 $count = DBA::count('circle-member-view', $condition);
576 $circle_name = sprintf('%s (%d)', $circle['name'], $count);
578 $circle_name = $circle['name'];
581 $display_circles[] = [
582 'id' => $circle['id'],
584 'text' => $circle_name,
585 'href' => $each . '/' . $circle['id'],
586 'edit' => $circleedit,
587 'selected' => $selected,
588 'ismember' => in_array($circle['id'], $member_of),
593 // Don't show the circles on the network page when there is only one
594 if ((count($display_circles) <= 2) && ($each == 'network')) {
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'),
618 * Fetch the circle id for the given contact id
620 * @param integer $id Contact ID
621 * @return integer Circle ID
623 public static function getIdForGroup(int $id): int
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']) {
631 $circle = DBA::selectFirst('group', ['id'], ['uid' => $contact['uid'], 'cid' => $id]);
632 if (empty($circle)) {
634 'uid' => $contact['uid'],
635 'name' => $contact['name'],
638 DBA::insert('group', $fields);
639 $gid = DBA::lastInsertId();
641 $gid = $circle['id'];
648 * Fetch the followers of a given contact id and store them as circle members
650 * @param integer $id Contact ID
653 public static function updateMembersForGroup(int $id)
655 Logger::info('Update group members', ['id' => $id]);
657 $contact = Contact::getById($id, ['uid', 'url']);
658 if (empty($contact)) {
662 $apcontact = APContact::getByURL($contact['url']);
663 if (empty($apcontact['followers'])) {
667 $gid = self::getIdForGroup($id);
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'));
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]);
684 $key = array_search($id, $current);
685 unset($current[$key]);
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])]);