-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #92 from linagora/client/server-devices
feat: get and put methods for device related APIs developped and tested
- Loading branch information
Showing
4 changed files
with
435 additions
and
76 deletions.
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
packages/matrix-client-server/src/devices/changeDevices.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) | ||
}) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) | ||
} | ||
} |
Oops, something went wrong.