]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Item.php
Added brackets
[friendica.git] / src / Model / Item.php
index 0050f930982a1f488a8e9f99ce63698905b86c55..8af14e8243c3bd46966b4089a1880020ef28e6d4 100644 (file)
@@ -29,6 +29,7 @@ use Friendica\Core\Protocol;
 use Friendica\Core\Renderer;
 use Friendica\Core\Session;
 use Friendica\Core\System;
+use Friendica\Model\Tag;
 use Friendica\Core\Worker;
 use Friendica\Database\DBA;
 use Friendica\Database\DBStructure;
@@ -40,11 +41,10 @@ use Friendica\Protocol\Diaspora;
 use Friendica\Util\DateTimeFormat;
 use Friendica\Util\Map;
 use Friendica\Util\Network;
-use Friendica\Util\Security;
 use Friendica\Util\Strings;
 use Friendica\Worker\Delivery;
-use Text_LanguageDetect;
 use Friendica\Repository\PermissionSet as RepPermissionSet;
+use LanguageDetection\Language;
 
 class Item
 {
@@ -57,6 +57,18 @@ class Item
        const PT_VIDEO = 18;
        const PT_DOCUMENT = 19;
        const PT_EVENT = 32;
+       const PT_TAG = 64;
+       const PT_TO = 65;
+       const PT_CC = 66;
+       const PT_BTO = 67;
+       const PT_BCC = 68;
+       const PT_FOLLOWER = 69;
+       const PT_ANNOUNCEMENT = 70;
+       const PT_COMMENT = 71;
+       const PT_STORED = 72;
+       const PT_GLOBAL = 73;
+       const PT_RELAY = 74;
+       const PT_FETCHED = 75;
        const PT_PERSONAL_NOTE = 128;
 
        // Field list that is used to display the items
@@ -68,6 +80,7 @@ class Item
                'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid', 'item_id',
                'author-id', 'author-link', 'author-name', 'author-avatar', 'author-network',
                'owner-id', 'owner-link', 'owner-name', 'owner-avatar', 'owner-network',
+               'causer-id', 'causer-link', 'causer-name', 'causer-avatar', 'causer-contact-type',
                'contact-id', 'contact-uid', 'contact-link', 'contact-name', 'contact-avatar',
                'writable', 'self', 'cid', 'alias', 'pinned',
                'event-id', 'event-created', 'event-edited', 'event-start', 'event-finish',
@@ -105,7 +118,7 @@ class Item
                        'title', 'content-warning', 'body', 'location', 'coord', 'app',
                        'rendered-hash', 'rendered-html', 'object-type', 'object', 'target-type', 'target',
                        'author-id', 'author-link', 'author-name', 'author-avatar', 'author-network',
-                       'owner-id', 'owner-link', 'owner-name', 'owner-avatar'];
+                       'owner-id', 'owner-link', 'owner-name', 'owner-avatar', 'causer-id'];
 
        // List of all verbs that don't need additional content data.
        // Never reorder or remove entries from this list. Just add new ones at the end, if needed.
@@ -201,19 +214,7 @@ class Item
                        return [];
                }
 
-               if (empty($condition) || !is_array($condition)) {
-                       $condition = ['iid' => $pinned];
-               } else {
-                       reset($condition);
-                       $first_key = key($condition);
-                       if (!is_int($first_key)) {
-                               $condition['iid'] = $pinned;
-                       } else {
-                               $values_string = substr(str_repeat("?, ", count($pinned)), 0, -2);
-                               $condition[0] = '(' . $condition[0] . ") AND `iid` IN (" . $values_string . ")";
-                               $condition = array_merge($condition, $pinned);
-                       }
-               }
+               $condition = DBA::mergeConditions(['iid' => $pinned], $condition);
 
                return self::selectThreadForUser($uid, $selected, $condition, $params);
        }
