]> git.mxchange.org Git - friendica.git/blob - src/Module/Post/Tag/Add.php
Use the post language for the language detection / config for quality
[friendica.git] / src / Module / Post / Tag / Add.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, 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\Module\Post\Tag;
23
24 use Friendica\App;
25 use Friendica\Core\Hook;
26 use Friendica\Core\L10n;
27 use Friendica\Core\Session\Capability\IHandleUserSessions;
28 use Friendica\Core\System;
29 use Friendica\Core\Worker;
30 use Friendica\Model\Contact;
31 use Friendica\Model\Item;
32 use Friendica\Model\Post;
33 use Friendica\Model\Tag;
34 use Friendica\Module\Response;
35 use Friendica\Protocol\Activity;
36 use Friendica\Protocol\Delivery;
37 use Friendica\Util\Profiler;
38 use Friendica\Util\XML;
39 use Psr\Log\LoggerInterface;
40
41 /**
42  * Asynchronous post tagging endpoint. Only used in Ajax calls.
43  */
44 class Add extends \Friendica\BaseModule
45 {
46         /** @var IHandleUserSessions */
47         private $session;
48
49         public function __construct(IHandleUserSessions $session, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, array $server, array $parameters = [])
50         {
51                 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
52
53                 $this->session = $session;
54         }
55
56         protected function post(array $request = [])
57         {
58                 if (!$this->session->isAuthenticated()) {
59                         return;
60                 }
61
62                 $term = trim($request['term'] ?? '');
63                 // no commas allowed
64                 $term = str_replace([',', ' ', '<', '>'], ['', '_', '', ''], $term);
65
66                 if (!$term) {
67                         return;
68                 }
69
70                 $item_id = $this->parameters['item_id'];
71
72                 $this->logger->debug('Tag', ['term' => $term, 'item_id' => $item_id]);
73
74                 $item = Post::selectFirst([], ['id' => $item_id]);
75                 if (!$item) {
76                         $this->logger->info('Item not found', ['item_id' => $item_id]);
77                         return;
78                 }
79
80                 $owner_uid = $item['uid'];
81                 if ($this->session->getLocalUserId() != $owner_uid) {
82                         return;
83                 }
84
85                 $contact = Contact::selectFirst([], ['self' => true, 'uid' => $this->session->getLocalUserId()]);
86                 if (!$contact) {
87                         $this->logger->warning('Self contact not found.', ['uid' => $this->session->getLocalUserId()]);
88                         return;
89                 }
90
91                 $targettype = $item['resource-id'] ? Activity\ObjectType::IMAGE : Activity\ObjectType::NOTE;
92                 $link       = XML::escape('<link rel="alternate" type="text/html" href="' . $this->baseUrl . '/display/' . $item['guid'] . '" />' . "\n");
93                 $body       = XML::escape($item['body']);
94
95                 $target = <<< EOT
96         <target>
97                 <type>$targettype</type>
98                 <local>1</local>
99                 <id>{$item['uri']}</id>
100                 <link>$link</link>
101                 <title></title>
102                 <content>$body</content>
103         </target>
104 EOT;
105
106                 $objtype = Activity\ObjectType::TAGTERM;
107                 $tagid   = $this->baseUrl . '/search?tag=' . urlencode($term);
108                 $xterm   = XML::escape($term);
109
110                 $obj = <<< EOT
111         <object>
112                 <type>$objtype</type>
113                 <local>1</local>
114                 <id>$tagid</id>
115                 <link>$tagid</link>
116                 <title>$xterm</title>
117                 <content>$xterm</content>
118         </object>
119 EOT;
120
121                 $tagger_link = '[url=' . $contact['url'] . ']' . $contact['name'] . '[/url]';
122                 $author_link = '[url=' . $item['author-link'] . ']' . $item['author-name'] . '[/url]';
123                 $post_link   = '[url=' . $item['plink'] . ']' . ($item['resource-id'] ? $this->t('photo') : $this->t('post')) . '[/url]';
124                 $term_link   = '#[url=' . $tagid . ']' . $term . '[/url]';
125
126                 $post = [
127                         'guid'          => System::createUUID(),
128                         'uri'           => Item::newURI(),
129                         'uid'           => $owner_uid,
130                         'contact-id'    => $contact['id'],
131                         'wall'          => $item['wall'],
132                         'gravity'       => Item::GRAVITY_COMMENT,
133                         'parent'        => $item['id'],
134                         'thr-parent'    => $item['uri'],
135                         'owner-name'    => $item['author-name'],
136                         'owner-link'    => $item['author-link'],
137                         'owner-avatar'  => $item['author-avatar'],
138                         'author-name'   => $contact['name'],
139                         'author-link'   => $contact['url'],
140                         'author-avatar' => $contact['thumb'],
141                         'body'          => $this->t('%1$s tagged %2$s\'s %3$s with %4$s', $tagger_link, $author_link, $post_link, $term_link),
142                         'verb'          => Activity::TAG,
143                         'target-type'   => $targettype,
144                         'target'        => $target,
145                         'object-type'   => $objtype,
146                         'object'        => $obj,
147                         'private'       => $item['private'],
148                         'allow_cid'     => $item['allow_cid'],
149                         'allow_gid'     => $item['allow_gid'],
150                         'deny_cid'      => $item['deny_cid'],
151                         'deny_gid'      => $item['deny_gid'],
152                         'visible'       => 1,
153                         'unseen'        => 1,
154                         'origin'        => 1,
155                 ];
156
157                 $post_id = Item::insert($post);
158
159                 if (!$item['visible']) {
160                         Item::update(['visible' => true], ['id' => $item['id']]);
161                 }
162
163                 Tag::store($item['uri-id'], Tag::HASHTAG, $term);
164
165                 $post['id'] = $post_id;
166                 Hook::callAll('post_local_end', $post);
167
168                 $post = Post::selectFirst(['uri-id', 'uid'], ['id' => $post_id]);
169
170                 Worker::add(Worker::PRIORITY_HIGH, 'Notifier', Delivery::POST, $post['uri-id'], $post['uid']);
171                 System::exit();
172         }
173 }