mirror of
https://codeberg.org/timelimit/timelimit-server.git
synced 2026-08-31 19:03:45 +02:00
Add ping support
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* server component for the TimeLimit App
|
||||
* Copyright (C) 2019 - 2022 Jonas Lochmann
|
||||
* Copyright (C) 2019 - 2026 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
|
||||
@@ -23,6 +23,7 @@ import {
|
||||
FinishKeyRequestAction,
|
||||
ForceSyncAction,
|
||||
MarkTaskPendingAction,
|
||||
PingAction,
|
||||
ReplyToKeyRequestAction,
|
||||
RemoveInstalledAppsAction,
|
||||
SendKeyRequestAction,
|
||||
@@ -41,6 +42,7 @@ import { dispatchAddUsedTimeVersion2 } from './addusedtime2'
|
||||
import { dispatchFinishKeyRequestAction } from './finishkeyrequest'
|
||||
import { dispatchForceSyncAction } from './forcesync'
|
||||
import { dispatchMarkTaskPendingAction } from './marktaskpendingaction'
|
||||
import { dispatchPingAction } from './ping'
|
||||
import { dispatchReplyToKeyRequestAction } from './replytokeyrequest'
|
||||
import { dispatchSendKeyRequestAction } from './sendkeyrequest'
|
||||
import { dispatchSignOutAtDevice } from './signoutatdevice'
|
||||
@@ -67,6 +69,8 @@ export const dispatchAppLogicAction = async ({ action, deviceId, cache, eventHan
|
||||
await dispatchForceSyncAction({ deviceId, action, cache })
|
||||
} else if (action instanceof MarkTaskPendingAction) {
|
||||
await dispatchMarkTaskPendingAction({ deviceId, action, cache })
|
||||
} else if (action instanceof PingAction) {
|
||||
await dispatchPingAction({ deviceId, action, cache })
|
||||
} else if (action instanceof ReplyToKeyRequestAction) {
|
||||
await dispatchReplyToKeyRequestAction({ deviceId, action, cache, eventHandler })
|
||||
} else if (action instanceof RemoveInstalledAppsAction) {
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
/*
|
||||
* server component for the TimeLimit App
|
||||
* Copyright (C) 2019 - 2026 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 { PingAction } from '../../../../action'
|
||||
import { types as pingTypes } from '../../../../database/ping'
|
||||
import { Cache } from '../cache'
|
||||
|
||||
export async function dispatchPingAction ({ action, cache, deviceId }: {
|
||||
deviceId: string
|
||||
action: PingAction
|
||||
cache: Cache
|
||||
}) {
|
||||
// handle insertion
|
||||
if (action.event === 'ping' || action.event === 'pong') {
|
||||
let type
|
||||
|
||||
if (action.event === 'ping') type = pingTypes.ping
|
||||
else if (action.event === 'pong') type = pingTypes.pong
|
||||
else throw new Error()
|
||||
|
||||
// delete any previous ping/pong
|
||||
await cache.database.ping.destroy({
|
||||
where: {
|
||||
familyId: cache.familyId,
|
||||
receiverDeviceId: action.deviceId,
|
||||
senderDeviceId: deviceId,
|
||||
type: type
|
||||
},
|
||||
transaction: cache.transaction
|
||||
})
|
||||
|
||||
// insert
|
||||
await cache.database.ping.create({
|
||||
familyId: cache.familyId,
|
||||
receiverDeviceId: action.deviceId,
|
||||
senderDeviceId: deviceId,
|
||||
type,
|
||||
token: action.token
|
||||
}, {
|
||||
transaction: cache.transaction
|
||||
})
|
||||
|
||||
// notify
|
||||
cache.incrementTargetedTriggeredSyncLevel(action.deviceId, 2)
|
||||
}
|
||||
|
||||
// handle deleting
|
||||
if (action.event === 'pong' || action.event === 'clear') {
|
||||
await cache.database.ping.destroy({
|
||||
where: {
|
||||
familyId: cache.familyId,
|
||||
receiverDeviceId: deviceId,
|
||||
senderDeviceId: action.deviceId,
|
||||
token: action.token
|
||||
},
|
||||
transaction: cache.transaction
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* server component for the TimeLimit App
|
||||
* Copyright (C) 2019 - 2024 Jonas Lochmann
|
||||
* Copyright (C) 2019 - 2026 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
|
||||
@@ -30,6 +30,7 @@ import { getDeviceDetailList } from './device-detail'
|
||||
import { getDeviceList } from './device-list'
|
||||
import { getDeviceDhKeys } from './dh-keys'
|
||||
import { getFamilyEntry } from './family-entry'
|
||||
import { getPings } from './pings'
|
||||
import { getUserList } from './user-list'
|
||||
import { getKeyRequests } from './key-requests'
|
||||
import { getKeyResponses } from './key-responses'
|
||||
@@ -52,13 +53,14 @@ export const generateServerDataStatus = async ({
|
||||
const doesClientSupportCryptoApps = clientLevel >= 4
|
||||
const doesClientSupportDh = clientLevel >= 5
|
||||
const doesClientSupportU2f = clientLevel >= 6
|
||||
const doesClientSupportPing = clientLevel >= 7
|
||||
|
||||
const result: ServerDataStatus = {
|
||||
fullVersion: config.alwaysPro ? 1 : (
|
||||
familyEntry.hasFullVersion ? parseInt(familyEntry.fullVersionUntil, 10) : 0
|
||||
),
|
||||
message: await getStatusMessage({ database, transaction }) || undefined,
|
||||
apiLevel: 8
|
||||
apiLevel: 9
|
||||
}
|
||||
|
||||
if (familyEntry.deviceListVersion !== clientStatus.devices) {
|
||||
@@ -160,5 +162,14 @@ export const generateServerDataStatus = async ({
|
||||
}) || undefined
|
||||
}
|
||||
|
||||
if (doesClientSupportPing) {
|
||||
result.pings = await getPings({
|
||||
database,
|
||||
transaction,
|
||||
familyEntry,
|
||||
deviceId
|
||||
})
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* server component for the TimeLimit App
|
||||
* Copyright (C) 2019 - 2026 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 { Database } from '../../../database'
|
||||
import { types as pingTypes } from '../../../database/ping'
|
||||
import { ServerPing } from '../../../object/serverdatastatus'
|
||||
import { FamilyEntry } from './family-entry'
|
||||
|
||||
export async function getPings ({
|
||||
database, transaction, familyEntry, deviceId
|
||||
}: {
|
||||
database: Database
|
||||
transaction: Sequelize.Transaction
|
||||
familyEntry: FamilyEntry
|
||||
deviceId: string
|
||||
}): Promise<Array<ServerPing>> {
|
||||
const savedData = await database.ping.findAll({
|
||||
where: {
|
||||
familyId: familyEntry.familyId,
|
||||
receiverDeviceId: deviceId,
|
||||
},
|
||||
attributes: ['senderDeviceId', 'type', 'token'],
|
||||
transaction
|
||||
})
|
||||
|
||||
return savedData.map((row) => ({
|
||||
deviceId: row.senderDeviceId,
|
||||
type: convertType(row.type),
|
||||
token: row.token
|
||||
}))
|
||||
}
|
||||
|
||||
function convertType(type: number): 'ping' | 'pong' {
|
||||
if (type === pingTypes.ping) return 'ping'
|
||||
else if (type === pingTypes.pong) return 'pong'
|
||||
else throw new Error()
|
||||
}
|
||||
Reference in New Issue
Block a user