@@ -653,7 +654,7 @@ class Item
                $fields = [];
 
                $fields['item'] = ['id', 'uid', 'parent', 'uri', 'parent-uri', 'thr-parent',
-                       'guid', 'uri-id', 'parent-uri-id', 'thr-parent-id', 'vid',
+                       'guid', 'uri-id', 'parent-uri-id', 'thr-parent-id', 'vid', 'causer-id',
                        'contact-id', 'owner-id', 'author-id', 'type', 'wall', 'gravity', 'extid',
                        'created', 'edited', 'commented', 'received', 'changed', 'psid',
                        'resource-id', 'event-id', 'attach', 'post-type', 'file',
@@ -681,11 +682,15 @@ class Item
                $fields['owner'] = ['url' => 'owner-link', 'name' => 'owner-name', 'addr' => 'owner-addr',
                        'thumb' => 'owner-avatar', 'nick' => 'owner-nick', 'network' => 'owner-network'];
 
+               $fields['causer'] = ['url' => 'causer-link', 'name' => 'causer-name', 'addr' => 'causer-addr',
+                       'thumb' => 'causer-avatar', 'nick' => 'causer-nick', 'network' => 'causer-network',
+                       'contact-type' => 'causer-contact-type'];
+
                $fields['contact'] = ['url' => 'contact-link', 'name' => 'contact-name', 'thumb' => 'contact-avatar',
                        'writable', 'self', 'id' => 'cid', 'alias', 'uid' => 'contact-uid',
                        'photo', 'name-date', 'uri-date', 'avatar-date', 'thumb', 'dfrn-id'];
 
-               $fields['parent-item'] = ['guid' => 'parent-guid', 'network' => 'parent-network'];
+               $fields['parent-item'] = ['guid' => 'parent-guid', 'network' => 'parent-network', 'author-id' => 'parent-author-id'];
 
                $fields['parent-item-author'] = ['url' => 'parent-author-link', 'name' => 'parent-author-name',
                        'network' => 'parent-author-network'];
@@ -769,6 +774,9 @@ class Item
                                $joins .= " LEFT JOIN `contact` AS `owner` ON `owner`.`id` = $master_table.`owner-id`";
                        }
                }
+               if (strpos($sql_commands, "`causer`.") !== false) {
+                       $joins .= " LEFT JOIN `contact` AS `causer` ON `causer`.`id` = `item`.`causer-id`";
+               }
 
                if (strpos($sql_commands, "`group_member`.") !== false) {
                        $joins .= " STRAIGHT_JOIN `group_member` ON `group_member`.`contact-id` = $master_table.`contact-id`";
@@ -1691,10 +1699,10 @@ class Item
 
                $item['plink'] = ($item['plink'] ?? '') ?: DI::baseUrl() . '/display/' . urlencode($item['guid']);
 
-               $item['language'] = self::getLanguage($item);
-
                $item['gravity'] = self::getGravity($item);
 
+               $item['language'] = self::getLanguage($item);
+
                $default = ['url' => $item['author-link'], 'name' => $item['author-name'],
                        'photo' => $item['author-avatar'], 'network' => $item['network']];
                $item['author-id'] = ($item['author-id'] ?? 0) ?: Contact::getIdForURL($item['author-link'], 0, null, $default);
@@ -1703,6 +1711,11 @@ class Item
                        'photo' => $item['owner-avatar'], 'network' => $item['network']];
                $item['owner-id'] = ($item['owner-id'] ?? 0) ?: Contact::getIdForURL($item['owner-link'], 0, null, $default);
 
+               $actor = ($item['gravity'] == GRAVITY_PARENT) ? $item['owner-id'] : $item['author-id'];
+               if (!$item['origin'] && ($item['uid'] != 0) && Contact::isSharing($actor, $item['uid'])) {
+                       $item['post-type'] = self::PT_FOLLOWER;
+               }
+
                // Ensure that there is an avatar cache
                Contact::checkAvatarCache($item['author-id']);
                Contact::checkAvatarCache($item['owner-id']);
@@ -1714,8 +1727,7 @@ class Item
                        return 0;
                }
 
-               // We don't store the causer, we only have it here for the checks in the function above
-               unset($item['causer-id']);
+               // We don't store the causer link, only the id
                unset($item['causer-link']);
 
                // We don't store these fields anymore in the item table
@@ -1822,6 +1834,11 @@ class Item
 
                if (!in_array($item['verb'], self::ACTIVITIES)) {
                        $item['icid'] = self::insertContent($item);
+                       if (empty($item['icid'])) {
+                               // This most likely happens when identical posts arrives from different sources at the same time
+                               Logger::warning('No content stored, quitting', ['guid' => $item['guid'], 'uri-id' => $item['uri-id'], 'causer-id' => ($item['causer-id'] ?? 0), 'post-type' => $item['post-type'], 'network' => $item['network']]);
+                               return 0;
+                       }
                }
 
                $body = $item['body'];
