Skip to content

Commit

Permalink
Merge pull request #92 from linagora/client/server-devices
Browse files Browse the repository at this point in the history
feat: get and put methods for device related APIs developped and tested
  • Loading branch information
guimard authored Jul 8, 2024
2 parents 66a2cf9 + 43f023c commit a843b43
Show file tree
Hide file tree
Showing 4 changed files with 435 additions and 76 deletions.
59 changes: 59 additions & 0 deletions packages/matrix-client-server/src/devices/changeDevices.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import {
errMsg,
type expressAppHandler,
jsonContent,
send,
validateParameters
} from '@twake/utils'
import type MatrixClientServer from '../index'
import { type Request } from 'express'

const schema = {
display_name: true
}

interface changeDeviceNameArgs {
display_name: string
}

export const changeDeviceName = (
clientServer: MatrixClientServer
): expressAppHandler => {
return (req, res) => {
const deviceId: string = (req as Request).params.deviceId
clientServer.authenticate(req, res, (data, id) => {
const userId = data.sub
jsonContent(req, res, clientServer.logger, (obj) => {
validateParameters(res, schema, obj, clientServer.logger, (obj) => {
// eslint-disable-next-line @typescript-eslint/naming-convention
const _display_name = (obj as changeDeviceNameArgs).display_name

clientServer.matrixDb
.updateWithConditions('devices', { display_name: _display_name }, [
{ field: 'device_id', value: deviceId },
{ field: 'user_id', value: userId }
])
.then((rows) => {
if (rows.length === 0) {
send(
res,
404,
errMsg(
'notFound',
'The current user has no device with the given ID'
)
)
} else {
clientServer.logger.debug('Device Name updated')
send(res, 200, {})
}
})
.catch((e) => {
/* istanbul ignore next */
clientServer.logger.error('Error querying profiles:', e)
})
})
})
})
}
}
74 changes: 74 additions & 0 deletions packages/matrix-client-server/src/devices/getDevices.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { errMsg, type expressAppHandler, send } from '@twake/utils'
import type MatrixClientServer from '../index'
import { type Request } from 'express'

export const getDevices = (
clientServer: MatrixClientServer
): expressAppHandler => {
return (req, res) => {
clientServer.authenticate(req, res, (data, id) => {
const userId = data.sub

clientServer.matrixDb
.get('devices', ['device_id', 'display_name', 'last_seen', 'ip'], {
user_id: userId
})
.then((rows) => {
// returns a list of devices
const _devices = rows.map((row) => {
return {
device_id: row.device_id,
display_name: row.display_name,
last_seen_ip: row.ip,
last_seen_ts: row.last_seen
}
})
send(res, 200, { devices: _devices })
})
.catch((e) => {
/* istanbul ignore next */
clientServer.logger.error('Error querying devices:', e)
})
})
}
}

export const getDeviceInfo = (
clientServer: MatrixClientServer
): expressAppHandler => {
return (req, res) => {
clientServer.authenticate(req, res, (data, id) => {
const userId = data.sub
const deviceId: string = (req as Request).params.deviceId

clientServer.matrixDb
.get('devices', ['display_name', 'last_seen', 'ip'], {
user_id: userId,
device_id: deviceId
})
.then((rows) => {
if (rows.length === 0) {
send(
res,
404,
errMsg(
'notFound',
'The current user has no device with the given ID'
)
)
} else {
send(res, 200, {
device_id: deviceId,
display_name: rows[0].display_name,
last_seen_ip: rows[0].ip,
last_seen_ts: rows[0].last_seen
})
}
})
.catch((e) => {
/* istanbul ignore next */
clientServer.logger.error('Error querying devices:', e)
})
})
}
}
Loading

0 comments on commit a843b43

Please sign in to comment.