]> git.mxchange.org Git - friendica-addons.git/blobdiff - appnet/appnet.php
"remote self" should work now with the addons
[friendica-addons.git] / appnet / appnet.php
index 10deb96d06eb11baeb245fabbe81df3f6d1e7dbb..151a81ee495735c2ccfc6008ffc4f18ab191ce12 100644 (file)
@@ -5,6 +5,7 @@
  * Description: Bidirectional (posting and reading) connector for app.net.
  * Version: 0.2
  * Author: Michael Vogel <https://pirati.ca/profile/heluecht>
+ * Status: Unsupported
  */
 
 /*
@@ -15,6 +16,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 +29,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 +41,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 +70,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/" );
 
@@ -89,7 +107,7 @@ function appnet_connect(&$a) {
        $clientId     = get_config('appnet','clientid');
        $clientSecret = get_config('appnet','clientsecret');
 
-       if (($clientId == "") OR ($clientSecret == "")) {
+       if (($clientId == "") || ($clientSecret == "")) {
                $clientId     = get_pconfig(local_user(),'appnet','clientid');
                $clientSecret = get_pconfig(local_user(),'appnet','clientsecret');
        }
@@ -136,7 +154,7 @@ function appnet_settings(&$a,&$s) {
        $app_clientId     = get_config('appnet','clientid');
        $app_clientSecret = get_config('appnet','clientsecret');
 
-       if (($app_clientId == "") OR ($app_clientSecret == "")) {
+       if (($app_clientId == "") || ($app_clientSecret == "")) {
                $app_clientId     = get_pconfig(local_user(),'appnet','clientid');
                $app_clientSecret = get_pconfig(local_user(),'appnet','clientsecret');
        }
@@ -195,7 +213,7 @@ function appnet_settings(&$a,&$s) {
                        $s .= t("<p>Error fetching user profile. Please clear the configuration and try again.</p>");
                }
 
-       } elseif (($app_clientId == '') OR ($app_clientSecret == '')) {
+       } elseif (($app_clientId == '') || ($app_clientSecret == '')) {
                $s .= t("<p>You have two ways to connect to App.net.</p>");
                $s .= "<hr />";
                $s .= t('<p>First way: Register an application at <a href="https://account.app.net/developer/apps/">https://account.app.net/developer/apps/</a> and enter Client ID and Client Secret. ');
@@ -227,7 +245,7 @@ function appnet_settings(&$a,&$s) {
                $s .= '<a href="'.$url.'">'.t("Sign in using App.net").'</a>';
        }
 
-       if (($app_clientId != '') OR ($app_clientSecret != '') OR ($token !='')) {
+       if (($app_clientId != '') || ($app_clientSecret != '') || ($token !='')) {
                $s .= '<div id="appnet-disconnect-wrapper">';
                $s .= '<label id="appnet-disconnect-label" for="appnet-disconnect">'. t('Clear OAuth configuration') .'</label>';
 
@@ -260,7 +278,7 @@ function appnet_settings_post(&$a,&$b) {
                if (isset($_POST["clientid"]))
                        set_pconfig(local_user(),'appnet','clientid', $_POST['clientid']);
 
-               if (isset($_POST["token"]) AND ($_POST["token"] != ""))
+               if (isset($_POST["token"]) && ($_POST["token"] != ""))
                        set_pconfig(local_user(),'appnet','token', $_POST['token']);
 
                set_pconfig(local_user(), 'appnet', 'post', intval($_POST['appnet']));
@@ -278,7 +296,7 @@ function appnet_post_local(&$a,&$b) {
                $appnet_enable = (($appnet_post && x($_REQUEST,'appnet_enable')) ? intval($_REQUEST['appnet_enable']) : 0);
 
                // if API is used, default to the chosen settings
-               if($_REQUEST['api_source'] && intval(get_pconfig(local_user(),'appnet','post_by_default')))
+               if($b['api_source'] && intval(get_pconfig(local_user(),'appnet','post_by_default')))
                        $appnet_enable = 1;
 
                if(! $appnet_enable)
@@ -357,12 +375,12 @@ function appnet_create_entities($a, $b, $postdata) {
                $start = $pos + 1;
        }
 
-       if (isset($postdata["url"]) AND isset($postdata["title"]) AND ($postdata["type"] != "photo")) {
+       if (isset($postdata["url"]) && isset($postdata["title"]) && ($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"]) AND ($postdata["type"] != "photo")) {
+       } elseif (isset($postdata["url"]) && ($postdata["type"] != "photo")) {
                $postdata["url"] = short_link($postdata["url"]);
                $max = 240;
                $text = shortenmsg($text, $max);
@@ -403,7 +421,7 @@ function appnet_send(&$a,&$b) {
                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::")) {
+               if ((substr($b["parent-uri"], 0, 5) != "adn::") && (substr($b["extid"], 0, 5) != "adn::") && (substr($b["thr-parent"], 0, 5) != "adn::")) {
                        logger("appnet_send: no app.net post ".$b["parent"]);
                        return;
                }
@@ -425,18 +443,18 @@ function appnet_send(&$a,&$b) {
                $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))
+               if ((strpos($b["body"], $nickname) === false) && (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'))
+               if($b['private'] || !strstr($b['postopts'],'appnet'))
                        return;
        }
 
-       if (($b['verb'] == ACTIVITY_POST) AND $b['deleted'])
+       if (($b['verb'] == ACTIVITY_POST) && $b['deleted'])
                appnet_action($a, $b["uid"], substr($orig_post["uri"], 5), "delete");
 
        if($b['verb'] == ACTIVITY_LIKE) {
@@ -524,7 +542,7 @@ function appnet_send(&$a,&$b) {
                                                "value" => $attached_data
                                                );
 
-               if (isset($post["url"]) AND !isset($post["title"]) AND ($post["type"] != "photo")) {
+               if (isset($post["url"]) && !isset($post["title"]) && ($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);
 
@@ -707,7 +725,7 @@ function appnet_fetchstream($a, $uid) {
 
                $lastid = $post["id"];
 
-               if (($item != 0) AND ($postarray['contact-id'] != $me["id"])) {
+               if (($item != 0) && ($postarray['contact-id'] != $me["id"]) && !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));
@@ -769,13 +787,17 @@ function appnet_fetchstream($a, $uid) {
 
                        $parent_id = 0;
                        logger('appnet_fetchstream: User '.$uid.' posted mention item '.$item);
+
+                       if ($item && 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'] != "")) {
+               if (($parent_id == 0) && ($postarray['uri'] != "")) {
                        $r = q("SELECT `id`, `parent` FROM `item` WHERE `uri` = '%s' AND `uid` = %d LIMIT 1",
                                dbesc($postarray['uri']),
                                intval($uid)
@@ -789,8 +811,8 @@ function appnet_fetchstream($a, $uid) {
 
                $lastid = $post["id"];
 
-               //if (($item != 0) AND ($postarray['contact-id'] != $me["id"])) {
-               if ($item != 0) {
+               //if (($item != 0) && ($postarray['contact-id'] != $me["id"])) {
+               if (($item != 0) && !function_exists("check_item_notification")) {
                        require_once('include/enotify.php');
                        notification(array(
                                'type'         => NOTIFY_TAGSELF,
@@ -864,7 +886,7 @@ function appnet_createpost($a, $uid, $post, $me, $user, $ownid, $createuser, $th
                        return($r[0]);
        }
 
-       if (isset($post["reply_to"]) AND ($post["reply_to"] != "")) {
+       if (isset($post["reply_to"]) && ($post["reply_to"] != "")) {
                $postarray['thr-parent'] = "adn::".$post["reply_to"];
 
                // Complete the thread (if the parent doesn't exists)
@@ -915,7 +937,7 @@ function appnet_createpost($a, $uid, $post, $me, $user, $ownid, $createuser, $th
                $postarray['object-type'] = ACTIVITY_OBJ_NOTE;
        }
 
-       if (($post["user"]["id"] != $ownid) OR ($postarray['thr-parent'] == $postarray['uri'])) {
+       if (($post["user"]["id"] != $ownid) || ($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"];
@@ -993,7 +1015,7 @@ function appnet_createpost($a, $uid, $post, $me, $user, $ownid, $createuser, $th
 
        if (is_array($content["annotations"])) {
                $photo = appnet_expand_annotations($a, $content["annotations"]);
-               if (($photo["large"] != "") AND ($photo["url"] != ""))
+               if (($photo["large"] != "") && ($photo["url"] != ""))
                        $page_info = "\n[url=".$photo["url"]."][img]".$photo["large"]."[/img][/url]";
                elseif ($photo["url"] != "")
                        $page_info = "\n[img]".$photo["url"]."[/img]";
@@ -1012,7 +1034,7 @@ function appnet_createpost($a, $uid, $post, $me, $user, $ownid, $createuser, $th
 
                if (trim($page_info) != "") {
                        $removedlink = preg_replace("/\[url\=".$url."\](.*?)\[\/url\]/ism", '', $postarray['body']);
-                       if (($removedlink == "") OR strstr($postarray['body'], $removedlink))
+                       if (($removedlink == "") || strstr($postarray['body'], $removedlink))
                                $postarray['body'] = $removedlink;
                }
        }
@@ -1053,7 +1075,7 @@ function appnet_expand_entities($a, $body, $entities) {
 
        foreach ($entities["links"] AS $links) {
                $url = "[url=".$links["url"]."]".$links["text"]."[/url]";
-               if (isset($links["amended_len"]) AND ($links["amended_len"] > $links["len"]))
+               if (isset($links["amended_len"]) && ($links["amended_len"] > $links["len"]))
                        $replace[$links["pos"]] = array("pos"=> $links["pos"], "len"=> $links["amended_len"], "replace"=> $url);
                else
                        $replace[$links["pos"]] = array("pos"=> $links["pos"], "len"=> $links["len"], "replace"=> $url);
@@ -1078,7 +1100,7 @@ function appnet_expand_entities($a, $body, $entities) {
 function appnet_expand_annotations($a, $annotations) {
        $photo = array("url" => "", "large" => "");
        foreach ($annotations AS $annotation) {
-               if (($annotation[type] == "net.app.core.oembed") AND
+               if (($annotation[type] == "net.app.core.oembed") &&
                        ($annotation["value"]["type"] == "photo")) {
                        if ($annotation["value"]["url"] != "")
                                $photo["url"] = $annotation["value"]["url"];
@@ -1086,7 +1108,7 @@ function appnet_expand_annotations($a, $annotations) {
                        if ($annotation["value"]["thumbnail_large_url"] != "")
                                $photo["large"] = $annotation["value"]["thumbnail_large_url"];
 
-                       //if (($annotation["value"]["thumbnail_large_url"] != "") AND ($annotation["value"]["url"] != ""))
+                       //if (($annotation["value"]["thumbnail_large_url"] != "") && ($annotation["value"]["url"] != ""))
                        //      $embedded = "\n[url=".$annotation["value"]["url"]."][img]".$annotation["value"]["thumbnail_large_url"]."[/img][/url]";
                        //elseif ($annotation["value"]["url"] != "")
                        //      $embedded = "\n[img]".$annotation["value"]["url"]."[/img]";
@@ -1097,39 +1119,22 @@ function appnet_expand_annotations($a, $annotations) {
 
 function appnet_fetchcontact($a, $uid, $contact, $me, $create_user) {
 
-       $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"])));
-
+       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"));
 
        $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `alias` = '%s' LIMIT 1",
                intval($uid), dbesc("adn::".$contact["id"]));
 
-       if(!count($r) AND !$create_user)
+       if(!count($r) && !$create_user)
                return($me["id"]);
 
        if ($contact["canonical_url"] == "")
                return($me["id"]);
 
-       if (count($r) AND ($r[0]["readonly"] OR $r[0]["blocked"])) {
+       if (count($r) && ($r[0]["readonly"] || $r[0]["blocked"])) {
                logger("appnet_fetchcontact: Contact '".$r[0]["nick"]."' is blocked or readonly.", LOGGER_DEBUG);
                return(-1);
        }
@@ -1145,8 +1150,8 @@ function appnet_fetchcontact($a, $uid, $contact, $me, $create_user) {
                // create contact record
                q("INSERT INTO `contact` (`uid`, `created`, `url`, `nurl`, `addr`, `alias`, `notify`, `poll`,
                                        `name`, `nick`, `photo`, `network`, `rel`, `priority`,
-                                       `writable`, `blocked`, `readonly`, `pending` )
-                                       VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, %d, 0, 0, 0 ) ",
+                                       `about`, `writable`, `blocked`, `readonly`, `pending` )
+                                       VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, '%s', %d, 0, 0, 0 ) ",
                        intval($uid),
                        dbesc(datetime_convert()),
                        dbesc($contact["canonical_url"]),
@@ -1161,6 +1166,7 @@ function appnet_fetchcontact($a, $uid, $contact, $me, $create_user) {
                        dbesc(NETWORK_APPNET),
                        intval(CONTACT_IS_FRIEND),
                        intval(1),
+                       dbesc($contact["description"]["text"]),
                        intval(1)
                );
 
@@ -1202,15 +1208,6 @@ function appnet_fetchcontact($a, $uid, $contact, $me, $create_user) {
                        dbesc(datetime_convert()),
                        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.
 
@@ -1237,7 +1234,8 @@ function appnet_fetchcontact($a, $uid, $contact, $me, $create_user) {
                                                `nurl` = '%s',
                                                `addr` = '%s',
                                                `name` = '%s',
-                                               `nick` = '%s'
+                                               `nick` = '%s',
+                                               `about` = '%s'
                                        WHERE `id` = %d",
                                dbesc($photos[0]),
                                dbesc($photos[1]),
@@ -1250,16 +1248,9 @@ function appnet_fetchcontact($a, $uid, $contact, $me, $create_user) {
                                dbesc($contact["username"]."@app.net"),
                                dbesc($contact["name"]),
                                dbesc($contact["username"]),
+                               dbesc($contact["description"]["text"]),
                                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'])
-                               );
                }
        }
 
@@ -1287,7 +1278,7 @@ function appnet_prepare_body(&$a,&$b) {
                        $nickname = "@[url=".$orig_post["author-link"]."]".$nicknameplain."[/url]";
                        $nicknameplain = "@".$nicknameplain;
 
-                       if ((strpos($item["body"], $nickname) === false) AND (strpos($item["body"], $nicknameplain) === false))
+                       if ((strpos($item["body"], $nickname) === false) && (strpos($item["body"], $nicknameplain) === false))
                                $item["body"] = $nickname." ".$item["body"];
                 }