@@ -1994,24 +2011,21 @@ class Item
        }
 
        /**
-        * Change the owner of a parent item if it had been shared by a forum or a user contact
+        * Change the owner of a parent item if it had been shared by a forum
         *
         * (public) forum posts in the new format consist of the regular post by the author
         * followed by an announce message sent from the forum account.
         * Changing the owner helps in grouping forum posts.
         *
-        * For other reshared posts this function changes the owner of that post
-        * if the author is not a user contact but the post had been shared by one.
-        * This helps in detecting why this post appeared in the timeline.
-        *
         * @param array $item
         * @return void
         */
        private static function setOwnerforResharedItem(array $item)
        {
-               $parent = self::selectFirst(['id', 'owner-id', 'author-id', 'author-link'], ['uri-id' => $item['parent-uri-id'], 'uid' => $item['uid']]);
+               $parent = self::selectFirst(['id', 'owner-id', 'author-id', 'author-link', 'origin', 'post-type'],
+                       ['uri-id' => $item['thr-parent-id'], 'uid' => $item['uid']]);
                if (!DBA::isResult($parent)) {
-                       Logger::error('Parent not found', ['uri-id' => $item['parent-uri-id'], 'uid' => $item['uid']]);
+                       Logger::error('Parent not found', ['uri-id' => $item['thr-parent-id'], 'uid' => $item['uid']]);
                        return;
                }
 
@@ -2021,24 +2035,24 @@ class Item
                        return;
                }
 
