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

Merged
merged 4 commits into from
Dec 11, 2024
Merged
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
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.
hoangdat marked this conversation as resolved.
Show resolved Hide resolved
- 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,
nqhhdev marked this conversation as resolved.
Show resolved Hide resolved
}) 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