2019-12-11 09:32:21 +01:00
|
|
|
<?php
|
2020-02-09 15:45:36 +01:00
|
|
|
/**
|
|
|
|
* @copyright Copyright (C) 2020, Friendica
|
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
2019-12-11 09:32:21 +01:00
|
|
|
|
|
|
|
namespace Friendica\Model;
|
|
|
|
|
|
|
|
use Friendica\BaseModel;
|
|
|
|
use Friendica\Core\Protocol;
|
2020-01-05 23:50:33 +01:00
|
|
|
use Friendica\Database\Database;
|
2019-12-11 09:32:21 +01:00
|
|
|
use Friendica\Network\HTTPException;
|
|
|
|
use Friendica\Protocol\ActivityPub;
|
|
|
|
use Friendica\Protocol\Diaspora;
|
2020-01-05 23:50:33 +01:00
|
|
|
use Friendica\Repository;
|
2019-12-11 09:32:21 +01:00
|
|
|
use Friendica\Util\DateTimeFormat;
|
2020-01-05 23:50:33 +01:00
|
|
|
use Psr\Log\LoggerInterface;
|
2019-12-11 09:32:21 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @property int uid
|
|
|
|
* @property int fid
|
|
|
|
* @property int contact-id
|
|
|
|
* @property bool knowyou
|
|
|
|
* @property bool duplex
|
|
|
|
* @property string note
|
|
|
|
* @property string hash
|
|
|
|
* @property string datetime
|
|
|
|
* @property bool blocked
|
2020-01-12 16:55:47 +01:00
|
|
|
* @property bool ignore
|
2019-12-11 09:32:21 +01:00
|
|
|
*/
|
2020-01-29 22:27:55 +01:00
|
|
|
class Introduction extends BaseModel
|
2019-12-11 09:32:21 +01:00
|
|
|
{
|
2020-01-05 23:50:33 +01:00
|
|
|
/** @var Repository\Introduction */
|
|
|
|
protected $intro;
|
|
|
|
|
|
|
|
public function __construct(Database $dba, LoggerInterface $logger, Repository\Introduction $intro, array $data = [])
|
|
|
|
{
|
|
|
|
parent::__construct($dba, $logger, $data);
|
|
|
|
|
|
|
|
$this->intro = $intro;
|
|
|
|
}
|
2019-12-11 09:32:21 +01:00
|
|
|
|
|
|
|
/**
|
2020-01-05 23:50:33 +01:00
|
|
|
* Confirms a follow request and sends a notice to the remote contact.
|
2019-12-11 09:32:21 +01:00
|
|
|
*
|
2020-01-05 23:50:33 +01:00
|
|
|
* @param bool $duplex Is it a follow back?
|
|
|
|
* @param bool|null $hidden Should this contact be hidden? null = no change
|
|
|
|
* @return bool
|
2019-12-11 09:32:21 +01:00
|
|
|
* @throws HTTPException\InternalServerErrorException
|
|
|
|
* @throws HTTPException\NotFoundException
|
2020-01-05 23:50:33 +01:00
|
|
|
* @throws \ImagickException
|
2019-12-11 09:32:21 +01:00
|
|
|
*/
|
|
|
|
public function confirm(bool $duplex = false, bool $hidden = null)
|
|
|
|
{
|
|
|
|
$this->logger->info('Confirming follower', ['cid' => $this->{'contact-id'}]);
|
|
|
|
|
2020-01-08 22:46:02 +01:00
|
|
|
$contact = Contact::selectFirst([], ['id' => $this->{'contact-id'}, 'uid' => $this->uid]);
|
2019-12-11 09:32:21 +01:00
|
|
|
|
|
|
|
if (!$contact) {
|
|
|
|
throw new HTTPException\NotFoundException('Contact record not found.');
|
|
|
|
}
|
|
|
|
|
2020-01-05 23:50:33 +01:00
|
|
|
$newRelation = $contact['rel'];
|
2019-12-11 09:32:21 +01:00
|
|
|
$writable = $contact['writable'];
|
|
|
|
|
|
|
|
if (!empty($contact['protocol'])) {
|
|
|
|
$protocol = $contact['protocol'];
|
|
|
|
} else {
|
|
|
|
$protocol = $contact['network'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($protocol == Protocol::ACTIVITYPUB) {
|
|
|
|
ActivityPub\Transmitter::sendContactAccept($contact['url'], $contact['hub-verify'], $contact['uid']);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (in_array($protocol, [Protocol::DIASPORA, Protocol::ACTIVITYPUB])) {
|
|
|
|
if ($duplex) {
|
2020-01-08 22:46:02 +01:00
|
|
|
$newRelation = Contact::FRIEND;
|
2019-12-11 09:32:21 +01:00
|
|
|
} else {
|
2020-01-08 22:46:02 +01:00
|
|
|
$newRelation = Contact::FOLLOWER;
|
2019-12-11 09:32:21 +01:00
|
|
|
}
|
|
|
|
|
2020-01-08 22:46:02 +01:00
|
|
|
if ($newRelation != Contact::FOLLOWER) {
|
2019-12-11 09:32:21 +01:00
|
|
|
$writable = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$fields = [
|
|
|
|
'name-date' => DateTimeFormat::utcNow(),
|
|
|
|
'uri-date' => DateTimeFormat::utcNow(),
|
|
|
|
'blocked' => false,
|
|
|
|
'pending' => false,
|
|
|
|
'protocol' => $protocol,
|
|
|
|
'writable' => $writable,
|
|
|
|
'hidden' => $hidden ?? $contact['hidden'],
|
2020-01-05 23:50:33 +01:00
|
|
|
'rel' => $newRelation,
|
2019-12-11 09:32:21 +01:00
|
|
|
];
|
|
|
|
$this->dba->update('contact', $fields, ['id' => $contact['id']]);
|
|
|
|
|
|
|
|
array_merge($contact, $fields);
|
|
|
|
|
2020-01-08 22:46:02 +01:00
|
|
|
if ($newRelation == Contact::FRIEND) {
|
2019-12-11 09:32:21 +01:00
|
|
|
if ($protocol == Protocol::DIASPORA) {
|
2020-01-29 22:20:59 +01:00
|
|
|
$ret = Diaspora::sendShare(User::getById($contact['uid']), $contact);
|
2019-12-11 09:32:21 +01:00
|
|
|
$this->logger->info('share returns', ['return' => $ret]);
|
|
|
|
} elseif ($protocol == Protocol::ACTIVITYPUB) {
|
|
|
|
ActivityPub\Transmitter::sendActivity('Follow', $contact['url'], $contact['uid']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-05 23:50:33 +01:00
|
|
|
return $this->intro->delete($this);
|
2019-12-11 09:32:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Silently ignores the introduction, hides it from notifications and prevents the remote contact from submitting
|
|
|
|
* additional follow requests.
|
|
|
|
*
|
2020-01-05 23:50:33 +01:00
|
|
|
* @return bool
|
2019-12-11 09:32:21 +01:00
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
public function ignore()
|
|
|
|
{
|
2020-01-12 16:55:47 +01:00
|
|
|
$this->ignore = true;
|
2019-12-11 09:32:21 +01:00
|
|
|
|
2020-01-05 23:50:33 +01:00
|
|
|
return $this->intro->update($this);
|
2019-12-11 09:32:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Discards the introduction and sends a rejection message to AP contacts.
|
|
|
|
*
|
2020-01-05 23:50:33 +01:00
|
|
|
* @return bool
|
2019-12-11 09:32:21 +01:00
|
|
|
* @throws HTTPException\InternalServerErrorException
|
|
|
|
* @throws HTTPException\NotFoundException
|
|
|
|
* @throws \ImagickException
|
|
|
|
*/
|
|
|
|
public function discard()
|
|
|
|
{
|
|
|
|
// If it is a friend suggestion, the contact is not a new friend but an existing friend
|
|
|
|
// that should not be deleted.
|
|
|
|
if (!$this->fid) {
|
|
|
|
// When the contact entry had been created just for that intro, we want to get rid of it now
|
|
|
|
$condition = ['id' => $this->{'contact-id'}, 'uid' => $this->uid,
|
2020-01-08 22:46:02 +01:00
|
|
|
'self' => false, 'pending' => true, 'rel' => [0, Contact::FOLLOWER]];
|
2019-12-11 09:32:21 +01:00
|
|
|
if ($this->dba->exists('contact', $condition)) {
|
2020-01-08 22:46:02 +01:00
|
|
|
Contact::remove($this->{'contact-id'});
|
2019-12-11 09:32:21 +01:00
|
|
|
} else {
|
|
|
|
$this->dba->update('contact', ['pending' => false], ['id' => $this->{'contact-id'}]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-08 22:46:02 +01:00
|
|
|
$contact = Contact::selectFirst([], ['id' => $this->{'contact-id'}, 'uid' => $this->uid]);
|
2019-12-11 09:32:21 +01:00
|
|
|
|
|
|
|
if (!$contact) {
|
|
|
|
throw new HTTPException\NotFoundException('Contact record not found.');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($contact['protocol'])) {
|
|
|
|
$protocol = $contact['protocol'];
|
|
|
|
} else {
|
|
|
|
$protocol = $contact['network'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($protocol == Protocol::ACTIVITYPUB) {
|
|
|
|
ActivityPub\Transmitter::sendContactReject($contact['url'], $contact['hub-verify'], $contact['uid']);
|
|
|
|
}
|
|
|
|
|
2020-01-05 23:50:33 +01:00
|
|
|
return $this->intro->delete($this);
|
2019-12-11 09:32:21 +01:00
|
|
|
}
|
|
|
|
}
|