Beispiel #1
0
function appnet_send(&$a, &$b)
{
    logger('appnet_send: invoked for post ' . $b['id'] . " " . $b['app']);
    if (!get_pconfig($b["uid"], 'appnet', 'import')) {
        if ($b['deleted'] || $b['private'] || $b['created'] !== $b['edited']) {
            return;
        }
    }
    if ($b['parent'] != $b['id']) {
        logger("appnet_send: parameter " . print_r($b, true), LOGGER_DATA);
        // Looking if its a reply to an app.net post
        if (substr($b["parent-uri"], 0, 5) != "adn::" and substr($b["extid"], 0, 5) != "adn::" and substr($b["thr-parent"], 0, 5) != "adn::") {
            logger("appnet_send: no app.net post " . $b["parent"]);
            return;
        }
        $r = q("SELECT * FROM item WHERE item.uri = '%s' AND item.uid = %d LIMIT 1", dbesc($b["thr-parent"]), intval($b["uid"]));
        if (!count($r)) {
            logger("appnet_send: no parent found " . $b["thr-parent"]);
            return;
        } else {
            $iscomment = true;
            $orig_post = $r[0];
        }
        $nicknameplain = preg_replace("=https?://alpha.app.net/(.*)=ism", "\$1", $orig_post["author-link"]);
        $nickname = "@[url=" . $orig_post["author-link"] . "]" . $nicknameplain . "[/url]";
        $nicknameplain = "@" . $nicknameplain;
        logger("appnet_send: comparing " . $nickname . " and " . $nicknameplain . " with " . $b["body"], LOGGER_DEBUG);
        if (strpos($b["body"], $nickname) === false and strpos($b["body"], $nicknameplain) === false) {
            $b["body"] = $nickname . " " . $b["body"];
        }
        logger("appnet_send: parent found " . print_r($orig_post, true), LOGGER_DATA);
    } else {
        $iscomment = false;
        if ($b['private'] or !strstr($b['postopts'], 'appnet')) {
            return;
        }
    }
    if ($b['verb'] == ACTIVITY_POST and $b['deleted']) {
        appnet_action($a, $b["uid"], substr($orig_post["uri"], 5), "delete");
    }
    if ($b['verb'] == ACTIVITY_LIKE) {
        logger("appnet_send: " . print_r($b, true), LOGGER_DEBUG);
        logger("appnet_send: parameter 2 " . substr($b["thr-parent"], 5), LOGGER_DEBUG);
        if ($b['deleted']) {
            appnet_action($a, $b["uid"], substr($b["thr-parent"], 5), "unlike");
        } else {
            appnet_action($a, $b["uid"], substr($b["thr-parent"], 5), "like");
        }
        return;
    }
    if ($b['deleted'] || $b['created'] !== $b['edited']) {
        return;
    }
    $token = get_pconfig($b['uid'], 'appnet', 'token');
    if ($token) {
        // If it's a repeated message from app.net then do a native repost and exit
        if (appnet_is_repost($a, $b['uid'], $b['body'])) {
            return;
        }
        require_once 'addon/appnet/AppDotNet.php';
        $clientId = get_pconfig($b["uid"], 'appnet', 'clientid');
        $clientSecret = get_pconfig($b["uid"], 'appnet', 'clientsecret');
        $app = new AppDotNet($clientId, $clientSecret);
        $app->setAccessToken($token);
        $data = array();
        require_once "include/plaintext.php";
        require_once "include/network.php";
        $post = plaintext($a, $b, 256, false, 6);
        logger("appnet_send: converted message " . $b["id"] . " result: " . print_r($post, true), LOGGER_DEBUG);
        if (isset($post["image"])) {
            $img_str = fetch_url($post['image'], true, $redirects, 10);
            $tempfile = tempnam(get_temppath(), "cache");
            file_put_contents($tempfile, $img_str);
            try {
                $photoFile = $app->createFile($tempfile, array(type => "com.github.jdolitsky.appdotnetphp.photo"));
                $data["annotations"][] = array("type" => "net.app.core.oembed", "value" => array("+net.app.core.file" => array("file_id" => $photoFile["id"], "file_token" => $photoFile["file_token"], "format" => "oembed")));
            } catch (AppDotNetException $e) {
                logger("appnet_send: Error creating file " . appnet_error($e->getMessage()));
            }
            unlink($tempfile);
        }
        // Adding a link to the original post, if it is a root post
        if ($b['parent'] == $b['id']) {
            $data["annotations"][] = array("type" => "net.app.core.crosspost", "value" => array("canonical_url" => $b["plink"]));
        }
        // Adding the original post
        $attached_data = get_attached_data($b["body"]);
        $attached_data["post-uri"] = $b["uri"];
        $attached_data["post-title"] = $b["title"];
        $attached_data["post-body"] = substr($b["body"], 0, 4000);
        // To-Do: Better shortening
        $attached_data["post-tag"] = $b["tag"];
        $attached_data["author-name"] = $b["author-name"];
        $attached_data["author-link"] = $b["author-link"];
        $attached_data["author-avatar"] = $b["author-avatar"];
        $data["annotations"][] = array("type" => "com.friendica.post", "value" => $attached_data);
        if (isset($post["url"]) and !isset($post["title"]) and $post["type"] != "photo") {
            $display_url = str_replace(array("http://www.", "https://www."), array("", ""), $post["url"]);
            $display_url = str_replace(array("http://", "https://"), array("", ""), $display_url);
            if (strlen($display_url) > 26) {
                $display_url = substr($display_url, 0, 25) . "…";
            }
            $post["title"] = $display_url;
        }
        $text = appnet_create_entities($a, $b, $post);
        $data["entities"]["parse_markdown_links"] = true;
        if ($iscomment) {
            $data["reply_to"] = substr($orig_post["uri"], 5);
        }
        try {
            logger("appnet_send: sending message " . $b["id"] . " " . $text . " " . print_r($data, true), LOGGER_DEBUG);
            $ret = $app->createPost($text, $data);
            logger("appnet_send: send message " . $b["id"] . " result: " . print_r($ret, true), LOGGER_DEBUG);
            if ($iscomment) {
                logger('appnet_send: Update extid ' . $ret["id"] . " for post id " . $b['id']);
                q("UPDATE `item` SET `extid` = '%s' WHERE `id` = %d", dbesc("adn::" . $ret["id"]), intval($b['id']));
            }
        } catch (AppDotNetException $e) {
            logger("appnet_send: Error sending message " . $b["id"] . " " . appnet_error($e->getMessage()));
        }
    }
}