]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - classes/Notice.php
Merge branch '0.9.x' into activityexport
[quix0rs-gnu-social.git] / classes / Notice.php
index fe014b942d86ef9e036bf7af5a072c4d796d55ed..4f23e35007f4cac4f099832fa6df805185082d2f 100644 (file)
@@ -121,16 +121,19 @@ class Notice extends Memcached_DataObject
             $deleted->insert();
         }
 
-        // Clear related records
+        if (Event::handle('NoticeDeleteRelated', array($this))) {
 
-        $this->clearReplies();
-        $this->clearRepeats();
-        $this->clearFaves();
-        $this->clearTags();
-        $this->clearGroupInboxes();
+            // Clear related records
 
-        // NOTE: we don't clear inboxes
-        // NOTE: we don't clear queue items
+            $this->clearReplies();
+            $this->clearRepeats();
+            $this->clearFaves();
+            $this->clearTags();
+            $this->clearGroupInboxes();
+
+            // NOTE: we don't clear inboxes
+            // NOTE: we don't clear queue items
+        }
 
         $result = parent::delete();
 
@@ -580,7 +583,9 @@ class Notice extends Memcached_DataObject
         if ($f2p->find()) {
             while ($f2p->fetch()) {
                 $f = File::staticGet($f2p->file_id);
-                $att[] = clone($f);
+                if ($f) {
+                    $att[] = clone($f);
+                }
             }
         }
         return $att;
@@ -748,8 +753,15 @@ class Notice extends Memcached_DataObject
     }
 
     /**
-     * @param $groups array of Group *objects*
-     * @param $recipients array of profile *ids*
+     * Pull up a full list of local recipients who will be getting
+     * this notice in their inbox. Results will be cached, so don't
+     * change the input data wily-nilly!
+     *
+     * @param array $groups optional list of Group objects;
+     *              if left empty, will be loaded from group_inbox records
+     * @param array $recipient optional list of reply profile ids
+     *              if left empty, will be loaded from reply records
+     * @return array associating recipient user IDs with an inbox source constant
      */
     function whoGets($groups=null, $recipients=null)
     {
@@ -782,27 +794,27 @@ class Notice extends Memcached_DataObject
             $ni[$id] = NOTICE_INBOX_SOURCE_SUB;
         }
 
-        $profile = $this->getProfile();
-
         foreach ($groups as $group) {
             $users = $group->getUserMembers();
             foreach ($users as $id) {
                 if (!array_key_exists($id, $ni)) {
-                    $user = User::staticGet('id', $id);
-                    if (!$user->hasBlocked($profile)) {
-                        $ni[$id] = NOTICE_INBOX_SOURCE_GROUP;
-                    }
+                    $ni[$id] = NOTICE_INBOX_SOURCE_GROUP;
                 }
             }
         }
 
         foreach ($recipients as $recipient) {
-
             if (!array_key_exists($recipient, $ni)) {
-                $recipientUser = User::staticGet('id', $recipient);
-                if (!empty($recipientUser)) {
-                    $ni[$recipient] = NOTICE_INBOX_SOURCE_REPLY;
-                }
+                $ni[$recipient] = NOTICE_INBOX_SOURCE_REPLY;
+            }
+        }
+
+        // Exclude any deleted, non-local, or blocking recipients.
+        $profile = $this->getProfile();
+        foreach ($ni as $id => $source) {
+            $user = User::staticGet('id', $id);
+            if (empty($user) || $user->hasBlocked($profile)) {
+                unset($ni[$id]);
             }
         }
 
@@ -1016,25 +1028,31 @@ class Notice extends Memcached_DataObject
         if (empty($uris)) {
             return;
         }
+
         $sender = Profile::staticGet($this->profile_id);
 
         foreach (array_unique($uris) as $uri) {
 
-            $user = User::staticGet('uri', $uri);
+            $profile = Profile::fromURI($uri);
 
-            if (!empty($user)) {
-                if ($user->hasBlocked($sender)) {
-                    continue;
-                }
+            if (empty($profile)) {
+                common_log(LOG_WARNING, "Unable to determine profile for URI '$uri'");
+                continue;
+            }
 
-                $reply = new Reply();
+            if ($profile->hasBlocked($sender)) {
+                common_log(LOG_INFO, "Not saving reply to profile {$profile->id} ($uri) from sender {$sender->id} because of a block.");
+                continue;
+            }
 
-                $reply->notice_id  = $this->id;
-                $reply->profile_id = $user->id;
-                common_log(LOG_INFO, __METHOD__ . ": saving reply: notice $this->id to profile $user->id");
+            $reply = new Reply();
 
-                $id = $reply->insert();
-            }
+            $reply->notice_id  = $this->id;
+            $reply->profile_id = $profile->id;
+
+            common_log(LOG_INFO, __METHOD__ . ": saving reply: notice $this->id to profile $profile->id");
+
+            $id = $reply->insert();
         }
 
         return;
@@ -1201,6 +1219,64 @@ class Notice extends Memcached_DataObject
         return $groups;
     }
 
+    function asActivity()
+    {
+        $profile = $this->getProfile();
+
+        $act = new Activity();
+
+        $act->actor     = ActivityObject::fromProfile($profile);
+        $act->verb      = ActivityVerb::POST;
+        $act->objects[] = ActivityObject::fromNotice($this);
+
+        $act->time    = strtotime($this->created);
+        $act->link    = $this->bestUrl();
+
+        $act->content = common_xml_safe_str($this->rendered);
+        $act->id      = $this->uri;
+        $act->title   = common_xml_safe_str($this->content);
+
+        $ctx = new ActivityContext();
+
+        if (!empty($this->reply_to)) {
+            $reply = Notice::staticGet('id', $this->reply_to);
+            if (!empty($reply)) {
+                $ctx->replyToID  = $reply->uri;
+                $ctx->replyToUrl = $reply->bestUrl();
+            }
+        }
+
+        $ctx->location = $this->getLocation();
+
+        $conv = null;
+
+        if (!empty($this->conversation)) {
+            $conv = Conversation::staticGet('id', $this->conversation);
+            if (!empty($conv)) {
+                $ctx->conversation = $conv->uri;
+            }
+        }
+
+        $reply_ids = $this->getReplies();
+
+        foreach ($reply_ids as $id) {
+            $profile = Profile::staticGet('id', $id);
+            if (!empty($profile)) {
+                $ctx->attention[] = $profile->getUri();
+            }
+        }
+
+        $groups = $this->getGroups();
+
+        foreach ($groups as $group) {
+            $ctx->attention[] = $group->uri;
+        }
+
+        $act->context = $ctx;
+
+        return $act;
+    }
+
     // This has gotten way too long. Needs to be sliced up into functional bits
     // or ideally exported to a utility class.