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,
'hash' => $introduction->hash,
'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());
}
/**
* 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
*
@ -152,7 +189,6 @@ class Introduction extends BaseDepository
{
try {
$fields = $this->convertToTableRow($introduction);
$fields['datetime'] = DateTimeFormat::utcNow();
if ($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 $fid
* @property-read int $cid
* @property-read int $sid
* @property-read int|null $fid
* @property-read int|null $cid
* @property-read bool $knowyou
* @property-read bool $duplex
* @property-read string $note
@ -42,11 +42,11 @@ class Introduction extends BaseEntity
/** @var int */
protected $uid;
/** @var int */
protected $fid;
/** @var int */
protected $cid;
/** @var int */
protected $sid;
/** @var int|null */
protected $fid;
/** @var int|null */
protected $cid;
/** @var bool */
protected $knowyou;
/** @var bool */
@ -66,22 +66,24 @@ class Introduction extends BaseEntity
/**
* @param int $uid
* @param int $fid
* @param int $cid
* @param int $sid
* @param int|null $fid
* @param int|null $cid
* @param bool $knowyou
* @param bool $duplex
* @param string $note
* @param string $hash
* @param \DateTime $datetime
* @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->sid = $sid;
$this->fid = $fid;
$this->cid = $cid;
$this->sid = $sid;
$this->knowyou = $knowyou;
$this->duplex = $duplex;
$this->note = $note;

View File

@ -24,6 +24,8 @@ namespace Friendica\Contact\Introduction\Factory;
use Friendica\BaseFactory;
use Friendica\Contact\Introduction\Entity;
use Friendica\Capabilities\ICanCreateFromTableRow;
use Friendica\Util\DateTimeFormat;
use Friendica\Util\Strings;
class Introduction extends BaseFactory implements ICanCreateFromTableRow
{
@ -33,18 +35,47 @@ class Introduction extends BaseFactory implements ICanCreateFromTableRow
public function createFromTableRow(array $row): Entity\Introduction
{
return new Entity\Introduction(
$row['uid'],
$row['fid'],
$row['contact-id'],
$row['suggested-cid'],
$row['uid'] ?? 0,
$row['suggest-cid'] ?? 0,
$row['fid'] ?? null,
$row['contact-id'] ?? null,
!empty($row['knowyou']),
!empty($row['dupley']),
$row['note'],
$row['hash'],
new \DateTime($row['datetime'], new \DateTimeZone('UTC')),
!empty($row['duplex']),
$row['note'] ?? '',
$row['hash'] ?? '',
new \DateTime($row['datetime'] ?? 'now', new \DateTimeZone('UTC')),
!empty($row['blocked']),
!empty($row['ignore']),
$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 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
{
return self::$dice->create(Security\PermissionSet\Depository\PermissionSet::class);

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)) {
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

@ -133,7 +133,7 @@ class Delegation extends BaseModule
$params = ['distinct' => true, 'expression' => 'convid'];
$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;
}

View File

@ -1352,7 +1352,7 @@ class DFRN
}
// 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;
}
@ -1366,10 +1366,13 @@ class DFRN
$suggest['title'] = '';
$suggest['body'] = $note;
$hash = Strings::getRandomHex();
$fields = ['uid' => $suggest['uid'], 'suggest-cid' => $cid, 'contact-id' => $suggest['cid'],
'note' => $suggest['body'], 'hash' => $hash, 'datetime' => DateTimeFormat::utcNow(), 'blocked' => false];
DBA::insert('intro', $fields);
DI::intro()->save(DI::introFactory()->createNew(
$suggest['uid'],
$suggest['cid'],
$suggest['body'],
null,
$cid
));
DI::notify()->createFromArray([
'type' => Notification\Type::SUGGEST,

View File

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