]> git.mxchange.org Git - friendica.git/commitdiff
New function for generating item URI
authorMichael <heluecht@pirati.ca>
Sat, 16 Jun 2018 06:44:19 +0000 (06:44 +0000)
committerMichael <heluecht@pirati.ca>
Sat, 16 Jun 2018 06:44:19 +0000 (06:44 +0000)
include/api.php
include/text.php
mod/item.php
mod/photos.php
mod/poke.php
mod/subthread.php
mod/tagger.php
src/Model/Event.php
src/Model/Item.php
src/Protocol/Diaspora.php

index 00c2173c38b1b8f4c4297047928e1b8396447f42..af71e2f1cf2693eda373c45167c385106f2fdb9c 100644 (file)
@@ -4599,7 +4599,7 @@ function save_media_to_database($mediatype, $media, $type, $album, $allow_cid, $
 function post_photo_item($hash, $allow_cid, $deny_cid, $allow_gid, $deny_gid, $filetype, $visibility = false)
 {
        // get data about the api authenticated user
-       $uri = item_new_uri(get_app()->get_hostname(), intval(api_user()));
+       $uri = Item::newURI(intval(api_user()));
        $owner_record = q("SELECT * FROM `contact` WHERE `uid`= %d AND `self` LIMIT 1", intval(api_user()));
 
        $arr = [];
index acc6bec4fbf2d75b4810a840f2ab869789bafe28..612100cb6715e6765059a46ec708d2418e412693 100644 (file)
@@ -462,33 +462,6 @@ function perms2str($p) {
        return $ret;
 }
 
-
-/**
- * generate a guaranteed unique (for this domain) item ID for ATOM
- * safe from birthday paradox
- *
- * @param string $hostname
- * @param int $uid
- * @return string
- */
-function item_new_uri($hostname, $uid, $guid = "") {
-
-       do {
-               if ($guid == "") {
-                       $hash = get_guid(32);
-               } else {
-                       $hash = $guid;
-                       $guid = "";
-               }
-
-               $uri = "urn:X-dfrn:" . $hostname . ':' . $uid . ':' . $hash;
-
-               $dups = dba::exists('item', ['uri' => $uri]);
-       } while ($dups == true);
-
-       return $uri;
-}
-
 /**
  * @deprecated
  * wrapper to load a view template, checking for alternate
index 823350874b0f422c28aff708eb39917b31565790..f164fb0ae2c6f4347225dd08ab112cdcd66ee1b4 100644 (file)
@@ -576,7 +576,7 @@ function item_post(App $a) {
 
        $notify_type = ($parent ? 'comment-new' : 'wall-new');
 
-       $uri = ($message_id ? $message_id : item_new_uri($a->get_hostname(), $profile_uid, $guid));
+       $uri = ($message_id ? $message_id : Item::newURI($profile_uid, $guid));
 
        // Fallback so that we alway have a parent uri
        if (!$thr_parent_uri || !$parent) {
index 9bc870ca13fe34265dac0fbf5d08071c1d7f33af..f6540c2608b57c407a4f2320979e8aeeaad0ef14 100644 (file)
@@ -454,7 +454,7 @@ function photos_post(App $a)
                if (!$item_id) {
                        // Create item container
                        $title = '';
-                       $uri = item_new_uri($a->get_hostname(),$page_owner_uid);
+                       $uri = Item::newURI($page_owner_uid);
 
                        $arr = [];
                        $arr['guid']          = get_guid(32);
@@ -624,7 +624,7 @@ function photos_post(App $a)
 
                        if (count($taginfo)) {
                                foreach ($taginfo as $tagged) {
-                                       $uri = item_new_uri($a->get_hostname(), $page_owner_uid);
+                                       $uri = Item::newURI($page_owner_uid);
 
                                        $arr = [];
                                        $arr['guid']          = get_guid(32);
@@ -848,7 +848,7 @@ function photos_post(App $a)
                $smallest = 2;
        }
 
-       $uri = item_new_uri($a->get_hostname(), $page_owner_uid);
+       $uri = Item::newURI($page_owner_uid);
 
        // Create item container
        $lat = $lon = null;
index 73e8e1740d78b2014277b8869c7246526a8f5a0c..470f1eb2d6bbe00a8afdcd892cbd00ad4812b374 100644 (file)
@@ -92,7 +92,7 @@ function poke_init(App $a) {
 
        $poster = $a->contact;
 
-       $uri = item_new_uri($a->get_hostname(), $uid);
+       $uri = Item::newURI($uid);
 
        $arr = [];
 
index db8b9ba4feb3c0169e084b18e75f0c67b896cb48..e2c11d0a3fe3f989384add6b0b304491d207038b 100644 (file)
@@ -92,7 +92,7 @@ function subthread_content(App $a) {
                return;
        }
 
-       $uri = item_new_uri($a->get_hostname(),$owner_uid);
+       $uri = Item::newURI($owner_uid);
 
        $post_type = (($item['resource-id']) ? L10n::t('photo') : L10n::t('status'));
        $objtype = (($item['resource-id']) ? ACTIVITY_OBJ_IMAGE : ACTIVITY_OBJ_NOTE );
index fa8dc35301f017898a15808171d8bc3c17c5b416..e72061b592435087deb1f16c4839ba3eb1e14d88 100644 (file)
@@ -65,7 +65,7 @@ function tagger_content(App $a) {
                return;
        }
 
-       $uri = item_new_uri($a->get_hostname(),$owner_uid);
+       $uri = Item::newURI($owner_uid);
        $xterm = xmlify($term);
        $post_type = (($item['resource-id']) ? L10n::t('photo') : L10n::t('status'));
        $targettype = (($item['resource-id']) ? ACTIVITY_OBJ_IMAGE : ACTIVITY_OBJ_NOTE );
index 1c848dcad74cec58ad790d726bd2be68e5718ef2..9b258960e6264ceded8d1bbb616678169ef98e81 100644 (file)
@@ -234,7 +234,7 @@ class Event extends BaseObject
                $event['id']        = intval(defaults($arr, 'id'       , 0));
                $event['uid']       = intval(defaults($arr, 'uid'      , 0));
                $event['cid']       = intval(defaults($arr, 'cid'      , 0));
-               $event['uri']       =        defaults($arr, 'uri'      , item_new_uri($a->get_hostname(), $event['uid']));
+               $event['uri']       =        defaults($arr, 'uri'      , Item::newURI($event['uid']));
                $event['type']      =        defaults($arr, 'type'     , 'event');
                $event['summary']   =        defaults($arr, 'summary'  , '');
                $event['desc']      =        defaults($arr, 'desc'     , '');
index 5fe096c8218647460bd46a174302fbc7b40bef66..a30ed373944b587fe5c27c722f3a297b016e47d0 100644 (file)
@@ -675,7 +675,7 @@ class Item extends BaseObject
                }
 
                $item['guid'] = self::guid($item, $notify);
-               $item['uri'] = notags(trim(defaults($item, 'uri', item_new_uri($a->get_hostname(), $item['uid'], $item['guid']))));
+               $item['uri'] = notags(trim(defaults($item, 'uri', self::newURI($item['uid'], $item['guid']))));
 
                // Store conversation data
                $item = Conversation::insert($item);
@@ -853,8 +853,8 @@ class Item extends BaseObject
                }
 
                //unset($item['author-link']);
-               //unset($item['author-name']);
-               //unset($item['author-avatar']);
+               unset($item['author-name']);
+               unset($item['author-avatar']);
 
                //unset($item['owner-link']);
                unset($item['owner-name']);
@@ -1528,6 +1528,29 @@ class Item extends BaseObject
                return $guid_prefix.$host_hash;
        }
 
+       /**
+        * generate an unique URI
+        *
+        * @param integer $uid User id
+        * @param string $guid An existing GUID (Otherwise it will be generated)
+        *
+        * @return string
+        */
+       public static function newURI($uid, $guid = "")
+       {
+               if ($guid == "") {
+                       $guid = get_guid(32);
+               }
+
+               $hostname = self::getApp()->get_hostname();
+
+               $user = dba::selectFirst('user', ['nickname'], ['uid' => $uid]);
+
+               $uri = "urn:X-dfrn:" . $hostname . ':' . $user['nickname'] . ':' . $guid;
+
+               return $uri;
+       }
+
        /**
         * @brief Set "success_update" and "last-item" to the date of the last time we heard from this contact
         *
@@ -1844,7 +1867,7 @@ class Item extends BaseObject
                        if ($contact['network'] != NETWORK_FEED) {
                                $datarray["guid"] = get_guid(32);
                                unset($datarray["plink"]);
-                               $datarray["uri"] = item_new_uri($a->get_hostname(), $contact['uid'], $datarray["guid"]);
+                               $datarray["uri"] = self::newURI($contact['uid'], $datarray["guid"]);
                                $datarray["parent-uri"] = $datarray["uri"];
                                $datarray["thr-parent"] = $datarray["uri"];
                                $datarray["extid"] = NETWORK_DFRN;
@@ -2316,7 +2339,7 @@ EOT;
 
                $new_item = [
                        'guid'          => get_guid(32),
-                       'uri'           => item_new_uri(self::getApp()->get_hostname(), $item['uid']),
+                       'uri'           => self::newURI($item['uid']),
                        'uid'           => $item['uid'],
                        'contact-id'    => $item_contact_id,
                        'type'          => 'activity',
index 2bb846134089e2d2ccaf8f01c0f3c4026c58680e..20d1d74fea12ad99133019a3b72f537815e24181 100644 (file)
@@ -1602,11 +1602,21 @@ class Diaspora
         */
        private static function getUriFromGuid($author, $guid, $onlyfound = false)
        {
-               $r = q("SELECT `uri` FROM `item` WHERE `guid` = '%s' LIMIT 1", dbesc($guid));
-               if (DBM::is_result($r)) {
-                       return $r[0]["uri"];
+               $item = dba::selectFirst('item', ['uri'], ['guid' => $guid]);
+               if (DBM::is_result($item)) {
+                       return $item["uri"];
                } elseif (!$onlyfound) {
-                       return $author.":".$guid;
+                       $contact = Contact::getDetailsByAddr($author, 0);
+                       if (!empty($contact['network'])) {
+                               $prefix = 'urn:X-' . $contact['network'] . ':';
+                       } else {
+                               // This fallback should happen most unlikely
+                               $prefix = 'urn:X-dspr:';
+                       }
+
+                       $author_parts = explode('@', $author);
+
+                       return $prefix . $author_parts[1] . ':' . $author_parts[0] . ':'. $guid;
                }
 
                return "";