Skip to content

Commit

Permalink
Merge pull request #2616 from nextcloud/fix/talk_app/reference-resolving
Browse files Browse the repository at this point in the history
  • Loading branch information
provokateurin authored Nov 1, 2024
2 parents 8a8841f + 33d8b7a commit c8b2857
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,7 @@ class _TalkMessageBloc extends Bloc implements TalkMessageBloc {
return;
}

final matches =
referenceRegex.allMatches(chatMessage.message).map((match) => match.group(0)!.trim()).toBuiltList();

references.add(
references.value.rebuild((b) {
for (final match in matches) {
b[match] ??= Result.loading();
}

b.removeWhere((key, value) => !matches.contains(key));
}),
);
matches = referenceRegex.allMatches(chatMessage.message).map((match) => match.group(0)!.trim()).toBuiltList();

if (matches.isNotEmpty) {
referencesBloc.loadReferences(matches);
Expand All @@ -55,12 +44,9 @@ class _TalkMessageBloc extends Bloc implements TalkMessageBloc {

referencesSubscription = referencesBloc.references.listen((result) {
references.add(
references.value.rebuild((b) {
for (final url in references.value.keys) {
b[url] = result[url] ?? Result.loading();
}

b.removeWhere((key, value) => !result.keys.contains(key));
BuiltMap({
for (final entry in result.entries)
if (matches.contains(entry.key)) entry.key: entry.value,
}),
);
});
Expand All @@ -70,6 +56,7 @@ class _TalkMessageBloc extends Bloc implements TalkMessageBloc {
final spreed.$ChatMessageInterface chatMessage;
final ReferencesBloc referencesBloc;
final bool isParent;
BuiltList<String> matches = BuiltList();
StreamSubscription<Result<RegExp?>>? referenceRegexSubscription;
StreamSubscription<BuiltMap<String, Result<core.Reference>>>? referencesSubscription;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -430,11 +430,10 @@ class _TalkCommentMessageState extends State<TalkCommentMessage> {
if (parent != null) parent,
text,
for (final entry in references.entries.take(3))
if (entry.value.data?.openGraphObject.name != entry.key)
TalkReferencePreview(
url: entry.key,
openGraphObject: entry.value.data?.openGraphObject,
),
TalkReferencePreview(
url: entry.key,
openGraphObject: entry.value.data?.openGraphObject,
),
if (!widget.isParent && widget.chatMessage.reactions.isNotEmpty)
TalkReactions(
chatMessage: widget.chatMessage,
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -51,21 +51,20 @@ void main() {

references.add(
BuiltMap({
for (final key in ['a', 'b']) key: Result.success(reference),
'a': Result<core.Reference>.loading(),
'b': Result.success(reference),
'c': Result<core.Reference>.error('error'),
'd': Result.success(reference),
}),
);

expect(
bloc.references,
emitsInOrder(<BuiltMap<String, Result<core.Reference>>>[
BuiltMap(),
BuiltMap({
'a': Result<core.Reference>.loading(),
'b': Result<core.Reference>.loading(),
'c': Result<core.Reference>.loading(),
}),
BuiltMap({
'a': Result.success(reference),
'b': Result.success(reference),
'c': Result<core.Reference>.error('error'),
}),
]),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -725,7 +725,7 @@ void main() {

await tester.pumpAndSettle();

expect(find.byType(TalkReferencePreview), findsExactly(2));
expect(find.byType(TalkReferencePreview), findsExactly(3));
for (final url in ['a', 'b']) {
expect(
find.byWidgetPredicate(
Expand All @@ -735,6 +735,13 @@ void main() {
findsOne,
);
}
expect(
find.byWidgetPredicate(
(widget) =>
widget is TalkReferencePreview && widget.url == 'c' && widget.openGraphObject == brokenOpenGraphObject,
),
findsOne,
);

await expectLater(
find.byType(TalkCommentMessage).first,
Expand Down

0 comments on commit c8b2857

Please sign in to comment.