<?php
/**
- * @copyright Copyright (C) 2010-2021, the Friendica project
+ * @copyright Copyright (C) 2010-2022, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
class Group extends BaseModule
{
- public static function post()
+ protected function post(array $request = [])
{
- $a = DI::app();
-
if (DI::mode()->isAjax()) {
- self::ajaxPost();
+ $this->ajaxPost();
}
if (!local_user()) {
if ((DI::args()->getArgc() == 2) && (DI::args()->getArgv()[1] === 'new')) {
BaseModule::checkFormSecurityTokenRedirectOnError('/group/new', 'group_edit');
- $name = trim($_POST['groupname']);
+ $name = trim($request['groupname']);
$r = Model\Group::create(local_user(), $name);
if ($r) {
$r = Model\Group::getIdByName(local_user(), $name);
}
}
- public static function ajaxPost()
+ public function ajaxPost()
{
try {
- $a = DI::app();
-
if (!local_user()) {
throw new \Exception(DI::l10n()->t('Permission denied.'), 403);
}
- // POST /group/123/add/123
- // POST /group/123/remove/123
- // @TODO: Replace with parameter from router
- if (DI::args()->getArgc() == 4) {
- list($group_id, $command, $contact_id) = array_slice(DI::args()->getArgv(), 1);
+ if (isset($this->parameters['command'])) {
+ $group_id = $this->parameters['group'];
+ $contact_id = $this->parameters['contact'];
if (!Model\Group::exists($group_id, local_user())) {
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);
}
throw new \Exception(DI::l10n()->t('Contact is deleted.'), 410);
}
- switch($command) {
+ 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);
}
$message = DI::l10n()->t('Contact successfully removed from group.');
break;
- default:
- throw new \Exception(DI::l10n()->t('Unknown group command.'), 400);
}
} else {
throw new \Exception(DI::l10n()->t('Bad request.'), 400);
}
}
- public static function content()
+ protected function content(array $request = []): string
{
$change = false;