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
No known key found for this signature in database
GPG key ID: 8B8C9AEE10FA5B36
19 changed files with 330 additions and 9 deletions

View file

@ -308,6 +308,8 @@
* [Untitled array in ServerDataStatus](./serverdatastatus-definitions-serverupdatedcategorybasedata-properties-networks.md) `https://timelimit.io/ServerDataStatus#/definitions/ServerUpdatedCategoryBaseData/properties/networks`
* [Untitled array in ServerDataStatus](./serverdatastatus-definitions-serverupdatedcategorybasedata-properties-atw.md) `https://timelimit.io/ServerDataStatus#/definitions/ServerUpdatedCategoryBaseData/properties/atw`
* [Untitled array in ServerDataStatus](./serverdatastatus-properties-categoryapp.md) `https://timelimit.io/ServerDataStatus#/properties/categoryApp`
* [Untitled array in ServerDataStatus](./serverdatastatus-definitions-serverupdatedcategoryassignedapps-properties-apps.md) `https://timelimit.io/ServerDataStatus#/definitions/ServerUpdatedCategoryAssignedApps/properties/apps`
@ -336,6 +338,8 @@
* [Untitled array in ServerDataStatus](./serverdatastatus-definitions-serverupdatedcategorybasedata-properties-networks.md) `https://timelimit.io/ServerDataStatus#/definitions/ServerUpdatedCategoryBaseData/properties/networks`
* [Untitled array in ServerDataStatus](./serverdatastatus-definitions-serverupdatedcategorybasedata-properties-atw.md) `https://timelimit.io/ServerDataStatus#/definitions/ServerUpdatedCategoryBaseData/properties/atw`
* [Untitled array in ServerDataStatus](./serverdatastatus-definitions-serverupdatedcategoryassignedapps-properties-apps.md) `https://timelimit.io/ServerDataStatus#/definitions/ServerUpdatedCategoryAssignedApps/properties/apps`
* [Untitled array in ServerDataStatus](./serverdatastatus-definitions-serverupdatedcategoryusedtimes-properties-times.md) `https://timelimit.io/ServerDataStatus#/definitions/ServerUpdatedCategoryUsedTimes/properties/times`

View file

@ -1165,6 +1165,9 @@
},
"flags": {
"type": "number"
},
"minutes": {
"type": "number"
}
},
"additionalProperties": false,

View file