-               if (($author['contact-type'] != Contact::TYPE_COMMUNITY) && (($parent['owner-id'] != $parent['author-id']) || ($item['uid'] == 0))) {
-                       logger::info('The resharer is no forum and the parent is reshared or not a personal post: quit', ['resharer' => $item['author-id'], 'owner' => $parent['owner-id'], 'author' => $parent['author-id'], 'uid' => $item['uid']]);
-                       return;
-               }
-
-               if (Contact::getIdForURL($parent['author-link'], $item['uid'])) {
-                       logger::info('The parent author is a user contact: quit', ['author' => $parent['author-link'], 'uid' => $item['uid']]);
+               $cid = Contact::getIdForURL($author['url'], $item['uid']);
+               if (empty($cid) || !Contact::isSharing($cid, $item['uid'])) {
+                       Logger::info('The resharer is not a following contact: quit', ['resharer' => $author['url'], 'uid' => $item['uid']]);
                        return;
                }
 
-               $cid = Contact::getIdForURL($author['url'], $item['uid']);
-               if (empty($cid)) {
-                       logger::info('The resharer is not a user contact: quit', ['resharer' => $author['url'], 'uid' => $item['uid']]);
+               if ($author['contact-type'] != Contact::TYPE_COMMUNITY) {
+                       if (Contact::isSharing($parent['owner-id'], $item['uid'])) {
+                               Logger::info('The resharer is no forum: quit', ['resharer' => $item['author-id'], 'owner' => $parent['owner-id'], 'author' => $parent['author-id'], 'uid' => $item['uid']]);
+                               return;
+                       }
+                       self::update(['post-type' => self::PT_ANNOUNCEMENT, 'causer-id' => $item['author-id']], ['id' => $parent['id']]);
+                       Logger::info('Set announcement post-type', ['uri-id' => $item['uri-id'], 'thr-parent-id' => $item['thr-parent-id'], 'uid' => $item['uid']]);
                        return;
                }
 
-               Item::update(['owner-id' => $item['author-id'], 'contact-id' => $cid], ['id' => $parent['id']]);
-               Logger::info('Change owner of the parent', ['uri-id' => $item['uri-id'], 'parent-uri-id' => $item['parent-uri-id'], 'uid' => $item['uid'], 'owner-id' => $item['author-id'], 'contact-id' => $cid]);
+               self::update(['owner-id' => $item['author-id'], 'contact-id' => $cid], ['id' => $parent['id']]);
+               Logger::info('Change owner of the parent', ['uri-id' => $item['uri-id'], 'thr-parent-id' => $item['thr-parent-id'], 'uid' => $item['uid'], 'owner-id' => $item['author-id'], 'contact-id' => $cid]);
        }
 
        /**
@@ -2054,8 +2068,14 @@ class Item
 
                $uids = Tag::getUIDListByURIId($item['uri-id']);
                foreach ($uids as $uid) {
-                       $stored = self::storeForUserByUriId($item['uri-id'], $uid);
-                       Logger::info('Stored item for users', ['uri-id' => $item['uri-id'], 'uid' => $uid, 'stored' => $stored]);
+                       if (Contact::isSharing($item['author-id'], $uid)) {
+                               $fields = [];
+                       } else {
+                               $fields = ['post-type' => self::PT_TAG];
+                       }
+
+                       $stored = self::storeForUserByUriId($item['uri-id'], $uid, $fields);
+                       Logger::info('Stored item for users', ['uri-id' => $item['uri-id'], 'uid' => $uid, 'fields' => $fields, 'stored' => $stored]);
                }
        }
 
@@ -2223,9 +2243,10 @@ class Item
         *
         * @param integer $uri_id URI-ID of the given item
         * @param integer $uid    The user that will receive the item entry
+        * @param array   $fields Additional fields to be stored
         * @return integer stored item id
         */
-       public static function storeForUserByUriId(int $uri_id, int $uid)
+       public static function storeForUserByUriId(int $uri_id, int $uid, array $fields = [])
        {
                $item = self::selectFirst(self::ITEM_FIELDLIST, ['uri-id' => $uri_id, 'uid' => 0]);
                if (!DBA::isResult($item)) {
@@ -2237,6 +2258,10 @@ class Item
                        return 0;
                }
 
+               $item['post-type'] = self::PT_STORED;
+
+               $item = array_merge($item, $fields);
+
                $stored = self::storeForUser($item, $uid);
                Logger::info('Public item stored for user', ['uri-id' => $item['uri-id'], 'uid' => $uid, 'stored' => $stored]);
                return $stored;
@@ -2360,6 +2385,7 @@ class Item
                        unset($item['starred']);
                        unset($item['postopts']);
                        unset($item['inform']);
+                       unset($item['post-type']);
                        if ($item['uri'] == $item['parent-uri']) {
                                $item['contact-id'] = $item['owner-id'];
                        } else {
@@ -2422,6 +2448,7 @@ class Item
                unset($item['starred']);
                unset($item['postopts']);
                unset($item['inform']);
+               unset($item['post-type']);
                $item['contact-id'] = Contact::getIdForURL($item['author-link']);
 
                $public_shadow = self::insert($item, false, true);
@@ -2445,11 +2472,14 @@ class Item
         */
        private static function getLanguage(array $item)
        {
+               if (!in_array($item['gravity'], [GRAVITY_PARENT, GRAVITY_COMMENT])) {
+                       return '';
+               }
+
                $naked_body = BBCode::toPlaintext($item['body'], false);
 
-               $ld = new Text_LanguageDetect();
-               $ld->setNameMode(2);
-               $languages = $ld->detect($naked_body, 3);
+               $ld = new Language();
+               $languages = $ld->detect($naked_body)->limit(0, 3)->close();
                if (is_array($languages)) {
                        return json_encode($languages);
                }
@@ -2669,6 +2699,16 @@ class Item
                        }
                }
 
+               if (!$mention) {
+                       $tags = Tag::getByURIId($item['uri-id'], [Tag::MENTION, Tag::EXCLUSIVE_MENTION]);
+                       foreach ($tags as $tag) {
+                               if (Strings::compareLink($link, $tag['url']) || Strings::compareLink($dlink, $tag['url'])) {
+                                       $mention = true;
+                                       DI::logger()->info('mention found in tag.', ['url' => $tag['url']]);
+                               }
+                       }
+               }
+               
                if (!$mention) {
                        if (($community_page || $prvgroup) &&
                                  !$item['wall'] && !$item['origin'] && ($item['gravity'] == GRAVITY_PARENT)) {
@@ -2724,9 +2764,7 @@ class Item
 
                Worker::add(['priority' => PRIORITY_HIGH, 'dont_fork' => true], 'Notifier', Delivery::POST, $item_id);
 
-               /// @todo This code should be activated by the end of the year 2020
-               // See also "createActivityFromItem"
-               //Item::performActivity($item_id, 'announce', $uid);
+               Item::performActivity($item_id, 'announce', $uid);
 
                return false;
        }
@@ -3560,11 +3598,9 @@ class Item
                                        DI::page()['htmlhead'] .= Renderer::replaceMacros(Renderer::getMarkupTemplate('videos_head.tpl'));
                                }
 
-                               $url_parts = explode('/', $the_url);
-                               $id = end($url_parts);
                                $as .= Renderer::replaceMacros(Renderer::getMarkupTemplate('video_top.tpl'), [
                                        '$video' => [
-                                               'id'     => $id,
+                                               'id'     => $item['author-id'],
                                                'title'  => DI::l10n()->t('View Video'),
                                                'src'    => $the_url,
                                                'mime'   => $mime,