3 * @copyright Copyright (C) 2020, Friendica
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\Protocol\Activity;
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($handle, $update = null, $network = Protocol::DIASPORA)
47 $person = DBA::selectFirst('fcontact', [], ['network' => $network, 'addr' => $handle]);
48 if (!DBA::isResult($person)) {
49 $urls = [$handle, str_replace('http://', 'https://', $handle), Strings::normaliseLink($handle)];
50 $person = DBA::selectFirst('fcontact', [], ['network' => $network, 'url' => $urls]);
53 if (DBA::isResult($person)) {
54 Logger::debug('In cache', ['person' => $person]);
56 if (is_null($update)) {
57 // update record occasionally so it doesn't get stale
58 $d = strtotime($person["updated"]." +00:00");
59 if ($d < strtotime("now - 14 days")) {
63 if ($person["guid"] == "") {
67 } elseif (is_null($update)) {
68 $update = !DBA::isResult($person);
74 Logger::info('create or refresh', ['handle' => $handle]);
75 $r = Probe::uri($handle, $network);
77 // Note that Friendica contacts will return a "Diaspora person"
78 // if Diaspora connectivity is enabled on their server
79 if ($r && ($r["network"] === $network)) {
80 self::updateFContact($r);
82 $person = self::getByURL($handle, false, $network);
90 * Updates the fcontact table
92 * @param array $arr The fcontact data
95 private static function updateFContact($arr)
97 $fields = ['name' => $arr["name"], 'photo' => $arr["photo"],
98 'request' => $arr["request"], 'nick' => $arr["nick"],
99 'addr' => strtolower($arr["addr"]), 'guid' => $arr["guid"],
100 'batch' => $arr["batch"], 'notify' => $arr["notify"],
101 'poll' => $arr["poll"], 'confirm' => $arr["confirm"],
102 'alias' => $arr["alias"], 'pubkey' => $arr["pubkey"],
103 'updated' => DateTimeFormat::utcNow()];
105 $condition = ['url' => $arr["url"], 'network' => $arr["network"]];
107 DBA::update('fcontact', $fields, $condition, true);
111 * get a url (scheme://domain.tld/u/user) from a given Diaspora*
114 * @param mixed $fcontact_guid Hexadecimal string guid
116 * @return string the contact url or null
119 public static function getUrlByGuid($fcontact_guid)
121 Logger::info('fcontact', ['guid' => $fcontact_guid]);
124 "SELECT `url` FROM `fcontact` WHERE `url` != '' AND `network` = '%s' AND `guid` = '%s'",
125 DBA::escape(Protocol::DIASPORA),
126 DBA::escape($fcontact_guid)
129 if (DBA::isResult($r)) {
137 * Suggest a given contact to a given user from a given contact
139 * @param integer $uid
140 * @param integer $cid
141 * @param integer $from_cid
142 * @return bool Was the adding successful?
144 public static function addSuggestion(int $uid, int $cid, int $from_cid, string $note = '')
146 $owner = User::getOwnerDataById($uid);
147 $contact = Contact::getById($cid);
148 $from_contact = Contact::getById($from_cid);
150 if (DBA::exists('contact', ['nurl' => Strings::normaliseLink($contact['url']), 'uid' => $uid])) {
154 $fcontact = self::getByURL($contact['url'], null, $contact['network']);
155 if (empty($fcontact)) {
156 Logger::warning('FContact had not been found', ['fcontact' => $contact['url']]);
160 $fid = $fcontact['id'];
162 // Quit if we already have an introduction for this person
163 if (DBA::exists('intro', ['uid' => $uid, 'fid' => $fid])) {
168 $suggest['uid'] = $uid;
169 $suggest['cid'] = $from_cid;
170 $suggest['url'] = $contact['url'];
171 $suggest['name'] = $contact['name'];
172 $suggest['photo'] = $contact['photo'];
173 $suggest['request'] = $contact['request'];
174 $suggest['title'] = '';
175 $suggest['body'] = $note;
177 $hash = Strings::getRandomHex();
178 $fields = ['uid' => $suggest['uid'], 'fid' => $fid, 'contact-id' => $suggest['cid'],
179 'note' => $suggest['body'], 'hash' => $hash, 'datetime' => DateTimeFormat::utcNow(), 'blocked' => false];
180 DBA::insert('intro', $fields);
183 'type' => Notification\Type::SUGGEST,
184 'otype' => Notification\ObjectType::INTRO,
185 'verb' => Activity::REQ_FRIEND,
186 'uid' => $owner['uid'],
187 'cid' => $from_contact['uid'],
189 'link' => DI::baseUrl().'/notifications/intros',