Add support for login intercept messages and announcements
This commit is contained in:
39
app/models/LoginMessage.model.js
Normal file
39
app/models/LoginMessage.model.js
Normal file
@@ -0,0 +1,39 @@
|
||||
const { Model } = require('flitter-orm')
|
||||
|
||||
class LoginMessageModel extends Model {
|
||||
static get schema() {
|
||||
return {
|
||||
seen: {type: Boolean, default: false},
|
||||
expires: {type: Date, default: () => {
|
||||
const date = new Date
|
||||
date.setYear(date.getUTCFullYear() + 1)
|
||||
return date
|
||||
}},
|
||||
title: String,
|
||||
message: String,
|
||||
user_id: String,
|
||||
}
|
||||
}
|
||||
|
||||
static async for_user(user) {
|
||||
return this.find({ user_id: user.id, seen: false, expires: { $gt: new Date } })
|
||||
}
|
||||
|
||||
static async create(user, title, message) {
|
||||
const msg = new this({
|
||||
message,
|
||||
title,
|
||||
user_id: user.id,
|
||||
})
|
||||
|
||||
await msg.save()
|
||||
return msg
|
||||
}
|
||||
|
||||
async mark_seen() {
|
||||
this.seen = true
|
||||
return this.save()
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = exports = LoginMessageModel
|
||||
@@ -66,6 +66,8 @@ class AnnouncementModel extends Model {
|
||||
await this.populate_emails()
|
||||
} else if ( this.type === 'banner' ) {
|
||||
await this.populate_banners()
|
||||
} else if ( this.type === 'login' ) {
|
||||
await this.populate_logins()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -92,6 +94,14 @@ class AnnouncementModel extends Model {
|
||||
await Message.create(user, `${this.title} - ${this.message}`)
|
||||
}
|
||||
}
|
||||
|
||||
async populate_logins() {
|
||||
const users = await this.all_users()
|
||||
const LoginMessage = this.models.get('LoginMessage')
|
||||
for ( const user of users ) {
|
||||
await LoginMessage.create(user, this.title, this.message)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = exports = AnnouncementModel
|
||||
|
||||
Reference in New Issue
Block a user