#include "api/ProdDevPlaceApi.hpp" #include "api/DevPlaceError.hpp" #include "api/LiveMessagesSocket.hpp" #include "core/UiQueue.hpp" #include "store/AppState.hpp" namespace dp { namespace { // Wraps `done` so it is skipped once the returned master token was cancelled by the caller. template Done guarded(const CancelPtr& master, Done done) { return [master, done = std::move(done)](Result r) { if (!master->cancelled()) done(std::move(r)); }; } } // namespace ProdDevPlaceApi& ProdDevPlaceApi::shared() { static ProdDevPlaceApi api; return api; } ProdDevPlaceApi::ProdDevPlaceApi(NetworkService& network) : request_(network) {} template CancelPtr ProdDevPlaceApi::failLater(Done done, Error error) { auto master = std::make_shared(); UiQueue::instance().post([master, done = std::move(done), error = std::move(error)] { if (!master->cancelled()) done(Result(error)); }); return master; } template CancelPtr ProdDevPlaceApi::withToken(Done done, Call call) { const auto token = AppState::instance().token(); if (!token) return failLater(std::move(done), DevPlaceError::notLoggedIn()); auto master = std::make_shared(); refreshTokenIfNeeded([master, token = *token, done = std::move(done), call = std::move(call)](Result r) mutable { if (master->cancelled()) return; if (!r.ok()) return done(Result(r.error())); call(token, guarded(master, std::move(done))); }); return master; } template CancelPtr ProdDevPlaceApi::afterRefresh(Done done, Call call) { auto master = std::make_shared(); refreshTokenIfNeeded([master, done = std::move(done), call = std::move(call)](Result r) mutable { if (master->cancelled()) return; if (!r.ok()) return done(Result(r.error())); call(AppState::instance().token(), guarded(master, std::move(done))); }); return master; } // ---- auth ---------------------------------------------------------------------------------------------------- void ProdDevPlaceApi::fetchOwnUser(const AuthToken& token, Done done) { request_.getProfile(std::nullopt, std::nullopt, token, [done = std::move(done)](Result r) { // Swift `try?`: a profile failure sets currentUser to nil, the login itself still succeeds. AppState::instance().setCurrentUser(r.ok() ? std::optional(r.value().user) : std::nullopt); done(Result(Unit{})); }); } CancelPtr ProdDevPlaceApi::logIn(const std::string& email, const std::string& password, Done done) { auto master = std::make_shared(); request_.getAuthToken(email, password, [this, master, done = std::move(done)](Result r) mutable { if (master->cancelled()) return; if (!r.ok()) return done(Result(r.error())); AuthToken token = r.value(); AppState::instance().setToken(token); fetchOwnUser(token, guarded(master, std::move(done))); }); return master; } CancelPtr ProdDevPlaceApi::signUp(const std::string& username, const std::string& email, const std::string& password, const std::string& confirmPassword, const BirthDate& birthDate, bool acceptTerms, Done done) { return request_.signUp(username, email, password, confirmPassword, birthDate, acceptTerms, std::move(done)); } CancelPtr ProdDevPlaceApi::deleteAccount(const std::string& password, Done done) { const auto& state = AppState::instance(); if (!state.token() || !state.currentUser()) return failLater(std::move(done), DevPlaceError::notLoggedIn()); const std::string username = state.currentUser()->username; return withToken(std::move(done), [this, username, password](const AuthToken& t, Done d) { request_.deleteAccount(username, password, t, std::move(d)); }); } // ---- reads --------------------------------------------------------------------------------------------------- CancelPtr ProdDevPlaceApi::feed(const std::optional& before, Done done) { return afterRefresh(std::move(done), [this, before](const std::optional& t, Done d) { request_.getFeed(std::nullopt, std::nullopt, std::nullopt, before, t, std::move(d)); }); } CancelPtr ProdDevPlaceApi::postDetail(const std::string& slug, Done done) { return afterRefresh(std::move(done), [this, slug](const std::optional& t, Done d) { request_.getPost(slug, t, std::move(d)); }); } CancelPtr ProdDevPlaceApi::profile(const Str& username, Done done) { return afterRefresh(std::move(done), [this, username](const std::optional& t, Done d) { if (!username && !t) { UiQueue::instance().post([d = std::move(d)] { d(Result(DevPlaceError::notLoggedIn())); }); return; } request_.getProfile(username, std::nullopt, t, std::move(d)); }); } // ---- writes (captured token) --------------------------------------------------------------------------------- #define DP_WITH_TOKEN(T, ...) \ withToken(std::move(done), [this, __VA_ARGS__](const AuthToken& t, Done d) CancelPtr ProdDevPlaceApi::createPost(const Str& title, const std::optional& topic, const std::string& content, const std::vector& attachments, const Str& pollQuestion, const std::vector& pollOptions, const Str& projectLink, Done done) { return DP_WITH_TOKEN(Unit, title, topic, content, attachments, pollQuestion, pollOptions, projectLink) { request_.createPost(title, topic, content, attachments, pollQuestion, pollOptions, projectLink, t, std::move(d)); }); } CancelPtr ProdDevPlaceApi::editPost(const std::string& slug, const std::string& title, PostTopic topic, const std::string& content, const std::string& projectLink, Done done) { return DP_WITH_TOKEN(Unit, slug, title, topic, content, projectLink) { request_.editPost(slug, title, topic, content, projectLink, t, std::move(d)); }); } CancelPtr ProdDevPlaceApi::deletePost(const std::string& slug, Done done) { return DP_WITH_TOKEN(Unit, slug) { request_.deletePost(slug, t, std::move(d)); }); } CancelPtr ProdDevPlaceApi::deleteComment(const std::string& uid, Done done) { return DP_WITH_TOKEN(Unit, uid) { request_.deleteComment(uid, t, std::move(d)); }); } CancelPtr ProdDevPlaceApi::editComment(const std::string& uid, const std::string& content, Done done) { return DP_WITH_TOKEN(Unit, uid, content) { request_.editComment(uid, content, t, std::move(d)); }); } CancelPtr ProdDevPlaceApi::createComment(TargetType targetType, const std::string& targetId, const std::string& content, const Str& parentId, const std::vector& attachments, Done done) { return DP_WITH_TOKEN(Unit, targetType, targetId, content, parentId, attachments) { request_.createComment(targetType, targetId, content, parentId, attachments, t, std::move(d)); }); } CancelPtr ProdDevPlaceApi::vote(TargetType targetType, const std::string& targetId, Vote vote, Done done) { return DP_WITH_TOKEN(Unit, targetType, targetId, vote) { request_.vote(targetType, targetId, vote, t, std::move(d)); }); } CancelPtr ProdDevPlaceApi::submitPollChoice(const std::string& pollId, const std::string& optionId, Done done) { return DP_WITH_TOKEN(Unit, pollId, optionId) { request_.submitPollChoice(pollId, optionId, t, std::move(d)); }); } CancelPtr ProdDevPlaceApi::react(TargetType targetType, const std::string& targetId, const std::string& emoji, Done done) { return DP_WITH_TOKEN(Unit, targetType, targetId, emoji) { request_.react(targetType, targetId, emoji, t, std::move(d)); }); } CancelPtr ProdDevPlaceApi::reportReasons(Done> done) { return request_.getReportReasons(std::move(done)); // no token, no refresh } CancelPtr ProdDevPlaceApi::report(TargetType targetType, const std::string& targetId, const std::string& reason, const Str& detail, Done done) { return DP_WITH_TOKEN(Unit, targetType, targetId, reason, detail) { request_.report(targetType, targetId, reason, detail, t, std::move(d)); }); } CancelPtr ProdDevPlaceApi::uploadFile(const std::string& data, const std::string& filename, const std::string& mimeType, Done done) { return DP_WITH_TOKEN(UploadResponse, data, filename, mimeType) { request_.uploadFile(data, filename, mimeType, t, std::move(d)); }); } CancelPtr ProdDevPlaceApi::uploadFromUrl(const std::string& url, const Str& filename, Done done) { return DP_WITH_TOKEN(UploadResponse, url, filename) { request_.uploadFromUrl(url, filename, t, std::move(d)); }); } CancelPtr ProdDevPlaceApi::deleteAttachment(const std::string& uid, Done done) { return DP_WITH_TOKEN(Unit, uid) { request_.deleteAttachment(uid, t, std::move(d)); }); } CancelPtr ProdDevPlaceApi::followUser(const std::string& username, Done done) { return DP_WITH_TOKEN(Unit, username) { request_.followUser(username, t, std::move(d)); }); } CancelPtr ProdDevPlaceApi::unfollowUser(const std::string& username, Done done) { return DP_WITH_TOKEN(Unit, username) { request_.unfollowUser(username, t, std::move(d)); }); } CancelPtr ProdDevPlaceApi::blockUser(const std::string& username, Done done) { return DP_WITH_TOKEN(Unit, username) { request_.blockUser(username, t, std::move(d)); }); } CancelPtr ProdDevPlaceApi::unblockUser(const std::string& username, Done done) { return DP_WITH_TOKEN(Unit, username) { request_.unblockUser(username, t, std::move(d)); }); } CancelPtr ProdDevPlaceApi::muteUser(const std::string& username, Done done) { return DP_WITH_TOKEN(Unit, username) { request_.muteUser(username, t, std::move(d)); }); } CancelPtr ProdDevPlaceApi::unmuteUser(const std::string& username, Done done) { return DP_WITH_TOKEN(Unit, username) { request_.unmuteUser(username, t, std::move(d)); }); } CancelPtr ProdDevPlaceApi::messages(const Str& withUid, Done done) { return DP_WITH_TOKEN(MessagesInbox, withUid) { request_.getMessages(withUid, std::nullopt, t, std::move(d)); }); } CancelPtr ProdDevPlaceApi::sendMessage(const std::string& receiverId, const std::string& content, const std::vector& attachments, Done done) { return DP_WITH_TOKEN(Unit, receiverId, content, attachments) { request_.sendMessage(receiverId, content, attachments, t, std::move(d)); }); } CancelPtr ProdDevPlaceApi::usersForMentioning(const std::string& query, Done done) { return DP_WITH_TOKEN(UserSearch, query) { request_.searchMessageRecipients(query, t, std::move(d)); }); } CancelPtr ProdDevPlaceApi::notifications(const std::optional& before, Done done) { return DP_WITH_TOKEN(Notifications, before) { request_.getNotifications(before, t, std::move(d)); }); } CancelPtr ProdDevPlaceApi::openNotification(const std::string& uid, Done done) { return DP_WITH_TOKEN(NotificationOpen, uid) { request_.openNotification(uid, t, std::move(d)); }); } CancelPtr ProdDevPlaceApi::notificationCounts(Done done) { return withToken(std::move(done), [this](const AuthToken& t, Done d) { request_.getNotificationCounts(t, std::move(d)); }); } CancelPtr ProdDevPlaceApi::markNotificationRead(const std::string& uid, Done done) { return DP_WITH_TOKEN(Unit, uid) { request_.markNotificationRead(uid, t, std::move(d)); }); } CancelPtr ProdDevPlaceApi::markAllNotificationsRead(Done done) { return withToken(std::move(done), [this](const AuthToken& t, Done d) { request_.markAllNotificationsRead(t, std::move(d)); }); } CancelPtr ProdDevPlaceApi::registerPushDevice(const std::string& deviceToken, Done done) { return DP_WITH_TOKEN(Unit, deviceToken) { request_.registerPushDevice(PushProvider::Apns, deviceToken, t, std::move(d)); }); } CancelPtr ProdDevPlaceApi::unregisterPushDevice(const std::string&, Done done) { // No backend endpoint: after the login check and the refresh this completes successfully (the Swift source marks this as not yet available). if (!AppState::instance().token()) return failLater(std::move(done), DevPlaceError::notLoggedIn()); auto master = std::make_shared(); refreshTokenIfNeeded(guarded(master, std::move(done))); return master; } #undef DP_WITH_TOKEN // ---- messages socket ----------------------------------------------------------------------------------------- std::unique_ptr ProdDevPlaceApi::makeMessagesSocket(const std::string& otherUserId) { auto provider = [this](Done done) -> CancelPtr { if (!AppState::instance().token()) return failLater(std::move(done), DevPlaceError::notLoggedIn()); auto master = std::make_shared(); refreshTokenIfNeeded([this, master, done = std::move(done)](Result r) mutable { if (master->cancelled()) return; if (!r.ok()) return done(Result(r.error())); const auto token = AppState::instance().token(); if (!token) return done(Result(DevPlaceError::notLoggedIn())); request_.getMessagesWebSocketTicket(*token, [this, master, done = std::move(done)](Result t) mutable { if (master->cancelled()) return; if (!t.ok()) return done(Result(t.error())); const std::string url = request_.messagesWebSocketUrl(t.value().ticket); if (url.empty()) return done(Result(DevPlaceError::invalidUrl(t.value().ticket))); done(Result(url)); }); }); return master; }; return std::make_unique(otherUserId, std::move(provider)); } } // namespace dp