Skip to content

Commit

Permalink
fixup! fix: getSeenByUsers method to return exact receipts count
Browse files Browse the repository at this point in the history
  • Loading branch information
Julian KOUNE committed Aug 29, 2023
1 parent 0e527a4 commit b778d5a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/utils/room_status_extension.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:fluffychat/utils/string_extension.dart';
import 'package:flutter/widgets.dart';

import 'package:flutter_gen/gen_l10n/l10n.dart';
Expand Down Expand Up @@ -92,10 +93,14 @@ extension RoomStatusExtension on Room {

final lastReceipts = <User>{};

// mainThread.others only contains the last event of the room sent by the current user
if (receiptState.mainThread != null) {
// receiptState contains all users and their last seen event
// we checked users that have seen the eventId argument or older
final mainThreadReceipts = receiptState.mainThread!.otherUsers.entries
.where((element) => element.value.eventId == eventId)
.where(
(element) => element.value.eventId
.isEventIdOlderOrSameAs(timeline, eventId!),
)
.map((e) => unsafeGetUserFromMemoryOrFallback(e.key))
.toList();
lastReceipts.addAll(mainThreadReceipts);
Expand Down
16 changes: 16 additions & 0 deletions lib/utils/string_extension.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:any_link_preview/any_link_preview.dart';
import 'package:collection/collection.dart';
import 'package:fluffychat/widgets/matrix.dart';
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart';
Expand Down Expand Up @@ -130,4 +131,19 @@ extension StringCasingExtension on String {

return unMarkdownedText;
}

bool isEventIdOlderOrSameAs(Timeline timeline, String eventId) {
if (timeline.events.isEmpty) return false;

final firstEvent =
timeline.events.firstWhereOrNull((e) => e.eventId == this);
final secondEvent =
timeline.events.firstWhereOrNull((e) => e.eventId == eventId);

if (secondEvent == null || firstEvent == null) return false;

return this == eventId ||
timeline.events.indexOf(secondEvent) >
timeline.events.indexOf(firstEvent);
}
}

0 comments on commit b778d5a

Please sign in to comment.