Merge branch 'krille/pagination-in-search-events' into 'main'
feat: Implement pagination for searchEvent endpoint See merge request famedly/company/frontend/famedlysdk!1240
This commit is contained in:
commit
74deba3d3e
|
|
@ -551,42 +551,52 @@ class Timeline {
|
||||||
String? searchTerm,
|
String? searchTerm,
|
||||||
int requestHistoryCount = 100,
|
int requestHistoryCount = 100,
|
||||||
int maxHistoryRequests = 10,
|
int maxHistoryRequests = 10,
|
||||||
|
String? sinceEventId,
|
||||||
|
int? limit,
|
||||||
bool Function(Event)? searchFunc,
|
bool Function(Event)? searchFunc,
|
||||||
}) async* {
|
}) async* {
|
||||||
assert(searchTerm != null || searchFunc != null);
|
assert(searchTerm != null || searchFunc != null);
|
||||||
searchFunc ??= (event) =>
|
searchFunc ??= (event) =>
|
||||||
event.body.toLowerCase().contains(searchTerm?.toLowerCase() ?? '');
|
event.body.toLowerCase().contains(searchTerm?.toLowerCase() ?? '');
|
||||||
final found = <Event>[];
|
final found = <Event>[];
|
||||||
// Search locally
|
|
||||||
for (final event in events) {
|
|
||||||
if (searchFunc(event)) {
|
|
||||||
yield found..add(event);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Search in database
|
if (sinceEventId == null) {
|
||||||
var start = events.length;
|
// Search locally
|
||||||
while (true) {
|
|
||||||
final eventsFromStore = await room.client.database?.getEventList(
|
|
||||||
room,
|
|
||||||
start: start,
|
|
||||||
limit: requestHistoryCount,
|
|
||||||
) ??
|
|
||||||
[];
|
|
||||||
if (eventsFromStore.isEmpty) break;
|
|
||||||
start += eventsFromStore.length;
|
|
||||||
for (final event in events) {
|
for (final event in events) {
|
||||||
if (searchFunc(event)) {
|
if (searchFunc(event)) {
|
||||||
yield found..add(event);
|
yield found..add(event);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Search in database
|
||||||
|
var start = events.length;
|
||||||
|
while (true) {
|
||||||
|
final eventsFromStore = await room.client.database?.getEventList(
|
||||||
|
room,
|
||||||
|
start: start,
|
||||||
|
limit: requestHistoryCount,
|
||||||
|
) ??
|
||||||
|
[];
|
||||||
|
if (eventsFromStore.isEmpty) break;
|
||||||
|
start += eventsFromStore.length;
|
||||||
|
for (final event in eventsFromStore) {
|
||||||
|
if (searchFunc(event)) {
|
||||||
|
yield found..add(event);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Search on the server
|
// Search on the server
|
||||||
var prevBatch = room.prev_batch;
|
var prevBatch = room.prev_batch;
|
||||||
|
if (sinceEventId != null) {
|
||||||
|
prevBatch =
|
||||||
|
(await room.client.getEventContext(room.id, sinceEventId)).end;
|
||||||
|
}
|
||||||
final encryption = room.client.encryption;
|
final encryption = room.client.encryption;
|
||||||
for (var i = 0; i < maxHistoryRequests; i++) {
|
for (var i = 0; i < maxHistoryRequests; i++) {
|
||||||
if (prevBatch == null) break;
|
if (prevBatch == null) break;
|
||||||
|
if (limit != null && found.length >= limit) break;
|
||||||
try {
|
try {
|
||||||
final resp = await room.client.getRoomEvents(
|
final resp = await room.client.getRoomEvents(
|
||||||
room.id,
|
room.id,
|
||||||
|
|
@ -608,9 +618,12 @@ class Timeline {
|
||||||
}
|
}
|
||||||
if (searchFunc(event)) {
|
if (searchFunc(event)) {
|
||||||
yield found..add(event);
|
yield found..add(event);
|
||||||
|
if (limit != null && found.length >= limit) break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
prevBatch = resp.end;
|
prevBatch = resp.end;
|
||||||
|
// We are at the beginning of the room
|
||||||
|
if (resp.chunk.length < requestHistoryCount) break;
|
||||||
} on MatrixException catch (e) {
|
} on MatrixException catch (e) {
|
||||||
// We have no permission anymore to request the history
|
// We have no permission anymore to request the history
|
||||||
if (e.error == MatrixError.M_FORBIDDEN) {
|
if (e.error == MatrixError.M_FORBIDDEN) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue