]> git.mxchange.org Git - friendica.git/blob - src/Factory/Api/Mastodon/Status.php
API: Counts added, local query improved
[friendica.git] / src / Factory / Api / Mastodon / Status.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
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\Factory\Api\Mastodon;
23
24 use Friendica\App\BaseURL;
25 use Friendica\BaseFactory;
26 use Friendica\Database\DBA;
27 use Friendica\DI;
28 use Friendica\Model\Item;
29 use Friendica\Model\Verb;
30 use Friendica\Network\HTTPException;
31 use Friendica\Protocol\Activity;
32 use Friendica\Repository\ProfileField;
33 use Psr\Log\LoggerInterface;
34
35 class Status extends BaseFactory
36 {
37         /** @var BaseURL */
38         protected $baseUrl;
39         /** @var ProfileField */
40         protected $profileField;
41         /** @var Field */
42         protected $mstdnField;
43
44         public function __construct(LoggerInterface $logger, BaseURL $baseURL, ProfileField $profileField, Field $mstdnField)
45         {
46                 parent::__construct($logger);
47
48                 $this->baseUrl = $baseURL;
49                 $this->profileField = $profileField;
50                 $this->mstdnField = $mstdnField;
51         }
52
53         /**
54          * @param int $uriId Uri-ID of the item
55          * @param int $uid   Item user
56          * @return \Friendica\Object\Api\Mastodon\Status
57          * @throws HTTPException\InternalServerErrorException
58          * @throws \ImagickException
59          */
60         public function createFromUriId(int $uriId, $uid = 0)
61         {
62                 $item = Item::selectFirst([], ['uri-id' => $uriId, 'uid' => $uid]);
63                 $account = DI::mstdnAccount()->createFromContactId($item['author-id']);
64
65                 $count['replies'] = DBA::count('item', ['thr-parent-id' => $uriId, 'uid' => $uid, 'gravity' => GRAVITY_COMMENT]);
66                 $count['reblogs'] = DBA::count('item', ['thr-parent-id' => $uriId, 'uid' => $uid, 'gravity' => GRAVITY_ACTIVITY, 'vid' => Verb::getID(Activity::ANNOUNCE)]);
67                 $count['favourites'] = DBA::count('item', ['thr-parent-id' => $uriId, 'uid' => $uid, 'gravity' => GRAVITY_ACTIVITY, 'vid' => Verb::getID(Activity::LIKE)]);
68
69                 return new \Friendica\Object\Api\Mastodon\Status($item, $account, $count);
70         }
71 }