]> git.mxchange.org Git - friendica.git/blob - src/Model/FContact.php
Changes:
[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\Database\DBA;
27 use Friendica\Network\Probe;
28 use Friendica\Util\DateTimeFormat;
29 use Friendica\Util\Strings;
30
31 class FContact
32 {
33         /**
34          * Fetches data for a given handle
35          *
36          * @param string $handle The handle
37          * @param boolean $update true = always update, false = never update, null = update when not found or outdated
38          *
39          * @return array the queried data
40          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
41          * @throws \ImagickException
42          */
43         public static function getByURL(string $handle, $update = null): array
44         {
45                 $person = DBA::selectFirst('fcontact', [], ['network' => Protocol::DIASPORA, 'addr' => $handle]);
46                 if (!DBA::isResult($person)) {
47                         $urls = [$handle, str_replace('http://', 'https://', $handle), Strings::normaliseLink($handle)];
48                         $person = DBA::selectFirst('fcontact', [], ['network' => Protocol::DIASPORA, 'url' => $urls]);
49                 }
50
51                 if (DBA::isResult($person)) {
52                         Logger::debug('In cache', ['person' => $person]);
53
54                         if (is_null($update)) {
55                                 // update record occasionally so it doesn't get stale
56                                 $d = strtotime($person["updated"]." +00:00");
57                                 if ($d < strtotime("now - 14 days")) {
58                                         $update = true;
59                                 }
60
61                                 if (empty($person['guid']) || empty($person['uri-id'])) {
62                                         $update = true;
63                                 }
64                         }
65                 } elseif (is_null($update)) {
66                         $update = !DBA::isResult($person);
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                 $contact = Contact::getByUriId($uriid, ['id']);
98                 if (!empty($contact['id'])) {
99                         $last_interaction = DateTimeFormat::utc('now - 180 days');
100
101                         $interacted  = DBA::count('contact-relation', ["`cid` = ? AND NOT `follows` AND `last-interaction` > ?", $contact['id'], $last_interaction]);
102                         $interacting = DBA::count('contact-relation', ["`relation-cid` = ? AND NOT `follows` AND `last-interaction` > ?", $contact['id'], $last_interaction]);
103                         $posts       = Post::countPosts(['author-id' => $contact['id'], 'gravity' => [GRAVITY_PARENT, GRAVITY_COMMENT]]);
104                 }
105
106                 $fields = [
107                         'name' => $arr['name'],
108                         'photo' => $arr['photo'],
109                         'request' => $arr['request'],
110                         'nick' => $arr['nick'],
111                         'addr' => strtolower($arr['addr']),
112                         'guid' => $arr['guid'],
113                         'batch' => $arr['batch'],
114                         'notify' => $arr['notify'],
115                         'poll' => $arr['poll'],
116                         'confirm' => $arr['confirm'],
117                         'alias' => $arr['alias'],
118                         'pubkey' => $arr['pubkey'],
119                         'uri-id' => $uriid,
120                         'interacting_count' => $interacting ?? 0,
121                         'interacted_count' => $interacted ?? 0,
122                         'post_count' => $posts ?? 0,
123                         'updated' => DateTimeFormat::utcNow(),
124                 ];
125
126                 $condition = ['url' => $arr['url'], 'network' => $arr['network']];
127
128                 DBA::update('fcontact', $fields, $condition, true);
129         }
130
131         /**
132          * get a url (scheme://domain.tld/u/user) from a given Diaspora*
133          * fcontact guid
134          *
135          * @param string $fcontact_guid Hexadecimal string guid
136          * @return string|null the contact url or null
137          * @throws \Exception
138          */
139         public static function getUrlByGuid(string $fcontact_guid)
140         {
141                 Logger::info('fcontact', ['guid' => $fcontact_guid]);
142
143                 $fcontact = DBA::selectFirst('fcontact', ['url'], ["`url` != ? AND `network` = ? AND `guid` = ?", '', Protocol::DIASPORA, $fcontact_guid]);
144                 if (DBA::isResult($fcontact)) {
145                         return $fcontact['url'];
146                 }
147
148                 return null;
149         }
150 }