@ -395,10 +395,17 @@
},
"blockNotificationDelay": {
"type": "number"
},
"atw": {
"type": "array",
"items": {
"type": "number"
}
}
},
"additionalProperties": false,
"required": [
"atw",
"blockAllNotifications",
"blockNotificationDelay",
"blockedTimes",

View file

@ -0,0 +1,15 @@
# Untitled number in SerializedParentAction Schema
```txt
https://timelimit.io/SerializedParentAction#/definitions/SerializedUpdateCategoryTimeWarningsAction/properties/minutes
```
| Abstract | Extensible | Status | Identifiable | Custom Properties | Additional Properties | Access Restrictions | Defined In |
| :------------------ | :--------- | :------------- | :---------------------- | :---------------- | :-------------------- | :------------------ | :------------------------------------------------------------------------------------------------ |
| Can be instantiated | No | Unknown status | Unknown identifiability | Forbidden | Allowed | none | [SerializedParentAction.schema.json\*](SerializedParentAction.schema.json "open original schema") |
## minutes Type
`number`

View file

@ -22,6 +22,7 @@ https://timelimit.io/SerializedParentAction#/definitions/SerializedUpdateCategor
| [categoryId](#categoryid) | `string` | Required | cannot be null | [SerializedParentAction](serializedparentaction-definitions-serializedupdatecategorytimewarningsaction-properties-categoryid.md "https://timelimit.io/SerializedParentAction#/definitions/SerializedUpdateCategoryTimeWarningsAction/properties/categoryId") |
| [enable](#enable) | `boolean` | Required | cannot be null | [SerializedParentAction](serializedparentaction-definitions-serializedupdatecategorytimewarningsaction-properties-enable.md "https://timelimit.io/SerializedParentAction#/definitions/SerializedUpdateCategoryTimeWarningsAction/properties/enable") |
| [flags](#flags) | `number` | Required | cannot be null | [SerializedParentAction](serializedparentaction-definitions-serializedupdatecategorytimewarningsaction-properties-flags.md "https://timelimit.io/SerializedParentAction#/definitions/SerializedUpdateCategoryTimeWarningsAction/properties/flags") |
| [minutes](#minutes) | `number` | Optional | cannot be null | [SerializedParentAction](serializedparentaction-definitions-serializedupdatecategorytimewarningsaction-properties-minutes.md "https://timelimit.io/SerializedParentAction#/definitions/SerializedUpdateCategoryTimeWarningsAction/properties/minutes") |
## type
@ -102,3 +103,21 @@ https://timelimit.io/SerializedParentAction#/definitions/SerializedUpdateCategor
### flags Type
`number`
## minutes
`minutes`
* is optional
* Type: `number`
* cannot be null
* defined in: [SerializedParentAction](serializedparentaction-definitions-serializedupdatecategorytimewarningsaction-properties-minutes.md "https://timelimit.io/SerializedParentAction#/definitions/SerializedUpdateCategoryTimeWarningsAction/properties/minutes")
### minutes Type
`number`

View file

@ -3501,6 +3501,7 @@ Reference this group by using
| [categoryId](#categoryid-17) | `string` | Required | cannot be null | [SerializedParentAction](serializedparentaction-definitions-serializedupdatecategorytimewarningsaction-properties-categoryid.md "https://timelimit.io/SerializedParentAction#/definitions/SerializedUpdateCategoryTimeWarningsAction/properties/categoryId") |
| [enable](#enable-2) | `boolean` | Required | cannot be null | [SerializedParentAction](serializedparentaction-definitions-serializedupdatecategorytimewarningsaction-properties-enable.md "https://timelimit.io/SerializedParentAction#/definitions/SerializedUpdateCategoryTimeWarningsAction/properties/enable") |
| [flags](#flags) | `number` | Required | cannot be null | [SerializedParentAction](serializedparentaction-definitions-serializedupdatecategorytimewarningsaction-properties-flags.md "https://timelimit.io/SerializedParentAction#/definitions/SerializedUpdateCategoryTimeWarningsAction/properties/flags") |
| [minutes](#minutes) | `number` | Optional | cannot be null | [SerializedParentAction](serializedparentaction-definitions-serializedupdatecategorytimewarningsaction-properties-minutes.md "https://timelimit.io/SerializedParentAction#/definitions/SerializedUpdateCategoryTimeWarningsAction/properties/minutes") |
### type
@ -3582,6 +3583,24 @@ Reference this group by using
`number`
### minutes
`minutes`
* is optional
* Type: `number`
* cannot be null
* defined in: [SerializedParentAction](serializedparentaction-definitions-serializedupdatecategorytimewarningsaction-properties-minutes.md "https://timelimit.io/SerializedParentAction#/definitions/SerializedUpdateCategoryTimeWarningsAction/properties/minutes")
#### minutes Type
`number`
## Definitions group SerializedUpdateCategoryTitleAction
Reference this group by using

View file

@ -0,0 +1,15 @@
# Untitled number in ServerDataStatus Schema
```txt
https://timelimit.io/ServerDataStatus#/definitions/ServerUpdatedCategoryBaseData/properties/atw/items
```
| Abstract | Extensible | Status | Identifiable | Custom Properties | Additional Properties | Access Restrictions | Defined In |
| :------------------ | :--------- | :------------- | :---------------------- | :---------------- | :-------------------- | :------------------ | :------------------------------------------------------------------------------------ |
| Can be instantiated | No | Unknown status | Unknown identifiability | Forbidden | Allowed | none | [ServerDataStatus.schema.json\*](ServerDataStatus.schema.json "open original schema") |
## items Type
`number`

View file

@ -0,0 +1,15 @@
# Untitled array in ServerDataStatus Schema
```txt
https://timelimit.io/ServerDataStatus#/definitions/ServerUpdatedCategoryBaseData/properties/atw
```
| Abstract | Extensible | Status | Identifiable | Custom Properties | Additional Properties | Access Restrictions | Defined In |
| :------------------ | :--------- | :------------- | :---------------------- | :---------------- | :-------------------- | :------------------ | :------------------------------------------------------------------------------------ |
| Can be instantiated | No | Unknown status | Unknown identifiability | Forbidden | Allowed | none | [ServerDataStatus.schema.json\*](ServerDataStatus.schema.json "open original schema") |
## atw Type
`number[]`

View file

@ -37,6 +37,7 @@ https://timelimit.io/ServerDataStatus#/definitions/ServerUpdatedCategoryBaseData
| [dlu](#dlu) | `number` | Required | cannot be null | [ServerDataStatus](serverdatastatus-definitions-serverupdatedcategorybasedata-properties-dlu.md "https://timelimit.io/ServerDataStatus#/definitions/ServerUpdatedCategoryBaseData/properties/dlu") |
| [flags](#flags) | `number` | Required | cannot be null | [ServerDataStatus](serverdatastatus-definitions-serverupdatedcategorybasedata-properties-flags.md "https://timelimit.io/ServerDataStatus#/definitions/ServerUpdatedCategoryBaseData/properties/flags") |
| [blockNotificationDelay](#blocknotificationdelay) | `number` | Required | cannot be null | [ServerDataStatus](serverdatastatus-definitions-serverupdatedcategorybasedata-properties-blocknotificationdelay.md "https://timelimit.io/ServerDataStatus#/definitions/ServerUpdatedCategoryBaseData/properties/blockNotificationDelay") |
| [atw](#atw) | `array` | Required | cannot be null | [ServerDataStatus](serverdatastatus-definitions-serverupdatedcategorybasedata-properties-atw.md "https://timelimit.io/ServerDataStatus#/definitions/ServerUpdatedCategoryBaseData/properties/atw") |
## categoryId
@ -379,3 +380,21 @@ https://timelimit.io/ServerDataStatus#/definitions/ServerUpdatedCategoryBaseData
### blockNotificationDelay Type
`number`
## atw
`atw`
* is required
* Type: `number[]`
* cannot be null
* defined in: [ServerDataStatus](serverdatastatus-definitions-serverupdatedcategorybasedata-properties-atw.md "https://timelimit.io/ServerDataStatus#/definitions/ServerUpdatedCategoryBaseData/properties/atw")
### atw Type
`number[]`

View file

@ -1296,6 +1296,7 @@ Reference this group by using
| [dlu](#dlu) | `number` | Required | cannot be null | [ServerDataStatus](serverdatastatus-definitions-serverupdatedcategorybasedata-properties-dlu.md "https://timelimit.io/ServerDataStatus#/definitions/ServerUpdatedCategoryBaseData/properties/dlu") |
| [flags](#flags) | `number` | Required | cannot be null | [ServerDataStatus](serverdatastatus-definitions-serverupdatedcategorybasedata-properties-flags.md "https://timelimit.io/ServerDataStatus#/definitions/ServerUpdatedCategoryBaseData/properties/flags") |
| [blockNotificationDelay](#blocknotificationdelay) | `number` | Required | cannot be null | [ServerDataStatus](serverdatastatus-definitions-serverupdatedcategorybasedata-properties-blocknotificationdelay.md "https://timelimit.io/ServerDataStatus#/definitions/ServerUpdatedCategoryBaseData/properties/blockNotificationDelay") |
| [atw](#atw) | `array` | Required | cannot be null | [ServerDataStatus](serverdatastatus-definitions-serverupdatedcategorybasedata-properties-atw.md "https://timelimit.io/ServerDataStatus#/definitions/ServerUpdatedCategoryBaseData/properties/atw") |
### categoryId
@ -1639,6 +1640,24 @@ Reference this group by using
`number`
### atw
`atw`
* is required
* Type: `number[]`
* cannot be null
* defined in: [ServerDataStatus](serverdatastatus-definitions-serverupdatedcategorybasedata-properties-atw.md "https://timelimit.io/ServerDataStatus#/definitions/ServerUpdatedCategoryBaseData/properties/atw")
#### atw Type
`number[]`
## Definitions group ServerCategoryNetworkId
Reference this group by using

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
}

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",

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

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),

View file

@ -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 })
})
}

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
@ -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
}

View file

@ -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)
}))
}

View file

@ -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) {

View file

@ -120,6 +120,8 @@ export interface ServerUpdatedCategoryBaseData {
dlu: number
flags: number
blockNotificationDelay: number
// atw = additionalTimeWarnings
atw: Array<number>
}
export interface ServerCategoryNetworkId {