mirror of
https://codeberg.org/timelimit/timelimit-server.git
synced 2026-08-31 19:03:45 +02:00
Add server support for disabling limitations temporarily per category
This commit is contained in:
@@ -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)
|
||||
}))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user