Example #1
0
function appnet_fetchstream($a, $uid)
{
    require_once "addon/appnet/AppDotNet.php";
    require_once 'include/items.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);
    $r = q("SELECT * FROM `contact` WHERE `self` = 1 AND `uid` = %d LIMIT 1", intval($uid));
    if (count($r)) {
        $me = $r[0];
    } else {
        logger("appnet_fetchstream: Own contact not found for user " . $uid, LOGGER_DEBUG);
        return;
    }
    $user = q("SELECT * FROM `user` WHERE `uid` = %d AND `account_expired` = 0 LIMIT 1", intval($uid));
    if (count($user)) {
        $user = $user[0];
    } else {
        logger("appnet_fetchstream: Own user not found for user " . $uid, LOGGER_DEBUG);
        return;
    }
    $ownid = get_pconfig($uid, 'appnet', 'ownid');
    // Fetch stream
    $param = array("count" => 200, "include_deleted" => false, "include_directed_posts" => true, "include_html" => false, "include_post_annotations" => true);
    $lastid = get_pconfig($uid, 'appnet', 'laststreamid');
    if ($lastid != "") {
        $param["since_id"] = $lastid;
    }
    try {
        $stream = $app->getUserStream($param);
    } catch (AppDotNetException $e) {
        logger("appnet_fetchstream: Error fetching stream for user " . $uid . " " . appnet_error($e->getMessage()));
        return;
    }
    if (!is_array($stream)) {
        $stream = array();
    }
    $stream = array_reverse($stream);
    foreach ($stream as $post) {
        $postarray = appnet_createpost($a, $uid, $post, $me, $user, $ownid, true);
        $item = item_store($postarray);
        $postarray["id"] = $item;
        logger('appnet_fetchstream: User ' . $uid . ' posted stream item ' . $item);
        $lastid = $post["id"];
        if ($item != 0 and $postarray['contact-id'] != $me["id"] and !function_exists("check_item_notification")) {
            $r = q("SELECT `thread`.`iid` AS `parent` FROM `thread`\n\t\t\t\tINNER JOIN `item` ON `thread`.`iid` = `item`.`parent` AND `thread`.`uid` = `item`.`uid`\n\t\t\t\tWHERE `item`.`id` = %d AND `thread`.`mention` LIMIT 1", dbesc($item));
            if (count($r)) {
                require_once 'include/enotify.php';
                notification(array('type' => NOTIFY_COMMENT, 'notify_flags' => $user['notify-flags'], 'language' => $user['language'], 'to_name' => $user['username'], 'to_email' => $user['email'], 'uid' => $user['uid'], 'item' => $postarray, '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'], 'verb' => ACTIVITY_POST, 'otype' => 'item', 'parent' => $r[0]["parent"]));
            }
        }
    }
    set_pconfig($uid, 'appnet', 'laststreamid', $lastid);
    // Fetch mentions
    $param = array("count" => 200, "include_deleted" => false, "include_directed_posts" => true, "include_html" => false, "include_post_annotations" => true);
    $lastid = get_pconfig($uid, 'appnet', 'lastmentionid');
    if ($lastid != "") {
        $param["since_id"] = $lastid;
    }
    try {
        $mentions = $app->getUserMentions("me", $param);
    } catch (AppDotNetException $e) {
        logger("appnet_fetchstream: Error fetching mentions for user " . $uid . " " . appnet_error($e->getMessage()));
        return;
    }
    if (!is_array($mentions)) {
        $mentions = array();
    }
    $mentions = array_reverse($mentions);
    foreach ($mentions as $post) {
        $postarray = appnet_createpost($a, $uid, $post, $me, $user, $ownid, false);
        if (isset($postarray["id"])) {
            $item = $postarray["id"];
            $parent_id = $postarray['parent'];
        } elseif (isset($postarray["body"])) {
            $item = item_store($postarray);
            $postarray["id"] = $item;
            $parent_id = 0;
            logger('appnet_fetchstream: User ' . $uid . ' posted mention item ' . $item);
            if ($item and function_exists("check_item_notification")) {
                check_item_notification($item, $uid, NOTIFY_TAGSELF);
            }
        } else {
            $item = 0;
            $parent_id = 0;
        }
        // Fetch the parent and id
        if ($parent_id == 0 and $postarray['uri'] != "") {
            $r = q("SELECT `id`, `parent` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1", dbesc($postarray['uri']), intval($uid));
            if (count($r)) {
                $item = $r[0]['id'];
                $parent_id = $r[0]['parent'];
            }
        }
        $lastid = $post["id"];
        //if (($item != 0) AND ($postarray['contact-id'] != $me["id"])) {
        if ($item != 0 and !function_exists("check_item_notification")) {
            require_once 'include/enotify.php';
            notification(array('type' => NOTIFY_TAGSELF, 'notify_flags' => $user['notify-flags'], 'language' => $user['language'], 'to_name' => $user['username'], 'to_email' => $user['email'], 'uid' => $user['uid'], 'item' => $postarray, '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'], 'verb' => ACTIVITY_TAG, 'otype' => 'item', 'parent' => $parent_id));
        }
    }
    set_pconfig($uid, 'appnet', 'lastmentionid', $lastid);
    /* To-Do
    	$param = array("interaction_actions" => "star");
    	$interactions = $app->getMyInteractions($param);
    	foreach ($interactions AS $interaction)
    		appnet_dolike($a, $uid, $interaction);
    */
}