Skip to content

Commit

Permalink
Add handling for new Lemmy comment link format (#1629)
Browse files Browse the repository at this point in the history
  • Loading branch information
micahmo authored Dec 17, 2024
1 parent 849b3ba commit b5e4a05
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 17 deletions.
10 changes: 9 additions & 1 deletion lib/thunder/cubits/deep_links_cubit/deep_links_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,18 @@ class DeepLinksCubit extends Cubit<DeepLinksState> {
linkType: LinkType.user,
));
} else if (link.contains("/post/")) {
LinkType linkType = LinkType.post;

// See if this is actually a comment link
Uri? uri = Uri.tryParse(link);
if (uri != null && uri.pathSegments.length >= 3 && int.tryParse(uri.pathSegments[2]) != null) {
linkType = LinkType.comment;
}

emit(state.copyWith(
deepLinkStatus: DeepLinkStatus.success,
link: link,
linkType: LinkType.post,
linkType: linkType,
));
} else if (link.contains("/comment/")) {
emit(state.copyWith(
Expand Down
47 changes: 31 additions & 16 deletions lib/utils/instance.dart
Original file line number Diff line number Diff line change
Expand Up @@ -128,27 +128,42 @@ Future<int?> getLemmyPostId(BuildContext context, String text) async {
}

final RegExp _comment = RegExp(r'^(https?:\/\/)(.*)\/comment\/([0-9]*).*$');
final RegExp _commentAlternate = RegExp(r'^(https?:\/\/)(.*)\/post\/([0-9]*)\/([0-9]*).*$');
Future<int?> getLemmyCommentId(BuildContext context, String text) async {
LemmyApiV3 lemmy = LemmyClient.instance.lemmyApiV3;

final RegExpMatch? commentMatch = _comment.firstMatch(text);
String? instance;
int? commentId;

// Try legacy comment link format
RegExpMatch? commentMatch = _comment.firstMatch(text);
if (commentMatch != null) {
final String? instance = commentMatch.group(2);
final int? commentId = int.tryParse(commentMatch.group(3)!);
if (commentId != null) {
if (instance == lemmy.host) {
return commentId;
} else {
// This is a comment on another instance. Try to resolve it
try {
// Show the loading page while we resolve the post
showLoadingPage(context);
// It's a match!
instance = commentMatch.group(2);
commentId = int.tryParse(commentMatch.group(3)!);
} else {
// Otherwise, try the new format
commentMatch = _commentAlternate.firstMatch(text);
if (commentMatch != null) {
// It's a match!
instance = commentMatch.group(2);
commentId = int.tryParse(commentMatch.group(4)!);
}
}

final ResolveObjectResponse resolveObjectResponse = await lemmy.run(ResolveObject(q: text));
return resolveObjectResponse.comment?.comment.id;
} catch (e) {
return null;
}
if (commentId != null) {
if (instance == lemmy.host) {
return commentId;
} else {
// This is a comment on another instance. Try to resolve it
try {
// Show the loading page while we resolve the post
showLoadingPage(context);

final ResolveObjectResponse resolveObjectResponse = await lemmy.run(ResolveObject(q: text));
return resolveObjectResponse.comment?.comment.id;
} catch (e) {
return null;
}
}
}
Expand Down

0 comments on commit b5e4a05

Please sign in to comment.