New fields in contact table / don't update on probe
authorMichael <heluecht@pirati.ca>
Thu, 4 Jul 2019 04:08:55 +0000 (04:08 +0000)
committerMichael <heluecht@pirati.ca>
Thu, 4 Jul 2019 04:08:55 +0000 (04:08 +0000)
config/dbstructure.config.php
database.sql
src/Model/APContact.php
src/Model/Contact.php
src/Model/GContact.php
src/Network/Probe.php
src/Protocol/ActivityPub/Processor.php
src/Protocol/DFRN.php
src/Protocol/Diaspora.php

index 415e389..3c2b813 100755 (executable)
@@ -253,6 +253,9 @@ return [
                        "pending" => ["type" => "boolean", "not null" => "1", "default" => "1", "comment" => ""],
                        "deleted" => ["type" => "boolean", "not null" => "1", "default" => "0", "comment" => "Contact has been deleted"],
                        "rating" => ["type" => "tinyint", "not null" => "1", "default" => "0", "comment" => ""],
+                       "unsearchable" => ["type" => "boolean", "not null" => "1", "default" => "0", "comment" => "Contact prefers to not be searchable"],
+                       "sensitive" => ["type" => "boolean", "not null" => "1", "default" => "0", "comment" => "Contact posts sensitive content"],
+                       "baseurl" => ["type" => "varchar(255)", "default" => "", "comment" => "baseurl of the contact"],
                        "reason" => ["type" => "text", "comment" => ""],
                        "closeness" => ["type" => "tinyint unsigned", "not null" => "1", "default" => "99", "comment" => ""],
                        "info" => ["type" => "mediumtext", "comment" => ""],
index 07d4e2f..7dc24b6 100644 (file)
@@ -210,6 +210,9 @@ CREATE TABLE IF NOT EXISTS `contact` (
        `pending` boolean NOT NULL DEFAULT '1' COMMENT '',
        `deleted` boolean NOT NULL DEFAULT '0' COMMENT 'Contact has been deleted',
        `rating` tinyint NOT NULL DEFAULT 0 COMMENT '',
+       `unsearchable` boolean NOT NULL DEFAULT '0' COMMENT 'Contact prefers to not be searchable',
+       `sensitive` boolean NOT NULL DEFAULT '0' COMMENT 'Contact posts sensitive content',
+       `baseurl` varchar(255) DEFAULT '' COMMENT 'baseurl of the contact',
        `reason` text COMMENT '',
        `closeness` tinyint unsigned NOT NULL DEFAULT 99 COMMENT '',
        `info` mediumtext COMMENT '',
index b027d6c..6d50582 100644 (file)
@@ -270,10 +270,7 @@ class APContact extends BaseObject
                }
 
                // Update the gcontact table
-               // These two fields don't exist in the gcontact table
-               unset($contact_fields['forum']);
-               unset($contact_fields['prv']);
-               DBA::update('gcontact', $contact_fields, ['nurl' => Strings::normaliseLink($url)]);
+               GContact::updateFromPublicContactURL($url);
 
                Logger::log('Updated profile for ' . $url, Logger::DEBUG);
 
index c980482..20dfc4d 100644 (file)
@@ -1400,6 +1400,7 @@ class Contact extends BaseObject
                                'request'   => defaults($data, 'request', ''),
                                'confirm'   => defaults($data, 'confirm', ''),
                                'poco'      => defaults($data, 'poco', ''),
+                               'baseurl'   => defaults($data, 'baseurl', ''),
                                'name-date' => DateTimeFormat::utcNow(),
                                'uri-date'  => DateTimeFormat::utcNow(),
                                'avatar-date' => DateTimeFormat::utcNow(),
@@ -1453,7 +1454,7 @@ class Contact extends BaseObject
                        self::updateAvatar($data['photo'], $uid, $contact_id);
                }
 
