3 namespace Friendica\Navigation\Notifications\Repository;
6 use Friendica\BaseCollection;
7 use Friendica\BaseRepository;
8 use Friendica\Database\Database;
9 use Friendica\Database\DBA;
10 use Friendica\Model\Verb;
11 use Friendica\Navigation\Notifications\Collection;
12 use Friendica\Navigation\Notifications\Entity;
13 use Friendica\Navigation\Notifications\Factory;
14 use Friendica\Network\HTTPException\NotFoundException;
15 use Friendica\Util\DateTimeFormat;
16 use Psr\Log\LoggerInterface;
18 class Notification extends BaseRepository
20 /** @var Factory\Notification */
23 protected static $table_name = 'notification';
25 public function __construct(Database $database, LoggerInterface $logger, Factory\Notification $factory = null)
27 parent::__construct($database, $logger, $factory ?? new Factory\Notification($logger));
31 * @param array $condition
32 * @param array $params
33 * @return Entity\Notification
34 * @throws NotFoundException
36 private function selectOne(array $condition, array $params = []): Entity\Notification
38 return parent::_selectOne($condition, $params);
41 private function select(array $condition, array $params = []): Collection\Notifications
43 return new Collection\Notifications(parent::_select($condition, $params)->getArrayCopy());
46 public function countForUser($uid, array $condition, array $params = []): int
48 $condition = DBA::mergeConditions($condition, ['uid' => $uid]);
50 return $this->count($condition, $params);
53 public function existsForUser($uid, array $condition): bool
55 $condition = DBA::mergeConditions($condition, ['uid' => $uid]);
57 return $this->exists($condition);
62 * @return Entity\Notification
63 * @throws NotFoundException
65 public function selectOneById(int $id): Entity\Notification
67 return $this->selectOne(['id' => $id]);
70 public function selectOneForUser(int $uid, array $condition, array $params = []): Entity\Notification
72 $condition = DBA::mergeConditions($condition, ['uid' => $uid]);
74 return $this->selectOne($condition, $params);
77 public function selectForUser(int $uid, array $condition = [], array $params = []): Collection\Notifications
79 $condition = DBA::mergeConditions($condition, ['uid' => $uid]);
81 return $this->select($condition, $params);
84 public function selectAllForUser(int $uid): Collection\Notifications
86 return $this->selectForUser($uid);
90 * @param array $condition
91 * @param array $params
92 * @param int|null $min_id Retrieve models with an id no fewer than this, as close to it as possible
93 * @param int|null $max_id Retrieve models with an id no greater than this, as close to it as possible
95 * @return BaseCollection
97 * @see _selectByBoundaries
99 public function selectByBoundaries(array $condition = [], array $params = [], int $min_id = null, int $max_id = null, int $limit = self::LIMIT)
101 $BaseCollection = parent::_selectByBoundaries($condition, $params, $min_id, $max_id, $limit);
103 return new Collection\Notifications($BaseCollection->getArrayCopy(), $BaseCollection->getTotalCount());
106 public function setAllSeenForUser(int $uid, array $condition = []): bool
108 $condition = DBA::mergeConditions($condition, ['uid' => $uid]);
110 return $this->db->update(self::$table_name, ['seen' => true], $condition);
113 public function setAllDismissedForUser(int $uid, array $condition = []): bool
115 $condition = DBA::mergeConditions($condition, ['uid' => $uid]);
117 return $this->db->update(self::$table_name, ['dismissed' => true], $condition);
121 * @param Entity\Notification $Notification
122 * @return Entity\Notification
125 public function save(Entity\Notification $Notification): Entity\Notification
128 'uid' => $Notification->uid,
129 'vid' => Verb::getID($Notification->verb),
130 'type' => $Notification->type,
131 'actor-id' => $Notification->actorId,
132 'target-uri-id' => $Notification->targetUriId,
133 'parent-uri-id' => $Notification->parentUriId,
134 'seen' => $Notification->seen,
135 'dismissed' => $Notification->dismissed,
138 if ($Notification->id) {
139 $this->db->update(self::$table_name, $fields, ['id' => $Notification->id]);
141 $fields['created'] = DateTimeFormat::utcNow();
142 $this->db->insert(self::$table_name, $fields);
144 $Notification = $this->selectOneById($this->db->lastInsertId());
147 return $Notification;