Skip to content
Repos

DevPlaceApp

C++ 5.6 KB84 lines Opens in site

Directory Raw Download

#pragma once
// Spec: shell.md section 6 (ProdDevPlaceApi). Network implementation over DevPlaceRequest. Pattern of every
// authenticated method (exactly like Swift): no token -> notLoggedIn; refreshTokenIfNeeded(); then the SDK call with
// the token captured BEFORE the refresh (feed, postDetail, profile and the websocket URL read it AFTER the refresh).
// Refresh has no lock: parallel requests can each re-login. logIn stores the token in AppState, then the own user
// (profile failure sets currentUser to nil, login still succeeds). No API-side error mapping: use AlertMessage.
// DIFF: unregisterPushDevice is a no-op after login+refresh (no backend endpoint), registerPushDevice sends "apns".
#include "api/DevPlaceApi.hpp"
#include "api/DevPlaceRequest.hpp"

namespace dp {

class ProdDevPlaceApi : public DevPlaceApi {
 public:
  using DevPlaceApi::feed;
  using DevPlaceApi::messages;
  using DevPlaceApi::notifications;

  static ProdDevPlaceApi& shared();
  explicit ProdDevPlaceApi(NetworkService& network = NetworkService::instance());

  DevPlaceRequest& request() { return request_; }

  CancelPtr logIn(const std::string& email, const std::string& password, Done<Unit> done) override;
  CancelPtr signUp(const std::string& username, const std::string& email, const std::string& password,
                   const std::string& confirmPassword, const BirthDate& birthDate, bool acceptTerms, Done<Unit> done) override;
  CancelPtr deleteAccount(const std::string& password, Done<Unit> done) override;
  CancelPtr feed(const std::optional<Date>& before, Done<Feed> done) override;
  CancelPtr postDetail(const std::string& slug, Done<PostDetail> done) override;
  CancelPtr createPost(const Str& title, const std::optional<PostTopic>& topic, const std::string& content,
                       const std::vector<UploadResponse>& attachments, const Str& pollQuestion,
                       const std::vector<std::string>& pollOptions, const Str& projectLink, Done<Unit> done) override;
  CancelPtr editPost(const std::string& slug, const std::string& title, PostTopic topic, const std::string& content,
                     const std::string& projectLink, Done<Unit> done) override;
  CancelPtr deletePost(const std::string& slug, Done<Unit> done) override;
  CancelPtr deleteComment(const std::string& uid, Done<Unit> done) override;
  CancelPtr editComment(const std::string& uid, const std::string& content, Done<Unit> done) override;
  CancelPtr createComment(TargetType targetType, const std::string& targetId, const std::string& content,
                          const Str& parentId, const std::vector<UploadResponse>& attachments, Done<Unit> done) override;
  CancelPtr profile(const Str& username, Done<Profile> done) override;
  CancelPtr followUser(const std::string& username, Done<Unit> done) override;
  CancelPtr unfollowUser(const std::string& username, Done<Unit> done) override;
  CancelPtr blockUser(const std::string& username, Done<Unit> done) override;
  CancelPtr unblockUser(const std::string& username, Done<Unit> done) override;
  CancelPtr muteUser(const std::string& username, Done<Unit> done) override;
  CancelPtr unmuteUser(const std::string& username, Done<Unit> done) override;
  CancelPtr messages(const Str& withUid, Done<MessagesInbox> done) override;
  CancelPtr sendMessage(const std::string& receiverId, const std::string& content,
                        const std::vector<UploadResponse>& attachments, Done<Unit> done) override;
  std::unique_ptr<MessagesSocket> makeMessagesSocket(const std::string& otherUserId) override;
  CancelPtr usersForMentioning(const std::string& query, Done<UserSearch> done) override;
  CancelPtr notifications(const std::optional<Date>& before, Done<Notifications> done) override;
  CancelPtr openNotification(const std::string& uid, Done<NotificationOpen> done) override;
  CancelPtr notificationCounts(Done<NotificationCounts> done) override;
  CancelPtr markNotificationRead(const std::string& uid, Done<Unit> done) override;
  CancelPtr markAllNotificationsRead(Done<Unit> done) override;
  CancelPtr registerPushDevice(const std::string& deviceToken, Done<Unit> done) override;
  CancelPtr unregisterPushDevice(const std::string& deviceToken, Done<Unit> done) override;
  CancelPtr vote(TargetType targetType, const std::string& targetId, Vote vote, Done<Unit> done) override;
  CancelPtr submitPollChoice(const std::string& pollId, const std::string& optionId, Done<Unit> done) override;
  CancelPtr react(TargetType targetType, const std::string& targetId, const std::string& emoji, Done<Unit> done) override;
  CancelPtr reportReasons(Done<std::vector<ReportReason>> done) override;
  CancelPtr report(TargetType targetType, const std::string& targetId, const std::string& reason, const Str& detail,
                   Done<Unit> done) override;
  CancelPtr uploadFile(const std::string& data, const std::string& filename, const std::string& mimeType,
                       Done<UploadResponse> done) override;
  CancelPtr uploadFromUrl(const std::string& url, const Str& filename, Done<UploadResponse> done) override;
  CancelPtr deleteAttachment(const std::string& uid, Done<Unit> done) override;

 private:
  // Captured-token pattern: call(token, done) runs after the refresh with the token read before it.
  template <class T, class Call>
  CancelPtr withToken(Done<T> done, Call call);
  // Fresh-token pattern: refresh first, then call(AppState token or nullopt, done).
  template <class T, class Call>
  CancelPtr afterRefresh(Done<T> done, Call call);
  template <class T>
  static CancelPtr failLater(Done<T> done, Error error);
  void fetchOwnUser(const AuthToken& token, Done<Unit> done);

  DevPlaceRequest request_;
};

}  // namespace dp
26.1ms