-               $fields = ['url', 'nurl', 'addr', 'alias', 'name', 'nick', 'keywords', 'location', 'about', 'avatar-date', 'pubkey'];
+               $fields = ['url', 'nurl', 'addr', 'alias', 'name', 'nick', 'keywords', 'location', 'about', 'avatar-date', 'pubkey', 'baseurl'];
                $contact = DBA::selectFirst('contact', $fields, ['id' => $contact_id]);
 
                // This condition should always be true
@@ -1467,7 +1468,8 @@ class Contact extends BaseObject
                        'url' => $data['url'],
                        'nurl' => Strings::normaliseLink($data['url']),
                        'name' => $data['name'],
-                       'nick' => $data['nick']
+                       'nick' => $data['nick'],
+                       'baseurl' => $data['baseurl']
                ];
 
                if (!empty($data['keywords'])) {
@@ -1820,7 +1822,7 @@ class Contact extends BaseObject
                 */
 
                $fields = ['avatar', 'uid', 'name', 'nick', 'url', 'addr', 'batch', 'notify',
-                       'poll', 'request', 'confirm', 'poco', 'network', 'alias'];
+                       'poll', 'request', 'confirm', 'poco', 'network', 'alias', 'baseurl'];
                $contact = DBA::selectFirst('contact', $fields, ['id' => $id]);
                if (!DBA::isResult($contact)) {
                        return false;
@@ -1849,7 +1851,7 @@ class Contact extends BaseObject
 
                // make sure to not overwrite existing values with blank entries
                foreach ($ret as $key => $val) {
-                       if (!isset($contact[$key])) {
+                       if (!array_key_exists($key, $contact)) {
                                unset($ret[$key]);
                        } elseif (($contact[$key] != '') && ($val == '')) {
                                $ret[$key] = $contact[$key];
@@ -2082,6 +2084,7 @@ class Contact extends BaseObject
                                'name'    => $ret['name'],
                                'nick'    => $ret['nick'],
                                'network' => $ret['network'],
+                               'baseurl' => $ret['baseurl'],
                                'protocol' => $protocol,
                                'pubkey'  => $ret['pubkey'],
                                'rel'     => $new_relation,
index 4fb7661..7da5f18 100644 (file)
@@ -873,20 +873,20 @@ class GContact
         * @brief Updates the gcontact entry from a given public contact url
         *
         * @param string $url contact url
-        * @return void
+        * @return integer gcontact id
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         * @throws \ImagickException
         */
        public static function updateFromPublicContactURL($url)
        {
-               self::updateFromPublicContact(['nurl' => Strings::normaliseLink($url)]);
+               return self::updateFromPublicContact(['nurl' => Strings::normaliseLink($url)]);
        }
 
        /**
         * @brief Helper function for updateFromPublicContactID and updateFromPublicContactURL
         *
         * @param array $condition contact condition
-        * @return void
+        * @return integer gcontact id
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         * @throws \ImagickException
         */
@@ -894,19 +894,18 @@ class GContact
        {
                $fields = ['name', 'nick', 'url', 'nurl', 'location', 'about', 'keywords', 'gender',
                        'bd', 'contact-type', 'network', 'addr', 'notify', 'alias', 'archive', 'term-date',
-                       'created', 'updated', 'avatar', 'success_update', 'failure_update', 'forum', 'prv'];
+                       'created', 'updated', 'avatar', 'success_update', 'failure_update', 'forum', 'prv',
+                       'baseurl', 'sensitive', 'unsearchable'];
+
                $contact = DBA::selectFirst('contact', $fields, array_merge($condition, ['uid' => 0, 'network' => Protocol::FEDERATED]));
                if (!DBA::isResult($contact)) {
-                       return;
+                       return 0;
                }
 
-               // These fields cannot be updated, since they don't exist in the contact table
-               // hide, nsfw, server_url
-               // "connect" does exist, but seems to contain the same as "addr"
-
                $fields = ['name', 'nick', 'url', 'nurl', 'location', 'about', 'keywords', 'gender', 'generation',
                        'birthday', 'contact-type', 'network', 'addr', 'notify', 'alias', 'archived', 'archive_date',
-                       'created', 'updated', 'photo', 'last_contact', 'last_failure', 'community', 'connect'];
+                       'created', 'updated', 'photo', 'last_contact', 'last_failure', 'community', 'connect',
+                       'server_url', 'nsfw', 'hide', 'id'];
 
                $old_gcontact = DBA::selectFirst('gcontact', $fields, ['nurl' => $contact['nurl']]);
                $do_insert = !DBA::isResult($old_gcontact);
@@ -917,6 +916,12 @@ class GContact
                $gcontact = $contact;
 
                // These fields are having different names but the same content
+               $gcontact['server_url'] = $gcontact['baseurl'];
+               unset($gcontact['baseurl']);
+               $gcontact['nsfw'] = $gcontact['sensitive'];
+               unset($gcontact['sensitive']);
+               $gcontact['hide'] = $gcontact['unsearchable'];
+               unset($gcontact['unsearchable']);
                $gcontact['archived'] = $gcontact['archive'];
                unset($gcontact['archive']);
                $gcontact['archive_date'] = $gcontact['term-date'];
@@ -958,8 +963,10 @@ class GContact
 
                if (!$do_insert) {
                        DBA::update('gcontact', $gcontact, ['nurl' => $contact['nurl']], $old_gcontact);
+                       return $old_gcontact['id'];
                } elseif (!$gcontact['archived']) {
                        DBA::insert('gcontact', $gcontact);
+                       return DBA::lastInsertId();
                }
        }
 
index a3fe3ca..5c20cc0 100644 (file)
@@ -17,7 +17,6 @@ use Friendica\Core\Logger;
 use Friendica\Core\Protocol;
 use Friendica\Core\System;
 use Friendica\Database\DBA;
-use Friendica\Model\Contact;
 use Friendica\Model\Profile;
 use Friendica\Protocol\ActivityPub;
 use Friendica\Protocol\Email;
@@ -396,114 +395,6 @@ class Probe
                // Only store into the cache if the value seems to be valid
                if (!in_array($data['network'], [Protocol::PHANTOM, Protocol::MAIL])) {
                        Cache::set('Probe::uri:' . $network . ':' . $uri, $data, Cache::DAY);
-
-                       /// @todo temporary fix - we need a real contact update function that updates only changing fields
-                       /// The biggest problem is the avatar picture that could have a reduced image size.
-                       /// It should only be updated if the existing picture isn't existing anymore.
-                       /// We only update the contact when it is no probing for a specific network.
-                       if (($data['network'] != Protocol::FEED)
-                               && ($network == '')
-                               && $data['name']
-                               && $data['nick']
-                               && $data['url']
-                               && $data['addr']
-                               && $data['poll']
-                       ) {
-                               $fields = [
-                                       'name' => $data['name'],
-                                       'nick' => $data['nick'],
-                                       'url' => $data['url'],
-                                       'addr' => $data['addr'],
-                                       'photo' => $data['photo'],
-                                       'keywords' => $data['keywords'],
-                                       'location' => $data['location'],
-                                       'about' => $data['about'],
-                                       'notify' => $data['notify'],
-                                       'network' => $data['network'],
-                                       'server_url' => $data['baseurl']
-                               ];
-
-                               // This doesn't cover the case when a community isn't a community anymore
-                               if (!empty($data['community']) && $data['community']) {
-                                       $fields['community'] = $data['community'];
-                                       $fields['contact-type'] = Contact::TYPE_COMMUNITY;
-                               }
-
-                               $fieldnames = [];
-
-                               foreach ($fields as $key => $val) {
-                                       if (empty($val)) {
-                                               unset($fields[$key]);
-                                       } else {
-                                               $fieldnames[] = $key;
-                                       }
-                               }
-
-                               $fields['updated'] = DateTimeFormat::utcNow();
-
-                               $condition = ['nurl' => Strings::normaliseLink($data['url'])];
-
-                               $old_fields = DBA::selectFirst('gcontact', $fieldnames, $condition);
-
-                               // When the gcontact doesn't exist, the value "true" will trigger an insert.
-                               // In difference to the public contacts we want to have every contact
-                               // in the world in our global contacts.
-                               if (!$old_fields) {
-                                       $old_fields = true;
-
-                                       // These values have to be set only on insert
-                                       $fields['photo'] = $data['photo'];
-                                       $fields['created'] = DateTimeFormat::utcNow();
-                               }
-
-                               DBA::update('gcontact', $fields, $condition, $old_fields);
-
-                               $fields = [
-                                       'name' => $data['name'],
-                                       'nick' => $data['nick'],
-                                       'url' => $data['url'],
-                                       'addr' => $data['addr'],
-                                       'alias' => $data['alias'],
-                                       'keywords' => $data['keywords'],
-                                       'location' => $data['location'],
-                                       'about' => $data['about'],
-                                       'batch' => $data['batch'],
-                                       'notify' => $data['notify'],
-                                       'poll' => $data['poll'],
-                                       'request' => $data['request'],
-                                       'confirm' => $data['confirm'],
-                                       'poco' => $data['poco'],
-                                       'network' => $data['network'],
-                                       'pubkey' => $data['pubkey'],
-                                       'priority' => $data['priority'],
-                                       'writable' => true,
-                                       'rel' => Contact::SHARING
-                               ];
-
-                               $fieldnames = [];
-
-                               foreach ($fields as $key => $val) {
-                                       if (empty($val)) {
-                                               unset($fields[$key]);
-                                       } else {
-                                               $fieldnames[] = $key;
-                                       }
-                               }
-
-                               $condition = ['nurl' => Strings::normaliseLink($data['url']), 'self' => false, 'uid' => 0];
-
-                               // "$old_fields" will return a "false" when the contact doesn't exist.
-                               // This won't trigger an insert. This is intended, since we only need
-                               // public contacts for everyone we store items from.
-                               // We don't need to store every contact on the planet.
-                               $old_fields = DBA::selectFirst('contact', $fieldnames, $condition);
-
-                               $fields['name-date'] = DateTimeFormat::utcNow();
-                               $fields['uri-date'] = DateTimeFormat::utcNow();
-                               $fields['success_update'] = DateTimeFormat::utcNow();
-
-                               DBA::update('contact', $fields, $condition, $old_fields);
-                       }
                }
 
                return $data;
index 41aed2f..1019426 100644 (file)
@@ -612,6 +612,7 @@ class Processor
 
                Logger::log('Updating profile for ' . $activity['object_id'], Logger::DEBUG);
                APContact::getByURL($activity['object_id'], true);
+//             Contact::updateFromProbe($activity['object_id'], $network = '', $force = false)
        }
 
        /**
index a5deb49..4c5f20c 100644 (file)
@@ -1742,6 +1742,11 @@ class DFRN
                                (strtotime($contact['avatar-date']) > strtotime($contact_old['avatar-date']) || ($author['avatar'] != $contact_old['avatar']))
                        );
 
+                       // Update the "hidden" status in the public contact
+                       // @todo Updating the contact with all fields and update the gcontact from that
+                       $condition = ['uid' => 0, 'nurl' => Strings::normaliseLink($contact_old['url'])];
+                       DBA::update('contact', ['unsearchable' => $hide], $condition, true);
+
                        /*
                         * The generation is a sign for the reliability of the provided data.
                         * It is used in the socgraph.php to prevent that old contact data
index f7d9425..31d9efa 100644 (file)
@@ -2258,8 +2258,8 @@ class Diaspora
                $fields = ['name' => $name, 'location' => $location,
                        'name-date' => DateTimeFormat::utcNow(),
                        'about' => $about, 'gender' => $gender,
-                       'addr' => $author, 'nick' => $nick,
-                       'keywords' => $keywords];
+                       'addr' => $author, 'nick' => $nick, 'keywords' => $keywords,
+                       'unsearchable' => !$searchable, 'sensitive' => $nsfw];
 
                if (!empty($birthday)) {
                        $fields['bd'] = $birthday;