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

WIP: Perform authentication on the store level #464

Closed
2 changes: 2 additions & 0 deletions assets/l10n/intl_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,8 @@
"@pinned": {},
"by": "by",
"@by": {},
"switch_account": "Switch account...",
"@switch_account": {},
"view_on": "View on another instance",
"@view_on": {},
"foreign_community_info": "Loading the community in the other instance. Note that if this is the first time the community is being viewed in the instance, this process may take a while.",
Expand Down
2 changes: 1 addition & 1 deletion lib/app_link_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class AppLinkHandler extends HookWidget {
goToCommunity.byName(context, uri.host, target);
break;
case 'u':
goToUser.byName(context, uri.host, target);
goToUser.byName(context, uri.host, target, null);
break;
case 'post':
goToPost(context, uri.host, int.parse(target));
Expand Down
24 changes: 24 additions & 0 deletions lib/hooks/logged_in_action.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,27 @@ Function useLoggedInActionForComment() {
},
);
}

VoidCallback Function(
void Function() action, [
String? message,
]) loggedInMessage(String instanceHost, {bool loggedIn = false}) {
final context = useContext();

return (action, [message]) {
if (!loggedIn) {
return () {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
duration: const Duration(seconds: 7),
content: Text(message ??
'This thread was retrieved via $instanceHost.\nYou are not logged in there.'),
action: SnackBarAction(
label: 'log in',
onPressed: () => goTo(context, (_) => AccountsConfigPage())),
));
};
}

return action;
};
}
11 changes: 5 additions & 6 deletions lib/liftoff_action.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'package:flutter/material.dart';
import 'package:lemmy_api_client/v3.dart';

import 'stores/accounts_store.dart';
import 'widgets/comment/comment_store.dart';
import 'widgets/post/post_store.dart';

Expand Down Expand Up @@ -35,7 +34,7 @@ abstract class LiftoffAction {
/// The function that will be invoked when the action is
/// triggered.
/// It will be wrapped in a call to [loggedInAction].
Future<void> Function(UserData userData) get invoke;
Future<void> Function() get invoke;
}

//////////////////////// Upvote actions ////////////////////////
Expand All @@ -49,7 +48,7 @@ class PostUpvoteAction extends _UpvoteAction {
});

@override
Future<void> Function(UserData userData) get invoke => post.upVote;
Future<void> Function() get invoke => post.upVote;

@override
bool get isActivated => post.postView.myVote == VoteType.up;
Expand All @@ -64,7 +63,7 @@ class CommentUpvoteAction extends _UpvoteAction {
});

@override
Future<void> Function(UserData userData) get invoke => comment.upVote;
Future<void> Function() get invoke => comment.upVote;

@override
bool get isActivated => comment.comment.myVote == VoteType.up;
Expand Down Expand Up @@ -100,7 +99,7 @@ class PostSaveAction extends _SaveAction {
});

@override
Future<void> Function(UserData userData) get invoke => post.save;
Future<void> Function() get invoke => post.save;

@override
bool get isActivated => post.postView.saved;
Expand All @@ -114,7 +113,7 @@ class CommentSaveAction extends _SaveAction {
});

@override
Future<void> Function(UserData userData) get invoke => comment.save;
Future<void> Function() get invoke => comment.save;

