]> git.mxchange.org Git - friendica.git/blob - src/Model/FContact.php
Quoted reshares containing quoted reshares should now look fine
[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\Model\Item;
30 use Friendica\Network\Probe;
31 use Friendica\Util\DateTimeFormat;
32 use Friendica\Util\Strings;
33
34 class FContact
35 {
36         /**
37          * Fetches data for a given handle
38          *
39          * @param string $handle The handle
40          * @param boolean $update true = always update, false = never update, null = update when not found or outdated
41          *
42          * @return array the queried data
43          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
44          * @throws \ImagickException
45          */
46         public static function getByURL(string $handle, $update = null): array
47         {
48                 Logger::debug('Fetch fcontact', ['handle' => $handle, 'update' => $update]);
49                 $person = DBA::selectFirst('fcontact', [], ['network' => Protocol::DIASPORA, 'addr' => $handle]);
50                 if (!DBA::isResult($person)) {
51                         $urls = [$handle, str_replace('http://', 'https://', $handle), Strings::normaliseLink($handle)];
52                         $person = DBA::selectFirst('fcontact', [], ['network' => Protocol::DIASPORA, 'url' => $urls]);
53                 }
54
55                 if (DBA::isResult($person)) {
56                         Logger::debug('In cache', ['handle' => $handle]);
57
58                         if (is_null($update)) {
59                                 $update = empty($person['guid']) || empty($person['uri-id']) || ($person['created'] <= DBA::NULL_DATETIME);
60                                 if (GServer::getNextUpdateDate(true, $person['created'], $person['updated'], false) < DateTimeFormat::utcNow()) {
61                                         Logger::debug('Start background update', ['handle' => $handle]);
62                                         Worker::add(['priority' => Worker::PRIORITY_LOW, 'dont_fork' => true], 'UpdateFContact', $handle);
63                                 }
64                         }
65                 } elseif (is_null($update)) {
66                         $update = true;
67                 } else {
68                         $person = [];
69                 }
70
71                 if ($update) {
72                         Logger::info('create or refresh', ['handle' => $handle]);
73                         $data = Probe::uri($handle, Protocol::DIASPORA);
74
75                         // Note that Friendica contacts will return a "Diaspora person"
76                         // if Diaspora connectivity is enabled on their server
77                         if ($data['network'] ?? '' === Protocol::DIASPORA) {
78                                 self::updateFromProbeArray($data);
79
80                                 $person = self::getByURL($handle, false);
81                         }
82                 }
83
84                 return $person;
85         }
86
87         /**
88          * Updates the fcontact table
89          *
90          * @param array $arr The fcontact data
91          * @throws \Exception
92          */
93         public static function updateFromProbeArray(array $arr)
94         {
95                 $uriid = ItemURI::insert(['uri' => $arr['url'], 'guid' => $arr['guid']]);
96
97                 $fcontact  = DBA::selectFirst('fcontact', ['created'], ['url' => $arr['url'], 'network' => $arr['network']]);
98                 $contact   = Contact::getByUriId($uriid, ['id', 'created']);
99                 $apcontact = APContact::getByURL($arr['url'], false);
100                 if (!empty($apcontact)) {
101                         $interacted  = $apcontact['following_count'];
102                         $interacting = $apcontact['followers_count'];
103                         $posts       = $apcontact['statuses_count'];
104                 } elseif (!empty($contact['id'])) {
105                         $last_interaction = DateTimeFormat::utc('now - 180 days');
106
107                         $interacted  = DBA::count('contact-relation', ["`cid` = ? AND NOT `follows` AND `last-interaction` > ?", $contact['id'], $last_interaction]);
108                         $interacting = DBA::count('contact-relation', ["`relation-cid` = ? AND NOT `follows` AND `last-interaction` > ?", $contact['id'], $last_interaction]);
109                         $posts       = DBA::count('post', ['author-id' => $contact['id'], 'gravity' => [Item::GRAVITY_PARENT, Item::GRAVITY_COMMENT]]);
110                 }
111
112                 $fields = [
113                         'name'              => $arr['name'],
114                         'photo'             => $arr['photo'],
115                         'request'           => $arr['request'],
116                         'nick'              => $arr['nick'],
117                         'addr'              => strtolower($arr['addr']),
118                         'guid'              => $arr['guid'],
119                         'batch'             => $arr['batch'],
120                         'notify'            => $arr['notify'],
121                         'poll'              => $arr['poll'],
122                         'confirm'           => $arr['confirm'],
123                         'alias'             => $arr['alias'],
124                         'pubkey'            => $arr['pubkey'],
125                         'uri-id'            => $uriid,
126                         'interacting_count' => $interacting ?? 0,
127                         'interacted_count'  => $interacted ?? 0,
128                         'post_count'        => $posts ?? 0,
129                         'updated'           => DateTimeFormat::utcNow(),
130                 ];
131
132                 if (empty($fcontact['created'])) {
133                         $fields['created'] = $fields['updated'];
134                 } elseif (!empty($contact['created']) && ($fcontact['created'] <= DBA::NULL_DATETIME)) {
135                         $fields['created'] = $contact['created'];
136                 }
137
138                 $fields = DI::dbaDefinition()->truncateFieldsForTable('fcontact', $fields);
139                 DBA::update('fcontact', $fields, ['url' => $arr['url'], 'network' => $arr['network']], true);
140         }
141
142         /**
143          * get a url (scheme://domain.tld/u/user) from a given Diaspora*
144          * fcontact guid
145          *
146          * @param string $fcontact_guid Hexadecimal string guid
147          * @return string|null the contact url or null
148          * @throws \Exception
149          */
150         public static function getUrlByGuid(string $fcontact_guid)
151         {
152                 Logger::info('fcontact', ['guid' => $fcontact_guid]);
153
154                 $fcontact = DBA::selectFirst('fcontact', ['url'], ["`url` != ? AND `network` = ? AND `guid` = ?", '', Protocol::DIASPORA, $fcontact_guid]);
155                 if (DBA::isResult($fcontact)) {
156                         return $fcontact['url'];
157                 }
158
159                 return null;
160         }
161 }