3 * @copyright Copyright (C) 2010-2022, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Model;
24 use Friendica\Core\Logger;
25 use Friendica\Core\Protocol;
26 use Friendica\Core\Worker;
27 use Friendica\Database\DBA;
29 use Friendica\Network\Probe;
30 use Friendica\Util\DateTimeFormat;
31 use Friendica\Util\Strings;
36 * Fetches data for a given handle
38 * @param string $handle The handle
39 * @param boolean $update true = always update, false = never update, null = update when not found or outdated
41 * @return array the queried data
42 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
43 * @throws \ImagickException
45 public static function getByURL(string $handle, $update = null): array
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]);
54 if (DBA::isResult($person)) {
55 Logger::debug('In cache', ['handle' => $handle]);
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);
64 } elseif (is_null($update)) {
71 Logger::info('create or refresh', ['handle' => $handle]);
72 $data = Probe::uri($handle, Protocol::DIASPORA);
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);
79 $person = self::getByURL($handle, false);
87 * Updates the fcontact table
89 * @param array $arr The fcontact data
92 public static function updateFromProbeArray(array $arr)
94 $uriid = ItemURI::insert(['uri' => $arr['url'], 'guid' => $arr['guid']]);
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');
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]]);
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'],
125 'interacting_count' => $interacting ?? 0,
126 'interacted_count' => $interacted ?? 0,
127 'post_count' => $posts ?? 0,
128 'updated' => DateTimeFormat::utcNow(),
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'];
137 $fields = DI::dbaDefinition()->truncateFieldsForTable('fcontact', $fields);
138 DBA::update('fcontact', $fields, ['url' => $arr['url'], 'network' => $arr['network']], true);
142 * get a url (scheme://domain.tld/u/user) from a given Diaspora*
145 * @param string $fcontact_guid Hexadecimal string guid
146 * @return string|null the contact url or null
149 public static function getUrlByGuid(string $fcontact_guid)
151 Logger::info('fcontact', ['guid' => $fcontact_guid]);
153 $fcontact = DBA::selectFirst('fcontact', ['url'], ["`url` != ? AND `network` = ? AND `guid` = ?", '', Protocol::DIASPORA, $fcontact_guid]);
154 if (DBA::isResult($fcontact)) {
155 return $fcontact['url'];