3 namespace Friendica\Model;
5 use Friendica\BaseModel;
6 use Friendica\Core\Protocol;
7 use Friendica\Network\HTTPException;
8 use Friendica\Protocol\ActivityPub;
9 use Friendica\Protocol\Diaspora;
10 use Friendica\Util\DateTimeFormat;
15 * @property int contact-id
16 * @property bool knowyou
17 * @property bool duplex
18 * @property string note
19 * @property string hash
20 * @property string datetime
21 * @property bool blocked
22 * @property bool ignored
24 * @package Friendica\Model
26 final class Introduction extends BaseModel
28 static $table_name = 'intro';
31 * Confirms a follow request and sends a notic to the remote contact.
33 * @param bool $duplex Is it a follow back?
34 * @param bool|null $hidden Should this contact be hidden? null = no change
35 * @throws HTTPException\InternalServerErrorException
36 * @throws \ImagickException
37 * @throws HTTPException\NotFoundException
39 public function confirm(bool $duplex = false, bool $hidden = null)
41 $this->logger->info('Confirming follower', ['cid' => $this->{'contact-id'}]);
43 $contact = Contact::selectFirst([], ['id' => $this->{'contact-id'}, 'uid' => $this->uid]);
46 throw new HTTPException\NotFoundException('Contact record not found.');
49 $new_relation = $contact['rel'];
50 $writable = $contact['writable'];
52 if (!empty($contact['protocol'])) {
53 $protocol = $contact['protocol'];
55 $protocol = $contact['network'];
58 if ($protocol == Protocol::ACTIVITYPUB) {
59 ActivityPub\Transmitter::sendContactAccept($contact['url'], $contact['hub-verify'], $contact['uid']);
62 if (in_array($protocol, [Protocol::DIASPORA, Protocol::ACTIVITYPUB])) {
64 $new_relation = Contact::FRIEND;
66 $new_relation = Contact::FOLLOWER;
69 if ($new_relation != Contact::FOLLOWER) {
75 'name-date' => DateTimeFormat::utcNow(),
76 'uri-date' => DateTimeFormat::utcNow(),
79 'protocol' => $protocol,
80 'writable' => $writable,
81 'hidden' => $hidden ?? $contact['hidden'],
82 'rel' => $new_relation,
84 $this->dba->update('contact', $fields, ['id' => $contact['id']]);
86 array_merge($contact, $fields);
88 if ($new_relation == Contact::FRIEND) {
89 if ($protocol == Protocol::DIASPORA) {
90 $ret = Diaspora::sendShare(User::getById($contact['uid']), $contact);
91 $this->logger->info('share returns', ['return' => $ret]);
92 } elseif ($protocol == Protocol::ACTIVITYPUB) {
93 ActivityPub\Transmitter::sendActivity('Follow', $contact['url'], $contact['uid']);
101 * Silently ignores the introduction, hides it from notifications and prevents the remote contact from submitting
102 * additional follow requests.
106 * @return Introduction
109 public function ignore()
111 $this->dba->update('intro', ['ignore' => true], ['id' => $this->id]);
117 * Discards the introduction and sends a rejection message to AP contacts.
119 * @throws HTTPException\InternalServerErrorException
120 * @throws HTTPException\NotFoundException
121 * @throws \ImagickException
123 public function discard()
125 // If it is a friend suggestion, the contact is not a new friend but an existing friend
126 // that should not be deleted.
128 // When the contact entry had been created just for that intro, we want to get rid of it now
129 $condition = ['id' => $this->{'contact-id'}, 'uid' => $this->uid,
130 'self' => false, 'pending' => true, 'rel' => [0, Contact::FOLLOWER]];
131 if ($this->dba->exists('contact', $condition)) {
132 Contact::remove($this->{'contact-id'});
134 $this->dba->update('contact', ['pending' => false], ['id' => $this->{'contact-id'}]);
138 $contact = Contact::selectFirst([], ['id' => $this->{'contact-id'}, 'uid' => $this->uid]);
141 throw new HTTPException\NotFoundException('Contact record not found.');
144 if (!empty($contact['protocol'])) {
145 $protocol = $contact['protocol'];
147 $protocol = $contact['network'];
150 if ($protocol == Protocol::ACTIVITYPUB) {
151 ActivityPub\Transmitter::sendContactReject($contact['url'], $contact['hub-verify'], $contact['uid']);