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\Model\Item;
30 use Friendica\Network\Probe;
31 use Friendica\Util\DateTimeFormat;
32 use Friendica\Util\Strings;
37 * Fetches data for a given handle
39 * @param string $handle The handle
40 * @param boolean $update true = always update, false = never update, null = update when not found or outdated
42 * @return array the queried data
43 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
44 * @throws \ImagickException
46 public static function getByURL(string $handle, $update = null): array
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]);
55 if (DBA::isResult($person)) {
56 Logger::debug('In cache', ['handle' => $handle]);
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);
65 } elseif (is_null($update)) {
72 Logger::info('create or refresh', ['handle' => $handle]);
73 $data = Probe::uri($handle, Protocol::DIASPORA);
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);
80 $person = self::getByURL($handle, false);
88 * Updates the fcontact table
90 * @param array $arr The fcontact data
93 public static function updateFromProbeArray(array $arr)
95 $uriid = ItemURI::insert(['uri' => $arr['url'], 'guid' => $arr['guid']]);
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');
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]]);
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'],
126 'interacting_count' => $interacting ?? 0,
127 'interacted_count' => $interacted ?? 0,
128 'post_count' => $posts ?? 0,
129 'updated' => DateTimeFormat::utcNow(),
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'];
138 $fields = DI::dbaDefinition()->truncateFieldsForTable('fcontact', $fields);
139 DBA::update('fcontact', $fields, ['url' => $arr['url'], 'network' => $arr['network']], true);
143 * get a url (scheme://domain.tld/u/user) from a given Diaspora*
146 * @param string $fcontact_guid Hexadecimal string guid
147 * @return string|null the contact url or null
150 public static function getUrlByGuid(string $fcontact_guid)
152 Logger::info('fcontact', ['guid' => $fcontact_guid]);
154 $fcontact = DBA::selectFirst('fcontact', ['url'], ["`url` != ? AND `network` = ? AND `guid` = ?", '', Protocol::DIASPORA, $fcontact_guid]);
155 if (DBA::isResult($fcontact)) {
156 return $fcontact['url'];