From 9e9a104320b4f6a20743bd5bee3f61329a44d2ca Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 18 Apr 2020 20:46:41 +0000 Subject: [PATCH] Tags can now be added and removed from photos --- mod/photos.php | 10 +++++++++- mod/tagrm.php | 9 ++++++++- src/Model/Tag.php | 46 +++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 62 insertions(+), 3 deletions(-) diff --git a/mod/photos.php b/mod/photos.php index 3f558429d4..0107a179d0 100644 --- a/mod/photos.php +++ b/mod/photos.php @@ -36,6 +36,7 @@ use Friendica\Model\Contact; use Friendica\Model\Item; use Friendica\Model\Photo; use Friendica\Model\Profile; +use Friendica\Model\Tag; use Friendica\Model\User; use Friendica\Module\BaseProfile; use Friendica\Network\Probe; @@ -421,7 +422,7 @@ function photos_post(App $a) } if ($item_id) { - $item = Item::selectFirst(['tag', 'inform'], ['id' => $item_id, 'uid' => $page_owner_uid]); + $item = Item::selectFirst(['tag', 'inform', 'uri-id'], ['id' => $item_id, 'uid' => $page_owner_uid]); if (DBA::isResult($item)) { $old_tag = $item['tag']; @@ -521,10 +522,17 @@ function photos_post(App $a) $profile = str_replace(',', '%2c', $profile); $str_tags .= '@[url=' . $profile . ']' . $newname . '[/url]'; + + if (!empty($item['uri-id'])) { + Tag::store($item['uri-id'], Tag::MENTION, $newname, $profile); + } } } elseif (strpos($tag, '#') === 0) { $tagname = substr($tag, 1); $str_tags .= '#[url=' . DI::baseUrl() . "/search?tag=" . $tagname . ']' . $tagname . '[/url],'; + if (!empty($item['uri-id'])) { + Tag::store($item['uri-id'], Tag::HASHTAG, $tagname); + } } } } diff --git a/mod/tagrm.php b/mod/tagrm.php index 2fa75133ca..51000c9854 100644 --- a/mod/tagrm.php +++ b/mod/tagrm.php @@ -24,6 +24,7 @@ use Friendica\Content\Text\BBCode; use Friendica\Database\DBA; use Friendica\DI; use Friendica\Model\Item; +use Friendica\Model\Tag; use Friendica\Model\Term; use Friendica\Util\Strings; @@ -62,7 +63,7 @@ function update_tags($item_id, $tags){ return; } - $item = Item::selectFirst(['tag'], ['id' => $item_id, 'uid' => local_user()]); + $item = Item::selectFirst(['tag', 'uri-id'], ['id' => $item_id, 'uid' => local_user()]); if (!DBA::isResult($item)) { return; } @@ -70,6 +71,12 @@ function update_tags($item_id, $tags){ $old_tags = explode(',', $item['tag']); foreach ($tags as $new_tag) { + if (preg_match_all('/([#@!])\[url\=([^\[\]]*)\]([^\[\]]*)\[\/url\]/ism', $new_tag, $results, PREG_SET_ORDER)) { + foreach ($results as $tag) { + Tag::removeByHash($item['uri-id'], $tag[1], $tag[3], $tag[2]); + } + } + foreach ($old_tags as $index => $old_tag) { if (strcmp($old_tag, $new_tag) == 0) { unset($old_tags[$index]); diff --git a/src/Model/Tag.php b/src/Model/Tag.php index c3d1c50162..b9e04ddd7a 100644 --- a/src/Model/Tag.php +++ b/src/Model/Tag.php @@ -92,7 +92,7 @@ class Tag } if (empty($cid)) { - $fields = ['name' => substr($name, 0, 96)]; + $fields = ['name' => substr($name, 0, 96), 'url' => '']; if (!empty($url) && ($url != $name)) { $fields['url'] = strtolower($url); @@ -163,4 +163,48 @@ class Tag self::storeByHash($uriid, $tag[1], $tag[3], $tag[2]); } } + + /** + * Remove tag/mention + * + * @param integer $uriid + * @param integer $type + * @param string $name + * @param string $url + */ + public static function remove(int $uriid, int $type, string $name, string $url = '') + { + $tag = DBA::fetchFirst("SELECT `id` FROM `tag` INNER JOIN `post-tag` ON `post-tag`.`tid` = `tag`.`id` + WHERE `uri-id` = ? AND `type` = ? AND `name` = ? AND `url` = ?", $uriid, $type, $name, $url); + if (!DBA::isResult($tag)) { + return; + } + Logger::info('Removing tag/mention', ['uri-id' => $uriid, 'tid' => $tag['id'], 'name' => $name, 'url' => $url]); + DBA::delete('post-tag', ['uri-id' => $uriid, 'tid' => $tag['id']]); + } + + /** + * Remove tag/mention + * + * @param integer $uriid + * @param string $hash + * @param string $name + * @param string $url + */ + public static function removeByHash(int $uriid, string $hash, string $name, string $url = '') + { + if ($hash == self::TAG_CHARACTER[self::MENTION]) { + $type = self::MENTION; + } elseif ($hash == self::TAG_CHARACTER[self::EXCLUSIVE_MENTION]) { + $type = self::EXCLUSIVE_MENTION; + } elseif ($hash == self::TAG_CHARACTER[self::IMPLICIT_MENTION]) { + $type = self::IMPLICIT_MENTION; + } elseif ($hash == self::TAG_CHARACTER[self::HASHTAG]) { + $type = self::HASHTAG; + } else { + return; + } + + self::remove($uriid, $type, $name, $url); + } } -- 2.39.5