Skip to content

Commit

Permalink
Merge pull request #94 from linagora/api-get-event-id
Browse files Browse the repository at this point in the history
feat : added getEventId api
  • Loading branch information
guimard authored Jul 8, 2024
2 parents 01d4f99 + a77c27d commit 9c114d5
Show file tree
Hide file tree
Showing 6 changed files with 499 additions and 22 deletions.
2 changes: 2 additions & 0 deletions packages/matrix-client-server/src/__testData__/buildUserDB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ const matrixDbQueries = [
'CREATE TABLE IF NOT EXISTS users( name TEXT, password_hash TEXT, creation_ts BIGINT, admin SMALLINT DEFAULT 0 NOT NULL, upgrade_ts BIGINT, is_guest SMALLINT DEFAULT 0 NOT NULL, appservice_id TEXT, consent_version TEXT, consent_server_notice_sent TEXT, user_type TEXT DEFAULT NULL, deactivated SMALLINT DEFAULT 0 NOT NULL, shadow_banned INT DEFAULT 0, consent_ts bigint, UNIQUE(name) )',
'CREATE TABLE IF NOT EXISTS user_ips ( user_id TEXT NOT NULL, access_token TEXT NOT NULL, device_id TEXT, ip TEXT NOT NULL, user_agent TEXT NOT NULL, last_seen BIGINT NOT NULL)',
'CREATE TABLE IF NOT EXISTS registration_tokens (token TEXT NOT NULL, uses_allowed INT, pending INT NOT NULL, completed INT NOT NULL, expiry_time BIGINT,UNIQUE (token))',
'CREATE TABLE IF NOT EXISTS events( stream_ordering INTEGER PRIMARY KEY, topological_ordering BIGINT NOT NULL, event_id TEXT NOT NULL, type TEXT NOT NULL, room_id TEXT NOT NULL, content TEXT, unrecognized_keys TEXT, processed BOOL NOT NULL, outlier BOOL NOT NULL, depth BIGINT DEFAULT 0 NOT NULL, origin_server_ts BIGINT, received_ts BIGINT, sender TEXT, contains_url BOOLEAN, instance_name TEXT, state_key TEXT DEFAULT NULL, rejection_reason TEXT DEFAULT NULL, UNIQUE (event_id) )',
'CREATE TABLE IF NOT EXISTS room_memberships( event_id TEXT NOT NULL, user_id TEXT NOT NULL, sender TEXT NOT NULL, room_id TEXT NOT NULL, membership TEXT NOT NULL, forgotten INTEGER DEFAULT 0, display_name TEXT, avatar_url TEXT, UNIQUE (event_id) )',
'CREATE TABLE IF NOT EXISTS devices (user_id TEXT NOT NULL, device_id TEXT NOT NULL, display_name TEXT, last_seen BIGINT, ip TEXT, user_agent TEXT, hidden BOOLEAN DEFAULT 0,CONSTRAINT device_uniqueness UNIQUE (user_id, device_id))',
'CREATE TABLE IF NOT EXISTS account_data( user_id TEXT NOT NULL, account_data_type TEXT NOT NULL, stream_id BIGINT NOT NULL, content TEXT NOT NULL, instance_name TEXT, CONSTRAINT account_data_uniqueness UNIQUE (user_id, account_data_type))'
]
Expand Down
200 changes: 188 additions & 12 deletions packages/matrix-client-server/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ jest.mock('nodemailer', () => ({
let conf: Config
let clientServer: ClientServer
let app: express.Application
let validToken: string

const logger: TwakeLogger = getLogger()

Expand Down Expand Up @@ -263,7 +262,45 @@ describe('Use configuration file', () => {
})
})

let validToken: string
let validToken2: string
let validToken3: string
describe('Endpoints with authentication', () => {
beforeAll(async () => {
validToken = randomString(64)
validToken2 = randomString(64)
validToken3 = randomString(64)
try {
await clientServer.matrixDb.insert('user_ips', {
user_id: '@testuser:example.com',
device_id: 'testdevice',
access_token: validToken,
ip: '127.0.0.1',
user_agent: 'curl/7.31.0-DEV',
last_seen: 1411996332123
})

await clientServer.matrixDb.insert('user_ips', {
user_id: '@testuser2:example.com',
device_id: 'testdevice2',
access_token: validToken2,
ip: '137.0.0.1',
user_agent: 'curl/7.31.0-DEV',
last_seen: 1411996332123
})

await clientServer.matrixDb.insert('user_ips', {
user_id: '@testuser3:example.com',
device_id: 'testdevice3',
access_token: validToken3,
ip: '147.0.0.1',
user_agent: 'curl/7.31.0-DEV',
last_seen: 1411996332123
})
} catch (e) {
logger.error('Error creating tokens for authentification', e)
}
})
describe('/_matrix/client/v3/account/whoami', () => {
it('should reject missing token (', async () => {
const response = await request(app)
Expand All @@ -286,15 +323,6 @@ describe('Use configuration file', () => {
expect(response.statusCode).toBe(401)
})
it('should accept valid token', async () => {
validToken = randomString(64)
await clientServer.matrixDb.insert('user_ips', {
user_id: '@testuser:example.com',
device_id: 'testdevice',
access_token: validToken,
ip: '127.0.0.1',
user_agent: 'curl/7.31.0-DEV',
last_seen: 1411996332123
})
await clientServer.matrixDb.insert('users', {
name: '@testuser:example.com',
password_hash: 'hashedpassword',
Expand Down Expand Up @@ -366,11 +394,11 @@ describe('Use configuration file', () => {
])
})
it('should work if the user has multiple devices and with multiple sessions', async () => {
const validToken2 = randomString(64)
const validTokenbis = randomString(64)
await clientServer.matrixDb.insert('user_ips', {
user_id: '@testuser:example.com',
device_id: 'testdevice2',
access_token: validToken2,
access_token: validTokenbis,
ip: '127.0.0.1',
last_seen: 1411996332123,
user_agent: 'curl/7.31.0-DEV'
Expand Down Expand Up @@ -1064,5 +1092,153 @@ describe('Use configuration file', () => {
})
})
})

describe('/_matrix/client/v3/rooms', () => {
describe('/_matrix/client/v3/rooms/{roomId}', () => {
describe('/_matrix/client/v3/rooms/{roomId}/event/{eventId}', () => {
beforeAll(async () => {
try {
await clientServer.matrixDb.insert('events', {
event_id: 'event_to_retrieve',
room_id: '!testroom:example.com',
sender: '@sender:example.com',
type: 'm.room.message',
state_key: '',
origin_server_ts: 1000,
content: '{ body: test message }',
topological_ordering: 0,
processed: 1,
outlier: 0
})

await clientServer.matrixDb.insert('room_memberships', {
room_id: '!testroom:example.com',
user_id: '@testuser:example.com',
membership: 'join',
event_id: 'adding_user',
sender: '@admin:example.com'
})

await clientServer.matrixDb.insert('events', {
event_id: 'adding_user',
room_id: '!testroom:example.com',
sender: '@admin:example.com',
type: 'm.room.message',
origin_server_ts: 0,
content: JSON.stringify({ body: 'test message' }),
topological_ordering: 0,
processed: 2,
outlier: 0
})

logger.info('Test event created')
} catch (e) {
logger.error('Error setting up test data:', e)
}
})

afterAll(async () => {
try {
await clientServer.matrixDb.deleteEqual(
'events',
'event_id',
'event_to_retrieve'
)
await clientServer.matrixDb.deleteEqual(
'events',
'event_id',
'adding_user'
)
await clientServer.matrixDb.deleteEqual(
'room_memberships',
'event_id',
'adding_user'
)
logger.info('Test event deleted')
} catch (e) {
logger.error('Error tearing down test data', e)
}
})
it('should return 404 if the event does not exist', async () => {
const response = await request(app)
.get(
'/_matrix/client/v3/rooms/!testroom:example.com/event/invalid_event_id'
)
.set('Authorization', `Bearer ${validToken}`)
.set('Accept', 'application/json')
expect(response.statusCode).toBe(404)
})
it('should 404 if the user has never been in the room', async () => {
const response = await request(app)
.get(
'/_matrix/client/v3/rooms/!testroom:example.com/event/event_to_retrieve'
)
.set('Authorization', `Bearer ${validToken2}`)
.set('Accept', 'application/json')
expect(response.statusCode).toBe(404)
})
it('should return 200 if the event can be retrieved by the user', async () => {
const response = await request(app)
.get(
'/_matrix/client/v3/rooms/!testroom:example.com/event/event_to_retrieve'
)
.set('Authorization', `Bearer ${validToken}`)
.set('Accept', 'application/json')
expect(response.statusCode).toBe(200)
expect(response.body).toHaveProperty(
'event_id',
'event_to_retrieve'
)
expect(response.body).toHaveProperty(
'room_id',
'!testroom:example.com'
)
expect(response.body).toHaveProperty(
'sender',
'@sender:example.com'
)
expect(response.body).toHaveProperty('type', 'm.room.message')
expect(response.body).toHaveProperty('origin_server_ts', 1000)
expect(response.body).toHaveProperty(
'content',
'{ body: test message }'
)
})
it('should return 404 if the user was not in the room at the time of the event', async () => {
try {
await clientServer.matrixDb.insert('room_memberships', {
room_id: '!testroom:example.com',
user_id: '@testuser:example.com',
membership: 'leave',
event_id: 'deleting_user',
sender: '@admin:example.com'
})

await clientServer.matrixDb.insert('events', {
event_id: 'deleting_user',
room_id: '!testroom:example.com',
sender: '@admin:example.com',
type: 'm.room.message',
origin_server_ts: 50,
content: JSON.stringify({ body: 'test message' }),
topological_ordering: 0,
processed: 2,
outlier: 0
})
logger.info('Test event created')
} catch (e) {
logger.error('Error tearing down test data', e)
}
const response = await request(app)
.get(
'/_matrix/client/v3/rooms/!testroom:example.com/event/event_to_retrieve'
)
.set('Authorization', `Bearer ${validToken}`)
.set('Accept', 'application/json')
expect(response.statusCode).toBe(404)
})
})
})
})
})
})
10 changes: 7 additions & 3 deletions packages/matrix-client-server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import putAccountData from './user/account_data/putAccountData'
import register from './register'
import { getDevices, getDeviceInfo } from './devices/getDevices'
import { changeDeviceName } from './devices/changeDevices'
import GetEventId from './rooms/{roomId}/getEventId'

const tables = {
ui_auth_sessions: 'session_id TEXT NOT NULL, stage_type TEXT NOT NULL'
Expand Down Expand Up @@ -113,7 +114,8 @@ export default class MatrixClientServer extends MatrixIdentityServer<clientDbCol
'/_matrix/client/v3/user/:userId/account_data/:type':
getAccountData(this),
'/_matrix/client/v3/devices': getDevices(this),
'/_matrix/client/v3/devices/:deviceId': getDeviceInfo(this)
'/_matrix/client/v3/devices/:deviceId': getDeviceInfo(this),
'/_matrix/client/v3/rooms/:roomId/event/:eventId': GetEventId(this)
}
this.api.post = {
'/_matrix/client/v3/account/whoami': badMethod,
Expand All @@ -124,7 +126,8 @@ export default class MatrixClientServer extends MatrixIdentityServer<clientDbCol
'/_matrix/client/v3/profile/:userId/displayname': badMethod,
'/_matrix/client/v3/user/:userId/account_data/:type': badMethod,
'/_matrix/client/v3/devices': badMethod,
'/_matrix/client/v3/devices/:deviceId': badMethod
'/_matrix/client/v3/devices/:deviceId': badMethod,
'/_matrix/client/v3/rooms/:roomId/event/:eventId': badMethod
}
this.api.put = {
'/_matrix/client/v3/account/whoami': badMethod,
Expand All @@ -138,7 +141,8 @@ export default class MatrixClientServer extends MatrixIdentityServer<clientDbCol
'/_matrix/client/v3/user/:userId/account_data/:type':
putAccountData(this),
'/_matrix/client/v3/devices': badMethod,
'/_matrix/client/v3/devices/:deviceId': changeDeviceName(this)
'/_matrix/client/v3/devices/:deviceId': changeDeviceName(this),
'/_matrix/client/v3/rooms/:roomId/event/:eventId': badMethod
}
resolve(true)
})
Expand Down
Loading

0 comments on commit 9c114d5

Please sign in to comment.