]> git.mxchange.org Git - friendica.git/blob - src/Module/Api/Mastodon/Accounts/UpdateCredentials.php
Make BaseApi->checkAllowedScope into an object method
[friendica.git] / src / Module / Api / Mastodon / Accounts / UpdateCredentials.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Module\Api\Mastodon\Accounts;
23
24 use Friendica\Core\Logger;
25 use Friendica\DI;
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;
31
32 /**
33  * @see https://docs.joinmastodon.org/methods/accounts/
34  */
35 class UpdateCredentials extends BaseApi
36 {
37         protected function patch(array $request = [])
38         {
39                 $this->checkAllowedScope(self::SCOPE_WRITE);
40                 $uid = self::getCurrentUserID();
41
42                 $owner = User::getOwnerDataById($uid);
43
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'],
51                         'avatar'            => [],
52                         'header'            => [],
53                 ], $request);
54
55                 $user    = [];
56                 $profile = [];
57
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;
63                 } else {
64                         $user['account-type'] = $owner['contact-type'];
65                 }
66
67                 $profile['net-publish'] = $request['discoverable'];
68
69                 if (!empty($request['display_name'])) {
70                         $user['username'] = $request['display_name'];
71                 }
72
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;
80                         }
81                 }
82
83                 if (!empty($request['note'])) {
84                         $profile['about'] = $request['note'];
85                 }
86
87                 Logger::debug('Patch data', ['data' => $request, 'files' => $_FILES]);
88
89                 Logger::info('Update profile and user', ['uid' => $uid, 'user' => $user, 'profile' => $profile]);
90
91                 if (!empty($request['avatar'])) {
92                         Photo::uploadAvatar($uid, $request['avatar']);
93                 }
94
95                 if (!empty($request['header'])) {
96                         Photo::uploadBanner($uid, $request['header']);
97                 }
98
99                 User::update($user, $uid);
100                 Profile::update($profile, $uid);
101
102                 $cdata = Contact::getPublicAndUserContactID($owner['id'], $uid);
103                 if (empty($cdata)) {
104                         DI::mstdnError()->InternalError();
105                 }
106
107                 $account = DI::mstdnAccount()->createFromContactId($cdata['user'], $uid);
108                 $this->response->addJsonContent($account->toArray());
109         }
110 }