Skip to content

Commit

Permalink
delete reaction
Browse files Browse the repository at this point in the history
  • Loading branch information
leo-lox committed Nov 19, 2024
1 parent 45e6f06 commit 18edfd0
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 16 deletions.
21 changes: 9 additions & 12 deletions lib/data_layer/repositories/note_repository_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -232,23 +232,20 @@ class NoteRepositoryImpl implements NoteRepository {
);

final response = dartNdkSource.dartNdk.requests.query(
timeout: 2,
filters: [filter],
name: 'getReactions-${postId.substring(5, 10)}',
name: 'getReactions-${postId.substring(5, 10)}-',
cacheRead: false,
cacheWrite: true,
cacheWrite: false,
);

return response.stream
final events = await response.future;

return events
.map(
(event) => NostrNoteModel.fromNDKEvent(event),
)
.timeout(
const Duration(seconds: 2),
onTimeout: (sink) {
log('getReactions timeout');
sink.close();
},
).toList();
(event) => NostrNoteModel.fromNDKEvent(event),
)
.toList();
}

@override
Expand Down
11 changes: 9 additions & 2 deletions lib/domain_layer/usecases/user_reactions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,16 @@ class UserReactions {
if (reactions.isEmpty) {
return null;
}
if (reactions.first.content == "+") {
return reactions.first;

final res = reactions.where((reaction) {
return reaction.tags.any((tag) => tag.type == "e" && tag.value == postId);
}).first;

if (res.content != "+") {
return null;
}

return res;
}

Future<void> likePost({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class PostLikeNotifier extends StateNotifier<PostLikeState> {
Future<void> _initializeLikeState() async {
final isLiked = await _userReactions.isPostSelfLiked(postId: _postId);

state = PostLikeState(isLiked: isLiked, isLoading: false);
state = state.copyWith(isLiked: isLiked, isLoading: false);
}

Future<void> toggleLike() async {
Expand All @@ -46,7 +46,7 @@ class PostLikeNotifier extends StateNotifier<PostLikeState> {

try {
if (state.isLiked) {
await _userReactions.deleteReaction(postId: _postId);
_userReactions.deleteReaction(postId: _postId);
} else {
await _userReactions.likePost(
pubkeyOfEventAuthor: _postAuthorPubkey,
Expand Down

0 comments on commit 18edfd0

Please sign in to comment.