3 * @copyright Copyright (C) 2010-2023, 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\Module\Post\Tag;
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;
42 * Asynchronous post tagging endpoint. Only used in Ajax calls.
44 class Add extends \Friendica\BaseModule
46 /** @var IHandleUserSessions */
49 public function __construct(IHandleUserSessions $session, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, array $server, array $parameters = [])
51 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
53 $this->session = $session;
56 protected function post(array $request = [])
58 if (!$this->session->isAuthenticated()) {
62 $term = trim($request['term'] ?? '');
64 $term = str_replace([',', ' ', '<', '>'], ['', '_', '', ''], $term);
70 $item_id = $this->parameters['item_id'];
72 $this->logger->debug('Tag', ['term' => $term, 'item_id' => $item_id]);
74 $item = Post::selectFirst([], ['id' => $item_id]);
76 $this->logger->info('Item not found', ['item_id' => $item_id]);
80 $owner_uid = $item['uid'];
81 if ($this->session->getLocalUserId() != $owner_uid) {
85 $contact = Contact::selectFirst([], ['self' => true, 'uid' => $this->session->getLocalUserId()]);
87 $this->logger->warning('Self contact not found.', ['uid' => $this->session->getLocalUserId()]);
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']);
97 <type>$targettype</type>
99 <id>{$item['uri']}</id>
102 <content>$body</content>
106 $objtype = Activity\ObjectType::TAGTERM;
107 $tagid = $this->baseUrl . '/search?tag=' . urlencode($term);
108 $xterm = XML::escape($term);
112 <type>$objtype</type>
116 <title>$xterm</title>
117 <content>$xterm</content>
121 $tagger_link = '[url=' . $contact['url'] . ']' . $contact['name'] . '[/url]';
122 $aauthor_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]';
127 'guid' => System::createUUID(),
128 'uri' => Item::newURI(),
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, $aauthor_link, $post_link, $term_link),
142 'verb' => Activity::TAG,
143 'target-type' => $targettype,
145 'object-type' => $objtype,
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'],
157 $post_id = Item::insert($post);
159 if (!$item['visible']) {
160 Item::update(['visible' => true], ['id' => $item['id']]);
163 Tag::store($item['uri-id'], Tag::HASHTAG, $term);
165 $post['id'] = $post_id;
166 Hook::callAll('post_local_end', $post);
168 $post = Post::selectFirst(['uri-id', 'uid'], ['id' => $post_id]);
170 Worker::add(Worker::PRIORITY_HIGH, 'Notifier', Delivery::POST, $post['uri-id'], $post['uid']);