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\Database\DBA;
28 use Friendica\Network\Probe;
29 use Friendica\Util\DateTimeFormat;
30 use Friendica\Util\Strings;
35 * Fetches data for a given handle
37 * @param string $handle The handle
38 * @param boolean $update true = always update, false = never update, null = update when not found or outdated
40 * @return array the queried data
41 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
42 * @throws \ImagickException
44 public static function getByURL(string $handle, $update = null): array
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]);
52 if (DBA::isResult($person)) {
53 Logger::debug('In cache', ['person' => $person]);
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')) {
62 if (empty($person['guid']) || empty($person['uri-id'])) {
66 } elseif (is_null($update)) {
67 $update = !DBA::isResult($person);
73 Logger::info('create or refresh', ['handle' => $handle]);
74 $data = Probe::uri($handle, Protocol::DIASPORA);
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);
81 $person = self::getByURL($handle, false);
89 * Updates the fcontact table
91 * @param array $arr The fcontact data
94 public static function updateFromProbeArray(array $arr)
96 $uriid = ItemURI::insert(['uri' => $arr['url'], 'guid' => $arr['guid']]);
98 $contact = Contact::getByUriId($uriid, ['id']);
99 if (!empty($contact['id'])) {
100 $last_interaction = DateTimeFormat::utc('now - 180 days');
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]]);
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'],
121 'interacting_count' => $interacting ?? 0,
122 'interacted_count' => $interacted ?? 0,
123 'post_count' => $posts ?? 0,
124 'updated' => DateTimeFormat::utcNow(),
127 $condition = ['url' => $arr['url'], 'network' => $arr['network']];
129 $fields = DI::dbaDefinition()->truncateFieldsForTable('fcontact', $fields);
130 DBA::update('fcontact', $fields, $condition, true);
134 * get a url (scheme://domain.tld/u/user) from a given Diaspora*
137 * @param string $fcontact_guid Hexadecimal string guid
138 * @return string|null the contact url or null
141 public static function getUrlByGuid(string $fcontact_guid)
143 Logger::info('fcontact', ['guid' => $fcontact_guid]);
145 $fcontact = DBA::selectFirst('fcontact', ['url'], ["`url` != ? AND `network` = ? AND `guid` = ?", '', Protocol::DIASPORA, $fcontact_guid]);
146 if (DBA::isResult($fcontact)) {
147 return $fcontact['url'];