Replace almost every Introduction places

This commit is contained in:
Philipp Holzer 2021-10-18 22:49:25 +02:00
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
*
@ -151,8 +188,7 @@ class Introduction extends BaseDepository
public function save(Entity\Introduction $introduction): Entity\Introduction
{
try {
$fields = $this->convertToTableRow($introduction);
$fields['datetime'] = DateTimeFormat::utcNow();
$fields = $this->convertToTableRow($introduction);
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 */
@ -65,23 +65,25 @@ class Introduction extends BaseEntity
protected $id;
/**
* @param int $uid
* @param int $fid
* @param int $cid
* @param bool $knowyou
* @param bool $duplex
* @param string $note
* @param string $hash
* @param bool $blocked
* @param bool $ignore
* @param int|null $id
* @param int $uid
* @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]);
}
}