]> git.mxchange.org Git - friendica-addons.git/blobdiff - twitter/twitter.php
RU translation of forumdirectory addon THX pztrn
[friendica-addons.git] / twitter / twitter.php
index eab557d4b3d5bdaf428a16b976971b87f76f0dc0..74121d295e7a8084d467a122880174b59a512737 100644 (file)
@@ -494,7 +494,8 @@ function twitter_post_hook(&$a,&$b) {
 
                if (isset($msgarr["url"]) AND ($msgarr["type"] != "photo"))
                        $msg .= "\n".$msgarr["url"];
-               elseif (isset($msgarr["image"]) AND ($msgarr["type"] != "video"))
+
+               if (isset($msgarr["image"]) AND ($msgarr["type"] != "video"))
                        $image = $msgarr["image"];
 
                // and now tweet it :-)
@@ -624,7 +625,12 @@ function twitter_cron($a,$b) {
        if(count($r)) {
                foreach($r as $rr) {
                        logger('twitter: fetching for user '.$rr['uid']);
-                       twitter_fetchtimeline($a, $rr['uid']);
+
+                       if (get_config("system", "worker")) {
+                               proc_run(PRIORITY_MEDIUM, "addon/twitter/twitter_sync.php", 1, $rr['uid']);
+                       } else {
+                               twitter_fetchtimeline($a, $rr['uid']);
+                       }
                }
        }
 
@@ -646,8 +652,12 @@ function twitter_cron($a,$b) {
                        }
 
                        logger('twitter: importing timeline from user '.$rr['uid']);
-                       twitter_fetchhometimeline($a, $rr["uid"]);
 
+                       if (get_config("system", "worker")) {
+                               proc_run(PRIORITY_MEDIUM, "addon/twitter/twitter_sync.php", 2, $rr['uid']);
+                       } else {
+                               twitter_fetchhometimeline($a, $rr["uid"]);
+                       }
 /*
                        // To-Do
                        // check for new contacts once a day
@@ -734,16 +744,27 @@ function twitter_prepare_body(&$a,&$b) {
        }
 }
 
+/**
+ * @brief Build the item array for the mirrored post
+ *
+ * @param object $a Application class
+ * @param integer $uid User id
+ * @param object $post Twitter object with the post
+ *
+ * @return array item data to be posted
+ */
 function twitter_do_mirrorpost($a, $uid, $post) {
        $datarray["type"] = "wall";
        $datarray["api_source"] = true;
        $datarray["profile_uid"] = $uid;
        $datarray["extid"] = NETWORK_TWITTER;
        $datarray['message_id'] = item_new_uri($a->get_hostname(), $uid, NETWORK_TWITTER.":".$post->id);
+       $datarray['object'] = json_encode($post);
        $datarray["title"] = "";
 
        if (is_object($post->retweeted_status)) {
-               $item = twitter_createpost($a, $uid, $post, array('id' => 0), false, false, true);
+               // We don't support nested shares, so we mustn't show quotes as shares on retweets
+               $item = twitter_createpost($a, $uid, $post->retweeted_status, array('id' => 0), false, false, true);
 
                $datarray['body'] = "\n".share_header($item['author-name'], $item['author-link'], $item['author-avatar'], "",
                                        $item['created'], $item['plink']);
@@ -790,7 +811,6 @@ function twitter_fetchtimeline($a, $uid) {
        require_once('library/twitteroauth.php');
        $connection = new TwitterOAuth($ckey,$csecret,$otoken,$osecret);
 
-       //$parameters = array("exclude_replies" => true, "trim_user" => false, "contributor_details" => true, "include_rts" => true);
        $parameters = array("exclude_replies" => true, "trim_user" => false, "contributor_details" => true, "include_rts" => true, "tweet_mode" => "extended");
 
        $first_time = ($lastid == "");
@@ -822,100 +842,8 @@ function twitter_fetchtimeline($a, $uid) {
 
                        $_REQUEST = twitter_do_mirrorpost($a, $uid, $post);
 
-/*
-                       unset($_REQUEST);
-                       $_REQUEST["type"] = "wall";
-                       $_REQUEST["api_source"] = true;
-                       $_REQUEST["profile_uid"] = $uid;
-                       //$_REQUEST["source"] = "Twitter";
-                       $_REQUEST["source"] = $post->source;
-                       $_REQUEST["extid"] = NETWORK_TWITTER;
-
-                       if (isset($post->id)) {
-                               $_REQUEST['message_id'] = item_new_uri($a->get_hostname(), $uid, NETWORK_TWITTER.":".$post->id);
-                       }
-
-                       //$_REQUEST["date"] = $post->created_at;
-
-                       $_REQUEST["title"] = "";
-
-                       if (is_object($post->retweeted_status)) {
-
-                               $_REQUEST['body'] = $post->retweeted_status->text;
-
-                               $picture = "";
-
-                               // media
-                               if (is_array($post->retweeted_status->entities->media)) {
-                                       foreach($post->retweeted_status->entities->media AS $media) {
-                                               switch($media->type) {
-                                                       case 'photo':
-                                                               //$_REQUEST['body'] = str_replace($media->url, "\n\n[img]".$media->media_url_https."[/img]\n", $_REQUEST['body']);
-                                                               //$has_picture = true;
-                                                               $_REQUEST['body'] = str_replace($media->url, "", $_REQUEST['body']);
-                                                               $picture = $media->media_url_https;
-                                                               break;
-                                               }
-                                       }
-                               }
-
-                               $converted = twitter_expand_entities($a, $_REQUEST['body'], $post->retweeted_status, true, $picture);
-                               $_REQUEST['body'] = $converted["body"];
-
-                               if (function_exists("share_header"))
-                                       $_REQUEST['body'] = share_header($post->retweeted_status->user->name, "https://twitter.com/".$post->retweeted_status->user->screen_name,
-                                                                       $post->retweeted_status->user->profile_image_url_https, "",
-                                                                       datetime_convert('UTC','UTC',$post->retweeted_status->created_at),
-                                                                       "https://twitter.com/".$post->retweeted_status->user->screen_name."/status/".$post->retweeted_status->id_str).
-                                                               $_REQUEST['body'];
-                               else
-                                       $_REQUEST['body'] = "[share author='".$post->retweeted_status->user->name.
-                                               "' profile='https://twitter.com/".$post->retweeted_status->user->screen_name.
-                                               "' avatar='".$post->retweeted_status->user->profile_image_url_https.
-                                               "' posted='".datetime_convert('UTC','UTC',$post->retweeted_status->created_at).
-                                               "' link='https://twitter.com/".$post->retweeted_status->user->screen_name."/status/".$post->retweeted_status->id_str."']".
-                                               $_REQUEST['body'];
-
-                               $_REQUEST['body'] .= "[/share]";
-                       } else {
-                               $_REQUEST["body"] = $post->text;
-
-                               $picture = "";
-
-                               if (is_array($post->entities->media)) {
-                                       foreach($post->entities->media AS $media) {
-                                               switch($media->type) {
-                                                       case 'photo':
-                                                               //$_REQUEST['body'] = str_replace($media->url, "\n\n[img]".$media->media_url_https."[/img]\n", $_REQUEST['body']);
-                                                               //$has_picture = true;
-                                                               $_REQUEST['body'] = str_replace($media->url, "", $_REQUEST['body']);
-                                                               $picture = $media->media_url_https;
-                                                               break;
-                                               }
-                                       }
-                               }
-
-                               $converted = twitter_expand_entities($a, $_REQUEST["body"], $post, true, $picture);
-                               $_REQUEST['body'] = $converted["body"];
-                       }
-
-                       if (is_string($post->place->name))
-                               $_REQUEST["location"] = $post->place->name;
-
-                       if (is_string($post->place->full_name))
-                               $_REQUEST["location"] = $post->place->full_name;
-
-                       if (is_array($post->geo->coordinates))
-                               $_REQUEST["coord"] = $post->geo->coordinates[0]." ".$post->geo->coordinates[1];
-
-                       if (is_array($post->coordinates->coordinates))
-                               $_REQUEST["coord"] = $post->coordinates->coordinates[1]." ".$post->coordinates->coordinates[0];
-*/
-                       //print_r($_REQUEST);
                        logger('twitter: posting for user '.$uid);
 
-//                     require_once('mod/item.php');
-
                        item_post($a);
                }
            }
@@ -1007,36 +935,11 @@ function twitter_fetch_contact($uid, $contact, $create_user) {
 
        $avatar = twitter_fix_avatar($contact->profile_image_url_https);
 
-       if (function_exists("update_gcontact"))
-               update_gcontact(array("url" => "https://twitter.com/".$contact->screen_name,
-                               "network" => NETWORK_TWITTER, "photo" => $avatar,  "hide" => true,
-                               "name" => $contact->name, "nick" => $contact->screen_name,
-                               "location" => $contact->location, "about" => $contact->description,
-                               "addr" => $contact->screen_name."@twitter.com", "generation" => 2));
-       else {
-               // Old Code
-               $r = q("SELECT id FROM unique_contacts WHERE url='%s' LIMIT 1",
-                               dbesc(normalise_link("https://twitter.com/".$contact->screen_name)));
-
-               if (count($r) == 0)
-                       q("INSERT INTO unique_contacts (url, name, nick, avatar) VALUES ('%s', '%s', '%s', '%s')",
-                               dbesc(normalise_link("https://twitter.com/".$contact->screen_name)),
-                               dbesc($contact->name),
-                               dbesc($contact->screen_name),
-                               dbesc($avatar));
-               else
-                       q("UPDATE unique_contacts SET name = '%s', nick = '%s', avatar = '%s' WHERE url = '%s'",
-                               dbesc($contact->name),
-                               dbesc($contact->screen_name),
-                               dbesc($avatar),
-                               dbesc(normalise_link("https://twitter.com/".$contact->screen_name)));
-
-               if (DB_UPDATE_VERSION >= "1177")
-                       q("UPDATE `unique_contacts` SET `location` = '%s', `about` = '%s' WHERE url = '%s'",
-                               dbesc($contact->location),
-                               dbesc($contact->description),
-                               dbesc(normalise_link("https://twitter.com/".$contact->screen_name)));
-       }
+       update_gcontact(array("url" => "https://twitter.com/".$contact->screen_name,
+                       "network" => NETWORK_TWITTER, "photo" => $avatar,  "hide" => true,
+                       "name" => $contact->name, "nick" => $contact->screen_name,
+                       "location" => $contact->location, "about" => $contact->description,
+                       "addr" => $contact->screen_name."@twitter.com", "generation" => 2));
 
        $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `alias` = '%s' LIMIT 1",
                intval($uid), dbesc("twitter::".$contact->id_str));
@@ -1051,10 +954,10 @@ function twitter_fetch_contact($uid, $contact, $create_user) {
 
        if(!count($r)) {
                // create contact record
-               q("INSERT INTO `contact` ( `uid`, `created`, `url`, `nurl`, `addr`, `alias`, `notify`, `poll`,
+               q("INSERT INTO `contact` (`uid`, `created`, `url`, `nurl`, `addr`, `alias`, `notify`, `poll`,
                                        `name`, `nick`, `photo`, `network`, `rel`, `priority`,
-                                       `writable`, `blocked`, `readonly`, `pending` )
-                                       VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, %d, 0, 0, 0) ",
+                                       `location`, `about`, `writable`, `blocked`, `readonly`, `pending`)
+                                       VALUES (%d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, '%s', '%s', %d, 0, 0, 0)",
                        intval($uid),
                        dbesc(datetime_convert()),
                        dbesc("https://twitter.com/".$contact->screen_name),
@@ -1069,6 +972,8 @@ function twitter_fetch_contact($uid, $contact, $create_user) {
                        dbesc(NETWORK_TWITTER),
                        intval(CONTACT_IS_FRIEND),
                        intval(1),
+                       dbesc($contact->location),
+                       dbesc($contact->description),
                        intval(1)
                );
 
@@ -1093,33 +998,25 @@ function twitter_fetch_contact($uid, $contact, $create_user) {
 
                require_once("Photo.php");
 
-               $photos = import_profile_photo($avatar,$uid,$contact_id);
-
-               q("UPDATE `contact` SET `photo` = '%s',
-                                       `thumb` = '%s',
-                                       `micro` = '%s',
-                                       `name-date` = '%s',
-                                       `uri-date` = '%s',
-                                       `avatar-date` = '%s'
-                               WHERE `id` = %d",
-                       dbesc($photos[0]),
-                       dbesc($photos[1]),
-                       dbesc($photos[2]),
-                       dbesc(datetime_convert()),
-                       dbesc(datetime_convert()),
-                       dbesc(datetime_convert()),
-                       intval($contact_id)
-               );
+               $photos = import_profile_photo($avatar, $uid, $contact_id, true);
 
-               if (DB_UPDATE_VERSION >= "1177")
-                       q("UPDATE `contact` SET `location` = '%s',
-                                               `about` = '%s'
+               if ($photos) {
+                       q("UPDATE `contact` SET `photo` = '%s',
+                                               `thumb` = '%s',
+                                               `micro` = '%s',
+                                               `name-date` = '%s',
+                                               `uri-date` = '%s',
+                                                       `avatar-date` = '%s'
                                        WHERE `id` = %d",
-                               dbesc($contact->location),
-                               dbesc($contact->description),
+                               dbesc($photos[0]),
+                               dbesc($photos[1]),
+                               dbesc($photos[2]),
+                               dbesc(datetime_convert()),
+                               dbesc(datetime_convert()),
+                               dbesc(datetime_convert()),
                                intval($contact_id)
                        );
-
+               }
        } else {
                // update profile photos once every two weeks as we have no notification of when they change.
 
@@ -1134,42 +1031,39 @@ function twitter_fetch_contact($uid, $contact, $create_user) {
 
                        require_once("Photo.php");
 
-                       $photos = import_profile_photo($avatar, $uid, $r[0]['id']);
-
-                       q("UPDATE `contact` SET `photo` = '%s',
-                                               `thumb` = '%s',
-                                               `micro` = '%s',
-                                               `name-date` = '%s',
-                                               `uri-date` = '%s',
-                                               `avatar-date` = '%s',
-                                               `url` = '%s',
-                                               `nurl` = '%s',
-                                               `addr` = '%s',
-                                               `name` = '%s',
-                                               `nick` = '%s'
-                                       WHERE `id` = %d",
-                               dbesc($photos[0]),
-                               dbesc($photos[1]),
-                               dbesc($photos[2]),
-                               dbesc(datetime_convert()),
-                               dbesc(datetime_convert()),
-                               dbesc(datetime_convert()),
-                               dbesc("https://twitter.com/".$contact->screen_name),
-                               dbesc(normalise_link("https://twitter.com/".$contact->screen_name)),
-                               dbesc($contact->screen_name."@twitter.com"),
-                               dbesc($contact->name),
-                               dbesc($contact->screen_name),
-                               intval($r[0]['id'])
-                       );
-
-                       if (DB_UPDATE_VERSION >= "1177")
-                               q("UPDATE `contact` SET `location` = '%s',
+                       $photos = import_profile_photo($avatar, $uid, $r[0]['id'], true);
+
+                       if ($photos) {
+                               q("UPDATE `contact` SET `photo` = '%s',
+                                                       `thumb` = '%s',
+                                                       `micro` = '%s',
+                                                       `name-date` = '%s',
+                                                       `uri-date` = '%s',
+                                                       `avatar-date` = '%s',
+                                                       `url` = '%s',
+                                                       `nurl` = '%s',
+                                                       `addr` = '%s',
+                                                       `name` = '%s',
+                                                       `nick` = '%s',
+                                                       `location` = '%s',
                                                        `about` = '%s'
                                                WHERE `id` = %d",
+                                       dbesc($photos[0]),
+                                       dbesc($photos[1]),
+                                       dbesc($photos[2]),
+                                       dbesc(datetime_convert()),
+                                       dbesc(datetime_convert()),
+                                       dbesc(datetime_convert()),
+                                       dbesc("https://twitter.com/".$contact->screen_name),
+                                       dbesc(normalise_link("https://twitter.com/".$contact->screen_name)),
+                                       dbesc($contact->screen_name."@twitter.com"),
+                                       dbesc($contact->name),
+                                       dbesc($contact->screen_name),
                                        dbesc($contact->location),
                                        dbesc($contact->description),
                                        intval($r[0]['id'])
                                );
+                       }
                }
        }
 
@@ -1381,8 +1275,8 @@ function twitter_media_entities($post, &$postarray) {
        }
 
        // When the post links to an external page, we only take one picture.
-       // This could be improved in the future.
-       if (count($post->entities->urls) > 0) {
+       // We only do this when there is exactly one media.
+       if ((count($post->entities->urls) > 0) AND (count($post->extended_entities->media) == 1)) {
                $picture = "";
                foreach($post->extended_entities->media AS $medium) {
                        if (isset($medium->media_url_https)) {
@@ -1439,12 +1333,14 @@ function twitter_createpost($a, $uid, $post, $self, $create_user, $only_existing
        $postarray['uri'] = "twitter::".$post->id_str;
        $postarray['object'] = json_encode($post);
 
+       // Don't import our own comments
        $r = q("SELECT * FROM `item` WHERE `extid` = '%s' AND `uid` = %d LIMIT 1",
                        dbesc($postarray['uri']),
                        intval($uid)
                );
 
        if (count($r)) {
+               logger("Item with extid ".$postarray['uri']." found.", LOGGER_DEBUG);
                return(array());
        }
 
@@ -1494,6 +1390,7 @@ function twitter_createpost($a, $uid, $post, $self, $create_user, $only_existing
                                $postarray['owner-link'] = $r[0]["url"];
                                $postarray['owner-avatar'] =  $r[0]["photo"];
                        } else {
+                               logger("No self contact for user ".$uid, LOGGER_DEBUG);
                                return(array());
                        }
                }
@@ -1515,6 +1412,7 @@ function twitter_createpost($a, $uid, $post, $self, $create_user, $only_existing
        if(($contactid == 0) AND !$only_existing_contact) {
                $contactid = $self['id'];
        } elseif ($contactid <= 0) {
+               logger("Contact ID is zero or less than zero.", LOGGER_DEBUG);
                return(array());
        }
 
@@ -1570,6 +1468,8 @@ function twitter_createpost($a, $uid, $post, $self, $create_user, $only_existing
                $retweet = twitter_createpost($a, $uid, $post->retweeted_status, $self, false, false, $noquote);
 
                $retweet['object'] = $postarray['object'];
+               $retweet['private'] = $postarray['private'];
+               $retweet['allow_cid'] = $postarray['allow_cid'];
                $retweet['contact-id'] = $postarray['contact-id'];
                $retweet['owner-name'] = $postarray['owner-name'];
                $retweet['owner-link'] = $postarray['owner-link'];