-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into locked-template-tests
- Loading branch information
Showing
29 changed files
with
800 additions
and
210 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
forge/db/migrations/20240711-01-add-Notifications-table.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** | ||
* Add Notifications table | ||
*/ | ||
const { DataTypes } = require('sequelize') | ||
|
||
module.exports = { | ||
/** | ||
* upgrade database | ||
* @param {QueryInterface} context Sequelize.QueryInterface | ||
*/ | ||
up: async (context) => { | ||
await context.createTable('Notifications', { | ||
id: { | ||
type: DataTypes.INTEGER, | ||
primaryKey: true, | ||
autoIncrement: true, | ||
allowNull: false | ||
}, | ||
type: { type: DataTypes.STRING, allowNull: false }, | ||
reference: { type: DataTypes.STRING, allowNull: true }, | ||
read: { type: DataTypes.BOOLEAN, defaultValue: false }, | ||
data: { type: DataTypes.TEXT }, | ||
createdAt: { type: DataTypes.DATE, allowNull: false }, | ||
UserId: { | ||
type: DataTypes.INTEGER, | ||
references: { model: 'Users', key: 'id' }, | ||
onDelete: 'CASCADE', | ||
onUpdate: 'CASCADE' | ||
} | ||
}) | ||
}, | ||
down: async (context) => { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/** | ||
* An user notification | ||
*/ | ||
|
||
const { DataTypes, Op } = require('sequelize') | ||
|
||
const { buildPaginationSearchClause } = require('../utils') | ||
|
||
module.exports = { | ||
name: 'Notification', | ||
schema: { | ||
type: { type: DataTypes.STRING, allowNull: false }, | ||
reference: { type: DataTypes.STRING, allowNull: true }, | ||
read: { type: DataTypes.BOOLEAN, defaultValue: false }, | ||
data: { | ||
type: DataTypes.TEXT, | ||
get () { | ||
const rawValue = this.getDataValue('data') | ||
if (rawValue === undefined || rawValue === null) { | ||
return {} | ||
} | ||
return JSON.parse(rawValue) | ||
}, | ||
set (value) { | ||
this.setDataValue('data', JSON.stringify(value)) | ||
} | ||
} | ||
}, | ||
options: { | ||
updatedAt: false | ||
}, | ||
associations: function (M) { | ||
this.belongsTo(M.User, { | ||
onDelete: 'CASCADE' | ||
}) | ||
}, | ||
finders: function (M) { | ||
return { | ||
static: { | ||
byId: async (id, user) => { | ||
if (typeof id === 'string') { | ||
id = M.Notification.decodeHashid(id) | ||
} | ||
return this.findOne({ | ||
where: { | ||
id, | ||
UserId: user.id | ||
} | ||
}) | ||
}, | ||
byReference: async (reference, user) => { | ||
return this.findOne({ | ||
where: { | ||
reference, | ||
UserId: user.id | ||
} | ||
}) | ||
}, | ||
forUser: async (user, pagination = {}) => { | ||
const limit = parseInt(pagination.limit) || 100 | ||
const where = { | ||
UserId: user.id | ||
} | ||
if (pagination.cursor) { | ||
// As we aren't using the default cursor behaviour (Op.gt) | ||
// set the appropriate clause and delete cursor so that | ||
// buildPaginationSearchClause doesn't do it for us | ||
where.id = { [Op.lt]: M.Notification.decodeHashid(pagination.cursor) } | ||
delete pagination.cursor | ||
} | ||
const { count, rows } = await this.findAndCountAll({ | ||
where: buildPaginationSearchClause( | ||
pagination, | ||
where | ||
), | ||
order: [['id', 'DESC']], | ||
include: { | ||
model: M.User, | ||
attributes: ['id', 'hashid', 'username'] | ||
}, | ||
limit | ||
}) | ||
|
||
return { | ||
meta: { | ||
next_cursor: rows.length === limit ? rows[rows.length - 1].hashid : undefined | ||
}, | ||
count, | ||
notifications: rows | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
meta: { | ||
slug: false, | ||
links: false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
module.exports = function (app) { | ||
app.addSchema({ | ||
$id: 'Notification', | ||
type: 'object', | ||
properties: { | ||
id: { type: 'string' }, | ||
type: { type: 'string' }, | ||
createdAt: { type: 'string' }, | ||
read: { type: 'boolean' }, | ||
data: { type: 'object', additionalProperties: true } | ||
} | ||
}) | ||
app.addSchema({ | ||
$id: 'NotificationList', | ||
type: 'array', | ||
items: { | ||
$ref: 'Notification' | ||
} | ||
}) | ||
|
||
function notificationList (notifications) { | ||
return notifications.map(notification => { | ||
return { | ||
id: notification.hashid, | ||
type: notification.type, | ||
createdAt: notification.createdAt, | ||
read: notification.read, | ||
data: notification.data | ||
} | ||
}) | ||
} | ||
|
||
return { | ||
notificationList | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
const fp = require('fastify-plugin') | ||
|
||
module.exports = fp(async function (app, _opts) { | ||
/** | ||
* Send a user a notification | ||
* @param {User} user who the notification is for | ||
* @param {string} type the type of the notification | ||
* @param {Object} data meta data for the notification - specific to the type | ||
* @param {string} reference a key that can be used to lookup this notification, for example: `invite:HASHID` | ||
* | ||
*/ | ||
async function send (user, type, data, reference = null) { | ||
return app.db.models.Notification.create({ | ||
UserId: user.id, | ||
type, | ||
reference, | ||
data | ||
}) | ||
} | ||
|
||
/** | ||
* Remove a notification for a user with the given reference. | ||
* For example, when an invite is accepted/rejected, we can clear the associated notification | ||
* @param {User} user | ||
* @param {string} reference | ||
*/ | ||
async function remove (user, reference) { | ||
const notification = await app.db.models.Notification.byReference(reference, user) | ||
if (notification) { | ||
await notification.destroy() | ||
} | ||
} | ||
|
||
app.decorate('notifications', { | ||
send, | ||
remove | ||
}) | ||
}, { name: 'app.notifications' }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.