feat: separate read and done statuses
Signed-off-by: Heikki Hellgren <heikki.hellgren@op.fi>
This commit is contained in:
@@ -25,6 +25,7 @@ exports.up = async function up(knex) {
|
||||
table.text('image').nullable();
|
||||
table.datetime('created').notNullable();
|
||||
table.datetime('read').nullable();
|
||||
table.datetime('done').nullable();
|
||||
table.boolean('saved').defaultTo(false).notNullable();
|
||||
});
|
||||
};
|
||||
|
||||
@@ -60,12 +60,14 @@ export class DatabaseNotificationsStore implements NotificationsStore {
|
||||
options: NotificationGetOptions | NotificationModifyOptions,
|
||||
) => {
|
||||
const { user_ref, type } = options;
|
||||
const query = this.db('notifications').where('userRef', user_ref);
|
||||
const query = this.db('notifications')
|
||||
.where('userRef', user_ref)
|
||||
.orderBy('created', 'desc');
|
||||
|
||||
if (type === 'unread') {
|
||||
query.whereNull('read');
|
||||
} else if (type === 'read') {
|
||||
query.whereNotNull('read');
|
||||
if (type === 'undone') {
|
||||
query.whereNull('done');
|
||||
} else if (type === 'done') {
|
||||
query.whereNotNull('done');
|
||||
} else if (type === 'saved') {
|
||||
query.where('saved', true);
|
||||
}
|
||||
@@ -116,6 +118,16 @@ export class DatabaseNotificationsStore implements NotificationsStore {
|
||||
await notificationQuery.update({ read: null });
|
||||
}
|
||||
|
||||
async markDone(options: NotificationModifyOptions): Promise<void> {
|
||||
const notificationQuery = this.getNotificationsBaseQuery(options);
|
||||
await notificationQuery.update({ done: new Date(), read: new Date() });
|
||||
}
|
||||
|
||||
async markUndone(options: NotificationModifyOptions): Promise<void> {
|
||||
const notificationQuery = this.getNotificationsBaseQuery(options);
|
||||
await notificationQuery.update({ done: null, read: null });
|
||||
}
|
||||
|
||||
async markSaved(options: NotificationModifyOptions): Promise<void> {
|
||||
const notificationQuery = this.getNotificationsBaseQuery(options);
|
||||
await notificationQuery.update({ saved: true });
|
||||
|
||||
@@ -43,6 +43,10 @@ export interface NotificationsStore {
|
||||
|
||||
markUnread(options: NotificationModifyOptions): Promise<void>;
|
||||
|
||||
markDone(options: NotificationModifyOptions): Promise<void>;
|
||||
|
||||
markUndone(options: NotificationModifyOptions): Promise<void>;
|
||||
|
||||
markSaved(options: NotificationModifyOptions): Promise<void>;
|
||||
|
||||
markUnsaved(options: NotificationModifyOptions): Promise<void>;
|
||||
|
||||
@@ -168,10 +168,47 @@ export async function createRouter(
|
||||
|
||||
router.get('/status', async (req, res) => {
|
||||
const user = await getUser(req);
|
||||
const status = await store.getStatus({ user_ref: user });
|
||||
const status = await store.getStatus({ user_ref: user, type: 'undone' });
|
||||
res.send(status);
|
||||
});
|
||||
|
||||
router.post('/done', async (req, res) => {
|
||||
const user = await getUser(req);
|
||||
const { ids } = req.body;
|
||||
if (!ids || !Array.isArray(ids)) {
|
||||
res.status(400).send();
|
||||
return;
|
||||
}
|
||||
await store.markDone({ user_ref: user, ids });
|
||||
|
||||
if (signalService) {
|
||||
await signalService.publish({
|
||||
recipients: [user],
|
||||
message: { action: 'refresh' },
|
||||
channel: 'notifications',
|
||||
});
|
||||
}
|
||||
res.status(200).send({ ids });
|
||||
});
|
||||
|
||||
router.post('/undo', async (req, res) => {
|
||||
const user = await getUser(req);
|
||||
const { ids } = req.body;
|
||||
if (!ids || !Array.isArray(ids)) {
|
||||
res.status(400).send();
|
||||
return;
|
||||
}
|
||||
await store.markUndone({ user_ref: user, ids });
|
||||
if (signalService) {
|
||||
await signalService.publish({
|
||||
recipients: [user],
|
||||
message: { action: 'refresh' },
|
||||
channel: 'notifications',
|
||||
});
|
||||
}
|
||||
res.status(200).send({ ids });
|
||||
});
|
||||
|
||||
router.post('/read', async (req, res) => {
|
||||
const user = await getUser(req);
|
||||
const { ids } = req.body;
|
||||
|
||||
Reference in New Issue
Block a user