mirror of
https://codeberg.org/timelimit/timelimit-server.git
synced 2026-08-31 19:03:45 +02:00
Add category flags
This commit is contained in:
+2
-1
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* server component for the TimeLimit App
|
||||
* Copyright (C) 2019 - 2020 Jonas Lochmann
|
||||
* Copyright (C) 2019 - 2021 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
|
||||
@@ -58,6 +58,7 @@ export { UpdateCategoryBatteryLimitAction } from './updatecategorybatterylimit'
|
||||
export { UpdateCategoryBlockAllNotificationsAction } from './updatecategoryblockallnotifications'
|
||||
export { UpdateCategoryBlockedTimesAction } from './updatecategoryblockedtimes'
|
||||
export { UpdateCategoryDisableLimitsAction } from './updatecategorydisablelimits'
|
||||
export { UpdateCategoryFlagsAction } from './updatecategoryflags'
|
||||
export { UpdateCategorySortingAction } from './updatecategorysorting'
|
||||
export { UpdateCategoryTemporarilyBlockedAction } from './updatecategorytemporarilyblocked'
|
||||
export { UpdateCategoryTimeWarningsAction } from './updatecategorytimewarnings'
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* server component for the TimeLimit App
|
||||
* Copyright (C) 2019 - 2020 Jonas Lochmann
|
||||
* Copyright (C) 2019 - 2021 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
|
||||
@@ -50,6 +50,7 @@ import { SerializedUpdateCategoryBatteryLimitAction, UpdateCategoryBatteryLimitA
|
||||
import { SerializedUpdateCategoryBlockAllNotificationsAction, UpdateCategoryBlockAllNotificationsAction } from '../updatecategoryblockallnotifications'
|
||||
import { SerializedUpdateCategoryBlockedTimesAction, UpdateCategoryBlockedTimesAction } from '../updatecategoryblockedtimes'
|
||||
import { SerializedUpdatCategoryDisableLimitsAction, UpdateCategoryDisableLimitsAction } from '../updatecategorydisablelimits'
|
||||
import { SerializedUpdateCategoryFlagsAction, UpdateCategoryFlagsAction } from '../updatecategoryflags'
|
||||
import { SerializedUpdateCategorySortingAction, UpdateCategorySortingAction } from '../updatecategorysorting'
|
||||
import { SerializedUpdateCategoryTemporarilyBlockedAction, UpdateCategoryTemporarilyBlockedAction } from '../updatecategorytemporarilyblocked'
|
||||
import { SerializedUpdateCategoryTimeWarningsAction, UpdateCategoryTimeWarningsAction } from '../updatecategorytimewarnings'
|
||||
@@ -98,6 +99,7 @@ export type SerializedParentAction =
|
||||
SerializedUpdateCategoryBlockAllNotificationsAction |
|
||||
SerializedUpdateCategoryBlockedTimesAction |
|
||||
SerializedUpdatCategoryDisableLimitsAction |
|
||||
SerializedUpdateCategoryFlagsAction |
|
||||
SerializedUpdateCategorySortingAction |
|
||||
SerializedUpdateCategoryTemporarilyBlockedAction |
|
||||
SerializedUpdateCategoryTimeWarningsAction |
|
||||
@@ -179,6 +181,8 @@ export const parseParentAction = (action: SerializedParentAction): ParentAction
|
||||
return UpdateCategoryBlockedTimesAction.parse(action)
|
||||
} else if (action.type === 'UPDATE_CATEGORY_DISABLE_LIMITS') {
|
||||
return UpdateCategoryDisableLimitsAction.parse(action)
|
||||
} else if (action.type === 'UPDATE_CATEGORY_FLAGS') {
|
||||
return UpdateCategoryFlagsAction.parse(action)
|
||||
} else if (action.type === 'UPDATE_CATEGORY_SORTING') {
|
||||
return UpdateCategorySortingAction.parse(action)
|
||||
} else if (action.type === 'UPDATE_CATEGORY_TIME_WARNINGS') {
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* server component for the TimeLimit App
|
||||
* Copyright (C) 2019 - 2021 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 { maxCategoryFlags } from '../database/category'
|
||||
import { ParentAction } from './basetypes'
|
||||
import { InvalidActionParameterException } from './meta/exception'
|
||||
import { assertIdWithinFamily, assertSafeInteger } from './meta/util'
|
||||
|
||||
const actionType = 'UpdateCategoryFlagsAction'
|
||||
|
||||
export class UpdateCategoryFlagsAction extends ParentAction {
|
||||
readonly categoryId: string
|
||||
readonly modifiedBits: number
|
||||
readonly newValues: number
|
||||
|
||||
constructor ({ categoryId, modifiedBits, newValues }: {
|
||||
categoryId: string
|
||||
modifiedBits: number
|
||||
newValues: number
|
||||
}) {
|
||||
super()
|
||||
|
||||
assertIdWithinFamily({ actionType, field: 'categoryId', value: categoryId })
|
||||
assertSafeInteger({ actionType, field: 'modifiedBits', value: modifiedBits })
|
||||
assertSafeInteger({ actionType, field: 'newValues', value: newValues })
|
||||
|
||||
if ((modifiedBits | maxCategoryFlags) !== maxCategoryFlags || (modifiedBits | newValues) !== modifiedBits) {
|
||||
throw new InvalidActionParameterException({
|
||||
actionType,
|
||||
staticMessage: 'flags are out of the valid range',
|
||||
dynamicMessage: 'flags are out of the valid range: ' + modifiedBits + ', ' + newValues
|
||||
})
|
||||
}
|
||||
|
||||
this.categoryId = categoryId
|
||||
this.modifiedBits = modifiedBits
|
||||
this.newValues = newValues
|
||||
}
|
||||
|
||||
static parse = ({ categoryId, modified, values }: SerializedUpdateCategoryFlagsAction) => (
|
||||
new UpdateCategoryFlagsAction({
|
||||
categoryId,
|
||||
modifiedBits: modified,
|
||||
newValues: values
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
export interface SerializedUpdateCategoryFlagsAction {
|
||||
type: 'UPDATE_CATEGORY_FLAGS'
|
||||
categoryId: string
|
||||
modified: number
|
||||
values: number
|
||||
}
|
||||
@@ -995,6 +995,33 @@ const definitions = {
|
||||
"type"
|
||||
]
|
||||
},
|
||||
"SerializedUpdateCategoryFlagsAction": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"type": {
|
||||
"type": "string",
|
||||
"enum": [
|
||||
"UPDATE_CATEGORY_FLAGS"
|
||||
]
|
||||
},
|
||||
"categoryId": {
|
||||
"type": "string"
|
||||
},
|
||||
"modified": {
|
||||
"type": "number"
|
||||
},
|
||||
"values": {
|
||||
"type": "number"
|
||||
}
|
||||
},
|
||||
"additionalProperties": false,
|
||||
"required": [
|
||||
"categoryId",
|
||||
"modified",
|
||||
"type",
|
||||
"values"
|
||||
]
|
||||
},
|
||||
"SerializedUpdateCategorySortingAction": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
@@ -2051,6 +2078,9 @@ const definitions = {
|
||||
},
|
||||
"dlu": {
|
||||
"type": "number"
|
||||
},
|
||||
"flags": {
|
||||
"type": "number"
|
||||
}
|
||||
},
|
||||
"additionalProperties": false,
|
||||
@@ -2062,6 +2092,7 @@ const definitions = {
|
||||
"dlu",
|
||||
"extraTime",
|
||||
"extraTimeDay",
|
||||
"flags",
|
||||
"mblCharging",
|
||||
"mblMobile",
|
||||
"networks",
|
||||
@@ -2686,6 +2717,9 @@ export const isSerializedParentAction: (value: object) => value is SerializedPar
|
||||
{
|
||||
"$ref": "#/definitions/SerializedUpdatCategoryDisableLimitsAction"
|
||||
},
|
||||
{
|
||||
"$ref": "#/definitions/SerializedUpdateCategoryFlagsAction"
|
||||
},
|
||||
{
|
||||
"$ref": "#/definitions/SerializedUpdateCategorySortingAction"
|
||||
},
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* server component for the TimeLimit App
|
||||
* Copyright (C) 2019 - 2020 Jonas Lochmann
|
||||
* Copyright (C) 2019 - 2021 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
|
||||
@@ -22,6 +22,12 @@ import { SequelizeAttributes } from './types'
|
||||
|
||||
export const allowedTimeWarningFlags = 1 | 2 | 4 | 8 | 16
|
||||
|
||||
export const categoryFlags = {
|
||||
hasBlockedNetworkList: 1
|
||||
}
|
||||
|
||||
export const maxCategoryFlags = categoryFlags.hasBlockedNetworkList
|
||||
|
||||
export interface CategoryAttributesVersion1 {
|
||||
familyId: string
|
||||
categoryId: string
|
||||
@@ -73,10 +79,14 @@ export interface CategoryAttributesVersion10 {
|
||||
taskListVersion: string
|
||||
}
|
||||
|
||||
export interface CategoryAttributesVersion11 {
|
||||
flags: string
|
||||
}
|
||||
|
||||
export type CategoryAttributes = CategoryAttributesVersion1 & CategoryAttributesVersion2 &
|
||||
CategoryAttributesVersion3 & CategoryAttributesVersion4 & CategoryAttributesVersion5 &
|
||||
CategoryAttributesVersion6 & CategoryAttributesVersion7 & CategoryAttributesVersion8 &
|
||||
CategoryAttributesVersion9 & CategoryAttributesVersion10
|
||||
CategoryAttributesVersion9 & CategoryAttributesVersion10 & CategoryAttributesVersion11
|
||||
|
||||
export type CategoryModel = Sequelize.Model<CategoryAttributes> & CategoryAttributes
|
||||
export type CategoryModelStatic = typeof Sequelize.Model & {
|
||||
@@ -210,6 +220,18 @@ export const attributesVersion10: SequelizeAttributes<CategoryAttributesVersion1
|
||||
}
|
||||
}
|
||||
|
||||
export const attributesVersion11: SequelizeAttributes<CategoryAttributesVersion11> = {
|
||||
flags: {
|
||||
type: Sequelize.BIGINT,
|
||||
allowNull: false,
|
||||
defaultValue: 0,
|
||||
validate: {
|
||||
min: 0,
|
||||
max: maxCategoryFlags
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const attributes: SequelizeAttributes<CategoryAttributes> = {
|
||||
...attributesVersion1,
|
||||
...attributesVersion2,
|
||||
@@ -220,7 +242,8 @@ export const attributes: SequelizeAttributes<CategoryAttributes> = {
|
||||
...attributesVersion7,
|
||||
...attributesVersion8,
|
||||
...attributesVersion9,
|
||||
...attributesVersion10
|
||||
...attributesVersion10,
|
||||
...attributesVersion11
|
||||
}
|
||||
|
||||
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 - 2021 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 { attributesVersion11 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', 'flags', {
|
||||
...categoryAttributes.flags
|
||||
}, {
|
||||
transaction
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
export async function down (queryInterface: QueryInterface, sequelize: Sequelize) {
|
||||
await sequelize.transaction({
|
||||
type: Transaction.TYPES.EXCLUSIVE
|
||||
}, async (transaction) => {
|
||||
await queryInterface.removeColumn('Categories', 'flags', { transaction })
|
||||
})
|
||||
}
|
||||
@@ -79,7 +79,8 @@ export async function dispatchCreateCategory ({ action, cache, fromChildSelfLimi
|
||||
disableLimitsUntil: '0',
|
||||
taskListVersion: generateVersionId(),
|
||||
minBatteryCharging: 0,
|
||||
minBatteryMobile: 0
|
||||
minBatteryMobile: 0,
|
||||
flags: '0'
|
||||
}, { transaction: cache.transaction })
|
||||
|
||||
// update the cache
|
||||
|
||||
@@ -50,6 +50,7 @@ import {
|
||||
UpdateCategoryBlockAllNotificationsAction,
|
||||
UpdateCategoryBlockedTimesAction,
|
||||
UpdateCategoryDisableLimitsAction,
|
||||
UpdateCategoryFlagsAction,
|
||||
UpdateCategorySortingAction,
|
||||
UpdateCategoryTemporarilyBlockedAction,
|
||||
UpdateCategoryTimeWarningsAction,
|
||||
@@ -100,6 +101,7 @@ import { dispatchUpdateCategoryBatteryLimit } from './updatecategorybatterylimit
|
||||
import { dispatchUpdateCategoryBlockAllNotifications } from './updatecategoryblockallnotifications'
|
||||
import { dispatchUpdateCategoryBlockedTimes } from './updatecategoryblockedtimes'
|
||||
import { dispatchUpdateCategoryDisableLimits } from './updatecategorydisablelimits'
|
||||
import { dispatchUpdateCategoryFlagsAction } from './updatecategoryflags'
|
||||
import { dispatchUpdateCategorySorting } from './updatecategorysorting'
|
||||
import { dispatchUpdateCategoryTemporarilyBlocked } from './updatecategorytemporarilyblocked'
|
||||
import { dispatchUpdateCategoryTimeWarnings } from './updatecategorytimewarnings'
|
||||
@@ -178,6 +180,8 @@ export const dispatchParentAction = async ({ action, cache, parentUserId, source
|
||||
return dispatchSetUserTimezone({ action, cache })
|
||||
} else if (action instanceof UpdateCategoryBatteryLimitAction) {
|
||||
return dispatchUpdateCategoryBatteryLimit({ action, cache })
|
||||
} else if (action instanceof UpdateCategoryFlagsAction) {
|
||||
return dispatchUpdateCategoryFlagsAction({ action, cache })
|
||||
} else if (action instanceof UpdateCategorySortingAction) {
|
||||
return dispatchUpdateCategorySorting({ action, cache })
|
||||
} else if (action instanceof IncrementCategoryExtraTimeAction) {
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* server component for the TimeLimit App
|
||||
* Copyright (C) 2019 - 2021 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 { UpdateCategoryFlagsAction } from '../../../../action'
|
||||
import { Cache } from '../cache'
|
||||
import { IllegalStateException } from '../exception/illegal-state'
|
||||
import { MissingCategoryException } from '../exception/missing-item'
|
||||
|
||||
export async function dispatchUpdateCategoryFlagsAction ({ action, cache }: {
|
||||
action: UpdateCategoryFlagsAction
|
||||
cache: Cache
|
||||
}) {
|
||||
const categoryEntry = await cache.database.category.findOne({
|
||||
where: {
|
||||
familyId: cache.familyId,
|
||||
categoryId: action.categoryId
|
||||
},
|
||||
transaction: cache.transaction
|
||||
})
|
||||
|
||||
if (!categoryEntry) {
|
||||
throw new MissingCategoryException()
|
||||
}
|
||||
|
||||
const oldFlags = parseInt(categoryEntry.flags, 10)
|
||||
|
||||
if (!Number.isSafeInteger(oldFlags)) {
|
||||
throw new IllegalStateException({ staticMessage: 'oldFlags is not a safe integer' })
|
||||
}
|
||||
|
||||
const newFlags = (oldFlags & ~action.modifiedBits) | action.newValues
|
||||
|
||||
categoryEntry.flags = newFlags.toString(10)
|
||||
|
||||
await categoryEntry.save({ transaction: cache.transaction })
|
||||
|
||||
cache.categoriesWithModifiedBaseData.add(action.categoryId)
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* server component for the TimeLimit App
|
||||
* Copyright (C) 2019 - 2020 Jonas Lochmann
|
||||
* Copyright (C) 2019 - 2021 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
|
||||
@@ -51,7 +51,8 @@ export async function getCategoryBaseDatas ({
|
||||
'minBatteryMobile',
|
||||
'temporarilyBlockedEndTime',
|
||||
'sort',
|
||||
'disableLimitsUntil'
|
||||
'disableLimitsUntil',
|
||||
'flags'
|
||||
],
|
||||
transaction
|
||||
})).map((item) => ({
|
||||
@@ -70,7 +71,8 @@ export async function getCategoryBaseDatas ({
|
||||
minBatteryMobile: item.minBatteryMobile,
|
||||
temporarilyBlockedEndTime: item.temporarilyBlockedEndTime,
|
||||
sort: item.sort,
|
||||
disableLimitsUntil: item.disableLimitsUntil
|
||||
disableLimitsUntil: item.disableLimitsUntil,
|
||||
flags: item.flags
|
||||
}))
|
||||
|
||||
const networkIdsForSyncing = (await database.categoryNetworkId.findAll({
|
||||
@@ -114,6 +116,7 @@ export async function getCategoryBaseDatas ({
|
||||
itemId: network.networkItemId,
|
||||
hashedNetworkId: network.hashedNetworkId
|
||||
})),
|
||||
dlu: parseInt(item.disableLimitsUntil, 10)
|
||||
dlu: parseInt(item.disableLimitsUntil, 10),
|
||||
flags: parseInt(item.flags, 10)
|
||||
}))
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* server component for the TimeLimit App
|
||||
* Copyright (C) 2019 - 2020 Jonas Lochmann
|
||||
* Copyright (C) 2019 - 2021 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
|
||||
@@ -117,6 +117,7 @@ export interface ServerUpdatedCategoryBaseData {
|
||||
networks: Array<ServerCategoryNetworkId>
|
||||
// disable limits until
|
||||
dlu: number
|
||||
flags: number
|
||||
}
|
||||
|
||||
export interface ServerCategoryNetworkId {
|
||||
|
||||
Reference in New Issue
Block a user