Skip to content

Commit

Permalink
Support filter for request history (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
nqhhdev authored Dec 11, 2024
1 parent 345aab0 commit 04ec6f3
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 15 deletions.
24 changes: 24 additions & 0 deletions doc/adr/0004-support-filter-for-request-history.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# 4. Support filter for request history

Date: 2024-12-09

## Status

Accepted

- Issue: [#1862](https://github.com/linagora/twake-on-matrix/issues/1862)

## Context

- The date of the last message in chat list display is not correct.
- The message in chat screen is not displayed correctly so block user by empty screen.

## Decision

- Add `StateFilter` to request history to request specific data from the server.

## Consequences

- The `timeLine` and `event` can be filtered by the user to get specific data from the server.
- Display the correct message in chat screen. Prevent block user by empty screen.
- In the chat list, we improve the display date time of the last message soon.
12 changes: 7 additions & 5 deletions lib/src/room.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1037,10 +1037,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,
}) async {
final prev_batch = this.prev_batch;

final storeInDatabase = !isArchived;
Expand All @@ -1053,7 +1055,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

0 comments on commit 04ec6f3

Please sign in to comment.