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)
48 $person = DBA::selectFirst('fcontact', [], ['network' => Protocol::DIASPORA, 'addr' => $handle]);
49 if (!DBA::isResult($person)) {
50 $urls = [$handle, str_replace('http://', 'https://', $handle), Strings::normaliseLink($handle)];
51 $person = DBA::selectFirst('fcontact', [], ['network' => Protocol::DIASPORA, '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, Protocol::DIASPORA);
78 // Note that Friendica contacts will return a "Diaspora person"
79 // if Diaspora connectivity is enabled on their server
80 if ($r && ($r["network"] === Protocol::DIASPORA)) {
81 self::updateFContact($r);
83 $person = self::getByURL($handle, false);
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 * Add suggestions for a given contact
140 * @param integer $uid
141 * @param integer $cid
142 * @return bool Was the adding successful?
144 public static function addSuggestion(int $uid, int $cid)
146 $owner = User::getOwnerDataById($uid);
147 $contact = Contact::getById($cid);
149 if (DBA::exists('contact', ['nurl' => Strings::normaliseLink($contact['url']), 'uid' => $uid])) {
154 $suggest['uid'] = $uid;
155 $suggest['cid'] = $contact['id'];
156 $suggest['url'] = $contact['url'];
157 $suggest['name'] = $contact['name'];
158 $suggest['photo'] = $contact['photo'];
159 $suggest['request'] = $contact['request'];
160 $suggest['title'] = '';
161 $suggest['body'] = '';
163 // Do we already have an fcontact record for this person?
165 $fcontact = DBA::selectFirst('fcontact', ['id'], ['url' => $suggest['url']]);
166 if (DBA::isResult($fcontact)) {
167 $fid = $fcontact['id'];
169 $fields = ['name' => $suggest['name'], 'photo' => $suggest['photo'], 'request' => $suggest['request']];
170 DBA::update('fcontact', $fields, ['id' => $fid]);
172 // Quit if we already have an introduction for this person
173 if (DBA::exists('intro', ['uid' => $suggest['uid'], 'fid' => $fid])) {
179 $fields = ['name' => $suggest['name'], 'url' => $suggest['url'],
180 'photo' => $suggest['photo'], 'request' => $suggest['request']];
181 DBA::insert('fcontact', $fields);
182 $fid = DBA::lastInsertId();
184 Logger::warning('FContact had not been created', ['fcontact' => $fields]);
189 $hash = Strings::getRandomHex();
190 $fields = ['uid' => $suggest['uid'], 'fid' => $fid, 'contact-id' => $suggest['cid'],
191 'note' => $suggest['body'], 'hash' => $hash, 'datetime' => DateTimeFormat::utcNow(), 'blocked' => false];
192 DBA::insert('intro', $fields);
195 'type' => Type::SUGGEST,
196 'notify_flags' => $owner['notify-flags'],
197 'language' => $owner['language'],
198 'to_name' => $owner['name'],
199 'to_email' => $owner['email'],
200 'uid' => $owner['uid'],
202 'link' => DI::baseUrl().'/notifications/intros',
203 'source_name' => $contact['name'],
204 'source_link' => $contact['url'],
205 'source_photo' => $contact['photo'],
206 'verb' => Activity::REQ_FRIEND,