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

Add modlog link handling #1223

Merged
merged 1 commit into from
Mar 23, 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
46 changes: 6 additions & 40 deletions lib/feed/widgets/feed_page_app_bar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@ import 'package:thunder/feed/utils/community_share.dart';
import 'package:thunder/feed/utils/user_share.dart';
import 'package:thunder/feed/utils/utils.dart';
import 'package:thunder/feed/view/feed_page.dart';
import 'package:thunder/modlog/view/modlog_page.dart';
import 'package:thunder/modlog/utils/navigate_modlog.dart';
import 'package:thunder/search/bloc/search_bloc.dart';
import 'package:thunder/search/pages/search_page.dart';
import 'package:thunder/shared/snackbar.dart';
import 'package:thunder/shared/sort_picker.dart';
import 'package:thunder/shared/thunder_popup_menu_item.dart';
import 'package:thunder/thunder/bloc/thunder_bloc.dart';
import 'package:thunder/utils/swipe.dart';

/// Holds the app bar for the feed page. The app bar actions changes depending on the type of feed (general, community, user)
class FeedPageAppBar extends StatelessWidget {
Expand Down Expand Up @@ -223,22 +222,10 @@ class FeedAppBarCommunityActions extends StatelessWidget {
),
ThunderPopupMenuItem(
onTap: () async {
final state = context.read<ThunderBloc>().state;
final reduceAnimations = state.reduceAnimations;

await Navigator.of(context).push(
SwipeablePageRoute(
transitionDuration: reduceAnimations ? const Duration(milliseconds: 100) : null,
backGestureDetectionWidth: 45,
canOnlySwipeFromEdge: true,
builder: (context) => MultiBlocProvider(
providers: [
BlocProvider.value(value: feedBloc),
BlocProvider.value(value: thunderBloc),
],
child: ModlogFeedPage(communityId: feedBloc.state.fullCommunityView!.communityView.community.id),
),
),
await navigateToModlogPage(
context,
feedBloc: feedBloc,
communityId: feedBloc.state.fullCommunityView!.communityView.community.id,
);
},
icon: Icons.shield_rounded,
Expand Down Expand Up @@ -339,28 +326,7 @@ class FeedAppBarGeneralActions extends StatelessWidget {
ThunderPopupMenuItem(
onTap: () async {
HapticFeedback.mediumImpact();

AuthBloc authBloc = context.read<AuthBloc>();
ThunderBloc thunderBloc = context.read<ThunderBloc>();

await Navigator.of(context).push(
SwipeablePageRoute(
transitionDuration: thunderBloc.state.reduceAnimations ? const Duration(milliseconds: 100) : null,
backGestureDetectionStartOffset: !kIsWeb && Platform.isAndroid ? 45 : 0,
backGestureDetectionWidth: 45,
canOnlySwipeFromEdge:
disableFullPageSwipe(isUserLoggedIn: authBloc.state.isLoggedIn, state: thunderBloc.state, isPostPage: false) || !thunderBloc.state.enableFullScreenSwipeNavigationGesture,
builder: (otherContext) {
return MultiBlocProvider(
providers: [
BlocProvider.value(value: feedBloc),
BlocProvider.value(value: thunderBloc),
],
child: const ModlogFeedPage(),
);
},
),
);
await navigateToModlogPage(context, feedBloc: feedBloc);
},
icon: Icons.shield_rounded,
title: l10n.modlog,
Expand Down
32 changes: 5 additions & 27 deletions lib/instance/pages/instance_page.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import 'dart:io';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:lemmy_api_client/v3.dart';
import 'package:swipeable_page_route/swipeable_page_route.dart';
import 'package:thunder/comment/widgets/comment_list_entry.dart';
import 'package:thunder/community/widgets/community_list_entry.dart';
import 'package:thunder/core/auth/bloc/auth_bloc.dart';
Expand All @@ -16,7 +12,7 @@ import 'package:thunder/instance/bloc/instance_bloc.dart';
import 'package:thunder/instance/cubit/instance_page_cubit.dart';
import 'package:thunder/instance/enums/instance_action.dart';
import 'package:thunder/instance/widgets/instance_view.dart';
import 'package:thunder/modlog/view/modlog_page.dart';
import 'package:thunder/modlog/utils/navigate_modlog.dart';
import 'package:thunder/search/widgets/search_action_chip.dart';
import 'package:thunder/shared/error_message.dart';
import 'package:thunder/shared/persistent_header.dart';
Expand All @@ -28,7 +24,6 @@ import 'package:thunder/utils/instance.dart';
import 'package:thunder/utils/links.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:thunder/utils/numbers.dart';
import 'package:thunder/utils/swipe.dart';

class InstancePage extends StatefulWidget {
final GetSiteResponse getSiteResponse;
Expand Down Expand Up @@ -166,28 +161,11 @@ class _InstancePageState extends State<InstancePage> {
ThunderPopupMenuItem(
onTap: () async {
HapticFeedback.mediumImpact();

AuthBloc authBloc = context.read<AuthBloc>();
ThunderBloc thunderBloc = context.read<ThunderBloc>();
FeedBloc feedBloc = context.read<FeedBloc>();

await Navigator.of(context).push(
SwipeablePageRoute(
transitionDuration: thunderBloc.state.reduceAnimations ? const Duration(milliseconds: 100) : null,
backGestureDetectionStartOffset: !kIsWeb && Platform.isAndroid ? 45 : 0,
backGestureDetectionWidth: 45,
canOnlySwipeFromEdge: disableFullPageSwipe(isUserLoggedIn: authBloc.state.isLoggedIn, state: thunderBloc.state, isPostPage: false) ||
!thunderBloc.state.enableFullScreenSwipeNavigationGesture,
builder: (otherContext) {
return MultiBlocProvider(
providers: [
BlocProvider.value(value: feedBloc),
BlocProvider.value(value: thunderBloc),
],
child: ModlogFeedPage(lemmyClient: feedBloc.lemmyClient),
);
},
),
navigateToModlogPage(
context,
feedBloc: feedBloc,
lemmyClient: feedBloc.lemmyClient,
);
},
icon: Icons.shield_rounded,
Expand Down
53 changes: 53 additions & 0 deletions lib/modlog/utils/navigate_modlog.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import 'dart:io';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:lemmy_api_client/v3.dart';
import 'package:swipeable_page_route/swipeable_page_route.dart';
import 'package:thunder/core/auth/bloc/auth_bloc.dart';
import 'package:thunder/core/singletons/lemmy_client.dart';
import 'package:thunder/feed/bloc/feed_bloc.dart';
import 'package:thunder/modlog/view/modlog_page.dart';
import 'package:thunder/thunder/bloc/thunder_bloc.dart';
import 'package:thunder/utils/swipe.dart';

Future<void> navigateToModlogPage(
BuildContext context, {
required FeedBloc feedBloc,
ModlogActionType? modlogActionType,
int? communityId,
int? userId,
int? moderatorId,
LemmyClient? lemmyClient,
}) async {
final ThunderBloc thunderBloc = context.read<ThunderBloc>();
final bool reduceAnimations = thunderBloc.state.reduceAnimations;

bool canOnlySwipeFromEdge = true;
try {
AuthBloc authBloc = context.read<AuthBloc>();
canOnlySwipeFromEdge = disableFullPageSwipe(isUserLoggedIn: authBloc.state.isLoggedIn, state: thunderBloc.state, isPostPage: false) || !thunderBloc.state.enableFullScreenSwipeNavigationGesture;
} catch (e) {}

await Navigator.of(context).push(
SwipeablePageRoute(
transitionDuration: reduceAnimations ? const Duration(milliseconds: 100) : null,
backGestureDetectionStartOffset: !kIsWeb && Platform.isAndroid ? 45 : 0,
canOnlySwipeFromEdge: canOnlySwipeFromEdge,
builder: (context) => MultiBlocProvider(
providers: [
BlocProvider.value(value: feedBloc),
BlocProvider.value(value: thunderBloc),
],
child: ModlogFeedPage(
modlogActionType: modlogActionType,
communityId: communityId,
userId: userId,
moderatorId: moderatorId,
lemmyClient: lemmyClient,
),
),
),
);
}
6 changes: 6 additions & 0 deletions lib/thunder/cubits/deep_links_cubit/deep_links_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ class DeepLinksCubit extends Cubit<DeepLinksState> {
link: link,
linkType: LinkType.community,
));
} else if (link.contains("/modlog")) {
emit(state.copyWith(
deepLinkStatus: DeepLinkStatus.success,
link: link,
linkType: LinkType.modlog,
));
} else if (Uri.tryParse(link)?.pathSegments.isEmpty == true) {
emit(state.copyWith(
deepLinkStatus: DeepLinkStatus.success,
Expand Down
10 changes: 9 additions & 1 deletion lib/thunder/enums/deep_link_enums.dart
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
enum LinkType { user, post, comment, instance, unknown, community }
enum LinkType {
user,
post,
comment,
instance,
unknown,
community,
modlog,
}
28 changes: 28 additions & 0 deletions lib/thunder/pages/thunder_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import 'package:thunder/feed/bloc/feed_bloc.dart';
import 'package:thunder/feed/feed.dart';
import 'package:thunder/feed/view/feed_page.dart';
import 'package:thunder/feed/widgets/feed_fab.dart';
import 'package:thunder/modlog/utils/navigate_modlog.dart';
import 'package:thunder/post/utils/post.dart';
import 'package:thunder/shared/common_markdown_body.dart';
import 'package:thunder/shared/snackbar.dart';
Expand Down Expand Up @@ -263,6 +264,8 @@ class _ThunderState extends State<Thunder> {
if (context.mounted) await _navigateToPost(_link);
case LinkType.community:
if (context.mounted) await _navigateToCommunity(_link);
case LinkType.modlog:
if (context.mounted) await _navigateToModlog(_link);
case LinkType.instance:
if (context.mounted) await _navigateToInstance(_link);
case LinkType.unknown:
Expand Down Expand Up @@ -333,6 +336,31 @@ class _ThunderState extends State<Thunder> {
}
}

Future<void> _navigateToModlog(String link) async {
try {
Uri? uri = Uri.tryParse(link);
if (uri != null) {
final LemmyClient lemmyClient = LemmyClient()..changeBaseUrl(uri.host);
FeedBloc feedBloc = FeedBloc(lemmyClient: lemmyClient);
await navigateToModlogPage(
context,
feedBloc: feedBloc,
modlogActionType: ModlogActionType.fromJson(uri.queryParameters['actionType'] ?? ModlogActionType.all.value),
communityId: int.tryParse(uri.queryParameters['communityId'] ?? ''),
userId: int.tryParse(uri.queryParameters['userId'] ?? ''),
moderatorId: int.tryParse(uri.queryParameters['modId'] ?? ''),
lemmyClient: lemmyClient,
);
return;
}
} catch (e) {}

// Show an error for any issues processing the link
if (context.mounted) {
_showLinkProcessingError(context, AppLocalizations.of(context)!.exceptionProcessingUri, link);
}
}

Future<void> _navigateToComment(String link) async {
final commentId = await getLemmyCommentId(link);
if (context.mounted && commentId != null) {
Expand Down
21 changes: 21 additions & 0 deletions lib/utils/links.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import 'package:link_preview_generator/link_preview_generator.dart';
import 'package:share_plus/share_plus.dart';
import 'package:swipeable_page_route/swipeable_page_route.dart';
import 'package:thunder/core/enums/browser_mode.dart';
import 'package:thunder/feed/bloc/feed_bloc.dart';
import 'package:thunder/instances.dart';
import 'package:thunder/modlog/utils/navigate_modlog.dart';
import 'package:thunder/shared/pages/loading_page.dart';
import 'package:thunder/shared/webview.dart';
import 'package:thunder/utils/bottom_sheet_list_picker.dart';
Expand Down Expand Up @@ -197,6 +199,25 @@ void handleLink(BuildContext context, {required String url}) async {
}
}

// Try navigate to modlog
Uri? uri = Uri.tryParse(url);
if (context.mounted && uri != null && instances.contains(uri.host) && url.contains('/modlog')) {
try {
final LemmyClient lemmyClient = LemmyClient()..changeBaseUrl(uri.host);
FeedBloc feedBloc = FeedBloc(lemmyClient: lemmyClient);
await navigateToModlogPage(
context,
feedBloc: feedBloc,
modlogActionType: ModlogActionType.fromJson(uri.queryParameters['actionType'] ?? ModlogActionType.all.value),
communityId: int.tryParse(uri.queryParameters['communityId'] ?? ''),
userId: int.tryParse(uri.queryParameters['userId'] ?? ''),
moderatorId: int.tryParse(uri.queryParameters['modId'] ?? ''),
lemmyClient: lemmyClient,
);
return;
} catch (e) {}
}

// Try opening it as an image
try {
if (isImageUrl(url) && context.mounted) {
Expand Down
Loading