]> git.mxchange.org Git - friendica.git/blob - src/Factory/Api/Twitter/User.php
Merge pull request #11035 from annando/api-next
[friendica.git] / src / Factory / Api / Twitter / User.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, 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\Factory\Api\Twitter;
23
24 use Friendica\BaseFactory;
25 use Friendica\Model\APContact;
26 use Friendica\Model\Contact;
27 use Friendica\Network\HTTPException;
28 use Friendica\Factory\Api\Twitter\Status;
29 use Friendica\Model\Item;
30 use Friendica\Model\Post;
31 use Psr\Log\LoggerInterface;
32
33 class User extends BaseFactory
34 {
35         /** @var Status entity */
36         private $status;
37
38         public function __construct(LoggerInterface $logger, Status $status)
39         {
40                 parent::__construct($logger);
41                 $this->status = $status;
42
43         }
44
45         /**
46          * @param int  $contactId
47          * @param int  $uid Public contact (=0) or owner user id
48          * @param bool $skip_status
49          * @param bool $include_user_entities
50          * @return \Friendica\Object\Api\Twitter\User
51          * @throws HTTPException\InternalServerErrorException
52          * @throws \ImagickException
53          */
54         public function createFromContactId(int $contactId, $uid = 0, $skip_status = true, $include_user_entities = true)
55         {
56                 $cdata = Contact::getPublicAndUserContactID($contactId, $uid);
57                 if (!empty($cdata)) {
58                         $publicContact = Contact::getById($cdata['public']);
59                         $userContact = Contact::getById($cdata['user']);
60                 } else {
61                         $publicContact = Contact::getById($contactId);
62                         $userContact = [];
63                 }
64
65                 $apcontact = APContact::getByURL($publicContact['url'], false);
66
67                 $status = null;
68
69                 if (!$skip_status) {
70                         $post = Post::selectFirstPost(['uri-id'],
71                                 ['author-id' => $publicContact['id'], 'gravity' => [GRAVITY_COMMENT, GRAVITY_PARENT], 'private'  => [Item::PUBLIC, Item::UNLISTED]],
72                                 ['order' => ['uri-id' => true]]);
73                         if (!empty($post['uri-id'])) {
74                                 $status = $this->status->createFromUriId($post['uri-id'], $uid)->toArray();
75                         }
76                 }
77
78                 return new \Friendica\Object\Api\Twitter\User($publicContact, $apcontact, $userContact, $status, $include_user_entities);
79         }
80
81         public function createFromUserId(int $uid, $skip_status = true, $include_user_entities = true)
82         {
83                 return $this->createFromContactId(Contact::getPublicIdByUserId($uid), $uid, $skip_status, $include_user_entities);
84         }
85 }