]> git.mxchange.org Git - friendica.git/blob - src/Model/FContact.php
Merge remote-tracking branch 'upstream/2022.09-rc' into quote-uri-id
[friendica.git] / src / Model / FContact.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Model;
23
24 use Friendica\Core\Logger;
25 use Friendica\Core\Protocol;
26 use Friendica\Core\Worker;
27 use Friendica\Database\DBA;
28 use Friendica\DI;
29 use Friendica\Network\Probe;
30 use Friendica\Util\DateTimeFormat;
31 use Friendica\Util\Strings;
32
33 class FContact
34 {
35         /**
36          * Fetches data for a given handle
37          *
38          * @param string $handle The handle
39          * @param boolean $update true = always update, false = never update, null = update when not found or outdated
40          *
41          * @return array the queried data
42          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
43          * @throws \ImagickException
44          */
45         public static function getByURL(string $handle, $update = null): array
46         {
47                 Logger::debug('Fetch fcontact', ['handle' => $handle, 'update' => $update]);
48                 $person = DBA::selectFirst('fcontact', [], ['network' => Protocol::DIASPORA, 'addr' => $handle]);
49                 if (!DBA::isResult($person)) {
50                         $urls = [$handle, str_replace('http://', 'https://', $handle), Strings::normaliseLink($handle)];
51                         $person = DBA::selectFirst('fcontact', [], ['network' => Protocol::DIASPORA, 'url' => $urls]);
52                 }
53
54                 if (DBA::isResult($person)) {
55                         Logger::debug('In cache', ['handle' => $handle]);
56
57                         if (is_null($update)) {
58                                 $update = empty($person['guid']) || empty($person['uri-id']) || ($person['created'] <= DBA::NULL_DATETIME);
59                                 if (GServer::getNextUpdateDate(true, $person['created'], $person['updated'], false) < DateTimeFormat::utcNow()) {
60                                         Logger::debug('Start background update', ['handle' => $handle]);
61                                         Worker::add(['priority' => PRIORITY_LOW, 'dont_fork' => true], 'UpdateFContact', $handle);
62                                 }
63                         }
64                 } elseif (is_null($update)) {
65                         $update = true;
66                 } else {
67                         $person = [];
68                 }
69
70                 if ($update) {
71                         Logger::info('create or refresh', ['handle' => $handle]);
72                         $data = Probe::uri($handle, Protocol::DIASPORA);
73
74                         // Note that Friendica contacts will return a "Diaspora person"
75                         // if Diaspora connectivity is enabled on their server
76                         if ($data['network'] ?? '' === Protocol::DIASPORA) {
77                                 self::updateFromProbeArray($data);
78
79                                 $person = self::getByURL($handle, false);
80                         }
81                 }
82
83                 return $person;
84         }
85
86         /**
87          * Updates the fcontact table
88          *
89          * @param array $arr The fcontact data
90          * @throws \Exception
91          */
92         public static function updateFromProbeArray(array $arr)
93         {
94                 $uriid = ItemURI::insert(['uri' => $arr['url'], 'guid' => $arr['guid']]);
95
96                 $fcontact  = DBA::selectFirst('fcontact', ['created'], ['url' => $arr['url'], 'network' => $arr['network']]);
97                 $contact   = Contact::getByUriId($uriid, ['id', 'created']);
98                 $apcontact = APContact::getByURL($arr['url'], false);
99                 if (!empty($apcontact)) {
100                         $interacted  = $apcontact['following_count'];
101                         $interacting = $apcontact['followers_count'];
102                         $posts       = $apcontact['statuses_count'];
103                 } elseif (!empty($contact['id'])) {
104                         $last_interaction = DateTimeFormat::utc('now - 180 days');
105
106                         $interacted  = DBA::count('contact-relation', ["`cid` = ? AND NOT `follows` AND `last-interaction` > ?", $contact['id'], $last_interaction]);
107                         $interacting = DBA::count('contact-relation', ["`relation-cid` = ? AND NOT `follows` AND `last-interaction` > ?", $contact['id'], $last_interaction]);
108                         $posts       = DBA::count('post', ['author-id' => $contact['id'], 'gravity' => [GRAVITY_PARENT, GRAVITY_COMMENT]]);
109                 }
110
111                 $fields = [
112                         'name' => $arr['name'],
113                         'photo' => $arr['photo'],
114                         'request' => $arr['request'],
115                         'nick' => $arr['nick'],
116                         'addr' => strtolower($arr['addr']),
117                         'guid' => $arr['guid'],
118                         'batch' => $arr['batch'],
119                         'notify' => $arr['notify'],
120                         'poll' => $arr['poll'],
121                         'confirm' => $arr['confirm'],
122                         'alias' => $arr['alias'],
123                         'pubkey' => $arr['pubkey'],
124                         'uri-id' => $uriid,
125                         'interacting_count' => $interacting ?? 0,
126                         'interacted_count' => $interacted ?? 0,
127                         'post_count' => $posts ?? 0,
128                         'updated' => DateTimeFormat::utcNow(),
129                 ];
130
131                 if (empty($fcontact['created'])) {
132                         $fields['created'] = $fields['updated'];
133                 } elseif (!empty($contact['created']) && ($fcontact['created'] <= DBA::NULL_DATETIME)) {
134                         $fields['created'] = $contact['created'];
135                 }
136
137                 $fields = DI::dbaDefinition()->truncateFieldsForTable('fcontact', $fields);
138                 DBA::update('fcontact', $fields, ['url' => $arr['url'], 'network' => $arr['network']], true);
139         }
140
141         /**
142          * get a url (scheme://domain.tld/u/user) from a given Diaspora*
143          * fcontact guid
144          *
145          * @param string $fcontact_guid Hexadecimal string guid
146          * @return string|null the contact url or null
147          * @throws \Exception
148          */
149         public static function getUrlByGuid(string $fcontact_guid)
150         {
151                 Logger::info('fcontact', ['guid' => $fcontact_guid]);
152
153                 $fcontact = DBA::selectFirst('fcontact', ['url'], ["`url` != ? AND `network` = ? AND `guid` = ?", '', Protocol::DIASPORA, $fcontact_guid]);
154                 if (DBA::isResult($fcontact)) {
155                         return $fcontact['url'];
156                 }
157
158                 return null;
159         }
160 }