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