Add server support for disabling limitations temporarily per category

This commit is contained in:
Jonas Lochmann
2020-11-02 01:00:00 +01:00
parent 35924c176c
commit 8b596ccc69
23 changed files with 574 additions and 38 deletions
+1
View File
@@ -58,6 +58,7 @@ export { UpdateAppActivitiesAction } from './updateappactivities'
export { UpdateCategoryBatteryLimitAction } from './updatecategorybatterylimit'
export { UpdateCategoryBlockAllNotificationsAction } from './updatecategoryblockallnotifications'
export { UpdateCategoryBlockedTimesAction } from './updatecategoryblockedtimes'
export { UpdateCategoryDisableLimitsAction } from './updatecategorydisablelimits'
export { UpdateCategorySortingAction } from './updatecategorysorting'
export { UpdateCategoryTemporarilyBlockedAction } from './updatecategorytemporarilyblocked'
export { UpdateCategoryTimeWarningsAction } from './updatecategorytimewarnings'
+4
View File
@@ -48,6 +48,7 @@ import { SerializedSetUserTimezoneAction, SetUserTimezoneAction } from '../setus
import { SerializedUpdateCategoryBatteryLimitAction, UpdateCategoryBatteryLimitAction } from '../updatecategorybatterylimit'
import { SerializedUpdateCategoryBlockAllNotificationsAction, UpdateCategoryBlockAllNotificationsAction } from '../updatecategoryblockallnotifications'
import { SerializedUpdateCategoryBlockedTimesAction, UpdateCategoryBlockedTimesAction } from '../updatecategoryblockedtimes'
import { SerializedUpdatCategoryDisableLimitsAction, UpdateCategoryDisableLimitsAction } from '../updatecategorydisablelimits'
import { SerializedUpdateCategorySortingAction, UpdateCategorySortingAction } from '../updatecategorysorting'
import { SerializedUpdateCategoryTemporarilyBlockedAction, UpdateCategoryTemporarilyBlockedAction } from '../updatecategorytemporarilyblocked'
import { SerializedUpdateCategoryTimeWarningsAction, UpdateCategoryTimeWarningsAction } from '../updatecategorytimewarnings'
@@ -93,6 +94,7 @@ export type SerializedParentAction =
SerializedUpdateCategoryBatteryLimitAction |
SerializedUpdateCategoryBlockAllNotificationsAction |
SerializedUpdateCategoryBlockedTimesAction |
SerializedUpdatCategoryDisableLimitsAction |
SerializedUpdateCategorySortingAction |
SerializedUpdateCategoryTemporarilyBlockedAction |
SerializedUpdateCategoryTimeWarningsAction |
@@ -169,6 +171,8 @@ export const parseParentAction = (action: SerializedParentAction): ParentAction
return UpdateCategoryBlockAllNotificationsAction.parse(action)
} else if (action.type === 'UPDATE_CATEGORY_BLOCKED_TIMES') {
return UpdateCategoryBlockedTimesAction.parse(action)
} else if (action.type === 'UPDATE_CATEGORY_DISABLE_LIMITS') {
return UpdateCategoryDisableLimitsAction.parse(action)
} else if (action.type === 'UPDATE_CATEGORY_SORTING') {
return UpdateCategorySortingAction.parse(action)
} else if (action.type === 'UPDATE_CATEGORY_TIME_WARNINGS') {
+57
View File
@@ -0,0 +1,57 @@
/*
* server component for the TimeLimit App
* Copyright (C) 2019 - 2020 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 { ParentAction } from './basetypes'
import { InvalidActionParameterException } from './meta/exception'
import { assertIdWithinFamily, assertSafeInteger } from './meta/util'
const actionType = 'UpdateCategoryDisableLimitsAction'
export class UpdateCategoryDisableLimitsAction extends ParentAction {
readonly categoryId: string
readonly endTime: number
constructor ({ categoryId, endTime }: {
categoryId: string
endTime: number
}) {
super()
assertIdWithinFamily({ actionType, field: 'categoryId', value: categoryId })
assertSafeInteger({ actionType, field: 'endTime', value: endTime })
if (endTime < 0) {
throw new InvalidActionParameterException({
actionType,
staticMessage: 'endTime must not be smaller than zero'
})
}
this.categoryId = categoryId
this.endTime = endTime
}
static parse = ({ categoryId, endTime }: SerializedUpdatCategoryDisableLimitsAction) => (
new UpdateCategoryDisableLimitsAction({ categoryId, endTime })
)
}
export interface SerializedUpdatCategoryDisableLimitsAction {
type: 'UPDATE_CATEGORY_DISABLE_LIMITS'
categoryId: string
endTime: number
}
+30
View File
@@ -939,6 +939,29 @@ const definitions = {
"type"
]
},
"SerializedUpdatCategoryDisableLimitsAction": {
"type": "object",
"properties": {
"type": {
"type": "string",
"enum": [
"UPDATE_CATEGORY_DISABLE_LIMITS"
]
},
"categoryId": {
"type": "string"
},
"endTime": {
"type": "number"
}
},
"additionalProperties": false,
"required": [
"categoryId",
"endTime",
"type"
]
},
"SerializedUpdateCategorySortingAction": {
"type": "object",
"properties": {
@@ -1935,6 +1958,9 @@ const definitions = {
"items": {
"$ref": "#/definitions/ServerCategoryNetworkId"
}
},
"dlu": {
"type": "number"
}
},
"additionalProperties": false,
@@ -1943,6 +1969,7 @@ const definitions = {
"blockedTimes",
"categoryId",
"childId",
"dlu",
"extraTime",
"extraTimeDay",
"mblCharging",
@@ -2505,6 +2532,9 @@ export const isSerializedParentAction: (value: object) => value is SerializedPar
{
"$ref": "#/definitions/SerializedUpdateCategoryBlockedTimesAction"
},
{
"$ref": "#/definitions/SerializedUpdatCategoryDisableLimitsAction"
},
{
"$ref": "#/definitions/SerializedUpdateCategorySortingAction"
},
+15 -2
View File
@@ -65,9 +65,14 @@ export interface CategoryAttributesVersion8 {
extraTimeDay: number
}
export interface CategoryAttributesVersion9 {
disableLimitsUntil: string
}
export type CategoryAttributes = CategoryAttributesVersion1 & CategoryAttributesVersion2 &
CategoryAttributesVersion3 & CategoryAttributesVersion4 & CategoryAttributesVersion5 &
CategoryAttributesVersion6 & CategoryAttributesVersion7 & CategoryAttributesVersion8
CategoryAttributesVersion6 & CategoryAttributesVersion7 & CategoryAttributesVersion8 &
CategoryAttributesVersion9
export type CategoryModel = Sequelize.Model & CategoryAttributes
export type CategoryModelStatic = typeof Sequelize.Model & {
@@ -187,6 +192,13 @@ export const attributesVersion8: SequelizeAttributes<CategoryAttributesVersion8>
}
}
export const attributesVersion9: SequelizeAttributes<CategoryAttributesVersion9> = {
disableLimitsUntil: {
...timestampColumn,
defaultValue: 0
}
}
export const attributes: SequelizeAttributes<CategoryAttributes> = {
...attributesVersion1,
...attributesVersion2,
@@ -195,7 +207,8 @@ export const attributes: SequelizeAttributes<CategoryAttributes> = {
...attributesVersion5,
...attributesVersion6,
...attributesVersion7,
...attributesVersion8
...attributesVersion8,
...attributesVersion9
}
export const createCategoryModel = (sequelize: Sequelize.Sequelize): CategoryModelStatic => sequelize.define('Category', attributes) as CategoryModelStatic
@@ -0,0 +1,39 @@
/*
* server component for the TimeLimit App
* Copyright (C) 2019 - 2020 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'
import { attributesVersion9 as categoryAttributes } from '../../category'
export async function up (queryInterface: QueryInterface, sequelize: Sequelize) {
await sequelize.transaction({
type: Transaction.TYPES.EXCLUSIVE
}, async (transaction) => {
await queryInterface.addColumn('Categories', 'disableLimitsUntil', {
...categoryAttributes.disableLimitsUntil
}, {
transaction
})
})
}
export async function down (queryInterface: QueryInterface, sequelize: Sequelize) {
await sequelize.transaction({
type: Transaction.TYPES.EXCLUSIVE
}, async (transaction) => {
await queryInterface.removeColumn('Categories', 'disableLimitsUntil', { transaction })
})
}
@@ -75,7 +75,8 @@ export async function dispatchCreateCategory ({ action, cache, fromChildSelfLimi
parentCategoryId: '',
blockAllNotifications: false,
timeWarningFlags: 0,
sort
sort,
disableLimitsUntil: 0
}, { transaction: cache.transaction })
// update the cache
@@ -48,6 +48,7 @@ import {
UpdateCategoryBatteryLimitAction,
UpdateCategoryBlockAllNotificationsAction,
UpdateCategoryBlockedTimesAction,
UpdateCategoryDisableLimitsAction,
UpdateCategorySortingAction,
UpdateCategoryTemporarilyBlockedAction,
UpdateCategoryTimeWarningsAction,
@@ -95,6 +96,7 @@ import { dispatchSetUserTimezone } from './setusertimezone'
import { dispatchUpdateCategoryBatteryLimit } from './updatecategorybatterylimit'
import { dispatchUpdateCategoryBlockAllNotifications } from './updatecategoryblockallnotifications'
import { dispatchUpdateCategoryBlockedTimes } from './updatecategoryblockedtimes'
import { dispatchUpdateCategoryDisableLimits } from './updatecategorydisablelimits'
import { dispatchUpdateCategorySorting } from './updatecategorysorting'
import { dispatchUpdateCategoryTemporarilyBlocked } from './updatecategorytemporarilyblocked'
import { dispatchUpdateCategoryTimeWarnings } from './updatecategorytimewarnings'
@@ -129,6 +131,8 @@ export const dispatchParentAction = async ({ action, cache, parentUserId, source
return dispatchUpdateCategoryTemporarilyBlocked({ action, cache, fromChildSelfLimitAddChildUserId })
} else if (action instanceof UpdateCategoryBlockedTimesAction) {
return dispatchUpdateCategoryBlockedTimes({ action, cache, fromChildSelfLimitAddChildUserId })
} else if (action instanceof UpdateCategoryDisableLimitsAction) {
return dispatchUpdateCategoryDisableLimits({ action, cache, fromChildSelfLimitAddChildUserId })
}
if (fromChildSelfLimitAddChildUserId !== null) {
@@ -0,0 +1,76 @@
/*
* server component for the TimeLimit App
* Copyright (C) 2019 - 2020 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 { UpdateCategoryDisableLimitsAction } from '../../../../action'
import { Cache } from '../cache'
import { MissingCategoryException } from '../exception/missing-item'
import { PremiumVersionMissingException } from '../exception/premium'
import { CanNotModifyOtherUsersBySelfLimitationException, SelfLimitationException } from '../exception/self-limit'
export async function dispatchUpdateCategoryDisableLimits ({ action, cache, fromChildSelfLimitAddChildUserId }: {
action: UpdateCategoryDisableLimitsAction
cache: Cache
fromChildSelfLimitAddChildUserId: string | null
}) {
if (action.endTime !== 0) {
if (!cache.hasFullVersion) {
throw new PremiumVersionMissingException()
}
if (fromChildSelfLimitAddChildUserId !== null) {
throw new SelfLimitationException({ staticMessage: 'the child may only disable the disabling of the limitations' })
}
}
const categoryEntryUnsafe = await cache.database.category.findOne({
where: {
familyId: cache.familyId,
categoryId: action.categoryId
},
transaction: cache.transaction,
attributes: ['childId']
})
if (!categoryEntryUnsafe) {
throw new MissingCategoryException()
}
const categoryEntry = {
childId: categoryEntryUnsafe.childId
}
if (fromChildSelfLimitAddChildUserId !== null) {
if (fromChildSelfLimitAddChildUserId !== categoryEntry.childId) {
throw new CanNotModifyOtherUsersBySelfLimitationException()
}
}
const [affectedRows] = await cache.database.category.update({
disableLimitsUntil: action.endTime
}, {
where: {
familyId: cache.familyId,
categoryId: action.categoryId
},
transaction: cache.transaction
})
if (affectedRows !== 0) {
cache.categoriesWithModifiedBaseData.add(action.categoryId)
cache.areChangesImportant = true
}
}
@@ -50,7 +50,8 @@ export async function getCategoryBaseDatas ({
'minBatteryCharging',
'minBatteryMobile',
'temporarilyBlockedEndTime',
'sort'
'sort',
'disableLimitsUntil'
],
transaction
})).map((item) => ({
@@ -68,7 +69,8 @@ export async function getCategoryBaseDatas ({
minBatteryCharging: item.minBatteryCharging,
minBatteryMobile: item.minBatteryMobile,
temporarilyBlockedEndTime: item.temporarilyBlockedEndTime,
sort: item.sort
sort: item.sort,
disableLimitsUntil: item.disableLimitsUntil
}))
const networkIdsForSyncing = (await database.categoryNetworkId.findAll({
@@ -111,6 +113,7 @@ export async function getCategoryBaseDatas ({
.map((network) => ({
itemId: network.networkItemId,
hashedNetworkId: network.hashedNetworkId
}))
})),
dlu: parseInt(item.disableLimitsUntil, 10)
}))
}
+2
View File
@@ -113,6 +113,8 @@ export interface ServerUpdatedCategoryBaseData {
mblMobile: number
sort: number
networks: Array<ServerCategoryNetworkId>
// disable limits until
dlu: number
}
export interface ServerCategoryNetworkId {