From 8bfe877629dca1e431cc6d31c5cc03ea4094665e Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Fri, 26 Nov 2021 09:48:37 -0500 Subject: [PATCH] Add backward compatibility with user contacts in Module\Group --- src/Model/Group.php | 36 +++++++++++++++++++++++++----------- src/Module/Group.php | 16 +++++++++++++--- 2 files changed, 38 insertions(+), 14 deletions(-) diff --git a/src/Model/Group.php b/src/Model/Group.php index 8e7f807cfe..f6700a4566 100644 --- a/src/Model/Group.php +++ b/src/Model/Group.php @@ -25,8 +25,10 @@ use Friendica\BaseModule; use Friendica\Core\Logger; use Friendica\Core\Protocol; use Friendica\Core\Renderer; +use Friendica\Database\Database; use Friendica\Database\DBA; use Friendica\DI; +use Friendica\Network\HTTPException; /** * functions for interacting with the group database table @@ -259,21 +261,24 @@ class Group * @return boolean * @throws \Exception */ - public static function addMember($gid, $cid) + public static function addMember(int $gid, int $cid): bool { if (!$gid || !$cid) { return false; } - $row_exists = DBA::exists('group_member', ['gid' => $gid, 'contact-id' => $cid]); - if ($row_exists) { - // Row already existing, nothing to do - $return = true; - } else { - $return = DBA::insert('group_member', ['gid' => $gid, 'contact-id' => $cid]); + // @TODO Backward compatibility with user contacts, remove by version 2022.03 + $group = DBA::selectFirst('group', ['uid'], ['id' => $gid]); + if (empty($group)) { + throw new HTTPException\NotFoundException('Group not found.'); } - return $return; + $cdata = Contact::getPublicAndUserContactID($cid, $group['uid']); + if (empty($cdata['user'])) { + throw new HTTPException\NotFoundException('Invalid contact.'); + } + + return DBA::insert('group_member', ['gid' => $gid, 'contact-id' => $cdata['user']], Database::INSERT_IGNORE); } /** @@ -284,15 +289,24 @@ class Group * @return boolean * @throws \Exception */ - public static function removeMember($gid, $cid) + public static function removeMember(int $gid, int $cid): bool { if (!$gid || !$cid) { return false; } - $return = DBA::delete('group_member', ['gid' => $gid, 'contact-id' => $cid]); + // @TODO Backward compatibility with user contacts, remove by version 2022.03 + $group = DBA::selectFirst('group', ['uid'], ['id' => $gid]); + if (empty($group)) { + throw new HTTPException\NotFoundException('Group not found.'); + } - return $return; + $cdata = Contact::getPublicAndUserContactID($cid, $group['uid']); + if (empty($cdata['user'])) { + throw new HTTPException\NotFoundException('Invalid contact.'); + } + + return DBA::delete('group_member', ['gid' => $gid, 'contact-id' => $cid]); } /** diff --git a/src/Module/Group.php b/src/Module/Group.php index 30ca0805a0..39eb896d40 100644 --- a/src/Module/Group.php +++ b/src/Module/Group.php @@ -93,7 +93,17 @@ class Group extends BaseModule throw new \Exception(DI::l10n()->t('Unknown group.'), 404); } - $contact = DBA::selectFirst('contact', ['deleted'], ['id' => $contact_id, 'uid' => local_user()]); + // @TODO Backward compatibility with user contacts, remove by version 2022.03 + $cdata = Model\Contact::getPublicAndUserContactID($contact_id, local_user()); + if (empty($cdata['public'])) { + throw new \Exception(DI::l10n()->t('Contact not found.'), 404); + } + + if (empty($cdata['user'])) { + throw new \Exception(DI::l10n()->t('Invalid contact.'), 404); + } + + $contact = Model\Contact::getById($cdata['user'], ['deleted']); if (!DBA::isResult($contact)) { throw new \Exception(DI::l10n()->t('Contact not found.'), 404); } @@ -104,14 +114,14 @@ class Group extends BaseModule switch($this->parameters['command']) { case 'add': - if (!Model\Group::addMember($group_id, $contact_id)) { + if (!Model\Group::addMember($group_id, $cdata['user'])) { throw new \Exception(DI::l10n()->t('Unable to add the contact to the group.'), 500); } $message = DI::l10n()->t('Contact successfully added to group.'); break; case 'remove': - if (!Model\Group::removeMember($group_id, $contact_id)) { + if (!Model\Group::removeMember($group_id, $cdata['user'])) { throw new \Exception(DI::l10n()->t('Unable to remove the contact from the group.'), 500); }