mirror of
https://codeberg.org/timelimit/timelimit-server.git
synced 2025-10-03 09:49:32 +02:00
Add new status message system
This commit is contained in:
parent
95160b0558
commit
17058c375c
9 changed files with 119 additions and 6 deletions
|
@ -57,9 +57,6 @@ This fixes the causes of lint warnings (where possible).
|
||||||
- MAIL_IMPRINT
|
- MAIL_IMPRINT
|
||||||
- a string which is added to the footer of the sent mails
|
- a string which is added to the footer of the sent mails
|
||||||
- default value: ``not defined``
|
- default value: ``not defined``
|
||||||
- STATUS_MESSAGE
|
|
||||||
- a message which is shown to all users in the overview screen
|
|
||||||
- default: null/ no shown message
|
|
||||||
- ADMIN_TOKEN
|
- ADMIN_TOKEN
|
||||||
- a password which allows to use some APIs
|
- a password which allows to use some APIs
|
||||||
- admin APIs are disabled when this is not set
|
- admin APIs are disabled when this is not set
|
||||||
|
|
6
package-lock.json
generated
6
package-lock.json
generated
|
@ -109,6 +109,12 @@
|
||||||
"integrity": "sha512-4CRCtDogTManXlOcj9ixIcxoHTKcaxpqaJWuMN/Zw9tYL1nqVkI64IWcx+CQ9XT/JnpP12buionHQqgrHcH34Q==",
|
"integrity": "sha512-4CRCtDogTManXlOcj9ixIcxoHTKcaxpqaJWuMN/Zw9tYL1nqVkI64IWcx+CQ9XT/JnpP12buionHQqgrHcH34Q==",
|
||||||
"dev": true
|
"dev": true
|
||||||
},
|
},
|
||||||
|
"@types/escape-html": {
|
||||||
|
"version": "0.0.20",
|
||||||
|
"resolved": "https://registry.npmjs.org/@types/escape-html/-/escape-html-0.0.20.tgz",
|
||||||
|
"integrity": "sha512-6dhZJLbA7aOwkYB2GDGdIqJ20wmHnkDzaxV9PJXe7O02I2dSFTERzRB6JrX6cWKaS+VqhhY7cQUMCbO5kloFUw==",
|
||||||
|
"dev": true
|
||||||
|
},
|
||||||
"@types/events": {
|
"@types/events": {
|
||||||
"version": "1.2.0",
|
"version": "1.2.0",
|
||||||
"resolved": "https://registry.npmjs.org/@types/events/-/events-1.2.0.tgz",
|
"resolved": "https://registry.npmjs.org/@types/events/-/events-1.2.0.tgz",
|
||||||
|
|
|
@ -27,6 +27,7 @@
|
||||||
"@types/basic-auth": "^1.1.2",
|
"@types/basic-auth": "^1.1.2",
|
||||||
"@types/body-parser": "^1.17.0",
|
"@types/body-parser": "^1.17.0",
|
||||||
"@types/email-templates": "^3.5.0",
|
"@types/email-templates": "^3.5.0",
|
||||||
|
"@types/escape-html": "0.0.20",
|
||||||
"@types/express": "^4.16.0",
|
"@types/express": "^4.16.0",
|
||||||
"@types/http-errors": "^1.6.1",
|
"@types/http-errors": "^1.6.1",
|
||||||
"@types/lodash": "^4.14.116",
|
"@types/lodash": "^4.14.116",
|
||||||
|
@ -46,6 +47,7 @@
|
||||||
"body-parser": "^1.18.3",
|
"body-parser": "^1.18.3",
|
||||||
"ejs": "^2.6.1",
|
"ejs": "^2.6.1",
|
||||||
"email-templates": "^5.0.4",
|
"email-templates": "^5.0.4",
|
||||||
|
"escape-html": "^1.0.3",
|
||||||
"express": "^4.16.3",
|
"express": "^4.16.3",
|
||||||
"google-auth-library": "^4.2.2",
|
"google-auth-library": "^4.2.2",
|
||||||
"http-errors": "^1.7.0",
|
"http-errors": "^1.7.0",
|
||||||
|
|
|
@ -16,9 +16,15 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { Router } from 'express'
|
import { Router } from 'express'
|
||||||
|
import { Database } from '../database'
|
||||||
import { WebsocketApi } from '../websocket'
|
import { WebsocketApi } from '../websocket'
|
||||||
|
import { setStatusMessage, getStatusMessage } from '../function/statusmessage'
|
||||||
|
import * as escape from 'escape-html'
|
||||||
|
import { json } from 'body-parser'
|
||||||
|
import { BadRequest } from 'http-errors'
|
||||||
|
|
||||||
export const createAdminRouter = ({ websocket }: {
|
export const createAdminRouter = ({ database, websocket }: {
|
||||||
|
database: Database
|
||||||
websocket: WebsocketApi
|
websocket: WebsocketApi
|
||||||
}) => {
|
}) => {
|
||||||
const router = Router()
|
const router = Router()
|
||||||
|
@ -29,5 +35,33 @@ export const createAdminRouter = ({ websocket }: {
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
router.get('/status-message', async (_, res, next) => {
|
||||||
|
try {
|
||||||
|
const currentStatusMessage = await getStatusMessage({ database })
|
||||||
|
|
||||||
|
res.send('<html><body><form action="/admin/status-message" method="post"><textarea>' + escape(currentStatusMessage) + '</textarea><input type="submit" value="Save"></form></body></html>')
|
||||||
|
} catch (ex) {
|
||||||
|
next(ex)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
router.post('/status-message', json(), async (req, res, next) => {
|
||||||
|
try {
|
||||||
|
if (typeof req.body !== 'object' || typeof req.body.smessage !== 'string') {
|
||||||
|
throw new BadRequest()
|
||||||
|
}
|
||||||
|
|
||||||
|
const newStatusMessage = req.body.smessage as string
|
||||||
|
|
||||||
|
await setStatusMessage({ database, newStatusMessage })
|
||||||
|
|
||||||
|
websocket.triggerImportantSyncAtAllDevicesInBackground()
|
||||||
|
|
||||||
|
res.json({ok: true})
|
||||||
|
} catch (ex) {
|
||||||
|
next(ex)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
return router
|
return router
|
||||||
}
|
}
|
||||||
|
|
|
@ -62,7 +62,7 @@ export const createApi = ({ database, websocket, connectedDevicesManager }: {
|
||||||
res.sendStatus(401)
|
res.sendStatus(401)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
createAdminRouter({ websocket })
|
createAdminRouter({ database, websocket })
|
||||||
)
|
)
|
||||||
|
|
||||||
return app
|
return app
|
||||||
|
|
|
@ -22,6 +22,7 @@ import { AppActivityModel, createAppActivityModel } from './appactivity'
|
||||||
import { AuthTokenModel, createAuthtokenModel } from './authtoken'
|
import { AuthTokenModel, createAuthtokenModel } from './authtoken'
|
||||||
import { CategoryModel, createCategoryModel } from './category'
|
import { CategoryModel, createCategoryModel } from './category'
|
||||||
import { CategoryAppModel, createCategoryAppModel } from './categoryapp'
|
import { CategoryAppModel, createCategoryAppModel } from './categoryapp'
|
||||||
|
import { ConfigModel, createConfigModel } from './config'
|
||||||
import { createDeviceModel, DeviceModel } from './device'
|
import { createDeviceModel, DeviceModel } from './device'
|
||||||
import { createFamilyModel, FamilyModel } from './family'
|
import { createFamilyModel, FamilyModel } from './family'
|
||||||
import { createMailLoginTokenModel, MailLoginTokenModel } from './maillogintoken'
|
import { createMailLoginTokenModel, MailLoginTokenModel } from './maillogintoken'
|
||||||
|
@ -39,6 +40,7 @@ export interface Database {
|
||||||
appActivity: AppActivityModel
|
appActivity: AppActivityModel
|
||||||
category: CategoryModel
|
category: CategoryModel
|
||||||
categoryApp: CategoryAppModel
|
categoryApp: CategoryAppModel
|
||||||
|
config: ConfigModel
|
||||||
device: DeviceModel
|
device: DeviceModel
|
||||||
family: FamilyModel
|
family: FamilyModel
|
||||||
mailLoginToken: MailLoginTokenModel
|
mailLoginToken: MailLoginTokenModel
|
||||||
|
@ -57,6 +59,7 @@ const createDatabase = (sequelize: Sequelize.Sequelize): Database => ({
|
||||||
appActivity: createAppActivityModel(sequelize),
|
appActivity: createAppActivityModel(sequelize),
|
||||||
category: createCategoryModel(sequelize),
|
category: createCategoryModel(sequelize),
|
||||||
categoryApp: createCategoryAppModel(sequelize),
|
categoryApp: createCategoryAppModel(sequelize),
|
||||||
|
config: createConfigModel(sequelize),
|
||||||
device: createDeviceModel(sequelize),
|
device: createDeviceModel(sequelize),
|
||||||
family: createFamilyModel(sequelize),
|
family: createFamilyModel(sequelize),
|
||||||
mailLoginToken: createMailLoginTokenModel(sequelize),
|
mailLoginToken: createMailLoginTokenModel(sequelize),
|
||||||
|
|
48
src/function/statusmessage/index.ts
Normal file
48
src/function/statusmessage/index.ts
Normal file
|
@ -0,0 +1,48 @@
|
||||||
|
/*
|
||||||
|
* server component for the TimeLimit App
|
||||||
|
* Copyright (C) 2019 Jonas Lochmann
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Affero General Public License as
|
||||||
|
* published by the Free Software Foundation, version 3 of the License.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU Affero General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Affero General Public License
|
||||||
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { Database } from '../../database'
|
||||||
|
import { configItemIds } from '../../database/config'
|
||||||
|
import * as Sequelize from 'sequelize'
|
||||||
|
|
||||||
|
export const getStatusMessage = async ({ database, transaction }: {
|
||||||
|
database: Database
|
||||||
|
transaction?: Sequelize.Transaction
|
||||||
|
}) => {
|
||||||
|
const currentStatusMessageItem = await database.config.findById(configItemIds.statusMessage, { transaction })
|
||||||
|
const currentStatusMessage = (currentStatusMessageItem ? currentStatusMessageItem.value : null) || ''
|
||||||
|
|
||||||
|
return currentStatusMessage
|
||||||
|
}
|
||||||
|
|
||||||
|
export const setStatusMessage = async ({ database, newStatusMessage }: {
|
||||||
|
database: Database
|
||||||
|
newStatusMessage: string
|
||||||
|
}) => {
|
||||||
|
if (newStatusMessage === '') {
|
||||||
|
await database.config.destroy({
|
||||||
|
where: {
|
||||||
|
id: configItemIds.statusMessage
|
||||||
|
}
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
await database.config.insertOrUpdate({
|
||||||
|
id: configItemIds.statusMessage,
|
||||||
|
value: newStatusMessage
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
|
@ -24,6 +24,7 @@ import {
|
||||||
ServerUpdatedCategoryBaseData, ServerUpdatedCategoryUsedTimes,
|
ServerUpdatedCategoryBaseData, ServerUpdatedCategoryUsedTimes,
|
||||||
ServerUpdatedTimeLimitRules
|
ServerUpdatedTimeLimitRules
|
||||||
} from '../../object/serverdatastatus'
|
} from '../../object/serverdatastatus'
|
||||||
|
import { getStatusMessage } from '../../function/statusmessage'
|
||||||
|
|
||||||
export const generateServerDataStatus = async ({ database, clientStatus, familyId, transaction }: {
|
export const generateServerDataStatus = async ({ database, clientStatus, familyId, transaction }: {
|
||||||
database: Database,
|
database: Database,
|
||||||
|
@ -57,7 +58,7 @@ export const generateServerDataStatus = async ({ database, clientStatus, familyI
|
||||||
|
|
||||||
let result: ServerDataStatus = {
|
let result: ServerDataStatus = {
|
||||||
fullVersion: familyEntry.hasFullVersion ? parseInt(familyEntry.fullVersionUntil, 10) : 0,
|
fullVersion: familyEntry.hasFullVersion ? parseInt(familyEntry.fullVersionUntil, 10) : 0,
|
||||||
message: process.env.STATUS_MESSAGE || undefined
|
message: await getStatusMessage({ database, transaction }) || undefined
|
||||||
}
|
}
|
||||||
|
|
||||||
if (familyEntry.deviceListVersion !== clientStatus.devices) {
|
if (familyEntry.deviceListVersion !== clientStatus.devices) {
|
||||||
|
|
|
@ -19,6 +19,7 @@ import * as io from 'socket.io'
|
||||||
import { ConnectedDevicesManager, VisibleConnectedDevicesManager } from '../connected-devices'
|
import { ConnectedDevicesManager, VisibleConnectedDevicesManager } from '../connected-devices'
|
||||||
import { Database } from '../database'
|
import { Database } from '../database'
|
||||||
import { deviceByAuthTokenRoom } from './rooms'
|
import { deviceByAuthTokenRoom } from './rooms'
|
||||||
|
import * as EventEmitter from 'events'
|
||||||
|
|
||||||
export const createWebsocketHandler = ({ connectedDevicesManager, database }: {
|
export const createWebsocketHandler = ({ connectedDevicesManager, database }: {
|
||||||
connectedDevicesManager: VisibleConnectedDevicesManager
|
connectedDevicesManager: VisibleConnectedDevicesManager
|
||||||
|
@ -27,6 +28,14 @@ export const createWebsocketHandler = ({ connectedDevicesManager, database }: {
|
||||||
websocketServer: io.Server
|
websocketServer: io.Server
|
||||||
websocketApi: WebsocketApi
|
websocketApi: WebsocketApi
|
||||||
} => {
|
} => {
|
||||||
|
const events = new EventEmitter()
|
||||||
|
|
||||||
|
// this disables warnings for many listeners
|
||||||
|
// this is required because very single socket causes listeners
|
||||||
|
events.setMaxListeners(0)
|
||||||
|
|
||||||
|
const eventTriggerImportantSyncForAll = 'triggerimportantsyncforall'
|
||||||
|
|
||||||
let socketCounter = 0
|
let socketCounter = 0
|
||||||
const server = io()
|
const server = io()
|
||||||
|
|
||||||
|
@ -43,6 +52,15 @@ export const createWebsocketHandler = ({ connectedDevicesManager, database }: {
|
||||||
|
|
||||||
socket.join(deviceByAuthTokenRoom(deviceAuthToken))
|
socket.join(deviceByAuthTokenRoom(deviceAuthToken))
|
||||||
|
|
||||||
|
const importantSyncForAllListener = () => {
|
||||||
|
setTimeout(() => {
|
||||||
|
socket.connected && socket.emit('should sync', { isImportant: true })
|
||||||
|
}, Math.random() * 1000 * 60 /* wait up to one minute */)
|
||||||
|
}
|
||||||
|
|
||||||
|
events.on(eventTriggerImportantSyncForAll, importantSyncForAllListener)
|
||||||
|
socket.on('disconnect', () => events.off(eventTriggerImportantSyncForAll, importantSyncForAllListener))
|
||||||
|
|
||||||
;(async () => {
|
;(async () => {
|
||||||
const deviceEntryUnsafe = await database.device.findOne({
|
const deviceEntryUnsafe = await database.device.findOne({
|
||||||
where: {
|
where: {
|
||||||
|
@ -94,6 +112,9 @@ export const createWebsocketHandler = ({ connectedDevicesManager, database }: {
|
||||||
.to(deviceByAuthTokenRoom(deviceAuthToken))
|
.to(deviceByAuthTokenRoom(deviceAuthToken))
|
||||||
.emit('sign out')
|
.emit('sign out')
|
||||||
},
|
},
|
||||||
|
triggerImportantSyncAtAllDevicesInBackground: () => {
|
||||||
|
events.emit(eventTriggerImportantSyncForAll)
|
||||||
|
},
|
||||||
countConnections: () => socketCounter
|
countConnections: () => socketCounter
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -106,5 +127,6 @@ export const createWebsocketHandler = ({ connectedDevicesManager, database }: {
|
||||||
export interface WebsocketApi {
|
export interface WebsocketApi {
|
||||||
triggerSyncByDeviceAuthToken: (params: {deviceAuthToken: string, isImportant: boolean}) => void
|
triggerSyncByDeviceAuthToken: (params: {deviceAuthToken: string, isImportant: boolean}) => void
|
||||||
triggerLogoutByDeviceAuthToken: (params: {deviceAuthToken: string}) => void
|
triggerLogoutByDeviceAuthToken: (params: {deviceAuthToken: string}) => void
|
||||||
|
triggerImportantSyncAtAllDevicesInBackground: () => void
|
||||||
countConnections: () => number
|
countConnections: () => number
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue