add blocking to comments

This commit is contained in:
krawieck 2021-09-09 14:46:01 +02:00
parent cb47bc5f72
commit 2e3ccac233
4 changed files with 68 additions and 7 deletions

View File

@ -8,6 +8,7 @@ import 'package:provider/provider.dart';
import '../../comment_tree.dart';
import '../../l10n/l10n.dart';
import '../../stores/config_store.dart';
import '../../util/async_store.dart';
import '../../util/async_store_listener.dart';
import '../../util/extensions/api.dart';
import '../../util/extensions/cake_day.dart';
@ -91,13 +92,35 @@ class CommentWidget extends StatelessWidget {
detached: detached,
hideOnRead: hideOnRead,
),
builder: (context, child) => AsyncStoreListener(
asyncStore: context.read<CommentStore>().votingState,
builder: (context, child) => ObserverListener<AsyncStore<BlockedPerson>>(
store: context.read<CommentStore>().blockingState,
listener: (context, store) {
final errorTerm = store.errorTerm;
if (errorTerm != null) {
ScaffoldMessenger.of(context)
..hideCurrentSnackBar()
..showSnackBar(SnackBar(content: Text(errorTerm.tr(context))));
} else if (store.asyncState is AsyncStateData) {
final state = store.asyncState as AsyncStateData<BlockedPerson>;
ScaffoldMessenger.of(context)
..hideCurrentSnackBar()
..showSnackBar(
SnackBar(
content: Text(
state.data.blocked ? 'User blocked' : 'User unblocked'),
),
);
}
},
child: AsyncStoreListener(
asyncStore: context.read<CommentStore>().deletingState,
asyncStore: context.read<CommentStore>().votingState,
child: AsyncStoreListener(
asyncStore: context.read<CommentStore>().savingState,
child: const _CommentWidget(),
asyncStore: context.read<CommentStore>().deletingState,
child: AsyncStoreListener(
asyncStore: context.read<CommentStore>().savingState,
child: const _CommentWidget(),
),
),
),
),

View File

@ -130,18 +130,33 @@ class _CommentMoreMenuPopup extends HookWidget {
Navigator.of(context).pop();
},
),
if (store.isMine)
if (store.isMine) ...[
ListTile(
leading: const Icon(Icons.edit),
title: const Text('Edit'),
onTap: handleEdit,
),
if (store.isMine)
ListTile(
leading: Icon(comment.deleted ? Icons.restore : Icons.delete),
title: Text(comment.deleted ? 'Restore' : 'Delete'),
onTap: loggedInAction(handleDelete),
),
] else
ListTile(
leading: store.blockingState.isLoading
? const SizedBox(
height: 20,
width: 20,
child: CircularProgressIndicator(),
)
: const Icon(Icons.block),
title: Text(
'${store.comment.creatorBlocked ? 'Unblock' : 'Block'} ${store.comment.creator.preferredName}'),
onTap: loggedInAction((token) {
Navigator.of(context).pop();
store.block(token);
}),
),
ListTile(
leading: const Icon(Icons.info_outline),
title: const Text('Nerd stuff'),

View File

@ -35,6 +35,7 @@ abstract class _CommentStore with Store {
final savingState = AsyncStore<FullCommentView>();
final markPersonMentionAsReadState = AsyncStore<PersonMentionView>();
final markAsReadState = AsyncStore<FullCommentView>();
final blockingState = AsyncStore<BlockedPerson>();
@computed
bool get isMine =>
@ -103,6 +104,21 @@ abstract class _CommentStore with Store {
if (result != null) comment = result.commentView;
}
@action
Future<void> block(Jwt token) async {
final result = await blockingState.runLemmy(
comment.instanceHost,
BlockPerson(
personId: comment.creator.id,
block: !comment.creatorBlocked,
auth: token.raw,
),
);
if (result != null) {
comment = comment.copyWith(creatorBlocked: result.blocked);
}
}
@action
Future<void> markAsRead(Jwt token) async {
if (userMentionId != null) {

View File

@ -102,6 +102,13 @@ mixin _$CommentStore on _CommentStore, Store {
return _$saveAsyncAction.run(() => super.save(token));
}
final _$blockAsyncAction = AsyncAction('_CommentStore.block');
@override
Future<void> block(Jwt token) {
return _$blockAsyncAction.run(() => super.block(token));
}
final _$markAsReadAsyncAction = AsyncAction('_CommentStore.markAsRead');
@override