3 namespace Friendica\Navigation\Notifications\Depository;
5 use Friendica\BaseDepository;
6 use Friendica\Core\Hook;
7 use Friendica\Database\Database;
8 use Friendica\Database\DBA;
9 use Friendica\Navigation\Notifications\Collection;
10 use Friendica\Navigation\Notifications\Entity;
11 use Friendica\Navigation\Notifications\Exception;
12 use Friendica\Navigation\Notifications\Factory;
13 use Friendica\Network\HTTPException;
14 use Friendica\Util\DateTimeFormat;
15 use Psr\Log\LoggerInterface;
17 class Notify extends BaseDepository
19 /** @var Factory\Notify */
22 protected static $table_name = 'notify';
24 public function __construct(Database $database, LoggerInterface $logger, Factory\Notify $factory = null)
26 parent::__construct($database, $logger, $factory ?? new Factory\Notify($logger));
30 * @param array $condition
31 * @param array $params
32 * @return Entity\Notify
33 * @throws HTTPException\NotFoundException
35 private function selectOne(array $condition, array $params = []): Entity\Notify
37 return parent::_selectOne($condition, $params);
40 private function select(array $condition, array $params = []): Collection\Notifies
42 return new Collection\Notifies(parent::_select($condition, $params)->getArrayCopy());
45 public function countForUser($uid, array $condition, array $params = []): int
47 $condition = DBA::mergeConditions($condition, ['uid' => $uid]);
49 return $this->count($condition, $params);
52 public function existsForUser($uid, array $condition): bool
54 $condition = DBA::mergeConditions($condition, ['uid' => $uid]);
56 return $this->exists($condition);
61 * @return Entity\Notify
62 * @throws HTTPException\NotFoundException
64 public function selectOneById(int $id): Entity\Notify
66 return $this->selectOne(['id' => $id]);
69 public function selectForUser(int $uid, array $condition, array $params): Collection\Notifies
71 $condition = DBA::mergeConditions($condition, ['uid' => $uid]);
73 return $this->select($condition, $params);
77 * Returns notifications for the user, unread first, ordered in descending chronological order.
81 * @return Collection\Notifies
83 public function selectAllForUser(int $uid, int $limit): Collection\Notifies
85 return $this->selectForUser($uid, [], ['order' => ['seen' => 'ASC', 'date' => 'DESC'], 'limit' => $limit]);
88 public function setAllSeenForUser(int $uid, array $condition = []): bool
90 $condition = DBA::mergeConditions($condition, ['uid' => $uid]);
92 return $this->db->update(self::$table_name, ['seen' => true], $condition);
96 * @param Entity\Notify $Notify
97 * @return Entity\Notify
98 * @throws HTTPException\NotFoundException
99 * @throws HTTPException\InternalServerErrorException
100 * @throws Exception\NotificationCreationInterceptedException
102 public function save(Entity\Notify $Notify): Entity\Notify
105 'type' => $Notify->type,
106 'name' => $Notify->name,
107 'url' => $Notify->url,
108 'photo' => $Notify->photo,
109 'msg' => $Notify->msg,
110 'uid' => $Notify->uid,
111 'link' => $Notify->link,
112 'iid' => $Notify->itemId,
113 'parent' => $Notify->parent,
114 'seen' => $Notify->seen,
115 'verb' => $Notify->verb,
116 'otype' => $Notify->otype,
117 'name_cache' => $Notify->name_cache,
118 'msg_cache' => $Notify->msg_cache,
119 'uri-id' => $Notify->uriId,
120 'parent-uri-id' => $Notify->parentUriId,
124 $this->db->update(self::$table_name, $fields, ['id' => $Notify->id]);
126 $fields['date'] = DateTimeFormat::utcNow();
127 Hook::callAll('enotify_store', $fields);
129 $this->db->insert(self::$table_name, $fields);
131 $Notify = $this->selectOneById($this->db->lastInsertId());
137 public function setAllSeenForRelatedNotify(Entity\Notify $Notify): bool
140 '(`link` = ? OR (`parent` != 0 AND `parent` = ? AND `otype` = ?)) AND `uid` = ?',
146 return $this->db->update(self::$table_name, ['seen' => true], $condition);