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;
27 use Friendica\Network\Probe;
28 use Friendica\Util\DateTimeFormat;
29 use Friendica\Util\Strings;
34 * Fetches data for a given handle
36 * @param string $handle The handle
37 * @param boolean $update true = always update, false = never update, null = update when not found or outdated
39 * @return array the queried data
40 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
41 * @throws \ImagickException
43 public static function getByURL($handle, $update = null)
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]);
51 if (DBA::isResult($person)) {
52 Logger::debug('In cache', ['person' => $person]);
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")) {
61 if (empty($person['guid']) || empty($person['uri-id'])) {
65 } elseif (is_null($update)) {
66 $update = !DBA::isResult($person);
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($arr)
95 $uriid = ItemURI::insert(['uri' => $arr['url'], 'guid' => $arr['guid']]);
97 $contact = Contact::getByUriId($uriid, ['id']);
98 if (!empty($contact['id'])) {
99 $last_interaction = DateTimeFormat::utc('now - 180 days');
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]]);
106 $fields = ['name' => $arr["name"], 'photo' => $arr["photo"],
107 'request' => $arr["request"], 'nick' => $arr["nick"],
108 'addr' => strtolower($arr["addr"]), 'guid' => $arr["guid"],
109 'batch' => $arr["batch"], 'notify' => $arr["notify"],
110 'poll' => $arr["poll"], 'confirm' => $arr["confirm"],
111 'alias' => $arr["alias"], 'pubkey' => $arr["pubkey"],
112 'uri-id' => $uriid, 'interacting_count' => $interacting ?? 0,
113 'interacted_count' => $interacted ?? 0, 'post_count' => $posts ?? 0,
114 'updated' => DateTimeFormat::utcNow()];
116 $condition = ['url' => $arr["url"], 'network' => $arr["network"]];
118 DBA::update('fcontact', $fields, $condition, true);
122 * get a url (scheme://domain.tld/u/user) from a given Diaspora*
125 * @param mixed $fcontact_guid Hexadecimal string guid
127 * @return string the contact url or null
130 public static function getUrlByGuid($fcontact_guid)
132 Logger::info('fcontact', ['guid' => $fcontact_guid]);
134 $fcontact = DBA::selectFirst('fcontact', ['url'], ["`url` != ? AND `network` = ? AND `guid` = ?", '', Protocol::DIASPORA, $fcontact_guid]);
135 if (DBA::isResult($fcontact)) {
136 return $fcontact['url'];