mirror of
https://codeberg.org/timelimit/timelimit-server.git
synced 2026-08-31 19:03:45 +02:00
Add support for more limits
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* server component for the TimeLimit App
|
||||
* Copyright (C) 2019 Jonas Lochmann
|
||||
* 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
|
||||
@@ -17,6 +17,7 @@
|
||||
|
||||
import * as Sequelize from 'sequelize'
|
||||
import { AddUsedTimeAction } from '../../../../action'
|
||||
import { MinuteOfDay } from '../../../../util/minuteofday'
|
||||
import { Cache } from '../cache'
|
||||
|
||||
export const getRoundedTimestamp = () => {
|
||||
@@ -68,7 +69,9 @@ export async function dispatchAddUsedTime ({ deviceId, action, cache }: {
|
||||
where: {
|
||||
familyId: cache.familyId,
|
||||
categoryId: categoryId,
|
||||
dayOfEpoch: action.dayOfEpoch
|
||||
dayOfEpoch: action.dayOfEpoch,
|
||||
startMinuteOfDay: MinuteOfDay.MIN,
|
||||
endMinuteOfDay: MinuteOfDay.MAX
|
||||
},
|
||||
transaction: cache.transaction
|
||||
})
|
||||
@@ -80,7 +83,9 @@ export async function dispatchAddUsedTime ({ deviceId, action, cache }: {
|
||||
categoryId: categoryId,
|
||||
dayOfEpoch: action.dayOfEpoch,
|
||||
usedTime: action.timeToAdd,
|
||||
lastUpdate: roundedTimestamp
|
||||
lastUpdate: roundedTimestamp,
|
||||
startMinuteOfDay: MinuteOfDay.MIN,
|
||||
endMinuteOfDay: MinuteOfDay.MAX
|
||||
}, {
|
||||
transaction: cache.transaction
|
||||
})
|
||||
|
||||
@@ -17,15 +17,23 @@
|
||||
|
||||
import * as Sequelize from 'sequelize'
|
||||
import { AddUsedTimeActionVersion2 } from '../../../../action'
|
||||
import { MinuteOfDay } from '../../../../util/minuteofday'
|
||||
import { Cache } from '../cache'
|
||||
import { getRoundedTimestamp } from './addusedtime'
|
||||
import { getRoundedTimestamp as getRoundedTimestampForUsedTime } from './addusedtime'
|
||||
|
||||
export const getRoundedTimestampForSessionDuration = () => {
|
||||
const now = Date.now()
|
||||
|
||||
return now - (now % (1000 * 60 * 60 * 12 /* 12 hours */))
|
||||
}
|
||||
|
||||
export async function dispatchAddUsedTimeVersion2 ({ deviceId, action, cache }: {
|
||||
deviceId: string
|
||||
action: AddUsedTimeActionVersion2
|
||||
cache: Cache
|
||||
}) {
|
||||
const roundedTimestamp = getRoundedTimestamp().toString(10)
|
||||
const roundedTimestampForUsedTime = getRoundedTimestampForUsedTime().toString(10)
|
||||
const roundedTimestampForSessionDuration = getRoundedTimestampForSessionDuration().toString(10)
|
||||
|
||||
for (let i = 0; i < action.items.length; i++) {
|
||||
const item = action.items[i]
|
||||
@@ -41,6 +49,7 @@ export async function dispatchAddUsedTimeVersion2 ({ deviceId, action, cache }:
|
||||
'extraTimeInMillis'
|
||||
]
|
||||
})
|
||||
|
||||
// verify that the category exists
|
||||
if (!categoryEntryUnsafe) {
|
||||
cache.requireFullSync()
|
||||
@@ -53,16 +62,19 @@ export async function dispatchAddUsedTimeVersion2 ({ deviceId, action, cache }:
|
||||
extraTimeInMillis: categoryEntryUnsafe.extraTimeInMillis
|
||||
}
|
||||
|
||||
if (item.timeToAdd !== 0) {
|
||||
// tslint:disable-next-line:no-inner-declarations
|
||||
async function handle (start: number, end: number) {
|
||||
// try to update first
|
||||
const [updatedRows] = await cache.database.usedTime.update({
|
||||
usedTime: Sequelize.literal(`usedTime + ${item.timeToAdd}`) as any,
|
||||
lastUpdate: roundedTimestamp
|
||||
lastUpdate: roundedTimestampForUsedTime
|
||||
}, {
|
||||
where: {
|
||||
familyId: cache.familyId,
|
||||
categoryId: item.categoryId,
|
||||
dayOfEpoch: action.dayOfEpoch
|
||||
dayOfEpoch: action.dayOfEpoch,
|
||||
startMinuteOfDay: start,
|
||||
endMinuteOfDay: end
|
||||
},
|
||||
transaction: cache.transaction
|
||||
})
|
||||
@@ -74,15 +86,75 @@ export async function dispatchAddUsedTimeVersion2 ({ deviceId, action, cache }:
|
||||
categoryId: item.categoryId,
|
||||
dayOfEpoch: action.dayOfEpoch,
|
||||
usedTime: item.timeToAdd,
|
||||
lastUpdate: roundedTimestamp
|
||||
lastUpdate: roundedTimestampForUsedTime,
|
||||
startMinuteOfDay: start,
|
||||
endMinuteOfDay: end
|
||||
}, {
|
||||
transaction: cache.transaction
|
||||
})
|
||||
}
|
||||
|
||||
cache.categoriesWithModifiedUsedTimes.push(item.categoryId)
|
||||
}
|
||||
|
||||
await handle(MinuteOfDay.MIN, MinuteOfDay.MAX)
|
||||
|
||||
for (let j = 0; j < item.additionalCountingSlots.length; j++) {
|
||||
const slot = item.additionalCountingSlots[j]
|
||||
|
||||
await handle(slot.start, slot.end)
|
||||
}
|
||||
|
||||
const hasTrustedTimestamp = action.trustedTimestamp !== 0
|
||||
|
||||
for (let j = 0; j < item.sessionDurationLimits.length; j++) {
|
||||
const limit = item.sessionDurationLimits[j]
|
||||
|
||||
const oldItem = await cache.database.sessionDuration.findOne({
|
||||
where: {
|
||||
familyId: cache.familyId,
|
||||
categoryId: item.categoryId,
|
||||
maxSessionDuration: limit.duration,
|
||||
sessionPauseDuration: limit.pause,
|
||||
startMinuteOfDay: limit.start,
|
||||
endMinuteOfDay: limit.end
|
||||
},
|
||||
transaction: cache.transaction
|
||||
})
|
||||
|
||||
if (oldItem) {
|
||||
if (hasTrustedTimestamp) {
|
||||
oldItem.lastUsage = action.trustedTimestamp.toString(10)
|
||||
}
|
||||
|
||||
if (
|
||||
hasTrustedTimestamp &&
|
||||
action.trustedTimestamp > parseInt(oldItem.lastUsage, 10) + oldItem.sessionPauseDuration
|
||||
) {
|
||||
oldItem.lastSessionDuration = item.timeToAdd
|
||||
} else {
|
||||
oldItem.lastSessionDuration = oldItem.lastSessionDuration + item.timeToAdd
|
||||
}
|
||||
|
||||
oldItem.roundedLastUpdate = roundedTimestampForSessionDuration
|
||||
|
||||
await oldItem.save({ transaction: cache.transaction })
|
||||
} else {
|
||||
await cache.database.sessionDuration.create({
|
||||
familyId: cache.familyId,
|
||||
categoryId: item.categoryId,
|
||||
maxSessionDuration: limit.duration,
|
||||
sessionPauseDuration: limit.pause,
|
||||
startMinuteOfDay: limit.start,
|
||||
endMinuteOfDay: limit.end,
|
||||
// end of primary key
|
||||
lastUsage: action.trustedTimestamp,
|
||||
lastSessionDuration: item.timeToAdd,
|
||||
roundedLastUpdate: roundedTimestampForSessionDuration
|
||||
}, { transaction: cache.transaction })
|
||||
}
|
||||
}
|
||||
|
||||
cache.categoriesWithModifiedUsedTimes.push(item.categoryId)
|
||||
|
||||
if (item.extraTimeToSubtract !== 0) {
|
||||
await cache.database.category.update({
|
||||
extraTimeInMillis: Math.max(0, categoryEntry.extraTimeInMillis - item.extraTimeToSubtract)
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* server component for the TimeLimit App
|
||||
* Copyright (C) 2019 Jonas Lochmann
|
||||
* 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
|
||||
@@ -34,7 +34,11 @@ export async function dispatchCreateTimeLimitRule ({ action, cache }: {
|
||||
categoryId: action.rule.categoryId,
|
||||
applyToExtraTimeUsage: action.rule.applyToExtraTimeUsage,
|
||||
maximumTimeInMillis: action.rule.maxTimeInMillis,
|
||||
dayMaskAsBitmask: action.rule.dayMask
|
||||
dayMaskAsBitmask: action.rule.dayMask,
|
||||
startMinuteOfDay: action.rule.start,
|
||||
endMinuteOfDay: action.rule.end,
|
||||
sessionDurationMilliseconds: action.rule.sessionDurationMilliseconds,
|
||||
sessionPauseMilliseconds: action.rule.sessionPauseMilliseconds
|
||||
}, { transaction: cache.transaction })
|
||||
|
||||
cache.categoriesWithModifiedTimeLimitRules.push(action.rule.categoryId)
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* server component for the TimeLimit App
|
||||
* Copyright (C) 2019 Jonas Lochmann
|
||||
* 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
|
||||
@@ -37,6 +37,10 @@ export async function dispatchUpdateTimelimitRule ({ action, cache }: {
|
||||
ruleEntry.applyToExtraTimeUsage = action.applyToExtraTimeUsage
|
||||
ruleEntry.dayMaskAsBitmask = action.dayMask
|
||||
ruleEntry.maximumTimeInMillis = action.maximumTimeInMillis
|
||||
ruleEntry.startMinuteOfDay = action.start
|
||||
ruleEntry.endMinuteOfDay = action.end
|
||||
ruleEntry.sessionDurationMilliseconds = action.sessionDurationMilliseconds
|
||||
ruleEntry.sessionPauseMilliseconds = action.sessionPauseMilliseconds
|
||||
|
||||
await ruleEntry.save({ transaction: cache.transaction })
|
||||
|
||||
|
||||
@@ -25,6 +25,7 @@ import {
|
||||
ServerUpdatedCategoryBaseData, ServerUpdatedCategoryUsedTimes,
|
||||
ServerUpdatedTimeLimitRules
|
||||
} from '../../object/serverdatastatus'
|
||||
import { MinuteOfDay } from '../../util/minuteofday'
|
||||
|
||||
export const generateServerDataStatus = async ({ database, clientStatus, familyId, transaction }: {
|
||||
database: Database,
|
||||
@@ -429,7 +430,11 @@ export const generateServerDataStatus = async ({ database, clientStatus, familyI
|
||||
'categoryId',
|
||||
'applyToExtraTimeUsage',
|
||||
'maximumTimeInMillis',
|
||||
'dayMaskAsBitmask'
|
||||
'dayMaskAsBitmask',
|
||||
'startMinuteOfDay',
|
||||
'endMinuteOfDay',
|
||||
'sessionDurationMilliseconds',
|
||||
'sessionPauseMilliseconds'
|
||||
],
|
||||
transaction
|
||||
})).map((item) => ({
|
||||
@@ -437,7 +442,11 @@ export const generateServerDataStatus = async ({ database, clientStatus, familyI
|
||||
categoryId: item.categoryId,
|
||||
applyToExtraTimeUsage: item.applyToExtraTimeUsage,
|
||||
maximumTimeInMillis: item.maximumTimeInMillis,
|
||||
dayMaskAsBitmask: item.dayMaskAsBitmask
|
||||
dayMaskAsBitmask: item.dayMaskAsBitmask,
|
||||
startMinuteOfDay: item.startMinuteOfDay,
|
||||
endMinuteOfDay: item.endMinuteOfDay,
|
||||
sessionDurationMilliseconds: item.sessionDurationMilliseconds,
|
||||
sessionPauseMilliseconds: item.sessionPauseMilliseconds
|
||||
}))
|
||||
|
||||
const getCategoryRulesVersion = (categoryId: string) => {
|
||||
@@ -456,26 +465,65 @@ export const generateServerDataStatus = async ({ database, clientStatus, familyI
|
||||
id: item.ruleId,
|
||||
extraTime: item.applyToExtraTimeUsage,
|
||||
dayMask: item.dayMaskAsBitmask,
|
||||
maxTime: item.maximumTimeInMillis
|
||||
maxTime: item.maximumTimeInMillis,
|
||||
start: item.startMinuteOfDay,
|
||||
end: item.endMinuteOfDay,
|
||||
session: item.sessionDurationMilliseconds,
|
||||
pause: item.sessionPauseMilliseconds
|
||||
})),
|
||||
version: getCategoryRulesVersion(categoryId)
|
||||
}))
|
||||
}
|
||||
|
||||
if (categoryIdsToSyncUsedTimes.length > 0) {
|
||||
const dataForSyncing = (await database.usedTime.findAll({
|
||||
const usedTimesForSyncing = (await database.usedTime.findAll({
|
||||
where: {
|
||||
familyId,
|
||||
categoryId: {
|
||||
[Sequelize.Op.in]: categoryIdsToSyncUsedTimes
|
||||
},
|
||||
...(clientStatus.clientLevel === undefined || clientStatus.clientLevel < 2) ? {
|
||||
startMinuteOfDay: MinuteOfDay.MIN,
|
||||
endMinuteOfDay: MinuteOfDay.MAX
|
||||
} : {}
|
||||
},
|
||||
attributes: [
|
||||
'categoryId', 'dayOfEpoch', 'usedTime', 'startMinuteOfDay', 'endMinuteOfDay'
|
||||
],
|
||||
transaction
|
||||
})).map((item) => ({
|
||||
categoryId: item.categoryId,
|
||||
dayOfEpoch: item.dayOfEpoch,
|
||||
usedTime: item.usedTime,
|
||||
startMinuteOfDay: item.startMinuteOfDay,
|
||||
endMinuteOfDay: item.endMinuteOfDay
|
||||
}))
|
||||
|
||||
const sessionDurationsForSyncing = (await database.sessionDuration.findAll({
|
||||
where: {
|
||||
familyId,
|
||||
categoryId: {
|
||||
[Sequelize.Op.in]: categoryIdsToSyncUsedTimes
|
||||
}
|
||||
},
|
||||
attributes: ['categoryId', 'dayOfEpoch', 'usedTime'],
|
||||
attributes: [
|
||||
'categoryId',
|
||||
'maxSessionDuration',
|
||||
'sessionPauseDuration',
|
||||
'startMinuteOfDay',
|
||||
'endMinuteOfDay',
|
||||
'lastUsage',
|
||||
'lastSessionDuration'
|
||||
],
|
||||
transaction
|
||||
})).map((item) => ({
|
||||
categoryId: item.categoryId,
|
||||
dayOfEpoch: item.dayOfEpoch,
|
||||
usedTime: item.usedTime
|
||||
maxSessionDuration: item.maxSessionDuration,
|
||||
sessionPauseDuration: item.sessionPauseDuration,
|
||||
startMinuteOfDay: item.startMinuteOfDay,
|
||||
endMinuteOfDay: item.endMinuteOfDay,
|
||||
lastUsage: item.lastUsage,
|
||||
lastSessionDuration: item.lastSessionDuration
|
||||
}))
|
||||
|
||||
const getCategoryUsedTimesVersion = (categoryId: string) => {
|
||||
@@ -490,9 +538,19 @@ export const generateServerDataStatus = async ({ database, clientStatus, familyI
|
||||
|
||||
result.usedTimes = categoryIdsToSyncUsedTimes.map((categoryId): ServerUpdatedCategoryUsedTimes => ({
|
||||
categoryId,
|
||||
times: dataForSyncing.filter((item) => item.categoryId === categoryId).map((item) => ({
|
||||
times: usedTimesForSyncing.filter((item) => item.categoryId === categoryId).map((item) => ({
|
||||
day: item.dayOfEpoch,
|
||||
time: item.usedTime
|
||||
time: item.usedTime,
|
||||
start: item.startMinuteOfDay,
|
||||
end: item.endMinuteOfDay
|
||||
})),
|
||||
sessionDurations: sessionDurationsForSyncing.filter((item) => item.categoryId === categoryId).map((item) => ({
|
||||
md: item.maxSessionDuration,
|
||||
spd: item.sessionPauseDuration,
|
||||
sm: item.startMinuteOfDay,
|
||||
em: item.endMinuteOfDay,
|
||||
l: parseInt(item.lastUsage, 10),
|
||||
d: item.lastSessionDuration
|
||||
})),
|
||||
version: getCategoryUsedTimesVersion(categoryId)
|
||||
}))
|
||||
|
||||
Reference in New Issue
Block a user