Beispiel #1
0
function appnet_createpost($a, $uid, $post, $me, $user, $ownid, $createuser, $threadcompletion = true, $nodupcheck = false)
{
    require_once 'include/items.php';
    if ($post["machine_only"]) {
        return;
    }
    if ($post["is_deleted"]) {
        return;
    }
    $postarray = array();
    $postarray['gravity'] = 0;
    $postarray['uid'] = $uid;
    $postarray['wall'] = 0;
    $postarray['verb'] = ACTIVITY_POST;
    $postarray['network'] = dbesc(NETWORK_APPNET);
    if (is_array($post["repost_of"])) {
        // You can't reply to reposts. So use the original id and thread-id
        $postarray['uri'] = "adn::" . $post["repost_of"]["id"];
        $postarray['parent-uri'] = "adn::" . $post["repost_of"]["thread_id"];
    } else {
        $postarray['uri'] = "adn::" . $post["id"];
        $postarray['parent-uri'] = "adn::" . $post["thread_id"];
    }
    if (!$nodupcheck) {
        $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1", dbesc($postarray['uri']), intval($uid));
        if (count($r)) {
            return $r[0];
        }
        $r = q("SELECT * FROM `item` WHERE `extid` = '%s' AND `uid` = %d LIMIT 1", dbesc($postarray['uri']), intval($uid));
        if (count($r)) {
            return $r[0];
        }
    }
    if (isset($post["reply_to"]) and $post["reply_to"] != "") {
        $postarray['thr-parent'] = "adn::" . $post["reply_to"];
        // Complete the thread (if the parent doesn't exists)
        if ($threadcompletion) {
            //$r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
            //	dbesc($postarray['thr-parent']),
            //	intval($uid)
            //	);
            //if (!count($r)) {
            logger("appnet_createpost: completing thread " . $post["thread_id"] . " for user " . $uid, LOGGER_DEBUG);
            require_once "addon/appnet/AppDotNet.php";
            $token = get_pconfig($uid, 'appnet', 'token');
            $clientId = get_pconfig($uid, 'appnet', 'clientid');
            $clientSecret = get_pconfig($uid, 'appnet', 'clientsecret');
            $app = new AppDotNet($clientId, $clientSecret);
            $app->setAccessToken($token);
            $param = array("count" => 200, "include_deleted" => false, "include_directed_posts" => true, "include_html" => false, "include_post_annotations" => true);
            try {
                $thread = $app->getPostReplies($post["thread_id"], $param);
            } catch (AppDotNetException $e) {
                logger("appnet_createpost: Error fetching thread for user " . $uid . " " . appnet_error($e->getMessage()));
            }
            $thread = array_reverse($thread);
            logger("appnet_createpost: fetched " . count($thread) . " items for thread " . $post["thread_id"] . " for user " . $uid, LOGGER_DEBUG);
            foreach ($thread as $tpost) {
                $threadpost = appnet_createpost($a, $uid, $tpost, $me, $user, $ownid, false, false);
                $item = item_store($threadpost);
                $threadpost["id"] = $item;
                logger("appnet_createpost: stored post " . $post["id"] . " thread " . $post["thread_id"] . " in item " . $item, LOGGER_DEBUG);
            }
            //}
        }
        // Don't create accounts of people who just comment something
        $createuser = false;
        $postarray['object-type'] = ACTIVITY_OBJ_COMMENT;
    } else {
        $postarray['thr-parent'] = $postarray['uri'];
        $postarray['object-type'] = ACTIVITY_OBJ_NOTE;
    }
    if ($post["user"]["id"] != $ownid or $postarray['thr-parent'] == $postarray['uri']) {
        $postarray['owner-name'] = $post["user"]["name"];
        $postarray['owner-link'] = $post["user"]["canonical_url"];
        $postarray['owner-avatar'] = $post["user"]["avatar_image"]["url"];
        $postarray['contact-id'] = appnet_fetchcontact($a, $uid, $post["user"], $me, $createuser);
    } else {
        $postarray['owner-name'] = $me["name"];
        $postarray['owner-link'] = $me["url"];
        $postarray['owner-avatar'] = $me["thumb"];
        $postarray['contact-id'] = $me["id"];
    }
    $links = array();
    if (is_array($post["repost_of"])) {
        $postarray['author-name'] = $post["repost_of"]["user"]["name"];
        $postarray['author-link'] = $post["repost_of"]["user"]["canonical_url"];
        $postarray['author-avatar'] = $post["repost_of"]["user"]["avatar_image"]["url"];
        $content = $post["repost_of"];
    } else {
        $postarray['author-name'] = $postarray['owner-name'];
        $postarray['author-link'] = $postarray['owner-link'];
        $postarray['author-avatar'] = $postarray['owner-avatar'];
        $content = $post;
    }
    $postarray['plink'] = $content["canonical_url"];
    if (is_array($content["entities"])) {
        $converted = appnet_expand_entities($a, $content["text"], $content["entities"]);
        $postarray['body'] = $converted["body"];
        $postarray['tag'] = $converted["tags"];
    } else {
        $postarray['body'] = $content["text"];
    }
    if (sizeof($content["entities"]["links"])) {
        foreach ($content["entities"]["links"] as $link) {
            $url = normalise_link($link["url"]);
            $links[$url] = $link["url"];
        }
    }
    /* if (sizeof($content["annotations"]))
    		foreach($content["annotations"] AS $annotation) {
    			if ($annotation[type] == "net.app.core.oembed") {
    				if (isset($annotation["value"]["embeddable_url"])) {
    					$url = normalise_link($annotation["value"]["embeddable_url"]);
    					if (isset($links[$url]))
    						unset($links[$url]);
    				}
    			} elseif ($annotation[type] == "com.friendica.post") {
    				//$links = array();
    				//if (isset($annotation["value"]["post-title"]))
    				//	$postarray['title'] = $annotation["value"]["post-title"];
    
    				//if (isset($annotation["value"]["post-body"]))
    				//	$postarray['body'] = $annotation["value"]["post-body"];
    
    				//if (isset($annotation["value"]["post-tag"]))
    				//	$postarray['tag'] = $annotation["value"]["post-tag"];
    
    				if (isset($annotation["value"]["author-name"]))
    					$postarray['author-name'] = $annotation["value"]["author-name"];
    
    				if (isset($annotation["value"]["author-link"]))
    					$postarray['author-link'] = $annotation["value"]["author-link"];
    
    				if (isset($annotation["value"]["author-avatar"]))
    					$postarray['author-avatar'] = $annotation["value"]["author-avatar"];
    			}
    
    		} */
    $page_info = "";
    if (is_array($content["annotations"])) {
        $photo = appnet_expand_annotations($a, $content["annotations"]);
        if ($photo["large"] != "" and $photo["url"] != "") {
            $page_info = "\n[url=" . $photo["url"] . "][img]" . $photo["large"] . "[/img][/url]";
        } elseif ($photo["url"] != "") {
            $page_info = "\n[img]" . $photo["url"] . "[/img]";
        }
        if ($photo["url"] != "") {
            $postarray['object-type'] = ACTIVITY_OBJ_IMAGE;
        }
    } else {
        $photo = array("url" => "", "large" => "");
    }
    if (sizeof($links)) {
        $link = array_pop($links);
        $url = str_replace(array('/', '.'), array('\\/', '\\.'), $link);
        $page_info = add_page_info($link, false, $photo["url"]);
        if (trim($page_info) != "") {
            $removedlink = preg_replace("/\\[url\\=" . $url . "\\](.*?)\\[\\/url\\]/ism", '', $postarray['body']);
            if ($removedlink == "" or strstr($postarray['body'], $removedlink)) {
                $postarray['body'] = $removedlink;
            }
        }
    }
    $postarray['body'] .= $page_info;
    $postarray['created'] = datetime_convert('UTC', 'UTC', $post["created_at"]);
    $postarray['edited'] = datetime_convert('UTC', 'UTC', $post["created_at"]);
    $postarray['app'] = $post["source"]["name"];
    return $postarray;
}