]> git.mxchange.org Git - friendica-addons.git/blobdiff - appnet/appnet.php
Clean up the code
[friendica-addons.git] / appnet / appnet.php
index 3a6046b3646b91a741d7e5641569c15a3f3ea96e..0c53a49f470e07a79a458e6274effa54ff3a898d 100644 (file)
@@ -2,7 +2,7 @@
 
 /**
  * Name: App.net Connector
- * Description: app.net postings import and export
+ * Description: Bidirectional (posting and reading) connector for app.net.
  * Version: 0.2
  * Author: Michael Vogel <https://pirati.ca/profile/heluecht>
  */
@@ -15,6 +15,9 @@
  - https://alpha.app.net/opendev/post/34396399 - location data
 */
 
+require_once('include/enotify.php');
+require_once("include/socgraph.php");
+
 define('APPNET_DEFAULT_POLL_INTERVAL', 5); // given in minutes
 
 function appnet_install() {
@@ -25,6 +28,7 @@ function appnet_install() {
        register_hook('connector_settings',     'addon/appnet/appnet.php', 'appnet_settings');
        register_hook('connector_settings_post','addon/appnet/appnet.php', 'appnet_settings_post');
        register_hook('prepare_body',           'addon/appnet/appnet.php', 'appnet_prepare_body');
+       register_hook('check_item_notification','addon/appnet/appnet.php', 'appnet_check_item_notification');
 }
 
 
@@ -36,6 +40,7 @@ function appnet_uninstall() {
        unregister_hook('connector_settings',   'addon/appnet/appnet.php', 'appnet_settings');
        unregister_hook('connector_settings_post', 'addon/appnet/appnet.php', 'appnet_settings_post');
        unregister_hook('prepare_body',         'addon/appnet/appnet.php', 'appnet_prepare_body');
+       unregister_hook('check_item_notification','addon/appnet/appnet.php', 'appnet_check_item_notification');
 }
 
 function appnet_module() {}
@@ -64,6 +69,18 @@ function appnet_content(&$a) {
        return $o;
 }
 
+function appnet_check_item_notification($a, &$notification_data) {
+        $own_id = get_pconfig($notification_data["uid"], 'appnet', 'ownid');
+
+        $own_user = q("SELECT `url` FROM `contact` WHERE `uid` = %d AND `alias` = '%s' LIMIT 1",
+                        intval($notification_data["uid"]),
+                        dbesc("adn::".$own_id)
+                );
+
+        if ($own_user)
+                $notification_data["profiles"][] = $own_user[0]["url"];
+}
+
 function appnet_plugin_admin(&$a, &$o){
         $t = get_markup_template( "admin.tpl", "addon/appnet/" );
 
@@ -357,12 +374,12 @@ function appnet_create_entities($a, $b, $postdata) {
                $start = $pos + 1;
        }
 
-       if (isset($postdata["url"]) AND isset($postdata["title"])) {
+       if (isset($postdata["url"]) AND isset($postdata["title"]) AND ($postdata["type"] != "photo")) {
                $postdata["title"] = shortenmsg($postdata["title"], 90);
                $max = 256 - strlen($postdata["title"]);
                $text = shortenmsg($text, $max);
                $text .= "\n[".$postdata["title"]."](".$postdata["url"].")";
-       } elseif (isset($postdata["url"])) {
+       } elseif (isset($postdata["url"]) AND ($postdata["type"] != "photo")) {
                $postdata["url"] = short_link($postdata["url"]);
                $max = 240;
                $text = shortenmsg($text, $max);
@@ -524,7 +541,7 @@ function appnet_send(&$a,&$b) {
                                                "value" => $attached_data
                                                );
 
-               if (isset($post["url"]) AND !isset($post["title"])) {
+               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);
 
@@ -690,18 +707,24 @@ function appnet_fetchstream($a, $uid) {
        }
        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"])) {
+               if (($item != 0) AND ($postarray['contact-id'] != $me["id"]) AND !function_exists("check_item_notification")) {
                        $r = q("SELECT `thread`.`iid` AS `parent` FROM `thread`
                                INNER JOIN `item` ON `thread`.`iid` = `item`.`parent` AND `thread`.`uid` = `item`.`uid`
                                WHERE `item`.`id` = %d AND `thread`.`mention` LIMIT 1", dbesc($item));
@@ -716,8 +739,7 @@ function appnet_fetchstream($a, $uid) {
                                        'to_email'     => $user['email'],
                                        'uid'          => $user['uid'],
                                        'item'         => $postarray,
-                                       //'link'         => $a->get_baseurl() . '/display/' . $user['nickname'] . '/' . $item,
-                                       'link'         => $a->get_baseurl().'/display/'.get_item_guid($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'],
@@ -745,23 +767,51 @@ function appnet_fetchstream($a, $uid) {
        }
        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"]))
+               if (isset($postarray["id"])) {
                        $item = $postarray["id"];
-               elseif (isset($postarray["body"])) {
+                       $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);
-               } else
+
+                       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 ($postarray['contact-id'] != $me["id"])) {
+               if (($item != 0) AND !function_exists("check_item_notification")) {
                        require_once('include/enotify.php');
                        notification(array(
                                'type'         => NOTIFY_TAGSELF,
@@ -771,13 +821,13 @@ function appnet_fetchstream($a, $uid) {
                                'to_email'     => $user['email'],
                                'uid'          => $user['uid'],
                                'item'         => $postarray,
-                               //'link'         => $a->get_baseurl() . '/display/' . $user['nickname'] . '/' . $item,
-                               'link'         => $a->get_baseurl().'/display/'.get_item_guid($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'],
                                'verb'         => ACTIVITY_TAG,
-                               'otype'        => 'item'
+                               'otype'        => 'item',
+                               'parent'       => $parent_id,
                        ));
                }
        }
@@ -793,7 +843,7 @@ function appnet_fetchstream($a, $uid) {
 */
 }
 
-function appnet_createpost($a, $uid, $post, $me, $user, $ownid, $createuser, $threadcompletion = true) {
+function appnet_createpost($a, $uid, $post, $me, $user, $ownid, $createuser, $threadcompletion = true, $nodupcheck = false) {
        require_once('include/items.php');
 
        if ($post["machine_only"])
@@ -808,25 +858,33 @@ function appnet_createpost($a, $uid, $post, $me, $user, $ownid, $createuser, $th
        $postarray['wall'] = 0;
        $postarray['verb'] = ACTIVITY_POST;
        $postarray['network'] =  dbesc(NETWORK_APPNET);
-       $postarray['uri'] = "adn::".$post["id"];
+       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"];
+       }
 
-       $r = q("SELECT * FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
-               dbesc($postarray['uri']),
-               intval($uid)
-               );
+       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]);
+               if (count($r))
+                       return($r[0]);
 
-       $r = q("SELECT * FROM `item` WHERE `extid` = '%s' AND `uid` = %d LIMIT 1",
-               dbesc($postarray['uri']),
-               intval($uid)
-               );
+               $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 (count($r))
+                       return($r[0]);
+       }
 
-       $postarray['parent-uri'] = "adn::".$post["thread_id"];
        if (isset($post["reply_to"]) AND ($post["reply_to"] != "")) {
                $postarray['thr-parent'] = "adn::".$post["reply_to"];
 
@@ -863,6 +921,8 @@ function appnet_createpost($a, $uid, $post, $me, $user, $ownid, $createuser, $th
                                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);
                                }
                        //}
@@ -876,8 +936,6 @@ function appnet_createpost($a, $uid, $post, $me, $user, $ownid, $createuser, $th
                $postarray['object-type'] = ACTIVITY_OBJ_NOTE;
        }
 
-       $postarray['plink'] = $post["canonical_url"];
-
        if (($post["user"]["id"] != $ownid) OR ($postarray['thr-parent'] == $postarray['uri'])) {
                $postarray['owner-name'] = $post["user"]["name"];
                $postarray['owner-link'] = $post["user"]["canonical_url"];
@@ -906,6 +964,8 @@ function appnet_createpost($a, $uid, $post, $me, $user, $ownid, $createuser, $th
                $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"];
@@ -919,7 +979,7 @@ function appnet_createpost($a, $uid, $post, $me, $user, $ownid, $createuser, $th
                        $links[$url] = $link["url"];
                }
 
-       if (sizeof($content["annotations"]))
+       /* if (sizeof($content["annotations"]))
                foreach($content["annotations"] AS $annotation) {
                        if ($annotation[type] == "net.app.core.oembed") {
                                if (isset($annotation["value"]["embeddable_url"])) {
@@ -928,7 +988,6 @@ function appnet_createpost($a, $uid, $post, $me, $user, $ownid, $createuser, $th
                                                unset($links[$url]);
                                }
                        } elseif ($annotation[type] == "com.friendica.post") {
-                               // Nur zum Testen deaktiviert
                                //$links = array();
                                //if (isset($annotation["value"]["post-title"]))
                                //      $postarray['title'] = $annotation["value"]["post-title"];
@@ -949,7 +1008,7 @@ function appnet_createpost($a, $uid, $post, $me, $user, $ownid, $createuser, $th
                                        $postarray['author-avatar'] = $annotation["value"]["author-avatar"];
                        }
 
-               }
+               } */
 
        $page_info = "";
 
@@ -970,11 +1029,13 @@ function appnet_createpost($a, $uid, $post, $me, $user, $ownid, $createuser, $th
                $link = array_pop($links);
                $url = str_replace(array('/', '.'), array('\/', '\.'), $link);
 
-               $removedlink = preg_replace("/\[url\=".$url."\](.*?)\[\/url\]/ism", '', $postarray['body']);
-               if (($removedlink == "") OR strstr($postarray['body'], $removedlink))
-                       $postarray['body'] = $removedlink;
-
                $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;
@@ -1056,6 +1117,38 @@ function appnet_expand_annotations($a, $annotations) {
 }
 
 function appnet_fetchcontact($a, $uid, $contact, $me, $create_user) {
+
+       if (function_exists("update_gcontact"))
+               update_gcontact(array("url" => $contact["canonical_url"], "generation" => 2,
+                               "network" => NETWORK_APPNET, "photo" => $contact["avatar_image"]["url"],
+                               "name" => $contact["name"], "nick" => $contact["username"],
+                               "about" => $contact["description"]["text"], "hide" => true,
+                               "addr" => $contact["username"]."@app.net"));
+       else {
+               // Old Code
+               $r = q("SELECT id FROM unique_contacts WHERE url='%s' LIMIT 1",
+                               dbesc(normalise_link($contact["canonical_url"])));
+
+               if (count($r) == 0)
+                       q("INSERT INTO unique_contacts (url, name, nick, avatar) VALUES ('%s', '%s', '%s', '%s')",
+                               dbesc(normalise_link($contact["canonical_url"])),
+                               dbesc($contact["name"]),
+                               dbesc($contact["username"]),
+                               dbesc($contact["avatar_image"]["url"]));
+               else
+                       q("UPDATE unique_contacts SET name = '%s', nick = '%s', avatar = '%s' WHERE url = '%s'",
+                               dbesc($contact["name"]),
+                               dbesc($contact["username"]),
+                               dbesc($contact["avatar_image"]["url"]),
+                               dbesc(normalise_link($contact["canonical_url"])));
+
+               if (DB_UPDATE_VERSION >= "1177")
+                       q("UPDATE `unique_contacts` SET `location` = '%s', `about` = '%s' WHERE url = '%s'",
+                               dbesc(""),
+                               dbesc($contact["description"]["text"]),
+                               dbesc(normalise_link($contact["canonical_url"])));
+       }
+
        $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `alias` = '%s' LIMIT 1",
                intval($uid), dbesc("adn::".$contact["id"]));
 
@@ -1139,6 +1232,14 @@ function appnet_fetchcontact($a, $uid, $contact, $me, $create_user) {
                        intval($contact_id)
                );
 
+               if (DB_UPDATE_VERSION >= "1177")
+                       q("UPDATE `contact` SET `location` = '%s',
+                                               `about` = '%s'
+                                       WHERE `id` = %d",
+                               dbesc(""),
+                               dbesc($contact["description"]["text"]),
+                               intval($contact_id)
+                       );
        } else {
                // update profile photos once every two weeks as we have no notification of when they change.
 
@@ -1180,6 +1281,14 @@ function appnet_fetchcontact($a, $uid, $contact, $me, $create_user) {
                                dbesc($contact["username"]),
                                intval($r[0]['id'])
                        );
+                       if (DB_UPDATE_VERSION >= "1177")
+                               q("UPDATE `contact` SET `location` = '%s',
+                                                       `about` = '%s'
+                                               WHERE `id` = %d",
+                                       dbesc(""),
+                                       dbesc($contact["description"]["text"]),
+                                       intval($r[0]['id'])
+                               );
                }
        }
 
@@ -1239,9 +1348,23 @@ function appnet_cron($a,$b) {
        }
        logger('appnet_cron: cron_start');
 
+       $abandon_days = intval(get_config('system','account_abandon_days'));
+       if ($abandon_days < 1)
+               $abandon_days = 0;
+
+       $abandon_limit = date("Y-m-d H:i:s", time() - $abandon_days * 86400);
+
        $r = q("SELECT * FROM `pconfig` WHERE `cat` = 'appnet' AND `k` = 'import' AND `v` = '1' ORDER BY RAND()");
        if(count($r)) {
                foreach($r as $rr) {
+                       if ($abandon_days != 0) {
+                               $user = q("SELECT `login_date` FROM `user` WHERE uid=%d AND `login_date` >= '%s'", $rr['uid'], $abandon_limit);
+                               if (!count($user)) {
+                                       logger('abandoned account: timeline from user '.$rr['uid'].' will not be imported');
+                                       continue;
+                               }
+                       }
+
                        logger('appnet_cron: importing timeline from user '.$rr['uid']);
                        appnet_fetchstream($a, $rr["uid"]);
                }