-
Notifications
You must be signed in to change notification settings - Fork 75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add getRoomMessages API #265
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,6 +43,7 @@ import { RustSdkCryptoStorageProvider } from "./storage/RustSdkCryptoStorageProv | |
import { DMs } from "./DMs"; | ||
import { ServerVersions } from "./models/ServerVersions"; | ||
import { RoomCreateOptions } from "./models/CreateRoom"; | ||
import { RoomMessagesResponse } from "./models/RoomMessagesResponse"; | ||
|
||
const SYNC_BACKOFF_MIN_MS = 5000; | ||
const SYNC_BACKOFF_MAX_MS = 15000; | ||
|
@@ -897,6 +898,34 @@ export class MatrixClient extends EventEmitter { | |
} | ||
} | ||
|
||
/** | ||
* This returns a list of message and state events for a room. | ||
* | ||
* It uses pagination query parameters to paginate history in the room. | ||
* | ||
* @param roomId the room ID to get the event in | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wrong docs? |
||
* @param dir The direction to return events from. | ||
* If this is set to `f`, events will be returned in chronological order starting at `from`. | ||
* If it is set to `b`, events will be returned in reverse chronolgical order, again starting at `from`. | ||
* @param from The token to start returning events from. | ||
* This token can be obtained from a prev_batch or next_batch token returned by the /sync endpoint, | ||
* or from an end token returned by a previous call to this function. | ||
* @param filter A JSON RoomEventFilter to filter returned events with. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We shouldn't be forcing the caller to encode their filter for us There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah yes, that would be silly. |
||
* @param limit The maximum number of events to return. Default: 10. | ||
* @param to The token to stop returning events at. | ||
* This token can be obtained from a `prev_batch` or `next_batch` token returned by the /sync endpoint, | ||
* or from an end token returned by a previous call to this function. | ||
*/ | ||
public async getRoomMessages(roomId: string, dir: 'f'|'b', from: string, opts: { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can probably assume that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The function name should also be |
||
filter?: string; | ||
limit?: number; | ||
to?: string; | ||
}): Promise<RoomMessagesResponse> { | ||
return this.doRequest("GET", `/_matrix/client/v3/rooms/${encodeURIComponent(roomId)}/messages`, { | ||
dir, from, ...opts, | ||
}); | ||
} | ||
|
||
/** | ||
* Gets an event for a room. If the event is encrypted, and the client supports encryption, | ||
* and the room is encrypted, then this will return a decrypted event. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { RoomEventData, RoomStateEventData } from "./events/RoomEvent"; | ||
|
||
/** | ||
* The options available when creating a room. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not the right docs. However, given the translation we'll need to do on the returned events anyways this interface can be deleted. |
||
* @category Models | ||
*/ | ||
export interface RoomMessagesResponse { | ||
start: string; | ||
chunk: RoomEventData[]; | ||
end?: string; | ||
state: RoomStateEventData[]; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,48 @@ export interface TypicalUnsigned { | |
[prop: string]: any; | ||
} | ||
|
||
export interface RoomEventData<T extends (Object | unknown) = unknown> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The SDK isn't really set up to have these interfaces exist: events are either There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll update the code to use MatrixEvent for now then, to make that transition easier. |
||
/** | ||
* The fields in this object will vary depending on the type of event. | ||
*/ | ||
content: T; | ||
/** | ||
* The globally unique event identifier. | ||
*/ | ||
event_id: string; | ||
/** | ||
* Timestamp in milliseconds on originating homeserver when this event was sent. | ||
*/ | ||
origin_server_ts: number; | ||
/** | ||
* The ID of the room associated with this event. Will not be present on events that arrive through /sync, despite being required everywhere else. | ||
*/ | ||
room_id: string; | ||
/** | ||
* Contains the fully-qualified ID of the user who sent this event. | ||
*/ | ||
sender: string; | ||
/** | ||
* The type of event. This SHOULD be namespaced similar to Java package naming conventions e.g. `com.example.subdomain.event.type` | ||
*/ | ||
type: string; | ||
/** | ||
* Contains optional extra information about the event. | ||
*/ | ||
unsinged: TypicalUnsigned; | ||
} | ||
|
||
export interface RoomStateEventData<T extends (Object | unknown) = unknown, P = T> extends RoomEventData<T> { | ||
/** | ||
* A unique key which defines the overwriting semantics for this piece of room state. | ||
*/ | ||
state_key: string; | ||
/** | ||
* The previous content for this event. If there is no previous content, this key will be missing. | ||
*/ | ||
prev_content?: P; | ||
} | ||
|
||
/** | ||
* Empty room event content. | ||
* @category Matrix event contents | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, that's how pagination works :p
Possibly something like this for the overall description?
We really shouldn't be copy/pasting from the spec, particularly when it doesn't make sense.