friendica/src/Module/Api/Friendica/Circle/Create.php

88 lines
2.7 KiB
PHP
Raw Normal View History

2022-01-15 22:38:19 +01:00
<?php
/**
2023-01-01 15:36:24 +01:00
* @copyright Copyright (C) 2010-2023, the Friendica project
2022-01-15 22:38:19 +01:00
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace Friendica\Module\Api\Friendica\Circle;
2022-01-15 22:38:19 +01:00
use Friendica\Database\DBA;
use Friendica\Model\Circle;
2022-01-15 22:38:19 +01:00
use Friendica\Module\BaseApi;
use Friendica\Network\HTTPException;
/**
* API endpoint: /api/friendica/circle_create
2022-01-15 22:38:19 +01:00
* API endpoint: /api/friendica/group_create
*/
class Create extends BaseApi
{
protected function post(array $request = [])
{
$this->checkAllowedScope(BaseApi::SCOPE_WRITE);
2022-01-15 22:38:19 +01:00
$uid = BaseApi::getCurrentUserID();
2022-01-16 08:14:09 +01:00
2022-01-15 22:38:19 +01:00
// params
2022-01-16 15:04:20 +01:00
$name = $this->getRequestValue($request, 'name', '');
$json = json_decode($request['json'], true);
2022-01-15 22:38:19 +01:00
$users = $json['user'];
2022-01-16 08:14:09 +01:00
2022-01-15 22:38:19 +01:00
// error if no name specified
if ($name == '') {
throw new HTTPException\BadRequestException('circle name not specified');
2022-01-15 22:38:19 +01:00
}
2022-01-16 08:14:09 +01:00
// error message if specified circle name already exists
2022-01-15 22:38:19 +01:00
if (DBA::exists('group', ['uid' => $uid, 'name' => $name, 'deleted' => false])) {
throw new HTTPException\BadRequestException('circle name already exists');
2022-01-15 22:38:19 +01:00
}
2022-01-16 08:14:09 +01:00
// Check if the circle needs to be reactivated
2022-01-15 22:38:19 +01:00
if (DBA::exists('group', ['uid' => $uid, 'name' => $name, 'deleted' => true])) {
$reactivate_circle = true;
2022-01-15 22:38:19 +01:00
}
2022-01-16 08:14:09 +01:00
$ret = Circle::create($uid, $name);
2022-01-15 22:38:19 +01:00
if ($ret) {
$gid = Circle::getIdByName($uid, $name);
2022-01-15 22:38:19 +01:00
} else {
throw new HTTPException\BadRequestException('other API error');
}
2022-01-16 08:14:09 +01:00
2022-01-15 22:38:19 +01:00
// add members
$erroraddinguser = false;
$errorusers = [];
2022-01-15 22:38:19 +01:00
foreach ($users as $user) {
$cid = $user['cid'];
if (DBA::exists('contact', ['id' => $cid, 'uid' => $uid])) {
Circle::addMember($gid, $cid);
2022-01-15 22:38:19 +01:00
} else {
$erroraddinguser = true;
$errorusers[] = $cid;
2022-01-15 22:38:19 +01:00
}
}
2022-01-16 08:14:09 +01:00
2022-01-15 22:38:19 +01:00
// return success message incl. missing users in array
$status = ($erroraddinguser ? 'missing user' : (!empty($reactivate_circle) ? 'reactivated' : 'ok'));
2022-01-16 08:14:09 +01:00
2022-01-15 22:38:19 +01:00
$result = ['success' => true, 'gid' => $gid, 'name' => $name, 'status' => $status, 'wrong users' => $errorusers];
2022-01-16 08:14:09 +01:00
$this->response->addFormattedContent('group_create', ['$result' => $result], $this->parameters['extension'] ?? null);
2022-01-15 22:38:19 +01:00
}
}