@override
bool get isActivated => comment.comment.saved;
Expand Down
5 changes: 4 additions & 1 deletion lib/pages/communities_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@ class CommunitiesListItem extends StatelessWidget {
: const SizedBox.shrink(),
onTap: () => goToCommunity.byId(
context, community.instanceHost, community.community.id),
leading: Avatar(url: community.community.icon),
leading: Avatar(
url: community.community.icon,
originPreferredName: community.community.originPreferredName,
),
);
}
}
3 changes: 3 additions & 0 deletions lib/pages/communities_tab.dart
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ class CommunitiesTab extends HookWidget {
onLongPress: () => toggleCollapse(i),
leading: Avatar(
url: instances[i].icon,
originPreferredName: instances[i].instanceHost,
alwaysShow: true,
),
title: Text(
Expand Down Expand Up @@ -213,6 +214,8 @@ class CommunitiesTab extends HookWidget {
Avatar(
radius: 15,
url: comm.community.icon,
originPreferredName:
comm.community.originPreferredName,
alwaysShow: true,
),
const SizedBox(width: 10),
Expand Down
33 changes: 12 additions & 21 deletions lib/pages/community/community.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import 'package:lemmy_api_client/v3.dart';
import 'package:nested/nested.dart';
import 'package:swipeable_page_route/swipeable_page_route.dart';

import '../../hooks/stores.dart';
import '../../l10n/l10n.dart';
import '../../stores/accounts_store.dart';
import '../../util/async_store.dart';
Expand Down Expand Up @@ -32,7 +31,6 @@ class CommunityPage extends HookWidget {
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
final accountsStore = useAccountsStore();
final scrollController = useScrollController();

return Nested(
Expand Down Expand Up @@ -61,9 +59,7 @@ class CommunityPage extends HookWidget {
body: Center(
child: (communityState.errorTerm != null)
? FailedToLoad(
refresh: () => store.refresh(context
.read<AccountsStore>()
.defaultUserDataFor(store.instanceHost)),
refresh: () => store.refresh(),
message: communityState.errorTerm!.tr(context),
)
: const CircularProgressIndicator.adaptive()),
Expand Down Expand Up @@ -137,27 +133,22 @@ class CommunityPage extends HookWidget {
page: page,
limit: batchSize,
savedOnly: false,
auth: accountsStore
.defaultUserDataFor(community.instanceHost)
?.jwt
.raw,
auth: store.userData?.jwt.raw,
))
.toPostStores(),
.toPostStores(store.userData),
),
InfiniteCommentList(
fetcher: (page, batchSize, sortType) =>
LemmyApiV3(community.instanceHost).run(GetComments(
communityId: community.community.id,
auth: accountsStore
.defaultUserDataFor(community.instanceHost)
?.jwt
.raw,
auth: store.userData?.jwt.raw,
type: CommentListingType.local,
sort: sortType,
limit: batchSize,
page: page,
savedOnly: false,
))),
)),
userData: store.userData),
CommmunityAboutTab(fullCommunityView),
],
),
Expand All @@ -172,9 +163,7 @@ class CommunityPage extends HookWidget {
return SwipeablePageRoute(
builder: (context) {
return MobxProvider.value(
value: store
..refresh(
context.read<AccountsStore>().defaultUserDataFor(instanceHost)),
value: store..refresh(),
child: const CommunityPage(),
);
},
Expand All @@ -188,10 +177,11 @@ class CommunityPage extends HookWidget {
);
}

static Route fromIdRoute(String instanceHost, int id) {
static Route fromIdRoute(UserData? userData, String instanceHost, int id) {
return _route(
instanceHost,
CommunityStore.fromId(id: id, instanceHost: instanceHost),
CommunityStore.fromId(
userData: userData, id: id, instanceHost: instanceHost),
);
}

Expand All @@ -206,9 +196,10 @@ class CommunityPage extends HookWidget {
builder: (buildContext, object) {
return MobxProvider.value(
value: CommunityStore.fromId(
userData: userData,
id: object.community!.community.id,
instanceHost: userData.instanceHost)
..refresh(userData),
..refresh(),
child: const CommunityPage(),
);
});
Expand Down
5 changes: 1 addition & 4 deletions lib/pages/community/community_about_tab.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import 'package:flutter/material.dart';
import 'package:lemmy_api_client/v3.dart';

import '../../l10n/l10n.dart';
import '../../stores/accounts_store.dart';
import '../../util/extensions/spaced.dart';
import '../../util/observer_consumers.dart';
import '../../widgets/bottom_safe.dart';
Expand All @@ -25,9 +24,7 @@ class CommmunityAboutTab extends StatelessWidget {

return PullToRefresh(
onRefresh: () async {
await context.read<CommunityStore>().refresh(context
.read<AccountsStore>()
.defaultUserDataFor(fullCommunityView.instanceHost));
await context.read<CommunityStore>().refresh();
},
child: ListView(
padding: const EdgeInsets.only(top: 20),
Expand Down
11 changes: 4 additions & 7 deletions lib/pages/community/community_follow_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:lemmy_api_client/v3.dart';

import '../../hooks/logged_in_action.dart';
import '../../l10n/l10n.dart';
import '../../util/observer_consumers.dart';
import '../view_on_menu.dart';
Expand All @@ -17,11 +16,6 @@ class CommunityFollowButton extends HookWidget {
Widget build(BuildContext context) {
final theme = Theme.of(context);

final loggedInAction = useLoggedInAction(
context.read<CommunityStore>().instanceHost, fallback: () {
ViewOnMenu.openForCommunity(context, communityView.community.actorId);
});

return ObserverBuilder<CommunityStore>(builder: (context, store) {
return ElevatedButtonTheme(
data: ElevatedButtonThemeData(
Expand All @@ -37,7 +31,10 @@ class CommunityFollowButton extends HookWidget {
child: ElevatedButton(
onPressed: store.subscribingState.isLoading
? () {}
: loggedInAction(store.subscribe),
: store.isAuthenticated
? store.subscribe
: () => ViewOnMenu.openForCommunity(
context, communityView.community.actorId),
child: store.subscribingState.isLoading
? const SizedBox(
width: 15,
Expand Down
17 changes: 7 additions & 10 deletions lib/pages/community/community_more_menu.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:lemmy_api_client/v3.dart';

import '../../hooks/logged_in_action.dart';
import '../../l10n/l10n.dart';
import '../../url_launcher.dart';
import '../../util/extensions/api.dart';
Expand All @@ -20,10 +19,9 @@ class CommunityMoreMenu extends HookWidget {

@override
Widget build(BuildContext context) {
final store = context.read<CommunityStore>();
final communityView = fullCommunityView.communityView;

final loggedInAction = useLoggedInAction(communityView.instanceHost);

return Column(
children: [
ListTile(
Expand All @@ -40,21 +38,20 @@ class CommunityMoreMenu extends HookWidget {
onTap: () => ViewOnMenu.openForCommunity(
context, communityView.community.actorId),
),
ObserverBuilder<CommunityStore>(builder: (context, store) {
return ListTile(
if (store.isAuthenticated)
ListTile(
leading: store.blockingState.isLoading
? const CircularProgressIndicator.adaptive()
: const Icon(Icons.block),
title: Text(
'${fullCommunityView.communityView.blocked ? L10n.of(context).unblock : L10n.of(context).block} ${communityView.community.preferredName}'),
onTap: store.blockingState.isLoading
? null
: loggedInAction((userData) {
store.block(userData);
: () {
store.block();
Navigator.of(context).pop();
}),
);
}),
},
),
ListTile(
leading: const Icon(Icons.info_outline),
title: Text(L10n.of(context).nerd_stuff),
Expand Down
2 changes: 2 additions & 0 deletions lib/pages/community/community_overview.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ class CommunityOverview extends StatelessWidget {
color: Colors.transparent,
child: Avatar(
url: community.community.icon,
originPreferredName:
community.community.originPreferredName,
radius: 83 / 2,
alwaysShow: true,
),
Expand Down
Loading
Loading