import 'package:lemmy_api_client/v3.dart'; import 'package:mobx/mobx.dart'; import '../../comment_tree.dart'; import '../../stores/accounts_store.dart'; import '../../util/async_store.dart'; part 'comment_store.g.dart'; class CommentStore = _CommentStore with _$CommentStore; abstract class _CommentStore with Store { @observable CommentView comment; final ObservableList children; final int? userMentionId; final int depth; final bool canBeMarkedAsRead; final bool detached; final bool hideOnRead; @observable bool selectable = false; @observable bool collapsed = false; @observable bool showRaw = false; final votingState = AsyncStore(); final deletingState = AsyncStore(); final savingState = AsyncStore(); final markPersonMentionAsReadState = AsyncStore(); final markAsReadState = AsyncStore(); final blockingState = AsyncStore(); final reportingState = AsyncStore(); @computed bool get isMine => comment.comment.creatorId == _accountsStore.defaultUserDataFor(comment.instanceHost)?.userId; @computed VoteType get myVote => comment.myVote ?? VoteType.none; @computed bool get isOP => comment.comment.creatorId == comment.post.creatorId; final AccountsStore _accountsStore; _CommentStore( this._accountsStore, { required CommentTree commentTree, this.userMentionId, required this.depth, required this.canBeMarkedAsRead, required this.detached, required this.hideOnRead, }) : comment = commentTree.comment, children = commentTree.children.asObservable(); @action void toggleShowRaw() { showRaw = !showRaw; } @action void toggleSelectable() { selectable = !selectable; } @action void toggleCollapsed() { collapsed = !collapsed; } @action Future report(Jwt token, String reason) async { if (reason.trim().isEmpty) throw ArgumentError('reason must not be empty'); await reportingState.runLemmy( comment.instanceHost, CreateCommentReport( commentId: comment.comment.id, reason: reason, auth: token.raw, ), ); } @action Future delete(Jwt token) async { final result = await deletingState.runLemmy( comment.instanceHost, DeleteComment( commentId: comment.comment.id, deleted: !comment.comment.deleted, auth: token.raw, ), ); if (result != null) comment = result.commentView; } @action Future save(Jwt token) async { final result = await savingState.runLemmy( comment.instanceHost, SaveComment( commentId: comment.comment.id, save: !comment.saved, auth: token.raw, ), ); if (result != null) comment = result.commentView; } @action Future 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 markAsRead(Jwt token) async { if (userMentionId != null) { final result = await markPersonMentionAsReadState.runLemmy( comment.instanceHost, MarkPersonMentionAsRead( personMentionId: userMentionId!, read: !comment.comment.read, auth: token.raw, ), ); if (result != null) { comment = comment.copyWith(comment: result.comment); } } else { final result = await markAsReadState.runLemmy( comment.instanceHost, MarkCommentAsRead( commentId: comment.comment.id, read: !comment.comment.read, auth: token.raw, ), ); if (result != null) comment = result.commentView; } } @action Future _vote(VoteType voteType, Jwt token) async { final result = await votingState.runLemmy( comment.instanceHost, CreateCommentLike( commentId: comment.comment.id, score: voteType, auth: token.raw, ), ); if (result != null) comment = result.commentView; } @action Future upVote(Jwt token) async { await _vote( myVote == VoteType.up ? VoteType.none : VoteType.up, token, ); } @action Future downVote(Jwt token) async { await _vote( myVote == VoteType.down ? VoteType.none : VoteType.down, token, ); } @action void addReply(CommentView commentView) { children.insert(0, CommentTree(commentView)); } }