summaryrefslogtreecommitdiff
path: root/bot/src/schemas/channel.cpp
blob: 540b7c0d7b44aaf2d1201d1c4f5421f869fb8ae4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include "channel.hpp"

#include <optional>

namespace bot::schemas {
  std::optional<ChannelFeature> string_to_channel_feature(
      const std::string &value) {
    if (value == "markov_responses") {
      return MARKOV_RESPONSES;
    } else if (value == "random_markov_responses") {
      return RANDOM_MARKOV_RESPONSES;
    } else {
      return std::nullopt;
    }
  }

  std::optional<std::string> channelfeature_to_string(
      const ChannelFeature &value) {
    switch (value) {
      case MARKOV_RESPONSES:
        return "markov_responses";
      case RANDOM_MARKOV_RESPONSES:
        return "random_markov_responses";
      default:
        std::nullopt;
    }
  }

  sol::table Channel::as_lua_table(std::shared_ptr<sol::state> luaState) const {
    sol::table o = luaState->create_table();

    o["id"] = this->id;
    o["alias_id"] = this->alias_id;
    o["alias_name"] = this->alias_name;

    o["joined_at"] =
        static_cast<long>(std::chrono::duration_cast<std::chrono::seconds>(
                              this->joined_at.time_since_epoch())
                              .count());
    if (this->opted_out_at.has_value()) {
      o["opted_out_at"] =
          static_cast<long>(std::chrono::duration_cast<std::chrono::seconds>(
                                this->opted_out_at->time_since_epoch())
                                .count());
    } else {
      o["opted_out_at"] = sol::lua_nil;
    }

    return o;
  }

  sol::table ChannelPreferences::as_lua_table(
      std::shared_ptr<sol::state> luaState) const {
    sol::table o = luaState->create_table();

    o["id"] = this->channel_id;  // TODO: remove it later too.
    o["channel_id"] = this->channel_id;
    o["prefix"] = this->prefix;
    o["language"] = this->locale;

    sol::table f = luaState->create_table();

    for (const ChannelFeature &feature : this->features) {
      std::optional<std::string> ff = channelfeature_to_string(feature);
      if (ff.has_value()) {
        f.add(ff.value());
      }
    }

    o["features"] = f;

    return o;
  }
}