]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Item.php
Merge pull request #11877 from annando/log-levels
[friendica.git] / src / Model / Item.php
index 754516193aea35174e1a91557132f73caa77bc5a..5a3af646ecaf0fedeffae3d7f0ad7161e4285384 100644 (file)
@@ -33,7 +33,6 @@ use Friendica\Model\Tag;
 use Friendica\Core\Worker;
 use Friendica\Database\DBA;
 use Friendica\DI;
-use Friendica\Model\Post;
 use Friendica\Protocol\Activity;
 use Friendica\Protocol\ActivityPub;
 use Friendica\Protocol\Diaspora;
@@ -280,7 +279,7 @@ class Item
                        if ($item['uid'] == $uid) {
                                self::markForDeletionById($item['id'], PRIORITY_HIGH);
                        } elseif ($item['uid'] != 0) {
-                               Logger::notice('Wrong ownership. Not deleting item', ['id' => $item['id']]);
+                               Logger::warning('Wrong ownership. Not deleting item', ['id' => $item['id']]);
                        }
                }
                DBA::close($items);
@@ -448,21 +447,46 @@ class Item
         */
        private static function contactId(array $item): int
        {
-               if (!empty($item['contact-id']) && DBA::exists('contact', ['self' => true, 'id' => $item['contact-id']])) {
-                       return $item['contact-id'];
-               } elseif (($item['gravity'] == GRAVITY_PARENT) && !empty($item['uid']) && !empty($item['contact-id']) && Contact::isSharing($item['contact-id'], $item['uid'])) {
-                       return $item['contact-id'];
-               } elseif (!empty($item['uid']) && !Contact::isSharing($item['author-id'], $item['uid'])) {
+               if ($item['uid'] == 0) {
                        return $item['author-id'];
-               } elseif (!empty($item['contact-id'])) {
-                       return $item['contact-id'];
-               } else {
-                       $contact_id = Contact::getIdForURL($item['author-link'], $item['uid']);
+               }
+
+               if ($item['origin']) {
+                       $owner = User::getOwnerDataById($item['uid']);
+                       return $owner['id'];
+               }
+
+               if (!empty($item['causer-id']) && Contact::isSharing($item['causer-id'], $item['uid'], true)) {
+                       $cdata = Contact::getPublicAndUserContactID($item['causer-id'], $item['uid']);
+                       if (!empty($cdata['user'])) {
+                               return $cdata['user'];
+                       }
+               }
+
+               if ($item['gravity'] == GRAVITY_PARENT) {
+                       if (Contact::isSharingByURL($item['owner-link'], $item['uid'], true)) {
+                               $contact_id = Contact::getIdForURL($item['owner-link'], $item['uid']);
+                       } else {
+                               $contact_id = Contact::getIdForURL($item['owner-link']);
+                       }
                        if (!empty($contact_id)) {
                                return $contact_id;
                        }
                }
-               return $item['author-id'];
+
+               if (Contact::isSharingByURL($item['author-link'], $item['uid'], true)) {
+                       $contact_id = Contact::getIdForURL($item['author-link'], $item['uid']);
+               } else {
+                       $contact_id = Contact::getIdForURL($item['author-link']);
+               }
+
+               if (!empty($contact_id)) {
+                       return $contact_id;
+               }
+
+               Logger::warning('contact-id could not be fetched, using self contact instead.', ['uid' => $item['uid'], 'item' => $item]);
+               $self = Contact::selectFirst(['id'], ['self' => true, 'uid' => $item['uid']]);
+               return $self['id'];
        }
 
        /**
@@ -556,7 +580,7 @@ class Item
                if (!empty($item['uid'])) {
                        $owner = User::getOwnerDataById($item['uid'], false);
                        if (!$owner) {
-                               Logger::notice('Missing item user owner data', ['uid' => $item['uid']]);
+                               Logger::warning('Missing item user owner data', ['uid' => $item['uid']]);
                                return false;
                        }
 
@@ -911,7 +935,6 @@ class Item
                Contact::checkAvatarCache($item['author-id']);
                Contact::checkAvatarCache($item['owner-id']);
 
-               // The contact-id should be set before "self::insert" was called - but there seems to be issues sometimes
                $item['contact-id'] = self::contactId($item);
 
                if (!empty($item['direction']) && in_array($item['direction'], [Conversation::PUSH, Conversation::RELAY]) &&
@@ -1625,13 +1648,13 @@ class Item
                $post = Post::selectFirst(['id'], ['uri-id' => $item['uri-id'], 'uid' => $uid]);
                if (!empty($post['id'])) {
                        if (!empty($item['event-id'])) {
-                               $post = Post::selectFirst(['event-id'], ['uri-id' => $item['uri-id'], 'uid' => $uid]);
-                               if (!empty($post['event-id'])) {
+                               $event_post = Post::selectFirst(['event-id'], ['uri-id' => $item['uri-id'], 'uid' => $uid]);
+                               if (!empty($event_post['event-id'])) {
                                        $event = DBA::selectFirst('event', ['edited', 'start', 'finish', 'summary', 'desc', 'location', 'nofinish', 'adjust'], ['id' => $item['event-id']]);
                                        if (!empty($event)) {
                                                // We aren't using "Event::store" here, since we don't want to trigger any further action
-                                               $ret = DBA::update('event', $event, ['id' => $post['event-id']]);
-                                               Logger::info('Event updated', ['uid' => $uid, 'source-event' => $item['event-id'], 'target-event' => $post['event-id'], 'ret' => $ret]);
+                                               $ret = DBA::update('event', $event, ['id' => $event_post['event-id']]);
+                                               Logger::info('Event updated', ['uid' => $uid, 'source-event' => $item['event-id'], 'target-event' => $event_post['event-id'], 'ret' => $ret]);
                                        }
                                }
                        }
@@ -1660,34 +1683,7 @@ class Item
                $item['origin'] = 0;
                $item['wall'] = 0;
 
-               if (!empty($item['causer-id']) && Contact::isSharing($item['causer-id'], $uid, true)) {
-                       $cdata = Contact::getPublicAndUserContactID($item['causer-id'], $uid);
-                       $contact_id = $cdata['user'] ?? 0;
-               }
-
-               if (empty($contact_id)) {
-                       if ($item['gravity'] == GRAVITY_PARENT) {
-                               if (Contact::isSharingByURL($item['owner-link'], $uid, true)) {
-                                       $contact_id = Contact::getIdForURL($item['owner-link'], $uid);
-                               } else {
-                                       $contact_id = Contact::getIdForURL($item['owner-link']);
-                               }
-                       } else {
-                               if (Contact::isSharingByURL($item['author-link'], $uid, true)) {
-                                       $contact_id = Contact::getIdForURL($item['author-link'], $uid);
-                               } else {
-                                       $contact_id = Contact::getIdForURL($item['author-link']);
-                               }
-                       }
-               }
-
-               if (empty($contact_id)) {
-                       Logger::warning('contact-id could not be fetched, using self contact instead.', ['uid' => $uid, 'item' => $item]);
-                       $self = Contact::selectFirst(['id'], ['self' => true, 'uid' => $uid]);
-                       $contact_id = $self['id'];
-               }
-
-               $item['contact-id'] = $contact_id;
+               $item['contact-id'] = self::contactId($item);
 
                $notify = false;
                if ($item['gravity'] == GRAVITY_PARENT) {
@@ -2519,7 +2515,7 @@ class Item
 
                $item = Post::selectFirst(self::ITEM_FIELDLIST, ['id' => $item_id]);
                if (!DBA::isResult($item)) {
-                       Logger::notice('like: unknown item', ['id' => $item_id]);
+                       Logger::warning('Post had not been fetched', ['id' => $item_id]);
                        return false;
                }
 
@@ -2548,7 +2544,7 @@ class Item
                }
 
                // Retrieve the current logged in user's public contact
-               $author_id = Contact::getIdForURL($owner['url']);
+               $author_id = Contact::getPublicIdByUserId($uid);
                if (empty($author_id)) {
                        Logger::info('Empty public contact');
                        return false;
@@ -2585,7 +2581,7 @@ class Item
                                $activity = Activity::ANNOUNCE;
                                break;
                        default:
-                               Logger::notice('unknown verb', ['verb' => $verb, 'item' => $item_id]);
+                               Logger::warning('unknown verb', ['verb' => $verb, 'item' => $item_id]);
                                return false;
                }
 
@@ -2655,7 +2651,7 @@ class Item
                $new_item = [
                        'guid'          => System::createUUID(),
                        'uri'           => self::newURI(),
-                       'uid'           => $item['uid'],
+                       'uid'           => $uid,
                        'contact-id'    => $owner['id'],
                        'wall'          => $item['wall'],
                        'origin'        => 1,
@@ -2678,22 +2674,20 @@ class Item
                        'unseen'        => 1,
                ];
 
-               $signed = Diaspora::createLikeSignature($uid, $new_item);
-               if (!empty($signed)) {
-                       $new_item['diaspora_signed_text'] = json_encode($signed);
+               if (in_array($activity, [Activity::LIKE, Activity::DISLIKE])) {
+                       $signed = Diaspora::createLikeSignature($uid, $new_item);
+                       if (!empty($signed)) {
+                               $new_item['diaspora_signed_text'] = json_encode($signed);
+                       }
                }
 
-               $new_item_id = self::insert($new_item);
+               self::insert($new_item, true);
 
                // If the parent item isn't visible then set it to visible
+               // @todo Check if this is still needed
                if (!$item['visible']) {
                        self::update(['visible' => true], ['id' => $item['id']]);
                }
-
-               $new_item['id'] = $new_item_id;
-
-               Hook::callAll('post_local_end', $new_item);
-
                return true;
        }