Add support for custom time warnings

This commit is contained in:
Jonas Lochmann
2022-03-28 02:00:00 +02:00
parent d7799f2d06
commit 2ab23ea811
19 changed files with 330 additions and 9 deletions
+21 -5
View File
@@ -1,6 +1,6 @@
/*
* server component for the TimeLimit App
* Copyright (C) 2019 - 2020 Jonas Lochmann
* Copyright (C) 2019 - 2022 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
@@ -16,6 +16,7 @@
*/
import { allowedTimeWarningFlags } from '../database/category'
import { categoryTimeWarningConstants } from '../database/categorytimewarning'
import { ParentAction } from './basetypes'
import { assertIdWithinFamily, assertSafeInteger, throwOutOfRange } from './meta/util'
@@ -25,11 +26,13 @@ export class UpdateCategoryTimeWarningsAction extends ParentAction {
readonly categoryId: string
readonly enable: boolean
readonly flags: number
readonly minutes?: number
constructor ({ categoryId, enable, flags }: {
constructor ({ categoryId, enable, flags, minutes }: {
categoryId: string
enable: boolean
flags: number
flags: number,
minutes?: number
}) {
super()
@@ -40,13 +43,25 @@ export class UpdateCategoryTimeWarningsAction extends ParentAction {
throwOutOfRange({ actionType, field: 'flags', value: flags })
}
if (minutes !== undefined) {
assertSafeInteger({ actionType, field: 'minutes', value: minutes })
if (
minutes < categoryTimeWarningConstants.minMinutes ||
minutes > categoryTimeWarningConstants.maxMinutes
) {
throwOutOfRange({ actionType, field: 'minutes', value: minutes })
}
}
this.categoryId = categoryId
this.enable = enable
this.flags = flags
this.minutes = minutes
}
static parse = ({ categoryId, enable, flags }: SerializedUpdateCategoryTimeWarningsAction) => (
new UpdateCategoryTimeWarningsAction({ categoryId, enable, flags })
static parse = ({ categoryId, enable, flags, minutes }: SerializedUpdateCategoryTimeWarningsAction) => (
new UpdateCategoryTimeWarningsAction({ categoryId, enable, flags, minutes })
)
}
@@ -55,4 +70,5 @@ export interface SerializedUpdateCategoryTimeWarningsAction {
categoryId: string
enable: boolean
flags: number
minutes?: number
}
+10
View File
@@ -1093,6 +1093,9 @@ const definitions = {
},
"flags": {
"type": "number"
},
"minutes": {
"type": "number"
}
},
"additionalProperties": false,
@@ -2057,10 +2060,17 @@ const definitions = {
},
"blockNotificationDelay": {
"type": "number"
},
"atw": {
"type": "array",
"items": {
"type": "number"
}
}
},
"additionalProperties": false,
"required": [
"atw",
"blockAllNotifications",
"blockNotificationDelay",
"blockedTimes",
+58
View File
@@ -0,0 +1,58 @@
/*
* server component for the TimeLimit App
* Copyright (C) 2019 - 2022 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 * as Sequelize from 'sequelize'
import { familyIdColumn, idWithinFamilyColumn } from './columns'
import { SequelizeAttributes } from './types'
export const categoryTimeWarningConstants = {
minMinutes: 1,
maxMinutes: 60 * 24 * 7 - 2
}
export interface CategoryTimeWarningAttributes {
familyId: string
categoryId: string
minutes: number
}
export type CategoryTimeWarningModel = Sequelize.Model<CategoryTimeWarningAttributes> & CategoryTimeWarningAttributes
export type CategoryTimeWarningModelStatic = typeof Sequelize.Model & {
new (values?: object, options?: Sequelize.BuildOptions): CategoryTimeWarningModel;
}
export const attributes: SequelizeAttributes<CategoryTimeWarningAttributes> = {
familyId: {
...familyIdColumn,
primaryKey: true
},
categoryId: {
...idWithinFamilyColumn,
primaryKey: true
},
minutes: {
type: Sequelize.INTEGER,
allowNull: false,
validate: {
min: categoryTimeWarningConstants.minMinutes,
max: categoryTimeWarningConstants.maxMinutes
},
primaryKey: true
}
}
export const createCategoryTimeWarningModel = (sequelize: Sequelize.Sequelize): CategoryTimeWarningModelStatic => sequelize.define('CategoryTimeWarning', attributes) as CategoryTimeWarningModelStatic
+3
View File
@@ -23,6 +23,7 @@ import { AuthTokenModelStatic, createAuthtokenModel } from './authtoken'
import { CategoryModelStatic, createCategoryModel } from './category'
import { CategoryAppModelStatic, createCategoryAppModel } from './categoryapp'
import { CategoryNetworkIdModelStatic, createCategoryNetworkIdModel } from './categorynetworkid'
import { CategoryTimeWarningModelStatic, createCategoryTimeWarningModel } from './categorytimewarning'
import { ChildTaskModelStatic, createChildTaskModel } from './childtask'
import { ConfigModelStatic, createConfigModel } from './config'
import { createDeviceModel, DeviceModelStatic } from './device'
@@ -47,6 +48,7 @@ export interface Database {
category: CategoryModelStatic
categoryApp: CategoryAppModelStatic
categoryNetworkId: CategoryNetworkIdModelStatic
categoryTimeWarning: CategoryTimeWarningModelStatic
childTask: ChildTaskModelStatic
config: ConfigModelStatic
device: DeviceModelStatic
@@ -72,6 +74,7 @@ const createDatabase = (sequelize: Sequelize.Sequelize): Database => ({
categoryApp: createCategoryAppModel(sequelize),
childTask: createChildTaskModel(sequelize),
categoryNetworkId: createCategoryNetworkIdModel(sequelize),
categoryTimeWarning: createCategoryTimeWarningModel(sequelize),
config: createConfigModel(sequelize),
device: createDeviceModel(sequelize),
family: createFamilyModel(sequelize),
@@ -0,0 +1,55 @@
/*
* server component for the TimeLimit App
* Copyright (C) 2019 - 2022 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 { QueryInterface, Sequelize, Transaction } from 'sequelize'
export async function up (_: QueryInterface, sequelize: Sequelize) {
await sequelize.transaction({
type: Transaction.TYPES.EXCLUSIVE
}, async (transaction) => {
const dialect = sequelize.getDialect()
const isMysql = dialect === 'mysql' || dialect === 'mariadb'
if (isMysql) {
await sequelize.query(
'CREATE TABLE `CategoryTimeWarnings` ' +
'(`familyId` VARCHAR(10) NOT NULL, `categoryId` VARCHAR(6) NOT NULL,' +
'`minutes` INTEGER NOT NULL, ' +
'PRIMARY KEY(`familyId`, `categoryId`, `minutes`), FOREIGN KEY(`familyId`, `categoryId`)' +
'REFERENCES `Categories`(`familyId`, `categoryId`) ON UPDATE CASCADE ON DELETE CASCADE )',
{ transaction }
)
} else {
await sequelize.query(
'CREATE TABLE "CategoryTimeWarnings" ' +
'("familyId" VARCHAR(10) NOT NULL, "categoryId" VARCHAR(6) NOT NULL,' +
'"minutes" INTEGER NOT NULL, ' +
'PRIMARY KEY("familyId", "categoryId", "minutes"), FOREIGN KEY("familyId", "categoryId")' +
'REFERENCES "Categories"("familyId", "categoryId") ON UPDATE CASCADE ON DELETE CASCADE )',
{ transaction }
)
}
})
}
export async function down (queryInterface: QueryInterface, sequelize: Sequelize) {
await sequelize.transaction({
type: Transaction.TYPES.EXCLUSIVE
}, async (transaction) => {
await queryInterface.dropTable('CategoryTimeWarnings', { transaction })
})
}
@@ -1,6 +1,6 @@
/*
* server component for the TimeLimit App
* Copyright (C) 2019 - 2020 Jonas Lochmann
* Copyright (C) 2019 - 2022 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
@@ -43,6 +43,28 @@ export async function dispatchUpdateCategoryTimeWarnings ({ action, cache }: {
await categoryEntry.save({ transaction: cache.transaction })
if (action.minutes !== undefined) {
if (action.enable) {
await cache.database.categoryTimeWarning.create({
familyId: cache.familyId,
categoryId: action.categoryId,
minutes: action.minutes
}, {
transaction: cache.transaction,
ignoreDuplicates: true
})
} else {
await cache.database.categoryTimeWarning.destroy({
where: {
familyId: cache.familyId,
categoryId: action.categoryId,
minutes: action.minutes
},
transaction: cache.transaction
})
}
}
cache.categoriesWithModifiedBaseData.add(action.categoryId)
cache.areChangesImportant = true
}
@@ -1,6 +1,6 @@
/*
* server component for the TimeLimit App
* Copyright (C) 2019 - 2021 Jonas Lochmann
* Copyright (C) 2019 - 2022 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
@@ -96,6 +96,23 @@ export async function getCategoryBaseDatas ({
hashedNetworkId: item.hashedNetworkId
}))
const additionalTimeWarningsForSyncing = (await database.categoryTimeWarning.findAll({
where: {
familyId: familyEntry.familyId,
categoryId: {
[Sequelize.Op.in]: categoryIdsToSyncBaseData
}
},
attributes: [
'categoryId',
'minutes'
],
transaction
})).map((item) => ({
categoryId: item.categoryId,
minutes: item.minutes
}))
return dataForSyncing.map((item): ServerUpdatedCategoryBaseData => ({
categoryId: item.categoryId,
childId: item.childId,
@@ -120,6 +137,9 @@ export async function getCategoryBaseDatas ({
})),
dlu: parseInt(item.disableLimitsUntil, 10),
flags: parseInt(item.flags, 10),
blockNotificationDelay: parseInt(item.blockNotificationDelay, 10)
blockNotificationDelay: parseInt(item.blockNotificationDelay, 10),
atw: additionalTimeWarningsForSyncing
.filter((timeWarning) => timeWarning.categoryId === item.categoryId)
.map((timeWarning) => timeWarning.minutes)
}))
}
@@ -44,7 +44,7 @@ export const generateServerDataStatus = async ({ database, clientStatus, familyI
familyEntry.hasFullVersion ? parseInt(familyEntry.fullVersionUntil, 10) : 0
),
message: await getStatusMessage({ database, transaction }) || undefined,
apiLevel: 2
apiLevel: 3
}
if (familyEntry.deviceListVersion !== clientStatus.devices) {
+2
View File
@@ -120,6 +120,8 @@ export interface ServerUpdatedCategoryBaseData {
dlu: number
flags: number
blockNotificationDelay: number
// atw = additionalTimeWarnings
atw: Array<number>
}
export interface ServerCategoryNetworkId {