From 6d82fcd55b9eedbc14dff763cb312341a1945ee9 Mon Sep 17 00:00:00 2001 From: Ben Liyanage Date: Thu, 9 Oct 2014 15:53:45 -0400 Subject: [PATCH] added a bunch of code for the post body --- fbsync/object/Facebook_Graph21.php | 140 +++++++++++++++++++++++++++-- fbsync/tests/fbsync_test.php | 6 +- 2 files changed, 139 insertions(+), 7 deletions(-) diff --git a/fbsync/object/Facebook_Graph21.php b/fbsync/object/Facebook_Graph21.php index cdd66550..e670f56b 100644 --- a/fbsync/object/Facebook_Graph21.php +++ b/fbsync/object/Facebook_Graph21.php @@ -32,7 +32,6 @@ Class Facebook_Graph21 extends Facebook $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['created'] = datetime_convert('UTC','UTC',date("c", $post->created_time)); @@ -62,12 +61,30 @@ Class Facebook_Graph21 extends Facebook //TODO: Parent Post Code //TODO: Set $postarray['contact-id'] = $contact_id; Should either be the actor_id or the source_id (not in graph?) + $postarray['contact-id'] = 1; - //TODO: Body does not seem to be used in graph or fql any more. What is this value supposed to be? - $postarray["body"] = ""; + //Set Object Type + if (!isset($post->attachment[0]->type)) + { + //Default Object Type + $postarray['object-type'] = ACTIVITY_OBJ_NOTE; // default value - is maybe changed later when media is attached + } else { + // Change the object type when an attachment is present + $postarray['object-type'] = $post->attachment[0]->type; // default value - is maybe changed later when media is attached + } + /* More type code. I think this is not necessary. + require_once("include/oembed.php"); + $oembed_data = oembed_fetch_url($post->attachment->href); + $type = $oembed_data->type; + if ($type == "rich") + $type = "link"; + */ - //TODO: Deal with attachments - //Kind of a big deal. + //TODO: Body needs more testing, and has some more frindge cases. + $postarray["body"] = $this->AssembleBody($post->name, $post->link, $post->description, $post->picture); //"This is the body. [quote]This is the quote.[/quote]"; + + //TODO: Do tags + $postarray["tag"] = "This is the tag"; $postarray['app'] = ($post->application->name == "" ? "Facebook" : $post->application->name); @@ -81,6 +98,119 @@ Class Facebook_Graph21 extends Facebook return $postarray; } + + function AssembleBody($Title, $Href, $Body, $Picture) + { + //TODO: Need to do prebody code still. + //TODO: Need to add class (aka type) code + $postarray["body"] = (isset($post->message) ? escape_tags($post->message) : ''); + + $msgdata = fbsync_convertmsg($a, $postarray["body"]); + + $postarray["body"] = $msgdata["body"]; + $postarray["tag"] = $msgdata["tags"]; + + $content = ""; + + if ($Picture != "") + { + $pictureStr = '[img]' . $Picture . '[/img]'; + if ($Href != "") + { + $pictureStr = '[url=' . $Href . ']' . $pictureStr . '[/url]'; + } + + $content .= "\n" . $pictureStr; + } + + if ($Title != "" and $Href != "") { + + $content .= "[bookmark=".$Href."]".$Title."[/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 ($Title != "") { + $content .= "[b]" . $post->attachment->name."[/b]"; + } + + $content .= "\n[quote]".trim($Body)."[/quote]"; + + + /* + + if (isset($post->attachment->media) AND (($type == "") OR ($type == "link"))) { + foreach ($post->attachment->media AS $media) { + + if (isset($media->type)) + $type = $media->type; + + 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 ($preview != "") + $content .= "\n".'[img]'.$preview.'[/img]'; + + // if just a link, it may be a wall photo - check + if (isset($post->link)) + $content .= fbpost_get_photo($media->href); + } + } + } + + if ($type == "link") + $postarray["object-type"] = ACTIVITY_OBJ_BOOKMARK; + + if ($content) + $postarray["body"] .= "\n"; + + if ($type) + $postarray["body"] .= "[class=type-".$type."]"; + + if ($content) + $postarray["body"] .= trim($content); + + if ($quote) + $postarray["body"] .= "\n[quote]".trim($quote)."[/quote]"; + + if ($type) + $postarray["body"] .= "[/class]"; + + $postarray["body"] = trim($postarray["body"]); + + if (trim($postarray["body"]) == "") + return; + + if ($prebody != "") + $postarray["body"] = $prebody.$postarray["body"]."[/share]"; + */ + + return $content; + } } ?> \ No newline at end of file diff --git a/fbsync/tests/fbsync_test.php b/fbsync/tests/fbsync_test.php index 7c9fdc1b..b4bbbe89 100644 --- a/fbsync/tests/fbsync_test.php +++ b/fbsync/tests/fbsync_test.php @@ -32,7 +32,9 @@ if ($myFBSync->access_token == '') die("failed to load access_token"); //Test FetchContact //Test CreatePost -$posts = json_decode(file_get_contents("./addon/fbsync/tests/graph2.1.txt")); +$posts = json_decode(file_get_contents("./addon/fbsync/tests/graph2.1-no-filter.txt")); + +var_dump($posts); $post = $myFBSync->CreatePost($a,0,0,0,$posts->data[0],0); @@ -52,7 +54,7 @@ SELECT action_links, actor_id, app_data, app_id, attachment, attribution, commen //Todo:Actions can probably be removed me/home?fields=actions&since=992438&updated_time=0&filter=nf&limit=1 -me/home?fields=actions,link,id,created_time,application,attachments,updated_time,object_id,with_tags,comments{can_comment,comment_count},likes,message,message_tags,description,parent_id,place,privacy,shares&limit=1 +me/home?fields=actions,link,id,created_time,application,attachments,updated_time,object_id,with_tags,comments{can_comment,comment_count},likes,message,message_tags,description,parent_id,place,privacy,shares,from&limit=1 https://developers.facebook.com/docs/graph-api/reference/v2.1/test-user