<?php
/**
- * @copyright Copyright (C) 2020, Friendica
+ * @copyright Copyright (C) 2010-2021, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
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
public static function countUnseen()
{
$stmt = DBA::p("SELECT `group`.`id`, `group`.`name`,
- (SELECT COUNT(*) FROM `post-view`
+ (SELECT COUNT(*) FROM `post-user`
WHERE `uid` = ?
AND `unseen`
AND `contact-id` IN
return $return;
}
- /**
- * Mark a group as deleted based on its name
- *
- * @param int $uid
- * @param string $name
- * @return bool
- * @throws \Exception
- * @deprecated Use Group::remove instead
- *
- */
- public static function removeByName($uid, $name)
- {
- $return = false;
- if (!empty($uid) && !empty($name)) {
- $gid = self::getIdByName($uid, $name);
-
- $return = self::remove($gid);
- }
-
- return $return;
- }
-
/**
* Adds a contact to a 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);
}
/**
* @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]);
-
- return $return;
- }
-
- /**
- * Removes a contact from a group based on its name
- *
- * @param int $uid
- * @param string $name
- * @param int $cid
- * @return boolean
- * @throws \Exception
- * @deprecated Use Group::removeMember instead
- *
- */
- public static function removeMemberByName($uid, $name, $cid)
- {
- $gid = self::getIdByName($uid, $name);
+ // @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 = self::removeMember($gid, $cid);
+ $cdata = Contact::getPublicAndUserContactID($cid, $group['uid']);
+ if (empty($cdata['user'])) {
+ throw new HTTPException\NotFoundException('Invalid contact.');
+ }
- return $return;
+ return DBA::delete('group_member', ['gid' => $gid, 'contact-id' => $cid]);
}
/**