]> git.mxchange.org Git - friendica.git/blobdiff - include/Contact.php
Issue 2957: The avatar problem with mastodon should now finally be solved
[friendica.git] / include / Contact.php
index 553bf938c23c979211d2edf6eeed60ccc4a33704..fc9f660f1377e81aba7ae63f274f9dff1a418302 100644 (file)
@@ -1,7 +1,5 @@
 <?php
 
-require_once('include/Probe.php');
-
 // Included here for completeness, but this is a very dangerous operation.
 // It is the caller's responsibility to confirm the requestor's intent and
 // authorisation to do this.
@@ -129,7 +127,7 @@ function mark_for_death($contact) {
        if($contact['archive'])
                return;
 
-       if($contact['term-date'] == '0000-00-00 00:00:00') {
+       if ($contact['term-date'] <= NULL_DATE) {
                q("UPDATE `contact` SET `term-date` = '%s' WHERE `id` = %d",
                                dbesc(datetime_convert()),
                                intval($contact['id'])
@@ -187,13 +185,13 @@ function unmark_for_death($contact) {
 
        // It's a miracle. Our dead contact has inexplicably come back to life.
        q("UPDATE `contact` SET `term-date` = '%s' WHERE `id` = %d",
-               dbesc('0000-00-00 00:00:00'),
+               dbesc(NULL_DATE),
                intval($contact['id'])
        );
 
        if ($contact['url'] != '') {
                q("UPDATE `contact` SET `term-date` = '%s' WHERE `nurl` = '%s'",
-                       dbesc('0000-00-00 00:00:00'),
+                       dbesc(NULL_DATE),
                        dbesc(normalise_link($contact['url']))
                );
        }
@@ -355,6 +353,7 @@ function get_contact_details_by_addr($addr, $uid = -1) {
                                dbesc($addr));
 
        if (!dbm::is_result($r)) {
+               require_once('include/Probe.php');
                $data = Probe::uri($addr);
 
                $profile = get_contact_details_by_url($data['url'], $uid);
@@ -530,20 +529,37 @@ function contacts_not_grouped($uid,$start = 0,$count = 0) {
  * @return integer Contact ID
  */
 function get_contact($url, $uid = 0, $no_update = false) {
-       require_once "include/Scrape.php";
-
        logger("Get contact data for url ".$url." and user ".$uid." - ".App::callstack(), LOGGER_DEBUG);;
 
        $data = array();
        $contact_id = 0;
 
-       // Catch-all query, may return multiple rows
+       // We first try the nurl (http://server.tld/nick), most common case
        $contacts = q("SELECT `id`, `avatar-date` FROM `contact`
-                               WHERE ('%s' IN (`url`, `addr`, `alias`) OR '%s' IN (`nurl`, `alias`))
-                               AND `uid` = %d",
-               dbesc($url),
-               dbesc(normalise_link($url)),
-               intval($uid));
+                                       WHERE `nurl` = '%s'
+                                       AND `uid` = %d",
+                       dbesc(normalise_link($url)),
+                       intval($uid));
+
+
+       // Then the addr (nick@server.tld)
+       if (! dbm::is_result($contacts)) {
+               $contacts = q("SELECT `id`, `avatar-date` FROM `contact`
+                                       WHERE `addr` = '%s'
+                                       AND `uid` = %d",
+                       dbesc($url),
+                       intval($uid));
+       }
+
+       // Then the alias (which could be anything)
+       if (! dbm::is_result($contacts)) {
+               $contacts = q("SELECT `id`, `avatar-date` FROM `contact`
+                                       WHERE `alias` IN ('%s', '%s')
+                                       AND `uid` = %d",
+                       dbesc($url),
+                       dbesc(normalise_link($url)),
+                       intval($uid));
+       }
 
        if (dbm::is_result($contacts)) {
                $contact_id = $contacts[0]["id"];
@@ -555,18 +571,15 @@ function get_contact($url, $uid = 0, $no_update = false) {
                        return $contact_id;
                }
        } elseif ($uid != 0) {
+               // Non-existing user-specific contact, exiting
                return 0;
        }
 
-       $data = probe_url($url);
-       if (!$data['url']) {
-               return 0;
-       }
-
-       $url = $data["url"];
+       require_once('include/Probe.php');
+       $data = Probe::uri($url);
 
-       // Does this address belongs to a valid network?
-       if (!in_array($data["network"], array(NETWORK_DFRN, NETWORK_OSTATUS, NETWORK_DIASPORA))) {
+       // Last try in gcontact for unsupported networks
+       if (!in_array($data["network"], array(NETWORK_DFRN, NETWORK_OSTATUS, NETWORK_DIASPORA, NETWORK_PUMPIO))) {
                if ($uid != 0) {
                        return 0;
                }
@@ -581,6 +594,7 @@ function get_contact($url, $uid = 0, $no_update = false) {
                $data = $gcontacts[0];
        }
 
+       $url = $data["url"];
 
        if (!$contact_id) {
                q("INSERT INTO `contact` (`uid`, `created`, `url`, `nurl`, `addr`, `alias`, `notify`, `poll`,
@@ -688,15 +702,6 @@ function posts_from_gcontact(App $a, $gcontact_id) {
        else
                $sql = "`item`.`uid` = %d";
 
-       if(get_config('system', 'old_pager')) {
-               $r = q("SELECT COUNT(*) AS `total` FROM `item`
-                       WHERE `gcontact-id` = %d and $sql",
-                       intval($gcontact_id),
-                       intval(local_user()));
-
-               $a->set_pager_total($r[0]['total']);
-       }
-
        $r = q("SELECT `item`.`uri`, `item`.*, `item`.`id` AS `item_id`,
                        `author-name` AS `name`, `owner-avatar` AS `photo`,
                        `owner-link` AS `url`, `owner-avatar` AS `thumb`
@@ -710,13 +715,9 @@ function posts_from_gcontact(App $a, $gcontact_id) {
                intval($a->pager['itemspage'])
        );
 
-       $o = conversation($a,$r,'community',false);
+       $o = conversation($a, $r, 'community', false);
 
-       if(!get_config('system', 'old_pager')) {
-               $o .= alt_pager($a,count($r));
-       } else {
-               $o .= paginate($a);
-       }
+       $o .= alt_pager($a, count($r));
 
        return $o;
 }
@@ -749,15 +750,6 @@ function posts_from_contact_url(App $a, $contact_url) {
 
        $author_id = intval($r[0]["author-id"]);
 
-       if (get_config('system', 'old_pager')) {
-               $r = q("SELECT COUNT(*) AS `total` FROM `item`
-                       WHERE `author-id` = %d and $sql",
-                       intval($author_id),
-                       intval(local_user()));
-
-               $a->set_pager_total($r[0]['total']);
-       }
-
        $r = q(item_query()." AND `item`.`author-id` = %d AND ".$sql.
                " ORDER BY `item`.`created` DESC LIMIT %d, %d",
                intval($author_id),
@@ -766,13 +758,9 @@ function posts_from_contact_url(App $a, $contact_url) {
                intval($a->pager['itemspage'])
        );
 
-       $o = conversation($a,$r,'community',false);
+       $o = conversation($a, $r, 'community', false);
 
-       if (!get_config('system', 'old_pager')) {
-               $o .= alt_pager($a,count($r));
-       } else {
-               $o .= paginate($a);
-       }
+       $o .= alt_pager($a, count($r));
 
        return $o;
 }