Replace almost every Introduction places

This commit is contained in:
Philipp Holzer 2021-10-18 22:49:25 +02:00
parent a40f503fdd
commit 7d7d310cc4
Signed by: nupplaPhil
GPG Key ID: 24A7501396EB5432
8 changed files with 128 additions and 43 deletions

View File

@ -76,7 +76,8 @@ class Introduction extends BaseDepository
'note' => $introduction->note, 'note' => $introduction->note,
'hash' => $introduction->hash, 'hash' => $introduction->hash,
'blocked' => $introduction->blocked ? 1 : 0, 'blocked' => $introduction->blocked ? 1 : 0,
'ignore' => $introduction->ignore ? 1 : 0 'ignore' => $introduction->ignore ? 1 : 0,
'datetime' => $introduction->datetime->format(DateTimeFormat::MYSQL),
]; ];
} }
@ -121,6 +122,42 @@ class Introduction extends BaseDepository
return new Collection\Introductions($BaseCollection->getArrayCopy(), $BaseCollection->getTotalCount()); return new Collection\Introductions($BaseCollection->getArrayCopy(), $BaseCollection->getTotalCount());
} }
/**
* Selects the introduction for a given contact
*
* @param int $cid
*
* @return Entity\Introduction
*
* @throws IntroductionNotFoundException in case there is not Introduction for this contact
*/
public function selectForContact(int $cid): Entity\Introduction
{
try {
return $this->selectOne(['contact-id' => $cid]);
} catch (NotFoundException $exception) {
throw new IntroductionNotFoundException(sprintf('There is no Introduction for the contact %d', $cid), $exception);
}
}
public function countActiveForUser($uid, array $params = []): int
{
try {
return $this->count(['blocked' => false, 'ignore' => false, 'uid' => $uid], $params);
} catch (\Exception $e) {
throw new IntroductionPersistenceException(sprintf('Cannot count Introductions for used %d', $uid), $e);
}
}
public function existsForContact(int $cid, int $uid): bool
{
try {
return $this->exists(['uid' => $uid, 'suggest-cid' => $cid]);
} catch (\Exception $e) {
throw new IntroductionPersistenceException(sprintf('Cannot check Introductions for contact %d and user %d', $cid, $uid), $e);
}
}
/** /**
* @param Entity\Introduction $introduction * @param Entity\Introduction $introduction
* *
@ -151,8 +188,7 @@ class Introduction extends BaseDepository
public function save(Entity\Introduction $introduction): Entity\Introduction public function save(Entity\Introduction $introduction): Entity\Introduction
{ {
try { try {
$fields = $this->convertToTableRow($introduction); $fields = $this->convertToTableRow($introduction);
$fields['datetime'] = DateTimeFormat::utcNow();
if ($introduction->id) { if ($introduction->id) {
$this->db->update(self::$table_name, $fields, ['id' => $introduction->id]); $this->db->update(self::$table_name, $fields, ['id' => $introduction->id]);

View File

@ -25,9 +25,9 @@ use Friendica\BaseEntity;
/** /**
* @property-read int $uid * @property-read int $uid
* @property-read int $fid
* @property-read int $cid
* @property-read int $sid * @property-read int $sid
* @property-read int|null $fid
* @property-read int|null $cid
* @property-read bool $knowyou * @property-read bool $knowyou
* @property-read bool $duplex * @property-read bool $duplex
* @property-read string $note * @property-read string $note
@ -42,11 +42,11 @@ class Introduction extends BaseEntity
/** @var int */ /** @var int */
protected $uid; protected $uid;
/** @var int */ /** @var int */
protected $fid;
/** @var int */
protected $cid;
/** @var int */
protected $sid; protected $sid;
/** @var int|null */
protected $fid;
/** @var int|null */
protected $cid;
/** @var bool */ /** @var bool */
protected $knowyou; protected $knowyou;
/** @var bool */ /** @var bool */
@ -65,23 +65,25 @@ class Introduction extends BaseEntity
protected $id; protected $id;
/** /**
* @param int $uid * @param int $uid
* @param int $fid * @param int $sid
* @param int $cid * @param int|null $fid
* @param bool $knowyou * @param int|null $cid
* @param bool $duplex * @param bool $knowyou
* @param string $note * @param bool $duplex
* @param string $hash * @param string $note
* @param bool $blocked * @param string $hash
* @param bool $ignore * @param \DateTime $datetime
* @param int|null $id * @param bool $blocked
* @param bool $ignore
* @param int|null $id
*/ */
public function __construct(int $uid, int $fid, int $cid, int $sid, bool $knowyou, bool $duplex, string $note, string $hash, \DateTime $datetime, bool $blocked, bool $ignore, ?int $id) public function __construct(int $uid, int $sid, ?int $fid, ?int $cid, bool $knowyou, bool $duplex, string $note, string $hash, \DateTime $datetime, bool $blocked, bool $ignore, ?int $id)
{ {
$this->uid = $uid; $this->uid = $uid;
$this->sid = $sid;
$this->fid = $fid; $this->fid = $fid;
$this->cid = $cid; $this->cid = $cid;
$this->sid = $sid;
$this->knowyou = $knowyou; $this->knowyou = $knowyou;
$this->duplex = $duplex; $this->duplex = $duplex;
$this->note = $note; $this->note = $note;

View File

@ -24,6 +24,8 @@ namespace Friendica\Contact\Introduction\Factory;
use Friendica\BaseFactory; use Friendica\BaseFactory;
use Friendica\Contact\Introduction\Entity; use Friendica\Contact\Introduction\Entity;
use Friendica\Capabilities\ICanCreateFromTableRow; use Friendica\Capabilities\ICanCreateFromTableRow;
use Friendica\Util\DateTimeFormat;
use Friendica\Util\Strings;
class Introduction extends BaseFactory implements ICanCreateFromTableRow class Introduction extends BaseFactory implements ICanCreateFromTableRow
{ {
@ -33,18 +35,47 @@ class Introduction extends BaseFactory implements ICanCreateFromTableRow
public function createFromTableRow(array $row): Entity\Introduction public function createFromTableRow(array $row): Entity\Introduction
{ {
return new Entity\Introduction( return new Entity\Introduction(
$row['uid'], $row['uid'] ?? 0,
$row['fid'], $row['suggest-cid'] ?? 0,
$row['contact-id'], $row['fid'] ?? null,
$row['suggested-cid'], $row['contact-id'] ?? null,
!empty($row['knowyou']), !empty($row['knowyou']),
!empty($row['dupley']), !empty($row['duplex']),
$row['note'], $row['note'] ?? '',
$row['hash'], $row['hash'] ?? '',
new \DateTime($row['datetime'], new \DateTimeZone('UTC')), new \DateTime($row['datetime'] ?? 'now', new \DateTimeZone('UTC')),
!empty($row['blocked']), !empty($row['blocked']),
!empty($row['ignore']), !empty($row['ignore']),
$row['id'] ?? null $row['id'] ?? null
); );
} }
public function createNew(
int $uid,
int $cid,
string $note,
int $fid = null,
int $sid = null,
bool $knowyou = false,
bool $duplex = false
): Entity\Introduction {
return $this->createFromTableRow([
'uid' => $uid,
'fid' => $fid,
'contact-id' => $cid,
'suggest-cid' => $sid,
'knowyou' => $knowyou,
'duplex' => $duplex,
'note' => $note,
'hash' => Strings::getRandomHex(),
'datetime' => DateTimeFormat::utcNow(),
'blocked' => false,
'ignore' => false,
]);
}
public function createDummy(int $id): Entity\Introduction
{
return $this->createFromTableRow(['id' => $id]);
}
} }

View File

@ -442,6 +442,14 @@ abstract class DI
return self::$dice->create(Contact\Introduction\Depository\Introduction::class); return self::$dice->create(Contact\Introduction\Depository\Introduction::class);
} }
/**
* @return Contact\Introduction\Factory\Introduction
*/
public static function introFactory()
{
return self::$dice->create(Contact\Introduction\Factory\Introduction::class);
}
public static function permissionSet(): Security\PermissionSet\Depository\PermissionSet public static function permissionSet(): Security\PermissionSet\Depository\PermissionSet
{ {
return self::$dice->create(Security\PermissionSet\Depository\PermissionSet::class); return self::$dice->create(Security\PermissionSet\Depository\PermissionSet::class);

View File

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

View File

@ -133,7 +133,7 @@ class Delegation extends BaseModule
$params = ['distinct' => true, 'expression' => 'convid']; $params = ['distinct' => true, 'expression' => 'convid'];
$notifications += DBA::count('mail', ['uid' => $identity['uid'], 'seen' => false], $params); $notifications += DBA::count('mail', ['uid' => $identity['uid'], 'seen' => false], $params);
$notifications += DBA::count('intro', ['blocked' => false, 'ignore' => false, 'uid' => $identity['uid']]); $notifications += DI::intro()->countActiveForUser($identity['uid']);
$identities[$key]['notifications'] = $notifications; $identities[$key]['notifications'] = $notifications;
} }

