Skip to content

Commit

Permalink
Support filter for request history
Browse files Browse the repository at this point in the history
  • Loading branch information
nqhhdev committed Dec 9, 2024
1 parent 345aab0 commit f36ff6d
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions lib/src/timeline.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import 'package:matrix/src/models/timeline_chunk.dart';
class Timeline {
final Room room;

List<Event> get events => chunk.events;

/// Map of event ID to map of type to set of aggregated events
Expand Down Expand Up @@ -79,14 +80,20 @@ class Timeline {
return room.prev_batch != null && events.last.type != EventTypes.RoomCreate;
}

Future<void> requestHistory(
{int historyCount = Room.defaultHistoryCount}) async {
Future<void> requestHistory({
int historyCount = Room.defaultHistoryCount,
StateFilter? filter,
}) async {
if (isRequestingHistory) {
return;
}

isRequestingHistory = true;
await _requestEvents(direction: Direction.b, historyCount: historyCount);
await _requestEvents(
direction: Direction.b,
historyCount: historyCount,
filter: filter,
);
isRequestingHistory = false;
}

Expand All @@ -104,9 +111,11 @@ class Timeline {
isRequestingFuture = false;
}

Future<void> _requestEvents(
{int historyCount = Room.defaultHistoryCount,
required Direction direction}) async {
Future<void> _requestEvents({
int historyCount = Room.defaultHistoryCount,
required Direction direction,
StateFilter? filter,
}) async {
onUpdate?.call();

try {
Expand Down Expand Up @@ -151,6 +160,7 @@ class Timeline {
await getRoomEvents(
historyCount: historyCount,
direction: direction,
filter: filter,
);
} else {
await room.requestHistory(
Expand All @@ -173,15 +183,19 @@ class Timeline {
/// be received maximum. When the request is answered, [onHistoryReceived] will be triggered **before**
/// the historical events will be published in the onEvent stream.
/// Returns the actual count of received timeline events.
Future<int> getRoomEvents(
{int historyCount = Room.defaultHistoryCount,
direction = Direction.b}) async {
Future<int> getRoomEvents({
int historyCount = Room.defaultHistoryCount,
direction = Direction.b,
StateFilter? filter,
}) async {
final resp = await room.client.getRoomEvents(
room.id,
direction,
from: direction == Direction.b ? chunk.prevBatch : chunk.nextBatch,
limit: historyCount,
filter: jsonEncode(StateFilter(lazyLoadMembers: true).toJson()),
filter: jsonEncode(
(filter ?? StateFilter(lazyLoadMembers: true)).toJson(),
),
);

if (resp.end == null) {
Expand Down

0 comments on commit f36ff6d

Please sign in to comment.