3 * @copyright Copyright (C) 2020, Friendica
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\Core\Logger;
26 use Friendica\Core\Protocol;
27 use Friendica\Core\Renderer;
28 use Friendica\Database\DBA;
32 * functions for interacting with the group database table
36 const FOLLOWERS = '~';
39 public static function getByUserId($uid, $includesDeleted = false)
41 $conditions = ['uid' => $uid];
43 if (!$includesDeleted) {
44 $conditions['deleted'] = false;
47 return DBA::selectToArray('group', [], $conditions);
51 * @param int $group_id
55 public static function exists($group_id, $uid = null)
57 $condition = ['id' => $group_id, 'deleted' => false];
65 return DBA::exists('group', $condition);
69 * Create a new contact group
71 * Note: If we found a deleted group with the same name, we restore it
78 public static function create($uid, $name)
81 if (!empty($uid) && !empty($name)) {
82 $gid = self::getIdByName($uid, $name); // check for dupes
84 // This could be a problem.
85 // Let's assume we've just created a group which we once deleted
86 // all the old members are gone, but the group remains so we don't break any security
87 // access lists. What we're doing here is reviving the dead group, but old content which
88 // was restricted to this group may now be seen by the new group members.
89 $group = DBA::selectFirst('group', ['deleted'], ['id' => $gid]);
90 if (DBA::isResult($group) && $group['deleted']) {
91 DBA::update('group', ['deleted' => 0], ['id' => $gid]);
92 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.') . EOL);
97 $return = DBA::insert('group', ['uid' => $uid, 'name' => $name]);
99 $return = DBA::lastInsertId();
106 * Update group information.
108 * @param int $id Group ID
109 * @param string $name Group name
111 * @return bool Was the update successful?
114 public static function update($id, $name)
116 return DBA::update('group', ['name' => $name], ['id' => $id]);
120 * Get a list of group ids a contact belongs to
126 public static function getIdsByContactId($cid)
130 $stmt = DBA::select('group_member', ['gid'], ['contact-id' => $cid]);
131 while ($group = DBA::fetch($stmt)) {
132 $return[] = $group['gid'];
137 $contact = Contact::getById($cid, ['rel']);
138 if ($contact['rel'] == Contact::FOLLOWER || $contact['rel'] == Contact::FRIEND) {
139 $return[] = self::FOLLOWERS;
142 if ($contact['rel'] == Contact::FRIEND) {
143 $return[] = self::MUTUALS;
150 * count unread group items
152 * Count unread items of each groups of the local user
156 * 'name' => group name
157 * 'count' => counted unseen group items
160 public static function countUnseen()
162 $stmt = DBA::p("SELECT `group`.`id`, `group`.`name`,
163 (SELECT COUNT(*) FROM `item` FORCE INDEX (`uid_unseen_contactid`)
169 WHERE `group_member`.`gid` = `group`.`id`)
172 WHERE `group`.`uid` = ?;",
177 return DBA::toArray($stmt);
181 * Get the group id for a user/name couple
183 * Returns false if no group has been found.
186 * @param string $name
187 * @return int|boolean
190 public static function getIdByName($uid, $name)
192 if (!$uid || !strlen($name)) {
196 $group = DBA::selectFirst('group', ['id'], ['uid' => $uid, 'name' => $name]);
197 if (DBA::isResult($group)) {
205 * Mark a group as deleted
211 public static function remove($gid)
217 $group = DBA::selectFirst('group', ['uid'], ['id' => $gid]);
218 if (!DBA::isResult($group)) {
222 // remove group from default posting lists
223 $user = DBA::selectFirst('user', ['def_gid', 'allow_gid', 'deny_gid'], ['uid' => $group['uid']]);
224 if (DBA::isResult($user)) {
227 if ($user['def_gid'] == $gid) {
228 $user['def_gid'] = 0;
231 if (strpos($user['allow_gid'], '<' . $gid . '>') !== false) {
232 $user['allow_gid'] = str_replace('<' . $gid . '>', '', $user['allow_gid']);
235 if (strpos($user['deny_gid'], '<' . $gid . '>') !== false) {
236 $user['deny_gid'] = str_replace('<' . $gid . '>', '', $user['deny_gid']);
241 DBA::update('user', $user, ['uid' => $group['uid']]);
245 // remove all members
246 DBA::delete('group_member', ['gid' => $gid]);
249 $return = DBA::update('group', ['deleted' => 1], ['id' => $gid]);
255 * Mark a group as deleted based on its name
258 * @param string $name
261 * @deprecated Use Group::remove instead
264 public static function removeByName($uid, $name)
267 if (!empty($uid) && !empty($name)) {
268 $gid = self::getIdByName($uid, $name);
270 $return = self::remove($gid);
277 * Adds a contact to a group
284 public static function addMember($gid, $cid)
286 if (!$gid || !$cid) {
290 $row_exists = DBA::exists('group_member', ['gid' => $gid, 'contact-id' => $cid]);
292 // Row already existing, nothing to do
295 $return = DBA::insert('group_member', ['gid' => $gid, 'contact-id' => $cid]);
302 * Removes a contact from a group
309 public static function removeMember($gid, $cid)
311 if (!$gid || !$cid) {
315 $return = DBA::delete('group_member', ['gid' => $gid, 'contact-id' => $cid]);
321 * Removes a contact from a group based on its name
324 * @param string $name
328 * @deprecated Use Group::removeMember instead
331 public static function removeMemberByName($uid, $name, $cid)
333 $gid = self::getIdByName($uid, $name);
335 $return = self::removeMember($gid, $cid);
341 * Returns the combined list of contact ids from a group id list
344 * @param array $group_ids
345 * @param boolean $check_dead
349 public static function expand($uid, array $group_ids, $check_dead = false)
351 if (!is_array($group_ids) || !count($group_ids)) {
357 $networks = Protocol::SUPPORT_PRIVATE;
359 $mailacct = DBA::selectFirst('mailacct', ['pubmail'], ['`uid` = ? AND `server` != ""', $uid]);
360 if (DBA::isResult($mailacct)) {
361 $pubmail = $mailacct['pubmail'];
365 $networks = array_diff($networks, [Protocol::MAIL]);
368 $key = array_search(self::FOLLOWERS, $group_ids);
369 if ($key !== false) {
370 $followers = Contact::selectToArray(['id'], [
372 'rel' => [Contact::FOLLOWER, Contact::FRIEND],
373 'network' => $networks,
374 'contact-type' => [Contact::TYPE_UNKNOWN, Contact::TYPE_PERSON],
380 foreach ($followers as $follower) {
381 $return[] = $follower['id'];
384 unset($group_ids[$key]);
387 $key = array_search(self::MUTUALS, $group_ids);
388 if ($key !== false) {
389 $mutuals = Contact::selectToArray(['id'], [
391 'rel' => [Contact::FRIEND],
392 'network' => $networks,
393 'contact-type' => [Contact::TYPE_UNKNOWN, Contact::TYPE_PERSON],
399 foreach ($mutuals as $mutual) {
400 $return[] = $mutual['id'];
403 unset($group_ids[$key]);
406 $stmt = DBA::select('group_member', ['contact-id'], ['gid' => $group_ids]);
407 while ($group_member = DBA::fetch($stmt)) {
408 $return[] = $group_member['contact-id'];
413 $return = Contact::pruneUnavailable($return);
420 * Returns a templated group selection list
423 * @param int $gid An optional pre-selected group
424 * @param string $label An optional label of the list
428 public static function displayGroupSelection($uid, $gid = 0, $label = '')
438 $stmt = DBA::select('group', [], ['deleted' => 0, 'uid' => $uid], ['order' => ['name']]);
439 while ($group = DBA::fetch($stmt)) {
440 $display_groups[] = [
441 'name' => $group['name'],
442 'id' => $group['id'],
443 'selected' => $gid == $group['id'] ? 'true' : ''
448 Logger::info('Got groups', $display_groups);
451 $label = DI::l10n()->t('Default privacy group for new contacts');
454 $o = Renderer::replaceMacros(Renderer::getMarkupTemplate('group_selection.tpl'), [
456 '$groups' => $display_groups
462 * Create group sidebar widget
464 * @param string $every
465 * @param string $each
466 * @param string $editmode
467 * 'standard' => include link 'Edit groups'
468 * 'extended' => include link 'Create new group'
469 * 'full' => include link 'Create new group' and provide for each group a link to edit this group
470 * @param string $group_id
475 public static function sidebarWidget($every = 'contact', $each = 'group', $editmode = 'standard', $group_id = '', $cid = 0)
483 'text' => DI::l10n()->t('Everybody'),
485 'selected' => (($group_id === 'everyone') ? 'group-selected' : ''),
492 $member_of = self::getIdsByContactId($cid);
495 $stmt = DBA::select('group', [], ['deleted' => 0, 'uid' => local_user()], ['order' => ['name']]);
496 while ($group = DBA::fetch($stmt)) {
497 $selected = (($group_id == $group['id']) ? ' group-selected' : '');
499 if ($editmode == 'full') {
501 'href' => 'group/' . $group['id'],
502 'title' => DI::l10n()->t('edit'),
508 $display_groups[] = [
509 'id' => $group['id'],
511 'text' => $group['name'],
512 'href' => $each . '/' . $group['id'],
513 'edit' => $groupedit,
514 'selected' => $selected,
515 'ismember' => in_array($group['id'], $member_of),
520 // Don't show the groups on the network page when there is only one
521 if ((count($display_groups) <= 2) && ($each == 'network')) {
525 $tpl = Renderer::getMarkupTemplate('group_side.tpl');
526 $o = Renderer::replaceMacros($tpl, [
527 '$add' => DI::l10n()->t('add'),
528 '$title' => DI::l10n()->t('Groups'),
529 '$groups' => $display_groups,
530 'newgroup' => $editmode == 'extended' || $editmode == 'full' ? 1 : '',
531 'grouppage' => 'group/',
532 '$edittext' => DI::l10n()->t('Edit group'),
533 '$ungrouped' => $every === 'contact' ? DI::l10n()->t('Contacts not in any group') : '',
534 '$ungrouped_selected' => (($group_id === 'none') ? 'group-selected' : ''),
535 '$createtext' => DI::l10n()->t('Create a new group'),
536 '$creategroup' => DI::l10n()->t('Group Name: '),
537 '$editgroupstext' => DI::l10n()->t('Edit groups'),
538 '$form_security_token' => BaseModule::getFormSecurityToken('group_edit'),