]> git.mxchange.org Git - friendica.git/blob - src/Factory/Api/Mastodon/Conversation.php
Standards
[friendica.git] / src / Factory / Api / Mastodon / Conversation.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\Mastodon;
23
24 use Friendica\BaseFactory;
25 use Friendica\Database\Database;
26 use Friendica\Model\Contact;
27 use Friendica\Network\HTTPException;
28 use ImagickException;
29 use Psr\Log\LoggerInterface;
30
31 class Conversation extends BaseFactory
32 {
33         /** @var Database */
34         private $dba;
35         /** @var Status */
36         private $mstdnStatusFactory;
37         /** @var Account */
38         private $mstdnAccountFactory;
39
40         public function __construct(LoggerInterface $logger, Database $dba, Status $mstdnStatusFactory, Account $mstdnAccountFactoryFactory)
41         {
42                 parent::__construct($logger);
43                 $this->dba                 = $dba;
44                 $this->mstdnStatusFactory  = $mstdnStatusFactory;
45                 $this->mstdnAccountFactory = $mstdnAccountFactoryFactory;
46         }
47
48         /**
49          * @throws ImagickException|HTTPException\InternalServerErrorException|HTTPException\NotFoundException
50          */
51         public function CreateFromConvId(int $id): \Friendica\Object\Api\Mastodon\Conversation
52         {
53                 $accounts    = [];
54                 $unread      = false;
55                 $last_status = null;
56
57                 $ids = [];
58
59                 $mails = $this->dba->select('mail', ['id', 'from-url', 'uid', 'seen'], ['convid' => $id], ['order' => ['id' => true]]);
60                 while ($mail = $this->dba->fetch($mails)) {
61                         if (!$mail['seen']) {
62                                 $unread = true;
63                         }
64
65                         $id = Contact::getIdForURL($mail['from-url'], 0, false);
66                         if (in_array($id, $ids)) {
67                                 continue;
68                         }
69
70                         $ids[] = $id;
71
72                         if (empty($last_status)) {
73                                 $last_status = $this->mstdnStatusFactory->createFromMailId($mail['id']);
74                         }
75
76                         $accounts[] = $this->mstdnAccountFactory->createFromContactId($id, 0);
77                 }
78
79                 return new \Friendica\Object\Api\Mastodon\Conversation($id, $accounts, $unread, $last_status);
80         }
81 }