]> git.mxchange.org Git - friendica.git/blobdiff - include/ostatus.php
Merge pull request #1726 from rabuzarus/acl-var
[friendica.git] / include / ostatus.php
index 463ebcd30a779d07235716bbf13d4d2338a1be78..7825418474cc5385ec959443ba45b4c43960a644 100644 (file)
@@ -10,6 +10,7 @@ require_once("include/Photo.php");
 
 define('OSTATUS_DEFAULT_POLL_INTERVAL', 30); // given in minutes
 define('OSTATUS_DEFAULT_POLL_TIMEFRAME', 1440); // given in minutes
+define('OSTATUS_DEFAULT_POLL_TIMEFRAME_MENTIONS', 14400); // given in minutes
 
 function ostatus_fetchauthor($xpath, $context, $importer, &$contact) {
 
@@ -200,6 +201,13 @@ function ostatus_import($xml,$importer,&$contact, &$hub) {
                $item["object"] = $xml;
                $item["verb"] = $xpath->query('activity:verb/text()', $entry)->item(0)->nodeValue;
 
+               // To-Do:
+               // Delete a message
+               if ($item["verb"] == "qvitter-delete-notice") {
+                       // ignore "Delete" messages (by now)
+                       continue;
+               }
+
                if ($item["verb"] == ACTIVITY_JOIN) {
                        // ignore "Join" messages
                        continue;
@@ -421,11 +429,9 @@ function ostatus_import($xml,$importer,&$contact, &$hub) {
                logger("Item was stored with id ".$item_id, LOGGER_DEBUG);
                $item["id"] = $item_id;
 
-               if (!isset($item["parent"]) OR ($item["parent"] == 0))
-                       $item["parent"] = $item_id;
-
                if ($mention) {
                        $u = q("SELECT `notify-flags`, `language`, `username`, `email` FROM user WHERE uid = %d LIMIT 1", intval($item['uid']));
+                       $r = q("SELECT `parent` FROM `item` WHERE `id` = %d", intval($item_id));
 
                        notification(array(
                                'type'         => NOTIFY_TAGSELF,
@@ -435,13 +441,13 @@ function ostatus_import($xml,$importer,&$contact, &$hub) {
                                'to_email'     => $u[0]["email"],
                                'uid'          => $item["uid"],
                                'item'         => $item,
-                               'link'         => $a->get_baseurl().'/display/'.urlencode(get_item_guid($item["id"])),
+                               'link'         => $a->get_baseurl().'/display/'.urlencode(get_item_guid($item_id)),
                                'source_name'  => $item["author-name"],
                                'source_link'  => $item["author-link"],
                                'source_photo' => $item["author-avatar"],
                                'verb'         => ACTIVITY_TAG,
                                'otype'        => 'item',
-                               'parent'       => $item["parent"]
+                               'parent'       => $r[0]["parent"]
                        ));
                }
        }
@@ -470,7 +476,7 @@ function ostatus_convert_href($href) {
        return $href;
 }
 
-function check_conversations($override = false) {
+function check_conversations($mentions = false, $override = false) {
        $last = get_config('system','ostatus_last_poll');
 
        $poll_interval = intval(get_config('system','ostatus_poll_interval'));
@@ -481,9 +487,16 @@ function check_conversations($override = false) {
        if (($poll_interval < 0) AND !$override)
                return;
 
-       $poll_timeframe = intval(get_config('system','ostatus_poll_timeframe'));
-       if (!$poll_timeframe)
-               $poll_timeframe = OSTATUS_DEFAULT_POLL_TIMEFRAME;
+       if (!$mentions) {
+               $poll_timeframe = intval(get_config('system','ostatus_poll_timeframe'));
+               if (!$poll_timeframe)
+                       $poll_timeframe = OSTATUS_DEFAULT_POLL_TIMEFRAME;
+       } else {
+               $poll_timeframe = intval(get_config('system','ostatus_poll_timeframe'));
+               if (!$poll_timeframe)
+                       $poll_timeframe = OSTATUS_DEFAULT_POLL_TIMEFRAME_MENTIONS;
+       }
+
 
        if ($last AND !$override) {
                $next = $last + ($poll_interval * 60);
@@ -496,8 +509,16 @@ function check_conversations($override = false) {
        logger('cron_start');
 
        $start = date("Y-m-d H:i:s", time() - ($poll_timeframe * 60));
-       $conversations = q("SELECT `oid`, `url`, `uid` FROM `term` WHERE `type` = 7 AND `term` > '%s' GROUP BY `url`, `uid` ORDER BY `term` DESC",
-                               dbesc($start));
+
+       if ($mentions)
+               $conversations = q("SELECT `term`.`oid`, `term`.`url`, `term`.`uid` FROM `term`
+                                       STRAIGHT_JOIN `thread` ON `thread`.`iid` = `term`.`oid` AND `thread`.`uid` = `term`.`uid`
+                                       WHERE `term`.`type` = 7 AND `term`.`term` > '%s' AND `thread`.`mention`
+                                       GROUP BY `term`.`url`, `term`.`uid` ORDER BY `term`.`term` DESC", dbesc($start));
+       else
+               $conversations = q("SELECT `oid`, `url`, `uid` FROM `term`
+                                       WHERE `type` = 7 AND `term` > '%s'
+                                       GROUP BY `url`, `uid` ORDER BY `term` DESC", dbesc($start));
 
        foreach ($conversations AS $conversation) {
                ostatus_completion($conversation['url'], $conversation['uid']);
@@ -510,12 +531,15 @@ function check_conversations($override = false) {
 
 function ostatus_completion($conversation_url, $uid, $item = array()) {
 
+       $a = get_app();
+
        $item_stored = -1;
 
        $conversation_url = ostatus_convert_href($conversation_url);
 
        // If the thread shouldn't be completed then store the item and go away
        if ((intval(get_config('system','ostatus_poll_interval')) == -2) AND (count($item) > 0)) {
+               //$arr["app"] .= " (OStatus-NoCompletion)";
                $item_stored = item_store($item, true);
                return($item_stored);
        }
@@ -584,6 +608,7 @@ function ostatus_completion($conversation_url, $uid, $item = array()) {
 
        if (!sizeof($items)) {
                if (count($item) > 0) {
+                       //$arr["app"] .= " (OStatus-NoConvFetched)";
                        $item_stored = item_store($item, true);
 
                        if ($item_stored) {
@@ -598,12 +623,16 @@ function ostatus_completion($conversation_url, $uid, $item = array()) {
 
        $items = array_reverse($items);
 
+       $r = q("SELECT `nurl` FROM `contact` WHERE `uid` = %d AND `self`", intval($uid));
+       $importer = $r[0];
+
        foreach ($items as $single_conv) {
 
                // Test - remove before flight
                //$tempfile = tempnam(get_temppath(), "conversation");
                //file_put_contents($tempfile, json_encode($single_conv));
 
+               $mention = false;
 
                if (isset($single_conv->object->id))
                        $single_conv->id = $single_conv->object->id;
@@ -654,17 +683,23 @@ function ostatus_completion($conversation_url, $uid, $item = array()) {
                        }
                }
 
+               $parent_uri = $parent["uri"];
+
+               // "context" only seems to exist on older servers
                if (isset($single_conv->context->inReplyTo->id)) {
-                       $parent_uri = $single_conv->context->inReplyTo->id;
+                       $parent_exists = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s' AND `network` IN ('%s','%s') LIMIT 1",
+                                               intval($uid), dbesc($single_conv->context->inReplyTo->id), dbesc(NETWORK_OSTATUS), dbesc(NETWORK_DFRN));
+                       if ($parent_exists)
+                               $parent_uri = $single_conv->context->inReplyTo->id;
+               }
 
+               // This is the current way
+               if (isset($single_conv->object->inReplyTo->id)) {
                        $parent_exists = q("SELECT `id` FROM `item` WHERE `uid` = %d AND `uri` = '%s' AND `network` IN ('%s','%s') LIMIT 1",
-                                               intval($uid), dbesc($parent_uri), dbesc(NETWORK_OSTATUS), dbesc(NETWORK_DFRN));
-                       if (!$parent_exists) {
-                               logger("Parent ".$parent_uri." wasn't found here", LOGGER_DEBUG);
-                               $parent_uri = $parent["uri"];
-                       }
-               } else
-                       $parent_uri = $parent["uri"];
+                                               intval($uid), dbesc($single_conv->object->inReplyTo->id), dbesc(NETWORK_OSTATUS), dbesc(NETWORK_DFRN));
+                       if ($parent_exists)
+                               $parent_uri = $single_conv->object->inReplyTo->id;
+               }
 
                $message_exists = q("SELECT `id`, `parent`, `uri` FROM `item` WHERE `uid` = %d AND `uri` = '%s' AND `network` IN ('%s','%s') LIMIT 1",
                                                intval($uid), dbesc($single_conv->id),
@@ -710,6 +745,11 @@ function ostatus_completion($conversation_url, $uid, $item = array()) {
                        continue;
                }
 
+               if (is_array($single_conv->to))
+                       foreach($single_conv->to AS $to)
+                               if ($importer["nurl"] == normalise_link($to->id))
+                                       $mention = true;
+
                $actor = $single_conv->actor->id;
                if (isset($single_conv->actor->url))
                        $actor = $single_conv->actor->url;
@@ -765,7 +805,7 @@ function ostatus_completion($conversation_url, $uid, $item = array()) {
                else
                        $arr["app"] = "OStatus";
 
-               $arr["app"] .= " (Conversation)";
+               //$arr["app"] .= " (Conversation)";
 
                $arr["object"] = json_encode($single_conv);
                $arr["verb"] = $parent["verb"];
@@ -831,7 +871,7 @@ function ostatus_completion($conversation_url, $uid, $item = array()) {
                                if (isset($item[$field]))
                                        $arr[$field] = $item[$field];
 
-                       $arr["app"] .= " (OStatus)";
+                       //$arr["app"] .= " (OStatus)";
                }
 
                $newitem = item_store($arr);
@@ -850,6 +890,28 @@ function ostatus_completion($conversation_url, $uid, $item = array()) {
                // Add the conversation entry (but don't fetch the whole conversation)
                ostatus_store_conversation($newitem, $conversation_url);
 
+               if ($mention) {
+                       $u = q("SELECT `notify-flags`, `language`, `username`, `email` FROM user WHERE uid = %d LIMIT 1", intval($uid));
+                       $r = q("SELECT `parent` FROM `item` WHERE `id` = %d", intval($newitem));
+
+                       notification(array(
+                               'type'         => NOTIFY_TAGSELF,
+                               'notify_flags' => $u[0]["notify-flags"],
+                               'language'     => $u[0]["language"],
+                               'to_name'      => $u[0]["username"],
+                               'to_email'     => $u[0]["email"],
+                               'uid'          => $uid,
+                               'item'         => $arr,
+                               'link'         => $a->get_baseurl().'/display/'.urlencode(get_item_guid($newitem)),
+                               'source_name'  => $arr["author-name"],
+                               'source_link'  => $arr["author-link"],
+                               'source_photo' => $arr["author-avatar"],
+                               'verb'         => ACTIVITY_TAG,
+                               'otype'        => 'item',
+                               'parent'       => $r[0]["parent"]
+                       ));
+               }
+
                // If the newly created item is the top item then change the parent settings of the thread
                // This shouldn't happen anymore. This is supposed to be absolote.
                if ($arr["uri"] == $first_id) {
@@ -862,6 +924,7 @@ function ostatus_completion($conversation_url, $uid, $item = array()) {
        }
 
        if (($item_stored < 0) AND (count($item) > 0)) {
+               //$arr["app"] .= " (OStatus-NoConvFound)";
                $item_stored = item_store($item, true);
                if ($item_stored) {
                        logger("Uri ".$item["uri"]." wasn't found in conversation ".$conversation_url, LOGGER_DEBUG);