View File

@ -1352,7 +1352,7 @@ class DFRN
} }
// Quit if we already have an introduction for this person // Quit if we already have an introduction for this person
if (DBA::exists('intro', ['uid' => $uid, 'suggest-cid' => $cid])) { if (DI::intro()->existsForContact($cid, $uid)) {
return false; return false;
} }
@ -1366,10 +1366,13 @@ class DFRN
$suggest['title'] = ''; $suggest['title'] = '';
$suggest['body'] = $note; $suggest['body'] = $note;
$hash = Strings::getRandomHex(); DI::intro()->save(DI::introFactory()->createNew(
$fields = ['uid' => $suggest['uid'], 'suggest-cid' => $cid, 'contact-id' => $suggest['cid'], $suggest['uid'],
'note' => $suggest['body'], 'hash' => $hash, 'datetime' => DateTimeFormat::utcNow(), 'blocked' => false]; $suggest['cid'],
DBA::insert('intro', $fields); $suggest['body'],
null,
$cid
));
DI::notify()->createFromArray([ DI::notify()->createFromArray([
'type' => Notification\Type::SUGGEST, 'type' => Notification\Type::SUGGEST,

View File

@ -24,6 +24,7 @@ namespace Friendica\Worker\Contact;
use Friendica\Core\Logger; use Friendica\Core\Logger;
use Friendica\Database\DBA; use Friendica\Database\DBA;
use Friendica\Database\DBStructure; use Friendica\Database\DBStructure;
use Friendica\DI;
use Friendica\Model\Photo; use Friendica\Model\Photo;
use Friendica\Model\Post; use Friendica\Model\Post;
@ -84,7 +85,7 @@ class RemoveContent
DBA::delete('user-contact', ['cid' => $id]); DBA::delete('user-contact', ['cid' => $id]);
DBA::delete('group_member', ['contact-id' => $id]); DBA::delete('group_member', ['contact-id' => $id]);
DBA::delete('intro', ['contact-id' => $id]); DI::intro()->delete(DI::introFactory()->createDummy($id));
return $contact; return $contact;
} }