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
174 * 'name' => circle name
175 * 'count' => counted unseen circle items
178 public static function countUnseen()
180 $stmt = DBA::p("SELECT `circle`.`id`, `circle`.`name`,
181 (SELECT COUNT(*) FROM `post-user-view`
186 FROM `group_member` AS `circle_member`
187 WHERE `circle_member`.`gid` = `circle`.`id`)
189 FROM `group` AS `circle`
190 WHERE `circle`.`uid` = ?;",
191 DI::userSession()->getLocalUserId(),
192 DI::userSession()->getLocalUserId()
195 return DBA::toArray($stmt);
199 * Get the circle id for a user/name couple
201 * Returns false if no circle has been found.
203 * @param int $uid User id
204 * @param string $name Circle name
205 * @return int|boolean Circle's id number or false on error
208 public static function getIdByName(int $uid, string $name)
210 if (!$uid || !strlen($name)) {
214 $circle = DBA::selectFirst('group', ['id'], ['uid' => $uid, 'name' => $name]);
215 if (DBA::isResult($circle)) {
216 return $circle['id'];
223 * Mark a circle as deleted
229 public static function remove(int $gid): bool
235 $circle = DBA::selectFirst('group', ['uid'], ['id' => $gid]);
236 if (!DBA::isResult($circle)) {
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)) {
245 if ($user['def_gid'] == $gid) {
246 $user['def_gid'] = 0;
249 if (strpos($user['allow_gid'], '<' . $gid . '>') !== false) {
250 $user['allow_gid'] = str_replace('<' . $gid . '>', '', $user['allow_gid']);
253 if (strpos($user['deny_gid'], '<' . $gid . '>') !== false) {
254 $user['deny_gid'] = str_replace('<' . $gid . '>', '', $user['deny_gid']);
259 DBA::update('user', $user, ['uid' => $circle['uid']]);
263 // remove all members
264 DBA::delete('group_member', ['gid' => $gid]);
267 $return = DBA::update('group', ['deleted' => 1], ['id' => $gid]);
273 * Adds a contact to a circle
280 public static function addMember(int $gid, int $cid): bool
282 if (!$gid || !$cid) {
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.');
292 $cdata = Contact::getPublicAndUserContactID($cid, $circle['uid']);
293 if (empty($cdata['user'])) {
294 throw new HTTPException\NotFoundException('Invalid contact.');
297 return DBA::insert('group_member', ['gid' => $gid, 'contact-id' => $cdata['user']], Database::INSERT_IGNORE);
301 * Removes a contact from a circle
308 public static function removeMember(int $gid, int $cid): bool
310 if (!$gid || !$cid) {
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.');
320 $cdata = Contact::getPublicAndUserContactID($cid, $circle['uid']);
321 if (empty($cdata['user'])) {
322 throw new HTTPException\NotFoundException('Invalid contact.');
325 return DBA::delete('group_member', ['gid' => $gid, 'contact-id' => $cid]);
329 * Adds contacts to a circle
332 * @param array $contacts Array with contact ids
336 public static function addMembers(int $gid, array $contacts)
338 if (!$gid || !$contacts) {
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.');
348 foreach ($contacts as $cid) {
349 $cdata = Contact::getPublicAndUserContactID($cid, $circle['uid']);
350 if (empty($cdata['user'])) {
351 throw new HTTPException\NotFoundException('Invalid contact.');
354 DBA::insert('group_member', ['gid' => $gid, 'contact-id' => $cdata['user']], Database::INSERT_IGNORE);
359 * Removes contacts from a circle
361 * @param int $gid Circle id
362 * @param array $contacts Contact ids
366 public static function removeMembers(int $gid, array $contacts)
368 if (!$gid || !$contacts) {
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.');
380 foreach ($contacts as $cid) {
381 $cdata = Contact::getPublicAndUserContactID($cid, $circle['uid']);
382 if (empty($cdata['user'])) {
383 throw new HTTPException\NotFoundException('Invalid contact.');
386 $contactIds[] = $cdata['user'];
389 // Return status of deletion
390 return DBA::delete('group_member', ['gid' => $gid, 'contact-id' => $contactIds]);
394 * Returns the combined list of contact ids from a circle id list
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
403 public static function expand(int $uid, array $circle_ids, bool $check_dead = false, bool $expand_followers = true): array
405 if (!is_array($circle_ids) || !count($circle_ids)) {
411 $followers_collection = false;
412 $networks = Protocol::SUPPORT_PRIVATE;
414 $mailacct = DBA::selectFirst('mailacct', ['pubmail'], ['`uid` = ? AND `server` != ""', $uid]);
415 if (DBA::isResult($mailacct)) {
416 $pubmail = $mailacct['pubmail'];
420 $networks = array_diff($networks, [Protocol::MAIL]);
423 $key = array_search(self::FOLLOWERS, $circle_ids);
424 if ($key !== false) {
425 if ($expand_followers) {
426 $followers = Contact::selectToArray(['id'], [
428 'rel' => [Contact::FOLLOWER, Contact::FRIEND],
429 'network' => $networks,
430 'contact-type' => [Contact::TYPE_UNKNOWN, Contact::TYPE_PERSON],
436 foreach ($followers as $follower) {
437 $return[] = $follower['id'];
440 $followers_collection = true;
442 unset($circle_ids[$key]);
445 $key = array_search(self::MUTUALS, $circle_ids);
446 if ($key !== false) {
447 $mutuals = Contact::selectToArray(['id'], [
449 'rel' => [Contact::FRIEND],
450 'network' => $networks,
451 'contact-type' => [Contact::TYPE_UNKNOWN, Contact::TYPE_PERSON],
457 foreach ($mutuals as $mutual) {
458 $return[] = $mutual['id'];
461 unset($circle_ids[$key]);
464 $stmt = DBA::select('group_member', ['contact-id'], ['gid' => $circle_ids]);
465 while ($circle_member = DBA::fetch($stmt)) {
466 $return[] = $circle_member['contact-id'];
471 $return = Contact::pruneUnavailable($return);
474 if ($followers_collection) {
482 * Returns a templated circle selection list
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
491 public static function getSelectorHTML(int $uid, int $gid, string $id, string $label): string
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' : ''
511 Logger::info('Got circles', $display_circles);
513 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('circle_selection.tpl'), [
516 '$circles' => $display_circles
522 * Create circle sidebar widget
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
535 public static function sidebarWidget(string $every = 'contact', string $each = 'circle', string $editmode = 'standard', $circle_id = '', int $cid = 0)
537 if (!DI::userSession()->getLocalUserId()) {
543 'text' => DI::l10n()->t('Everybody'),
545 'selected' => (($circle_id === 'everyone') ? 'circle-selected' : ''),
552 $member_of = self::getIdsByContactId($cid);
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' : '');
559 if ($editmode == 'full') {
561 'href' => 'circle/' . $circle['id'],
562 'title' => DI::l10n()->t('edit'),
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);
574 $count = DBA::count('circle-member-view', $condition);
575 $circle_name = sprintf('%s (%d)', $circle['name'], $count);
577 $circle_name = $circle['name'];
580 $display_circles[] = [
581 'id' => $circle['id'],
583 'text' => $circle_name,
584 'href' => $each . '/' . $circle['id'],
585 'edit' => $circleedit,
586 'selected' => $selected,
587 'ismember' => in_array($circle['id'], $member_of),
592 // Don't show the circles on the network page when there is only one
593 if ((count($display_circles) <= 2) && ($each == 'network')) {
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'),
617 * Fetch the circle id for the given contact id
619 * @param integer $id Contact ID
620 * @return integer Circle ID
622 public static function getIdForGroup(int $id): int
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']) {
630 $circle = DBA::selectFirst('group', ['id'], ['uid' => $contact['uid'], 'cid' => $id]);
631 if (empty($circle)) {
633 'uid' => $contact['uid'],
634 'name' => $contact['name'],
637 DBA::insert('group', $fields);
638 $gid = DBA::lastInsertId();
640 $gid = $circle['id'];
647 * Fetch the followers of a given contact id and store them as circle members
649 * @param integer $id Contact ID
652 public static function updateMembersForGroup(int $id)
654 Logger::info('Update group members', ['id' => $id]);
656 $contact = Contact::getById($id, ['uid', 'url']);
657 if (empty($contact)) {
661 $apcontact = APContact::getByURL($contact['url']);
662 if (empty($apcontact['followers'])) {
666 $gid = self::getIdForGroup($id);
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'));
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]);
683 $key = array_search($id, $current);
684 unset($current[$key]);
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])]);