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);
76 public function selectAllForUser(int $uid, array $params = []): Collection\Notifies
78 return $this->selectForUser($uid, [], $params);
81 public function setAllSeenForUser(int $uid, array $condition = []): bool
83 $condition = DBA::mergeConditions($condition, ['uid' => $uid]);
85 return $this->db->update(self::$table_name, ['seen' => true], $condition);
89 * @param Entity\Notify $Notify
90 * @return Entity\Notify
91 * @throws HTTPException\NotFoundException
92 * @throws HTTPException\InternalServerErrorException
93 * @throws Exception\NotificationCreationInterceptedException
95 public function save(Entity\Notify $Notify): Entity\Notify
98 'type' => $Notify->type,
99 'name' => $Notify->name,
100 'url' => $Notify->url,
101 'photo' => $Notify->photo,
102 'msg' => $Notify->msg,
103 'uid' => $Notify->uid,
104 'link' => $Notify->link,
105 'iid' => $Notify->itemId,
106 'parent' => $Notify->parent,
107 'seen' => $Notify->seen,
108 'verb' => $Notify->verb,
109 'otype' => $Notify->otype,
110 'name_cache' => $Notify->name_cache,
111 'msg_cache' => $Notify->msg_cache,
112 'uri-id' => $Notify->uriId,
113 'parent-uri-id' => $Notify->parentUriId,
117 $this->db->update(self::$table_name, $fields, ['id' => $Notify->id]);
119 $fields['date'] = DateTimeFormat::utcNow();
120 Hook::callAll('enotify_store', $fields);
122 $this->db->insert(self::$table_name, $fields);
124 $Notify = $this->selectOneById($this->db->lastInsertId());
130 public function setAllSeenForRelatedNotify(Entity\Notify $Notify): bool
133 '(`link` = ? OR (`parent` != 0 AND `parent` = ? AND `otype` = ?)) AND `uid` = ?',
139 return $this->db->update(self::$table_name, ['seen' => true], $condition);