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\Model\Notify\Type;
29 use Friendica\Network\Probe;
30 use Friendica\Protocol\Activity;
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($handle, $update = null, $network = Protocol::DIASPORA)
48 $person = DBA::selectFirst('fcontact', [], ['network' => $network, 'addr' => $handle]);
49 if (!DBA::isResult($person)) {
50 $urls = [$handle, str_replace('http://', 'https://', $handle), Strings::normaliseLink($handle)];
51 $person = DBA::selectFirst('fcontact', [], ['network' => $network, 'url' => $urls]);
54 if (DBA::isResult($person)) {
55 Logger::debug('In cache', ['person' => $person]);
57 if (is_null($update)) {
58 // update record occasionally so it doesn't get stale
59 $d = strtotime($person["updated"]." +00:00");
60 if ($d < strtotime("now - 14 days")) {
64 if ($person["guid"] == "") {
68 } elseif (is_null($update)) {
69 $update = !DBA::isResult($person);
75 Logger::info('create or refresh', ['handle' => $handle]);
76 $r = Probe::uri($handle, $network);
78 // Note that Friendica contacts will return a "Diaspora person"
79 // if Diaspora connectivity is enabled on their server
80 if ($r && ($r["network"] === $network)) {
81 self::updateFContact($r);
83 $person = self::getByURL($handle, false, $network);
91 * Updates the fcontact table
93 * @param array $arr The fcontact data
96 private static function updateFContact($arr)
98 $fields = ['name' => $arr["name"], 'photo' => $arr["photo"],
99 'request' => $arr["request"], 'nick' => $arr["nick"],
100 'addr' => strtolower($arr["addr"]), 'guid' => $arr["guid"],
101 'batch' => $arr["batch"], 'notify' => $arr["notify"],
102 'poll' => $arr["poll"], 'confirm' => $arr["confirm"],
103 'alias' => $arr["alias"], 'pubkey' => $arr["pubkey"],
104 'updated' => DateTimeFormat::utcNow()];
106 $condition = ['url' => $arr["url"], 'network' => $arr["network"]];
108 DBA::update('fcontact', $fields, $condition, true);
112 * get a url (scheme://domain.tld/u/user) from a given Diaspora*
115 * @param mixed $fcontact_guid Hexadecimal string guid
117 * @return string the contact url or null
120 public static function getUrlByGuid($fcontact_guid)
122 Logger::info('fcontact', ['guid' => $fcontact_guid]);
125 "SELECT `url` FROM `fcontact` WHERE `url` != '' AND `network` = '%s' AND `guid` = '%s'",
126 DBA::escape(Protocol::DIASPORA),
127 DBA::escape($fcontact_guid)
130 if (DBA::isResult($r)) {
138 * Suggest a given contact to a given user from a given contact
140 * @param integer $uid
141 * @param integer $cid
142 * @param integer $from_cid
143 * @return bool Was the adding successful?
145 public static function addSuggestion(int $uid, int $cid, int $from_cid, string $note = '')
147 $owner = User::getOwnerDataById($uid);
148 $contact = Contact::getById($cid);
149 $from_contact = Contact::getById($from_cid);
151 if (DBA::exists('contact', ['nurl' => Strings::normaliseLink($contact['url']), 'uid' => $uid])) {
155 $fcontact = self::getByURL($contact['url'], null, $contact['network']);
156 if (empty($fcontact)) {
157 Logger::warning('FContact had not been found', ['fcontact' => $contact['url']]);
161 $fid = $fcontact['id'];
163 // Quit if we already have an introduction for this person
164 if (DBA::exists('intro', ['uid' => $uid, 'fid' => $fid])) {
169 $suggest['uid'] = $uid;
170 $suggest['cid'] = $from_cid;
171 $suggest['url'] = $contact['url'];
172 $suggest['name'] = $contact['name'];
173 $suggest['photo'] = $contact['photo'];
174 $suggest['request'] = $contact['request'];
175 $suggest['title'] = '';
176 $suggest['body'] = $note;
178 $hash = Strings::getRandomHex();
179 $fields = ['uid' => $suggest['uid'], 'fid' => $fid, 'contact-id' => $suggest['cid'],
180 'note' => $suggest['body'], 'hash' => $hash, 'datetime' => DateTimeFormat::utcNow(), 'blocked' => false];
181 DBA::insert('intro', $fields);
184 'type' => Type::SUGGEST,
185 'otype' => Notify\ObjectType::INTRO,
186 'verb' => Activity::REQ_FRIEND,
187 'uid' => $owner['uid'],
188 'cid' => $from_contact['uid'],
190 'link' => DI::baseUrl().'/notifications/intros',