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 '../../comment_tree.dart';
import '../../l10n/l10n.dart'; import '../../l10n/l10n.dart';
import '../../stores/config_store.dart'; import '../../stores/config_store.dart';
import '../../util/async_store.dart';
import '../../util/async_store_listener.dart'; import '../../util/async_store_listener.dart';
import '../../util/extensions/api.dart'; import '../../util/extensions/api.dart';
import '../../util/extensions/cake_day.dart'; import '../../util/extensions/cake_day.dart';
@ -91,7 +92,28 @@ class CommentWidget extends StatelessWidget {
detached: detached, detached: detached,
hideOnRead: hideOnRead, hideOnRead: hideOnRead,
), ),
builder: (context, child) => AsyncStoreListener( 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>().votingState, asyncStore: context.read<CommentStore>().votingState,
child: AsyncStoreListener( child: AsyncStoreListener(
asyncStore: context.read<CommentStore>().deletingState, asyncStore: context.read<CommentStore>().deletingState,
@ -101,6 +123,7 @@ class CommentWidget extends StatelessWidget {
), ),
), ),
), ),
),
); );
} }
} }

View File

@ -130,18 +130,33 @@ class _CommentMoreMenuPopup extends HookWidget {
Navigator.of(context).pop(); Navigator.of(context).pop();
}, },
), ),
if (store.isMine) if (store.isMine) ...[
ListTile( ListTile(
leading: const Icon(Icons.edit), leading: const Icon(Icons.edit),
title: const Text('Edit'), title: const Text('Edit'),
onTap: handleEdit, onTap: handleEdit,
), ),
if (store.isMine)
ListTile( ListTile(
leading: Icon(comment.deleted ? Icons.restore : Icons.delete), leading: Icon(comment.deleted ? Icons.restore : Icons.delete),
title: Text(comment.deleted ? 'Restore' : 'Delete'), title: Text(comment.deleted ? 'Restore' : 'Delete'),
onTap: loggedInAction(handleDelete), 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( ListTile(
leading: const Icon(Icons.info_outline), leading: const Icon(Icons.info_outline),
title: const Text('Nerd stuff'), title: const Text('Nerd stuff'),

View File

@ -35,6 +35,7 @@ abstract class _CommentStore with Store {
final savingState = AsyncStore<FullCommentView>(); final savingState = AsyncStore<FullCommentView>();
final markPersonMentionAsReadState = AsyncStore<PersonMentionView>(); final markPersonMentionAsReadState = AsyncStore<PersonMentionView>();
final markAsReadState = AsyncStore<FullCommentView>(); final markAsReadState = AsyncStore<FullCommentView>();
final blockingState = AsyncStore<BlockedPerson>();
@computed @computed
bool get isMine => bool get isMine =>
@ -103,6 +104,21 @@ abstract class _CommentStore with Store {
if (result != null) comment = result.commentView; 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 @action
Future<void> markAsRead(Jwt token) async { Future<void> markAsRead(Jwt token) async {
if (userMentionId != null) { if (userMentionId != null) {

View File

@ -102,6 +102,13 @@ mixin _$CommentStore on _CommentStore, Store {
return _$saveAsyncAction.run(() => super.save(token)); 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'); final _$markAsReadAsyncAction = AsyncAction('_CommentStore.markAsRead');
@override @override