3 * @copyright Copyright (C) 2010-2023, the Friendica project
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\Module\Api\Mastodon\Accounts;
24 use Friendica\Core\Logger;
26 use Friendica\Model\Contact;
27 use Friendica\Model\Photo;
28 use Friendica\Model\Profile;
29 use Friendica\Model\User;
30 use Friendica\Module\BaseApi;
33 * @see https://docs.joinmastodon.org/methods/accounts/
35 class UpdateCredentials extends BaseApi
37 protected function patch(array $request = [])
39 self::checkAllowedScope(self::SCOPE_WRITE);
40 $uid = self::getCurrentUserID();
42 $owner = User::getOwnerDataById($uid);
44 $request = $this->getRequest([
45 'bot' => ($owner['contact-type'] == Contact::TYPE_NEWS),
46 'discoverable' => $owner['net-publish'],
47 'display_name' => $owner['name'],
48 'fields_attributes' => [],
49 'locked' => $owner['manually-approve'],
50 'note' => $owner['about'],
58 if ($request['bot']) {
59 $user['account-type'] = Contact::TYPE_NEWS;
60 $user['page-flags'] = User::PAGE_FLAGS_SOAPBOX;
61 } elseif ($owner['contact-type'] == Contact::TYPE_NEWS) {
62 $user['account-type'] = Contact::TYPE_PERSON;
64 $user['account-type'] = $owner['contact-type'];
67 $profile['net-publish'] = $request['discoverable'];
69 if (!empty($request['display_name'])) {
70 $user['username'] = $request['display_name'];
73 if ($user['account-type'] == Contact::TYPE_COMMUNITY) {
74 $user['page-flags'] = $request['locked'] ? User::PAGE_FLAGS_PRVGROUP : User::PAGE_FLAGS_COMMUNITY;
75 } elseif ($user['account-type'] == Contact::TYPE_PERSON) {
76 if ($request['locked']) {
77 $user['page-flags'] = User::PAGE_FLAGS_NORMAL;
78 } elseif ($owner['page-flags'] == User::PAGE_FLAGS_NORMAL) {
79 $user['page-flags'] = User::PAGE_FLAGS_SOAPBOX;
83 if (!empty($request['note'])) {
84 $profile['about'] = $request['note'];
87 Logger::debug('Patch data', ['data' => $request, 'files' => $_FILES]);
89 Logger::info('Update profile and user', ['uid' => $uid, 'user' => $user, 'profile' => $profile]);
91 if (!empty($request['avatar'])) {
92 Photo::uploadAvatar($uid, $request['avatar']);
95 if (!empty($request['header'])) {
96 Photo::uploadBanner($uid, $request['header']);
99 User::update($user, $uid);
100 Profile::update($profile, $uid);
102 $cdata = Contact::getPublicAndUserContactID($owner['id'], $uid);
104 DI::mstdnError()->InternalError();
107 $account = DI::mstdnAccount()->createFromContactId($cdata['user'], $uid);
108 $this->response->exitWithJson($account->toArray());