Skip to content

Commit

Permalink
Initial ability to block instances for Lemmy 0.19.x (thunder-app#857)
Browse files Browse the repository at this point in the history
* added initial instance blocking capability

* added checks to see if instance supports instance blocking

* added some more styling

* added back instance in feed drawer

* added instance autocomplete

* linting and cleanup of some functions/issues wrt instance blocking

* updated changelog

* fixed issue where blocking instances/communities would not show a snackbar on feed

* fixed issues with instance bloc not available in user page or feed page

* allow direct instance input on instance autocomplete

* addressed changing order of post card actions, and feedback from PR

* fixed dependency
  • Loading branch information
hjiangsu authored Oct 29, 2023
1 parent 981ef88 commit 30da55c
Show file tree
Hide file tree
Showing 29 changed files with 977 additions and 488 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
- Support new scaled and controversial sort types - contribution from @micahmo
- Added support to open Lemmy links in app. Android only. - contribution from @ggichure
- Added support for receiving share intents. Android only. - contribution from @ggichure
- Added ability to block instances from long-press menu and user settings
- Add more search options - contribution from @micahmo

### Changed
Expand Down
14 changes: 12 additions & 2 deletions lib/community/bloc/community_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import 'package:bloc_concurrency/bloc_concurrency.dart';
import 'package:equatable/equatable.dart';
import 'package:lemmy_api_client/v3.dart';
import 'package:stream_transform/stream_transform.dart';
import 'package:thunder/community/enums/community_action.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';

import 'package:thunder/community/enums/community_action.dart';
import 'package:thunder/core/singletons/lemmy_client.dart';
import 'package:thunder/feed/utils/community.dart';
import 'package:thunder/post/bloc/post_bloc.dart';
import 'package:thunder/utils/global_context.dart';

part 'community_event.dart';
part 'community_state.dart';
Expand Down Expand Up @@ -46,12 +48,20 @@ class CommunityBloc extends Bloc<CommunityEvent, CommunityState> {
Future<void> _onCommunityAction(CommunityActionEvent event, Emitter<CommunityState> emit) async {
emit(state.copyWith(status: CommunityStatus.fetching));

final l10n = AppLocalizations.of(GlobalContext.context)!;

// TODO: Check if the current account has permission to perform the CommunityAction
switch (event.communityAction) {
case CommunityAction.block:
try {
BlockCommunityResponse blockCommunityResponse = await blockCommunity(event.communityId, event.value);
emit(state.copyWith(status: CommunityStatus.success, communityView: blockCommunityResponse.communityView));
emit(state.copyWith(
status: CommunityStatus.success,
communityView: blockCommunityResponse.communityView,
message: blockCommunityResponse.blocked
? l10n.successfullyBlockedCommunity(blockCommunityResponse.communityView.community.name)
: l10n.successfullyUnblockedCommunity(blockCommunityResponse.communityView.community.name),
));
} catch (e) {
return emit(state.copyWith(status: CommunityStatus.failure));
}
Expand Down
34 changes: 28 additions & 6 deletions lib/community/utils/post_card_action_helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ import 'package:thunder/community/bloc/community_bloc.dart';
import 'package:thunder/community/enums/community_action.dart';
import 'package:thunder/core/enums/media_type.dart';
import 'package:thunder/core/models/post_view_media.dart';
import 'package:thunder/core/singletons/lemmy_client.dart';
import 'package:thunder/feed/bloc/feed_bloc.dart';
import 'package:thunder/feed/utils/utils.dart';
import 'package:thunder/feed/view/feed_page.dart';
import 'package:thunder/instance/bloc/instance_bloc.dart';
import 'package:thunder/instance/enums/instance_action.dart';
import 'package:thunder/post/enums/post_action.dart';
import 'package:thunder/shared/advanced_share_sheet.dart';
import 'package:thunder/shared/picker_item.dart';
Expand All @@ -34,6 +37,7 @@ enum PostCardAction {
sharePost,
shareMedia,
shareLink,
blockInstance,
blockCommunity,
upvote,
downvote,
Expand Down Expand Up @@ -65,6 +69,11 @@ class ExtendedPostCardActions {
}

final List<ExtendedPostCardActions> postCardActionItems = [
ExtendedPostCardActions(
postCardAction: PostCardAction.visitProfile,
icon: Icons.person_search_rounded,
label: AppLocalizations.of(GlobalContext.context)!.visitUserProfile,
),
ExtendedPostCardActions(
postCardAction: PostCardAction.visitCommunity,
icon: Icons.home_work_rounded,
Expand All @@ -76,16 +85,17 @@ final List<ExtendedPostCardActions> postCardActionItems = [
label: AppLocalizations.of(GlobalContext.context)!.blockCommunity,
shouldEnable: (isUserLoggedIn) => isUserLoggedIn,
),
ExtendedPostCardActions(
postCardAction: PostCardAction.visitProfile,
icon: Icons.person_search_rounded,
label: AppLocalizations.of(GlobalContext.context)!.visitUserProfile,
),
ExtendedPostCardActions(
postCardAction: PostCardAction.visitInstance,
icon: Icons.language,
label: AppLocalizations.of(GlobalContext.context)!.visitInstance,
),
ExtendedPostCardActions(
postCardAction: PostCardAction.blockInstance,
icon: Icons.block_rounded,
label: AppLocalizations.of(GlobalContext.context)!.blockInstance,
shouldEnable: (isUserLoggedIn) => isUserLoggedIn,
),
ExtendedPostCardActions(
postCardAction: PostCardAction.sharePost,
icon: Icons.share_rounded,
Expand Down Expand Up @@ -153,7 +163,11 @@ void showPostActionBottomModalSheet(
final bool useAdvancedShareSheet = context.read<ThunderBloc>().state.useAdvancedShareSheet;

actionsToInclude ??= [];
final postCardActionItemsToUse = postCardActionItems.where((extendedAction) => actionsToInclude!.any((action) => extendedAction.postCardAction == action)).toList();
List<ExtendedPostCardActions> postCardActionItemsToUse = postCardActionItems.where((extendedAction) => actionsToInclude!.any((action) => extendedAction.postCardAction == action)).toList();

if (actionsToInclude.contains(PostCardAction.blockInstance) && !LemmyClient.instance.supportsFeature(LemmyFeature.blockInstance)) {
postCardActionItemsToUse.removeWhere((ExtendedPostCardActions postCardActionItem) => postCardActionItem.postCardAction == PostCardAction.blockInstance);
}

multiActionsToInclude ??= [];
final multiPostCardActionItemsToUse = postCardActionItems.where((extendedAction) => multiActionsToInclude!.any((action) => extendedAction.postCardAction == action)).toList();
Expand Down Expand Up @@ -273,6 +287,14 @@ void onSelected(BuildContext context, PostCardAction postCardAction, PostViewMed
case PostCardAction.shareLink:
if (postViewMedia.media.first.originalUrl != null) Share.share(postViewMedia.media.first.originalUrl!);
break;
case PostCardAction.blockInstance:
context.read<InstanceBloc>().add(InstanceActionEvent(
instanceAction: InstanceAction.block,
instanceId: postViewMedia.postView.community.instanceId,
domain: fetchInstanceNameFromUrl(postViewMedia.postView.community.actorId),
value: true,
));
break;
case PostCardAction.blockCommunity:
context.read<CommunityBloc>().add(CommunityActionEvent(communityAction: CommunityAction.block, communityId: postViewMedia.postView.community.id, value: true));
break;
Expand Down
12 changes: 6 additions & 6 deletions lib/community/widgets/community_drawer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,12 @@ class _CommunityDrawerState extends State<CommunityDrawer> {
),
],
),
// Text(
// isLoggedIn ? context.read<AccountBloc>().state.personView?.instanceHost ?? '' : anonymousInstance,
// style: Theme.of(context).textTheme.bodyMedium,
// maxLines: 1,
// overflow: TextOverflow.ellipsis,
// ),
Text(
isLoggedIn ? context.read<AuthBloc>().state.account?.instance ?? '' : anonymousInstance,
style: Theme.of(context).textTheme.bodyMedium,
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
],
),
Expanded(
Expand Down
1 change: 1 addition & 0 deletions lib/community/widgets/post_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ class _PostCardState extends State<PostCard> {
actionsToInclude: [
PostCardAction.visitInstance,
PostCardAction.visitProfile,
PostCardAction.blockInstance,
PostCardAction.visitCommunity,
PostCardAction.blockCommunity,
],
Expand Down
1 change: 1 addition & 0 deletions lib/community/widgets/post_card_view_comfortable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ class PostCardViewComfortable extends StatelessWidget {
actionsToInclude: [
PostCardAction.visitInstance,
PostCardAction.visitProfile,
PostCardAction.blockInstance,
PostCardAction.visitCommunity,
PostCardAction.blockCommunity,
],
Expand Down
3 changes: 2 additions & 1 deletion lib/core/singletons/lemmy_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ class LemmyClient {
enum LemmyFeature {
sortTypeControversial(0, 19, 0, preRelease: ["rc", "1"]),
sortTypeScaled(0, 19, 0, preRelease: ["rc", "1"]),
commentSortTypeControversial(0, 19, 0, preRelease: ["rc", "1"]);
commentSortTypeControversial(0, 19, 0, preRelease: ["rc", "1"]),
blockInstance(0, 19, 0, preRelease: ["rc", "1"]);

final int major;
final int minor;
Expand Down
3 changes: 3 additions & 0 deletions lib/feed/utils/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'package:thunder/community/bloc/anonymous_subscriptions_bloc.dart';
import 'package:thunder/community/bloc/community_bloc.dart';
import 'package:thunder/core/auth/bloc/auth_bloc.dart';
import 'package:thunder/feed/feed.dart';
import 'package:thunder/instance/bloc/instance_bloc.dart';
import 'package:thunder/shared/sort_picker.dart';
import 'package:thunder/community/widgets/community_drawer.dart';
import 'package:thunder/thunder/bloc/thunder_bloc.dart';
Expand Down Expand Up @@ -60,6 +61,7 @@ Future<void> navigateToFeedPage(BuildContext context, {required FeedType feedTyp
AuthBloc authBloc = context.read<AuthBloc>();
ThunderBloc thunderBloc = context.read<ThunderBloc>();
CommunityBloc communityBloc = context.read<CommunityBloc>();
InstanceBloc instanceBloc = context.read<InstanceBloc>();
AnonymousSubscriptionsBloc anonymousSubscriptionsBloc = context.read<AnonymousSubscriptionsBloc>();

ThunderState thunderState = thunderBloc.state;
Expand Down Expand Up @@ -88,6 +90,7 @@ Future<void> navigateToFeedPage(BuildContext context, {required FeedType feedTyp
BlocProvider.value(value: accountBloc),
BlocProvider.value(value: authBloc),
BlocProvider.value(value: thunderBloc),
BlocProvider.value(value: instanceBloc),
BlocProvider.value(value: anonymousSubscriptionsBloc),
BlocProvider.value(value: communityBloc),
],
Expand Down
Loading

0 comments on commit 30da55c

Please sign in to comment.