]> git.mxchange.org Git - friendica.git/blobdiff - include/conversation.php
Prevention of endless loops in the conversation
[friendica.git] / include / conversation.php
index c4605cf5d112dc2abde53e87f6a8e823719e64e1..3b2eb54bde12c655b1df3421bd70f8d9df48d03d 100644 (file)
@@ -100,7 +100,11 @@ function localize_item(&$item){
                $item['body'] = item_redir_and_replace_images($extracted['body'], $extracted['images'], $item['contact-id']);
 
        $xmlhead="<"."?xml version='1.0' encoding='UTF-8' ?".">";
-       if (activity_match($item['verb'],ACTIVITY_LIKE) || activity_match($item['verb'],ACTIVITY_DISLIKE)){
+       if (activity_match($item['verb'],ACTIVITY_LIKE)
+               || activity_match($item['verb'],ACTIVITY_DISLIKE)
+               || activity_match($item['verb'],ACTIVITY_ATTEND)
+               || activity_match($item['verb'],ACTIVITY_ATTENDNO)
+               || activity_match($item['verb'],ACTIVITY_ATTENDMAYBE)){
 
                $r = q("SELECT * from `item`,`contact` WHERE
                                `item`.`contact-id`=`contact`.`id` AND `item`.`uri`='%s';",
@@ -139,6 +143,15 @@ function localize_item(&$item){
                elseif(activity_match($item['verb'],ACTIVITY_DISLIKE)) {
                        $bodyverb = t('%1$s doesn\'t like %2$s\'s %3$s');
                }
+               elseif(activity_match($item['verb'],ACTIVITY_ATTEND)) {
+                       $bodyverb = t('%1$s attends %2$s\'s %3$s');
+               }
+               elseif(activity_match($item['verb'],ACTIVITY_ATTENDNO)) {
+                       $bodyverb = t('%1$s doesn\'t attend %2$s\'s %3$s');
+               }
+               elseif(activity_match($item['verb'],ACTIVITY_ATTENDMAYBE)) {
+                       $bodyverb = t('%1$s attends maybe %2$s\'s %3$s');
+               }
                $item['body'] = sprintf($bodyverb, $author, $objauthor, $plink);
 
        }
@@ -204,7 +217,7 @@ function localize_item(&$item){
                // we can't have a translation string with three positions but no distinguishable text
                // So here is the translate string.
                $txt = t('%1$s poked %2$s');
-               
+
                // now translate the verb
                $poked_t = trim(sprintf($txt, "",""));
                $txt = str_replace( $poked_t, t($verb), $txt);
@@ -403,25 +416,25 @@ function conversation(&$a, $items, $mode, $update, $preview = false) {
                $page_writeable = true;
                if(!$update) {
                        // The special div is needed for liveUpdate to kick in for this page.
-                       // We only launch liveUpdate if you aren't filtering in some incompatible 
+                       // We only launch liveUpdate if you aren't filtering in some incompatible
                        // way and also you aren't writing a comment (discovered in javascript).
 
                        $live_update_div = '<div id="live-network"></div>' . "\r\n"
-                               . "<script> var profile_uid = " . $_SESSION['uid'] 
+                               . "<script> var profile_uid = " . $_SESSION['uid']
                                . "; var netargs = '" . substr($a->cmd,8)
                                . '?f='
                                . ((x($_GET,'cid'))    ? '&cid='    . $_GET['cid']    : '')
-                               . ((x($_GET,'search')) ? '&search=' . $_GET['search'] : '') 
-                               . ((x($_GET,'star'))   ? '&star='   . $_GET['star']   : '') 
-                               . ((x($_GET,'order'))  ? '&order='  . $_GET['order']  : '') 
-                               . ((x($_GET,'bmark'))  ? '&bmark='  . $_GET['bmark']  : '') 
-                               . ((x($_GET,'liked'))  ? '&liked='  . $_GET['liked']  : '') 
-                               . ((x($_GET,'conv'))   ? '&conv='   . $_GET['conv']   : '') 
-                               . ((x($_GET,'spam'))   ? '&spam='   . $_GET['spam']   : '') 
-                               . ((x($_GET,'nets'))   ? '&nets='   . $_GET['nets']   : '') 
-                               . ((x($_GET,'cmin'))   ? '&cmin='   . $_GET['cmin']   : '') 
-                               . ((x($_GET,'cmax'))   ? '&cmax='   . $_GET['cmax']   : '') 
-                               . ((x($_GET,'file'))   ? '&file='   . $_GET['file']   : '') 
+                               . ((x($_GET,'search')) ? '&search=' . $_GET['search'] : '')
+                               . ((x($_GET,'star'))   ? '&star='   . $_GET['star']   : '')
+                               . ((x($_GET,'order'))  ? '&order='  . $_GET['order']  : '')
+                               . ((x($_GET,'bmark'))  ? '&bmark='  . $_GET['bmark']  : '')
+                               . ((x($_GET,'liked'))  ? '&liked='  . $_GET['liked']  : '')
+                               . ((x($_GET,'conv'))   ? '&conv='   . $_GET['conv']   : '')
+                               . ((x($_GET,'spam'))   ? '&spam='   . $_GET['spam']   : '')
+                               . ((x($_GET,'nets'))   ? '&nets='   . $_GET['nets']   : '')
+                               . ((x($_GET,'cmin'))   ? '&cmin='   . $_GET['cmin']   : '')
+                               . ((x($_GET,'cmax'))   ? '&cmax='   . $_GET['cmax']   : '')
+                               . ((x($_GET,'file'))   ? '&file='   . $_GET['file']   : '')
 
                                . "'; var profile_page = " . $a->pager['page'] . "; </script>\r\n";
                }
@@ -438,7 +451,7 @@ function conversation(&$a, $items, $mode, $update, $preview = false) {
                                // because browser prefetching might change it on us. We have to deliver it with the page.
 
                                $live_update_div = '<div id="live-profile"></div>' . "\r\n"
-                                       . "<script> var profile_uid = " . $a->profile['profile_uid'] 
+                                       . "<script> var profile_uid = " . $a->profile['profile_uid']
                                        . "; var netargs = '?f='; var profile_page = " . $a->pager['page'] . "; </script>\r\n";
                        }
                }
@@ -448,7 +461,7 @@ function conversation(&$a, $items, $mode, $update, $preview = false) {
                $page_writeable = true;
                if(!$update) {
                        $live_update_div = '<div id="live-notes"></div>' . "\r\n"
-                               . "<script> var profile_uid = " . local_user() 
+                               . "<script> var profile_uid = " . local_user()
                                . "; var netargs = '/?f='; var profile_page = " . $a->pager['page'] . "; </script>\r\n";
                }
        }
@@ -665,7 +678,7 @@ function conversation(&$a, $items, $mode, $update, $preview = false) {
                                        'name' => $profile_name_e,
                                        'sparkle' => $sparkle,
                                        'lock' => $lock,
-                                       'thumb' => proxy_url($profile_avatar),
+                                       'thumb' => proxy_url($profile_avatar, false, PROXY_SIZE_THUMB),
                                        'title' => $item['title_e'],
                                        'body' => $body_e,
                                        'tags' => $tags_e,
@@ -684,7 +697,7 @@ function conversation(&$a, $items, $mode, $update, $preview = false) {
                                        'indent' => '',
                                        'owner_name' => $owner_name_e,
                                        'owner_url' => $owner_url,
-                                       'owner_photo' => proxy_url($owner_photo),
+                                       'owner_photo' => proxy_url($owner_photo, false, PROXY_SIZE_THUMB),
                                        'plink' => get_plink($item),
                                        'edpost' => false,
                                        'isstarred' => $isstarred,
@@ -800,10 +813,16 @@ function best_link_url($item,&$sparkle,$ssl_state = false) {
                        if($a->contacts[$clean_url]['network'] === NETWORK_DFRN) {
                                $best_url = $a->get_baseurl($ssl_state) . '/redir/' . $a->contacts[$clean_url]['id'];
                                $sparkle = true;
-                       }
-                       else
+                       } else
                                $best_url = $a->contacts[$clean_url]['url'];
                }
+       } elseif (local_user()) {
+               $r = q("SELECT `id`, `network` FROM `contact` WHERE `network` = '%s' AND `uid` = %d AND `nurl` = '%s'",
+                       dbesc(NETWORK_DFRN), intval(local_user()), dbesc(normalise_link($clean_url)));
+               if ($r) {
+                       $best_url = $a->get_baseurl($ssl_state).'/redir/'.$r[0]['id'];
+                       $sparkle = true;
+               }
        }
        if(! $best_url) {
                if(strlen($item['author-link']))
@@ -856,9 +875,17 @@ function item_photo_menu($item){
                $profile_link = zrl($profile_link);
                if(local_user() && local_user() == $item['uid'] && link_compare($item['url'],$item['author-link'])) {
                        $cid = $item['contact-id'];
-               }
-               else {
-                       $cid = 0;
+               } else {
+                       $r = q("SELECT `id`, `network` FROM `contact` WHERE `uid` = %d AND `nurl` = '%s' ORDER BY `uid` DESC LIMIT 1",
+                               intval(local_user()), dbesc(normalise_link($item['author-link'])));
+                       if ($r) {
+                               $cid = $r[0]["id"];
+
+                               if ($r[0]["network"] == NETWORK_DIASPORA)
+                                       $pm_url = $a->get_baseurl($ssl_state) . '/message/new/' . $cid;
+
+                       } else
+                               $cid = 0;
                }
        }
        if(($cid) && (! $item['self'])) {
@@ -878,17 +905,25 @@ function item_photo_menu($item){
 
        }
 
-       $menu = Array(
-               t("Follow Thread") => $sub_link,
-               t("View Status") => $status_link,
-               t("View Profile") => $profile_link,
-               t("View Photos") => $photos_link,
-               t("Network Posts") => $posts_link,
-               t("Edit Contact") => $contact_url,
-               t("Send PM") => $pm_url,
-               t("Poke") => $poke_link
-       );
-
+       if (local_user()) {
+               $menu = Array(
+                       t("Follow Thread") => $sub_link,
+                       t("View Status") => $status_link,
+                       t("View Profile") => $profile_link,
+                       t("View Photos") => $photos_link,
+                       t("Network Posts") => $posts_link,
+                       t("Edit Contact") => $contact_url,
+                       t("Send PM") => $pm_url
+               );
+
+               if ($a->contacts[$clean_url]['network'] === NETWORK_DFRN)
+                       $menu[t("Poke")] = $poke_link;
+
+               if ((($cid == 0) OR ($a->contacts[$clean_url]['rel'] == CONTACT_IS_FOLLOWER)) AND
+                       in_array($item['network'], array(NETWORK_DFRN, NETWORK_OSTATUS, NETWORK_DIASPORA)))
+                       $menu[t("Connect/Follow")] = $a->get_baseurl($ssl_state)."/follow?url=".urlencode($item['author-link']);
+       } else
+               $menu = array(t("View Profile") => $item['author-link']);
 
        $args = array('item' => $item, 'menu' => $menu);
 
@@ -949,15 +984,15 @@ function builtin_activity_puller($item, &$conv_responses) {
                                $url = z_root(true) . '/redir/' . $item['contact-id'];
                                $sparkle = ' class="sparkle" ';
                        }
-                       else 
+                       else
                                $url = zrl($url);
-                       
-                       $url = '<a href="'. $url . '"'. $sparkle .'>' . $item['author-name'] . '</a>';
+
+                       $url = '<a href="'. $url . '"'. $sparkle .'>' . htmlentities($item['author-name']) . '</a>';
 
                        if(! $item['thr-parent'])
                                $item['thr-parent'] = $item['parent-uri'];
 
-                       if(! ((isset($conv_responses[$mode][$item['thr-parent'] . '-l'])) 
+                       if(! ((isset($conv_responses[$mode][$item['thr-parent'] . '-l']))
                                && (is_array($conv_responses[$mode][$item['thr-parent'] . '-l']))))
                                $conv_responses[$mode][$item['thr-parent'] . '-l'] = array();
 
@@ -990,10 +1025,31 @@ function format_like($cnt,$arr,$type,$id) {
        $o = '';
        $expanded = '';
 
-       if($cnt == 1)
+       if($cnt == 1) {
                $likers = $arr[0];
 
-       else {
+               // Phrase if there is only one liker. In other cases it will be uses for the expanded
+               // list which show all likers
+               switch($type) {
+                       case 'like' :
+                               $phrase = sprintf( t('%s likes this.'), $likers);
+                               break;
+                       case 'dislike' :
+                               $phrase = sprintf( t('%s doesn\'t like this.'), $likers);
+                               break;
+                       case 'attendyes' :
+                               $phrase = sprintf( t('%s attends.'), $likers);
+                               break;
+                       case 'attendno' :
+                               $phrase = sprintf( t('%s doesn\'t attend.'), $likers);
+                               break;
+                       case 'attendmaybe' :
+                               $phrase = sprintf( t('%s attends maybe.'), $likers);
+                               break;
+               }
+       }
+
+       if($cnt > 1) {
                $total = count($arr);
                if($total >= MAX_LIKERS)
                        $arr = array_slice($arr, 0, MAX_LIKERS - 1);
@@ -1008,55 +1064,33 @@ function format_like($cnt,$arr,$type,$id) {
                }
 
                $likers = $str;
-       }
 
-       // Phrase if there is only one liker. In other cases it will be uses for the expanded
-       // list which show all likers
-       switch($type) {
-               case 'like' :
-                       $phrase = sprintf( t('%s likes this.'), $likers);
-                       break;
-               case 'dislike' :
-                       $phrase = sprintf( t('%s doesn\'t like this.'), $likers);
-                       break;
-               case 'attendyes' :
-                       $phrase = sprintf( t('%s attends.'), $likers);
-                       break;
-               case 'attendno' :
-                       $phrase = sprintf( t('%s doesn\'t attend.'), $likers);
-                       break;
-               case 'attendmaybe' :
-                       $phrase = sprintf( t('%s attends maybe.'), $likers);
-                       break;
-       }
-
-       if($cnt > 1) {
                $spanatts = "class=\"fakelink\" onclick=\"openClose('{$type}list-$id');\"";
-               $expanded .= "\t" . '<div class="wall-item-' . $type . '-expanded" id="' . $type . 'list-' . $id . '" style="display: none;" >' . $phrase . EOL . '</div>';
+
                switch($type) {
                        case 'like':
                                $phrase = sprintf( t('<span  %1$s>%2$d people</span> like this'), $spanatts, $cnt);
+                               $explikers = sprintf( t('%s like this.'), $likers);
                                break;
                        case 'dislike':
                                $phrase = sprintf( t('<span  %1$s>%2$d people</span> don\'t like this'), $spanatts, $cnt);
+                               $explikers = sprintf( t('%s don\'t like this.'), $likers);
                                break;
                        case 'attendyes':
                                $phrase = sprintf( t('<span  %1$s>%2$d people</span> attend'), $spanatts, $cnt);
+                               $explikers = sprintf( t('%s attend.'), $likers);
                                break;
                        case 'attendno':
                                $phrase = sprintf( t('<span  %1$s>%2$d people</span> don\'t attend'), $spanatts, $cnt);
+                               $explikers = sprintf( t('%s don\'t attend.'), $likers);
                                break;
                        case 'attendmaybe':
                                $phrase = sprintf( t('<span  %1$s>%2$d people</span> anttend maybe'), $spanatts, $cnt);
-                       case 'agree':
-                               $phrase = sprintf( t('<span  %1$s>%2$d people</span> agree'), $spanatts, $cnt);
+                               $explikers = sprintf( t('%s anttend maybe.'), $likers);
                                break;
-                       case 'disagree':
-                               $phrase = sprintf( t('<span  %1$s>%2$d people</span> don\'t agree'), $spanatts, $cnt);
-                               break;
-                       case 'abstain':
-                               $phrase = sprintf( t('<span  %1$s>%2$d people</span> abstains'), $spanatts, $cnt);
                }
+
+               $expanded .= "\t" . '<div class="wall-item-' . $type . '-expanded" id="' . $type . 'list-' . $id . '" style="display: none;" >' . $explikers . EOL . '</div>';
        }
 
        $phrase .= EOL ;
@@ -1257,6 +1291,15 @@ function conv_sort($arr,$order) {
 
        $parents = array();
        $children = array();
+       $newarr = array();
+
+       // This is a preparation for having two different items with the same uri in one thread
+       // This will otherwise lead to an endless loop.
+       foreach($arr as $x)
+               if (!isset($newarr[$x['uri']]))
+                       $newarr[$x['uri']] = $x;
+
+       $arr = $newarr;
 
        foreach($arr as $x)
                if($x['id'] == $x['parent'])