]> git.mxchange.org Git - friendica-addons.git/blobdiff - fbsync/fbsync.php
Merge pull request #211 from tobiasd/20140622
[friendica-addons.git] / fbsync / fbsync.php
index a1f88af9245b0852d7dc7436d509a5b947fdd8a5..51d2747f93d8640570032d8eda81f575c796b412 100644 (file)
@@ -2,7 +2,7 @@
 /**
  * Name: Facebook Sync
  * Description: Synchronizes the Facebook Newsfeed
- * Version: 0.0.1 alpha
+ * Version: 1.0
  * Author: Michael Vogel <https://pirati.ca/profile/heluecht>
  */
 
@@ -26,6 +26,7 @@ function fbsync_install() {
        register_hook('connector_settings_post', 'addon/fbsync/fbsync.php', 'fbsync_settings_post');
        register_hook('cron', 'addon/fbsync/fbsync.php', 'fbsync_cron');
        register_hook('follow', 'addon/fbsync/fbsync.php', 'fbsync_follow');
+       register_hook('expire', 'addon/fbsync/fbsync.php', 'fbsync_expire');
 }
 
 function fbsync_uninstall() {
@@ -33,6 +34,7 @@ function fbsync_uninstall() {
        unregister_hook('connector_settings_post', 'addon/fbsync/fbsync.php', 'fbsync_settings_post');
        unregister_hook('cron', 'addon/fbsync/fbsync.php', 'fbsync_cron');
        unregister_hook('follow', 'addon/fbsync/fbsync.php', 'fbsync_follow');
+       unregister_hook('expire', 'addon/fbsync/fbsync.php', 'fbsync_expire');
 }
 
 function fbsync_follow($a, &$contact) {
@@ -185,8 +187,34 @@ function fbsync_cron($a,$b) {
        set_config('fbsync','last_poll', time());
 }
 
+function fbsync_expire($a,$b) {
+
+       $days = get_config('fbsync', 'expire');
+
+       if ($days == 0)
+               return;
+
+       $r = q("DELETE FROM `item` WHERE `deleted` AND `network` = '%s'", dbesc(NETWORK_FACEBOOK));
+
+       require_once("include/items.php");
+
+       logger('fbsync_expire: expire_start');
+
+       $r = q("SELECT * FROM `pconfig` WHERE `cat` = 'fbsync' AND `k` = 'sync' AND `v` = '1' ORDER BY RAND()");
+       if(count($r)) {
+               foreach($r as $rr) {
+                       logger('fbsync_expire: user '.$rr['uid']);
+                       item_expire($rr['uid'], $days, NETWORK_FACEBOOK, true);
+               }
+       }
+
+       logger('fbsync_expire: expire_end');
+}
+
 function fbsync_createpost($a, $uid, $self, $contacts, $applications, $post, $create_user) {
 
+       $access_token = get_pconfig($uid,'facebook','access_token');
+
        require_once("include/oembed.php");
 
        // check if it was already imported
@@ -203,6 +231,7 @@ function fbsync_createpost($a, $uid, $self, $contacts, $applications, $post, $cr
        $postarray['wall'] = 0;
 
        $postarray['verb'] = ACTIVITY_POST;
+       $postarray['object-type'] = ACTIVITY_OBJ_NOTE; // default value - is maybe changed later when media is attached
        $postarray['network'] =  dbesc(NETWORK_FACEBOOK);
 
        $postarray['uri'] = "fb::".$post->post_id;
@@ -252,7 +281,19 @@ function fbsync_createpost($a, $uid, $self, $contacts, $applications, $post, $cr
        }
 
        if ($contact_id <= 0) {
-               $contact_id = fbsync_fetch_contact($uid, $contacts[$post->source_id], $create_user);
+               if ($post->actor_id != $post->source_id) {
+                       // Testing if we know the source or the actor
+                       $contact_id = fbsync_fetch_contact($uid, $contacts[$post->source_id], false);
+
+                       if (($contact_id == 0) and array_key_exists($post->actor_id, $contacts))
+                               $contact_id = fbsync_fetch_contact($uid, $contacts[$post->actor_id], false);
+
+                       // If we don't know anyone, we guess we should know the source. Could be the wrong decision
+                       if ($contact_id == 0)
+                               $contact_id = fbsync_fetch_contact($uid, $contacts[$post->source_id], $create_user);
+               } else
+                       $contact_id = fbsync_fetch_contact($uid, $contacts[$post->source_id], $create_user);
+
 
                if ($contact_id == -1) {
                        logger('fbsync_createpost: Contact is blocked. Post not imported '.print_r($post, true), LOGGER_DEBUG);
@@ -276,54 +317,96 @@ function fbsync_createpost($a, $uid, $self, $contacts, $applications, $post, $cr
        $postarray["body"] = $msgdata["body"];
        $postarray["tag"] = $msgdata["tags"];
 
+       // Change the object type when an attachment is present
+       if (isset($post->attachment->fb_object_type))
+               logger('fb_object_type: '.$post->attachment->fb_object_type." ".print_r($post->attachment, true), LOGGER_DEBUG);
+               switch ($post->attachment->fb_object_type) {
+                       case 'photo':
+                               $postarray['object-type'] = ACTIVITY_OBJ_IMAGE; // photo is deprecated: http://activitystrea.ms/head/activity-schema.html#image
+                               break;
+                       case 'video':
+                               $postarray['object-type'] = ACTIVITY_OBJ_VIDEO;
+                               break;
+                       case '':
+                               //$postarray['object-type'] = ACTIVITY_OBJ_BOOKMARK;
+                               break;
+                       default:
+                               logger('Unknown object type '.$post->attachment->fb_object_type, LOGGER_DEBUG);
+                               break;
+               }
+
        $content = "";
        $type = "";
 
        if (isset($post->attachment->name) and isset($post->attachment->href)) {
                $oembed_data = oembed_fetch_url($post->attachment->href);
                $type = $oembed_data->type;
+               if ($type == "rich")
+                       $type = "link";
+
                $content = "[bookmark=".$post->attachment->href."]".$post->attachment->name."[/bookmark]";
+
+               // If a link is not only attached but also added in the body, look if it can be removed in the body.
+               $removedlink = trim(str_replace($post->attachment->href, "", $postarray["body"]));
+
+               if (($removedlink == "") OR strstr($postarray["body"], $removedlink))
+                       $postarray["body"] = $removedlink;
+
        } elseif (isset($post->attachment->name) AND ($post->attachment->name != ""))
                $content = "[b]" . $post->attachment->name."[/b]";
 
        $quote = "";
-       if(isset($post->attachment->description) and ($post->attachment->fb_object_type != "photo"))
+       if (isset($post->attachment->description) and ($post->attachment->fb_object_type != "photo"))
                $quote = $post->attachment->description;
 
-       if(isset($post->attachment->caption) and ($post->attachment->fb_object_type == "photo"))
+       if (isset($post->attachment->caption) and ($post->attachment->fb_object_type == "photo"))
                $quote = $post->attachment->caption;
 
        if ($quote.$post->attachment->href.$content.$postarray["body"] == "")
                return;
 
-       if (isset($post->attachment->media) // AND !strstr($post->attachment->href, "://www.youtube.com/")
-               //AND !strstr($post->attachment->href, "://youtu.be/")
-               //AND !strstr($post->attachment->href, ".vimeo.com/"))
-               AND (($type == "") OR ($type == "link"))) {
+       if (isset($post->attachment->media) AND (($type == "") OR ($type == "link"))) {
                foreach ($post->attachment->media AS $media) {
-                       //$media->photo->owner = number_format($media->photo->owner, 0, '', '');
-                       //if ($media->photo->owner != '') {
-                       //      $postarray['author-name'] = $contacts[$media->photo->owner]->name;
-                       //      $postarray['author-link'] = $contacts[$media->photo->owner]->url;
-                       //      $postarray['author-avatar'] = $contacts[$media->photo->owner]->pic_square;
-                       //}
 
                        if (isset($media->type))
                                $type = $media->type;
 
-                       if(isset($media->src) && isset($media->href) AND ($media->src != "") AND ($media->href != ""))
-                               $content .= "\n".'[url='.$media->href.'][img]'.fpost_cleanpicture($media->src).'[/img][/url]';
+                       if (isset($media->src))
+                               $preview = $media->src;
+
+                       if (isset($media->photo)) {
+                               if (isset($media->photo->images) AND (count($media->photo->images) > 1))
+                                       $preview = $media->photo->images[1]->src;
+
+                               if (isset($media->photo->fbid)) {
+                                       logger('fbsync_createpost: fetching fbid '.$media->photo->fbid, LOGGER_DEBUG);
+                                       $url = "https://graph.facebook.com/".$media->photo->fbid."?access_token=".$access_token;
+                                       $feed = fetch_url($url);
+                                       $data = json_decode($feed);
+                                       if (isset($data->images)) {
+                                               $preview = $data->images[0]->source;
+                                               logger('fbsync_createpost: got fbid '.$media->photo->fbid.' image '.$preview, LOGGER_DEBUG);
+                                       } else
+                                               logger('fbsync_createpost: error fetching fbid '.$media->photo->fbid.' '.print_r($data, true), LOGGER_DEBUG);
+                               }
+                       }
+
+                       if (isset($media->href) AND ($preview != "") AND ($media->href != ""))
+                               $content .= "\n".'[url='.$media->href.'][img]'.$preview.'[/img][/url]';
                        else {
-                               if (isset($media->src) AND ($media->src != ""))
-                                       $content .= "\n".'[img]'.fpost_cleanpicture($media->src).'[/img]';
+                               if ($preview != "")
+                                       $content .= "\n".'[img]'.$preview.'[/img]';
 
                                // if just a link, it may be a wall photo - check
-                               if(isset($post->link))
+                               if (isset($post->link))
                                        $content .= fbpost_get_photo($media->href);
                        }
                }
        }
 
+       if ($type == "link")
+               $postarray["object-type"] = ACTIVITY_OBJ_BOOKMARK;
+
        if ($content)
                $postarray["body"] .= "\n";
 
@@ -370,7 +453,6 @@ function fbsync_createpost($a, $uid, $self, $contacts, $applications, $post, $cr
        //      $postarray["body"] = "Type: ".$post->type."\n".$postarray["body"];
        //print_r($post);
        //print_r($postarray);
-
        $item = item_store($postarray);
        logger('fbsync_createpost: User '.$self[0]["nick"].' posted feed item '.$item, LOGGER_DEBUG);
 }
@@ -450,6 +532,7 @@ function fbsync_createcomment($a, $uid, $self_id, $self, $user, $contacts, $appl
        $postarray['wall'] = 0;
 
        $postarray['verb'] = ACTIVITY_POST;
+       $postarray['object-type'] = ACTIVITY_OBJ_COMMENT;
        $postarray['network'] =  dbesc(NETWORK_FACEBOOK);
 
        $postarray['uri'] = "fb::".$comment->id;
@@ -549,7 +632,7 @@ function fbsync_createcomment($a, $uid, $self_id, $self, $user, $contacts, $appl
                                        'to_email'     => $user[0]['email'],
                                        'uid'          => $user[0]['uid'],
                                        'item'         => $postarray,
-                                       'link'             => $a->get_baseurl() . '/display/' . $user[0]['nickname'] . '/' . $item,
+                                       'link'         => $a->get_baseurl().'/display/'.urlencode(get_item_guid($item)),
                                        'source_name'  => $postarray['author-name'],
                                        'source_link'  => $postarray['author-link'],
                                        'source_photo' => $postarray['author-avatar'],
@@ -601,8 +684,11 @@ function fbsync_createlike($a, $uid, $self_id, $self, $contacts, $like) {
 
        $likedata = array();
        $likedata['parent'] = $orig_post['id'];
+
        $likedata['verb'] = ACTIVITY_LIKE;
+       $likedata['object-type'] = ACTIVITY_OBJ_NOTE;
        $likedate['network'] =  dbesc(NETWORK_FACEBOOK);
+
        $likedata['gravity'] = 3;
        $likedata['uid'] = $uid;
        $likedata['wall'] = 0;
@@ -652,6 +738,9 @@ function fbsync_createlike($a, $uid, $self_id, $self, $contacts, $like) {
 
 function fbsync_fetch_contact($uid, $contact, $create_user) {
 
+       if($contact->url == "")
+               return(0);
+
        // Check if the unique contact is existing
        // To-Do: only update once a while
        $r = q("SELECT id FROM unique_contacts WHERE url='%s' LIMIT 1",
@@ -933,7 +1022,6 @@ function fbsync_fetchfeed($a, $uid) {
        $url = "https://graph.facebook.com/fql?q=".urlencode(json_encode($fql))."&access_token=".$access_token;
 
        $feed = fetch_url($url);
-//file_put_contents("/home/ike/pirati.ca/htdocs/fb.".$uid, $feed);
        $data = json_decode($feed);
 
        if (!is_array($data->data)) {
@@ -1017,7 +1105,6 @@ function fbsync_fetchfeed($a, $uid) {
        foreach ($post_data AS $post) {
                if ($post->updated_time > $last_updated)
                        $last_updated = $post->updated_time;
-
                fbsync_createpost($a, $uid, $self, $contacts, $application_data, $post, $create_user);
        }