Skip to content
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

Support filter for request history #66

Open
wants to merge 2 commits into
base: twake-supported-0.22.6
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions lib/src/room.dart
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ class Room {
threadRootEventId: threadRootEventId,
threadLastEventId: threadLastEventId);
}

final eventContent = getEventContentFromMsgText(
message: message,
parseMarkdown: parseMarkdown,
Expand Down Expand Up @@ -1030,10 +1030,12 @@ class Room {
/// 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> requestHistory(
{int historyCount = defaultHistoryCount,
void Function()? onHistoryReceived,
direction = Direction.b}) async {
Future<int> requestHistory({
int historyCount = defaultHistoryCount,
void Function()? onHistoryReceived,
direction = Direction.b,
StateFilter? filter,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should have an ADR in SDK

}) async {
final prev_batch = this.prev_batch;

final storeInDatabase = !isArchived;
Expand All @@ -1046,7 +1048,7 @@ class Room {
direction,
from: prev_batch,
limit: historyCount,
filter: jsonEncode(StateFilter(lazyLoadMembers: true).toJson()),
filter: jsonEncode((filter ?? StateFilter(lazyLoadMembers: true)).toJson()),
);

if (onHistoryReceived != null) onHistoryReceived();
Expand Down
35 changes: 25 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,11 +160,13 @@ class Timeline {
await getRoomEvents(
historyCount: historyCount,
direction: direction,
filter: filter,
);
} else {
await room.requestHistory(
historyCount: historyCount,
direction: direction,
filter: filter,
onHistoryReceived: () {
_collectHistoryUpdates = true;
},
Expand All @@ -173,15 +184,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