3 * @copyright Copyright (C) 2010-2022, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Factory\Api\Twitter;
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;
33 class DirectMessage extends BaseFactory
37 /** @var twitterUser entity */
40 public function __construct(LoggerInterface $logger, Database $dba, TwitterUser $twitteruser)
42 parent::__construct($logger);
44 $this->twitterUser = $twitteruser;
48 * Create a direct message from a given mail id
50 * @todo Processing of "getUserObjects" (true/false) and "getText" (html/plain)
52 * @param int $id Mail id
53 * @param int $uid Mail user
54 * @param string $text_mode Either empty, "html" or "plain"
56 * @return \Friendica\Object\Api\Twitter\DirectMessage
58 public function createFromMailId(int $id, int $uid, string $text_mode = ''): \Friendica\Object\Api\Twitter\DirectMessage
60 $mail = $this->dba->selectFirst('mail', [], ['id' => $id, 'uid' => $uid]);
62 throw new HTTPException\NotFoundException('Direct message with ID ' . $mail . ' not found.');
65 if (!empty($text_mode)) {
66 $title = $mail['title'];
67 if ($text_mode == 'html') {
68 $text = BBCode::convertForUriId($mail['uri-id'], $mail['body'], BBCode::TWITTER_API);
69 } elseif ($text_mode == 'plain') {
70 $text = HTML::toPlaintext(BBCode::convertForUriId($mail['uri-id'], $mail['body'], BBCode::TWITTER_API), 0);
74 $text = $mail['title'] . "\n" . HTML::toPlaintext(BBCode::convertForUriId($mail['uri-id'], $mail['body'], BBCode::TWITTER_API), 0);
77 $pcid = Contact::getPublicIdByUserId($uid);
79 if ($mail['author-id'] == $pcid) {
80 $sender = $this->twitterUser->createFromUserId($uid, true);
81 $recipient = $this->twitterUser->createFromContactId($mail['contact-id'], $uid, true);
83 $sender = $this->twitterUser->createFromContactId($mail['author-id'], $uid, true);
84 $recipient = $this->twitterUser->createFromUserId($uid, true);
87 return new \Friendica\Object\Api\Twitter\DirectMessage($mail, $sender, $recipient, $text, $title);