Skip to content
Repos

DevPlaceApp

C++ 3.4 KB95 lines Opens in site

Directory Raw Download

#include "model/ChatSocketCodec.hpp"

#include <cstdio>

#include "core/JsonDecode.hpp"
#include "spec/NetLimits.hpp"
#include "spec/SocketSpec.hpp"

namespace dp {

namespace {

using D = JsonDecode;
using spec::ChatFrames;

ChatSocketIncomingMessage decodeMessage(const Json& j) {
  ChatSocketIncomingMessage m;
  m.uid = D::req<std::string>(j, "uid");
  m.senderUid = D::req<std::string>(j, "sender_uid");
  m.receiverUid = D::req<std::string>(j, "receiver_uid");
  m.content = D::req<std::string>(j, "content");
  m.createdAt = D::reqDate(j, "created_at");
  m.attachments = D::optList<Attachment>(j, "attachments", [](const Json& item) { return Attachment::fromChatFrame(item); });
  m.clientId = D::opt<std::string>(j, "client_id");
  return m;
}

}  // namespace

std::optional<ChatSocketEvent> ChatSocketCodec::decode(const std::string& frame) {
  if (frame.size() > spec::NetLimits::kMaxSocketFrameBytes) return std::nullopt;
  try {
    const Json j = Json::parse(frame);
    const auto type = D::opt<std::string>(j, "type");
    if (!type) return std::nullopt;
    if (*type == ChatFrames::kServerReady) return ChatSocketEvent::ready(D::req<std::string>(j, "user_uid"));
    if (*type == ChatFrames::kServerMessage) return ChatSocketEvent::incoming(decodeMessage(j));
    if (*type == ChatFrames::kServerTyping) return ChatSocketEvent::typing(D::req<std::string>(j, "from_uid"));
    if (*type == ChatFrames::kServerRead) return ChatSocketEvent::read(D::req<std::string>(j, "by_uid"));
    if (*type == ChatFrames::kServerError) {
      return ChatSocketEvent::failed(D::opt<std::string>(j, "client_id"), D::req<std::string>(j, "text"));
    }
    return std::nullopt;
  } catch (const std::exception&) {
    return std::nullopt;
  }
}

std::string ChatSocketCodec::quote(const std::string& text) {
  std::string out = "\"";
  for (const unsigned char c : text) {
    switch (c) {
      case '"': out += "\\\""; break;
      case '\\': out += "\\\\"; break;
      case '/': out += "\\/"; break;
      case '\b': out += "\\b"; break;
      case '\f': out += "\\f"; break;
      case '\n': out += "\\n"; break;
      case '\r': out += "\\r"; break;
      case '\t': out += "\\t"; break;
      default:
        if (c < 0x20) {
          char buffer[8];
          std::snprintf(buffer, sizeof buffer, "\\u%04x", c);
          out += buffer;
        } else {
          out.push_back(static_cast<char>(c));
        }
    }
  }
  out.push_back('"');
  return out;
}

std::string ChatSocketCodec::encodeSend(const std::string& receiverUid, const std::string& content,
                                        const std::vector<std::string>& attachmentUids, const std::string& clientId) {
  std::string uids = "[";
  for (std::size_t i = 0; i < attachmentUids.size(); ++i) {
    if (i) uids.push_back(',');
    uids += quote(attachmentUids[i]);
  }
  uids.push_back(']');
  return std::string("{\"type\":") + quote(ChatFrames::kClientSend) + ",\"receiver_uid\":" + quote(receiverUid) +
         ",\"content\":" + quote(content) + ",\"attachment_uids\":" + uids + ",\"client_id\":" + quote(clientId) + "}";
}

std::string ChatSocketCodec::encodeTyping(const std::string& receiverUid) {
  return std::string("{\"type\":") + quote(ChatFrames::kClientTyping) + ",\"receiver_uid\":" + quote(receiverUid) + "}";
}

std::string ChatSocketCodec::encodeRead(const std::string& withUid) {
  return std::string("{\"type\":") + quote(ChatFrames::kClientRead) + ",\"with_uid\":" + quote(withUid) + "}";
}

}  // namespace dp
29.1ms