#include "screens/feed/FeedPostActions.hpp"
#include "core/Log.hpp"
#include "screens/AppContext.hpp"
#include "store/AppState.hpp"
namespace dp {
namespace {
void reloadFeedLogged(const char* what) {
AppState::instance().loadFeed(AppContext::api(), [what](Result<Unit> r) {
if (!r.ok()) Log::warn(std::string(what) + " failed: " + r.error().message);
});
}
} // namespace
bool FeedPostActions::doubleTapPost(const Post& post) {
if (AppState::instance().isCurrentUser(post.data.userId)) return false;
const std::string id = post.data.id;
const std::int64_t stars = post.data.stars;
const Vote previous = post.myVote;
AppState::instance().performVoteToggle(TargetType::Post, id, previous, AppContext::api(), [id, stars, previous](Vote newVote) {
AppState::instance().updatePostVoteInFeed(id, newVote, votedCount(stars, previous, newVote));
});
return true;
}
void FeedPostActions::doubleTapComment(const CommentRef& comment) {
if (AppState::instance().isCurrentUser(comment.userId)) return;
const std::string id = comment.id;
AppState::instance().performVoteToggle(TargetType::Comment, id, comment.myVote, AppContext::api(),
[id](Vote newVote) { AppState::instance().updateCommentVoteInFeed(id, newVote); });
}
void FeedPostActions::deletePost(const Post& post) {
if (!post.data.slug) return;
AppContext::api().deletePost(*post.data.slug, [](Result<Unit> r) {
if (!r.ok()) return Log::warn("Delete post failed: " + r.error().message);
reloadFeedLogged("Reload feed");
});
}
void FeedPostActions::reloadFeed() { reloadFeedLogged("Reload feed"); }
void FeedPostActions::deleteComment(const CommentRef& comment) {
AppContext::api().deleteComment(comment.id, [](Result<Unit> r) {
if (!r.ok()) return Log::warn("Delete comment failed: " + r.error().message);
reloadFeedLogged("Reload feed");
});
}
void FeedPostActions::editComment(const CommentRef& comment, const std::string& content) {
AppContext::api().editComment(comment.id, content, [](Result<Unit> r) {
if (!r.ok()) return Log::warn("Edit comment failed: " + r.error().message);
reloadFeedLogged("Reload feed");
});
}
void FeedPostActions::createComment(TargetType targetType, const std::string& targetId, const std::optional<std::string>& parentId,
const std::string& content, const std::vector<UploadResponse>& attachments,
std::function<void(bool)> done) {
AppContext::api().createComment(targetType, targetId, content, parentId, attachments, [done](Result<Unit> r) {
if (!r.ok()) {
Log::warn("Create comment failed: " + r.error().message);
if (done) done(false);
return;
}
AppState::instance().loadFeed(AppContext::api(), [done](Result<Unit> f) {
if (!f.ok()) Log::warn("Create comment failed: " + f.error().message);
if (done) done(f.ok());
});
});
}
void FeedPostActions::reactPost(const Post& post, const std::string& emoji) {
const std::string id = post.data.id;
AppState::instance().performReactionToggle(TargetType::Post, id, emoji, AppContext::api(),
[id, emoji] { AppState::instance().updatePostReactionInFeed(id, emoji); });
}
void FeedPostActions::reactComment(const CommentRef& comment, const std::string& emoji) {
const std::string id = comment.id;
AppState::instance().performReactionToggle(TargetType::Comment, id, emoji, AppContext::api(),
[id, emoji] { AppState::instance().updateCommentReactionInFeed(id, emoji); });
}
} // namespace dp