Skip to content
Repos

DevPlaceApp

C++ 1.6 KB54 lines Opens in site

Directory Raw Download

#pragma once
// Spec: models.md Comment (+ Model Updates). Wire is a wrapper: comment, author, my_vote, votes, attachments,
// children (recursive, depth capped by ModelLimits::kMaxCommentDepth), reactions.
#include <cstdint>
#include <optional>
#include <string>
#include <vector>

#include "core/Date.hpp"
#include "core/Json.hpp"
#include "model/Attachment.hpp"
#include "model/Reactions.hpp"
#include "model/User.hpp"
#include "model/Vote.hpp"

namespace dp {

struct Comment {
  struct Data {
    std::string id;
    std::string userId;
    std::string content;
    std::optional<std::string> parentId;
    std::string targetType;  // raw string
    std::string targetId;
    Date createdAt{};
    static Data fromJson(const Json& j);
  };
  struct Votes {
    std::int64_t up = 0;
    std::int64_t down = 0;
    static Votes fromJson(const Json& j);
  };

  Data data;
  User author;
  Vote myVote;
  Votes votes;
  std::vector<Attachment> attachments;
  std::vector<Comment> children;
  Reactions reactions;

  static Comment fromJson(const Json& j) { return fromJson(j, 0); }
  static Comment fromJson(const Json& j, std::size_t depth);
  std::string id() const { return "comment:" + data.id; }
  std::int64_t voteCount() const { return votes.up - votes.down; }

  Comment replacingReactions(const Reactions& newReactions, const std::vector<Comment>& newChildren) const;
  Comment replacingChildren(const std::vector<Comment>& newChildren) const;
  // Moves the viewer's vote: undoes the old one, applies the new one on the counters.
  Comment applyingVote(Vote newVote, const std::vector<Comment>& newChildren) const;
};

}  // namespace dp
47.0ms