Compare commits

...

4 Commits

Author SHA1 Message Date
b1ek 99184b9568
add some methods to permission class 2023-03-05 19:01:03 +10:00
b1ek 45d276c5af
mode styles into a class 2023-03-05 19:00:39 +10:00
b1ek 04f961e25d
add announcement 2023-03-05 18:29:19 +10:00
b1ek 24598c2068
add debug mode warning 2023-03-05 17:41:32 +10:00
3 changed files with 97 additions and 7 deletions

View File

@ -1,17 +1,82 @@
const { Model, DataTypes } = require('sequelize');
const { sequelize } = require('.');
class Permission extends Model {
/** @type {{data: Permission[], updated: number, expired: function}} */
let cached = {
data: undefined,
updated: 0,
expired: () => {
return (Date.now() - updated) >= 1000
}
}
class Permission extends Model {
static async cache() {
if (!cached.expired()) return;
cached.data = await Permission.findAll();
cached.updated = Date.now();
}
/**
* Get value from cache
* @param {number} user
* @param {string} permission
* @returns {Permission?}
*/
static async findFromCache(user, permission) {
await this.cache();
for (const perm of cached.data) {
if (perm.user == userid && perm.permission == permission)
return perm;
}
return null;
}
/**
* Get a permission for user
* @param {number} user
* @param {string} permission
* @returns {boolean}
*/
static async forUser(user, permission) {
await this.cache();
const data = await this.findFromCache(user, permission);
if (!data) return false;
return data.value != 0;
}
/**
* Set a permission for user
* @param {number} user
* @param {string} permission
* @param {number} value
* @returns {Permission}
*/
static async set(user, permission, value) {
const existing = await Permission.findOne({where: {user, permission}});
let perm;
if (!existing) perm = await Permission.create({user, permission, value});
else perm = await Permission.update({user, permission, value}, {where: {user, permission}});
await this.cache();
return perm;
}
static async userAllowed(user, permission) {
return this.forUser(user, permission);
}
}
Permission.structure = {
// id: {
// type: DataTypes.BIGINT,
// primaryKey: true,
// autoIncrement: true,
// allowNull: false
// },
id: {
type: DataTypes.BIGINT,
primaryKey: true,
autoIncrement: true,
allowNull: false
},
user: {
type: DataTypes.BIGINT,
allowNull: false

1
public/announce.json Normal file
View File

@ -0,0 +1 @@
{"broadcast": true, "data": {"inline": "blek! Announcement is EOL and will be shut down at 2nd July 2023. Please update your app to the latest version to stop recieiving these notifications."}}

View File

@ -31,8 +31,32 @@ html(style='overflow-y:auto' lang='en_US')
//- UX
meta(name='viewport' content='width=device-width, initial-scale=1')
if (process.env.APP_DEBUG == 'true')
style.
.debug_warning {
margin:0;padding:0;
color:darkred;
font-weight:bold;
font-size:16pt;
position:fixed;
top:50%;
left:50%;
transform:translate(-50%,-50%);
text-align:center;
pointer-events:none;
opacity:0.4;
text-shadow:0 2px 1px #a07060a0
}
block head
body(style='overflow-y:auto')
if (process.env.APP_DEBUG == 'true')
p(class='debug_warning')
| Warning: THE WEBSITE IS IN DEBUG MODE!
br
| If the site is not currently running locally, please
br
| contact the maintainers and notify them of the issue.
table(width='100%' height='100%' class='body_table')
tr
td(class='side_menu')