]> git.mxchange.org Git - friendica.git/blob - src/Factory/Api/Twitter/DirectMessage.php
Use Args::getMethod() at various places
[friendica.git] / src / Factory / Api / Twitter / DirectMessage.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\Content\Text\BBCode;
26 use Friendica\Content\Text\HTML;
27 use Friendica\Database\Database;
28 use Friendica\Factory\Api\Twitter\User as TwitterUser;
29 use Friendica\Model\Contact;
30 use Friendica\Network\HTTPException;
31 use Psr\Log\LoggerInterface;
32
33 class DirectMessage extends BaseFactory
34 {
35         /** @var Database */
36         private $dba;
37         /** @var twitterUser entity */
38         private $twitterUser;
39
40         public function __construct(LoggerInterface $logger, Database $dba, TwitterUser $twitteruser)
41         {
42                 parent::__construct($logger);
43                 $this->dba         = $dba;
44                 $this->twitterUser = $twitteruser;
45         }
46
47         /**
48          * Create a direct message from a given mail id
49          *
50          * @todo Processing of "getUserObjects" (true/false) and "getText" (html/plain)
51          *
52          * @param int    $id        Mail id
53          * @param int    $uid       Mail user
54          * @param string $text_mode Either empty, "html" or "plain"
55          *
56          * @return \Friendica\Object\Api\Twitter\DirectMessage
57          */
58         public function createFromMailId(int $id, int $uid, string $text_mode = ''): \Friendica\Object\Api\Twitter\DirectMessage
59         {
60                 $mail = $this->dba->selectFirst('mail', [], ['id' => $id, 'uid' => $uid]);
61                 if (!$mail) {
62                         throw new HTTPException\NotFoundException('Direct message with ID ' . $mail . ' not found.');
63                 }
64
65                 if (!empty($text_mode)) {
66                         $title = $mail['title'];
67                         if ($text_mode == 'html') {
68                                 $text = BBCode::convertForUriId($mail['uri-id'], $mail['body'], BBCode::API);
69                         } elseif ($text_mode == 'plain') {
70                                 $text = HTML::toPlaintext(BBCode::convertForUriId($mail['uri-id'], $mail['body'], BBCode::API), 0);
71                         }
72                 } else {
73                         $title = '';
74                         $text  = $mail['title'] . "\n" . HTML::toPlaintext(BBCode::convertForUriId($mail['uri-id'], $mail['body'], BBCode::API), 0);
75                 }
76
77                 $pcid = Contact::getPublicIdByUserId($uid);
78
79                 if ($mail['author-id'] == $pcid) {
80                         $sender    = $this->twitterUser->createFromUserId($uid, true);
81                         $recipient = $this->twitterUser->createFromContactId($mail['contact-id'], $uid, true);
82                 } else {
83                         $sender    = $this->twitterUser->createFromContactId($mail['author-id'], $uid, true);
84                         $recipient = $this->twitterUser->createFromUserId($uid, true);
85                 }
86
87                 return new \Friendica\Object\Api\Twitter\DirectMessage($mail, $sender, $recipient, $text, $title);
88         }
89 }