Merge remote-tracking branch 'upstream/develop' into logging

This commit is contained in:
Michael 2021-10-22 06:06:39 +00:00
commit a497bd3a3d
36 changed files with 1144 additions and 754 deletions

View file

@ -22,6 +22,7 @@
namespace Friendica\Model;
use Friendica\App\BaseURL;
use Friendica\Contact\Introduction\Exception\IntroductionNotFoundException;
use Friendica\Content\Pager;
use Friendica\Content\Text\HTML;
use Friendica\Core\Hook;
@ -1085,9 +1086,11 @@ class Contact
];
if (!empty($contact['pending'])) {
$intro = DBA::selectFirst('intro', ['id'], ['contact-id' => $contact['id']]);
if (DBA::isResult($intro)) {
$menu['follow'] = [DI::l10n()->t('Approve'), 'notifications/intros/' . $intro['id'], true];
try {
$intro = DI::intro()->selectForContact($contact['id']);
$menu['follow'] = [DI::l10n()->t('Approve'), 'notifications/intros/' . $intro->id, true];
} catch (IntroductionNotFoundException $exception) {
DI::logger()->error('Pending contact doesn\'t have an introduction.', ['exception' => $exception]);
}
}
}
@ -2706,12 +2709,13 @@ class Contact
$user = DBA::selectFirst('user', $fields, ['uid' => $importer['uid']]);
if (DBA::isResult($user) && !in_array($user['page-flags'], [User::PAGE_FLAGS_SOAPBOX, User::PAGE_FLAGS_FREELOVE, User::PAGE_FLAGS_COMMUNITY])) {
// create notification
$hash = Strings::getRandomHex();
if (is_array($contact_record)) {
DBA::insert('intro', ['uid' => $importer['uid'], 'contact-id' => $contact_record['id'],
'blocked' => false, 'knowyou' => false, 'note' => $note,
'hash' => $hash, 'datetime' => DateTimeFormat::utcNow()]);
$intro = DI::introFactory()->createNew(
$importer['uid'],
$contact_record['id'],
$note
);
DI::intro()->save($intro);
}
Group::addMember(User::getDefaultGroup($importer['uid'], $contact_record["network"]), $contact_record['id']);

View file

@ -19,64 +19,43 @@
*
*/
namespace Friendica\Model;
namespace Friendica\Model\Contact;
use Friendica\BaseModel;
use Friendica\Contact\Introduction\Entity;
use Friendica\Core\Protocol;
use Friendica\Database\Database;
use Friendica\DI;
use Friendica\Network\HTTPException;
use Friendica\Model\Contact;
use Friendica\Model\User;
use Friendica\Protocol\ActivityPub;
use Friendica\Protocol\Diaspora;
use Friendica\Repository;
use Friendica\Util\DateTimeFormat;
use Psr\Log\LoggerInterface;
/**
* @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
* @property bool ignore
*/
class Introduction extends BaseModel
class Introduction
{
/** @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;
}
/**
* Confirms a follow request and sends a notice to the remote contact.
*
* @param bool $duplex Is it a follow back?
* @param bool|null $hidden Should this contact be hidden? null = no change
* @return bool
* @param Entity\Introduction $introduction
* @param bool $duplex Is it a follow back?
* @param bool|null $hidden Should this contact be hidden? null = no change
*
* @throws HTTPException\InternalServerErrorException
* @throws HTTPException\NotFoundException
* @throws \ImagickException
*/
public function confirm(bool $duplex = false, bool $hidden = null)
public static function confirm(Entity\Introduction $introduction, bool $duplex = false, ?bool $hidden = null): void
{
$this->logger->info('Confirming follower', ['cid' => $this->{'contact-id'}]);
DI::logger()->info('Confirming follower', ['cid' => $introduction->cid]);
$contact = Contact::selectFirst([], ['id' => $this->{'contact-id'}, 'uid' => $this->uid]);
$contact = Contact::selectFirst([], ['id' => $introduction->cid, 'uid' => $introduction->uid]);
if (!$contact) {
throw new HTTPException\NotFoundException('Contact record not found.');
}
$newRelation = $contact['rel'];
$writable = $contact['writable'];
$writable = $contact['writable'];
if (!empty($contact['protocol'])) {
$protocol = $contact['protocol'];
@ -117,53 +96,24 @@ class Introduction extends BaseModel
if ($newRelation == Contact::FRIEND) {
if ($protocol == Protocol::DIASPORA) {
$ret = Diaspora::sendShare(User::getById($contact['uid']), $contact);
$this->logger->info('share returns', ['return' => $ret]);
DI::logger()->info('share returns', ['return' => $ret]);
} elseif ($protocol == Protocol::ACTIVITYPUB) {
ActivityPub\Transmitter::sendActivity('Follow', $contact['url'], $contact['uid']);
}
}
return $this->intro->delete($this);
}
/**
* Silently ignores the introduction, hides it from notifications and prevents the remote contact from submitting
* additional follow requests.
*
* @return bool
* @throws \Exception
*/
public function ignore()
{
$this->ignore = true;
return $this->intro->update($this);
}
/**
* Discards the introduction and sends a rejection message to AP contacts.
*
* @return bool
* @param Entity\Introduction $introduction
*
* @throws HTTPException\InternalServerErrorException
* @throws HTTPException\NotFoundException
* @throws \ImagickException
*/
public function discard()
public static function discard(Entity\Introduction $introduction): void
{
// 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,
'self' => false, 'pending' => true, 'rel' => [0, Contact::FOLLOWER]];
if ($this->dba->exists('contact', $condition)) {
Contact::remove($this->{'contact-id'});
} else {
Contact::update(['pending' => false], ['id' => $this->{'contact-id'}]);
}
}
$contact = Contact::selectFirst([], ['id' => $this->{'contact-id'}, 'uid' => $this->uid]);
$contact = Contact::selectFirst([], ['id' => $introduction->cid, 'uid' => $introduction->uid]);
if (!empty($contact)) {
if (!empty($contact['protocol'])) {
$protocol = $contact['protocol'];
@ -175,7 +125,5 @@ class Introduction extends BaseModel
ActivityPub\Transmitter::sendContactReject($contact['url'], $contact['hub-verify'], $contact['uid']);
}
}
return $this->intro->delete($this);
}
}