|
| 1 | +// |
| 2 | +// messages.hpp |
| 3 | +// ************ |
| 4 | +// |
| 5 | +// Copyright (c) 2020 Sharon Fox (sharon at xandium dot io) |
| 6 | +// |
| 7 | +// Distributed under the MIT License. (See accompanying file LICENSE) |
| 8 | +// |
| 9 | + |
| 10 | +#pragma once |
| 11 | + |
| 12 | +#include "aegis/config.hpp" |
| 13 | +#include "aegis/shards/shard.hpp" |
| 14 | +#include "aegis/rest/rest_reply.hpp" |
| 15 | +#include "aegis/gateway/objects/message.hpp" |
| 16 | +#include <nlohmann/json.hpp> |
| 17 | + |
| 18 | +namespace aegis |
| 19 | +{ |
| 20 | + |
| 21 | +namespace gateway |
| 22 | +{ |
| 23 | + |
| 24 | +namespace objects |
| 25 | +{ |
| 26 | + |
| 27 | +class message; |
| 28 | + |
| 29 | +/// \cond TEMPLATES |
| 30 | +inline void from_json(const nlohmann::json& j, objects::messages& m); |
| 31 | + |
| 32 | +inline void to_json(nlohmann::json& j, const objects::messages& m); |
| 33 | +/// \endcond |
| 34 | + |
| 35 | +/**\todo Needs documentation |
| 36 | + */ |
| 37 | +class messages |
| 38 | +{ |
| 39 | +public: |
| 40 | + |
| 41 | + /// Constructor for the messages array |
| 42 | + /** |
| 43 | + * @param _core Pointer of core object |
| 44 | + */ |
| 45 | + messages(aegis::core * _core) noexcept |
| 46 | + : _core(_core) |
| 47 | + { |
| 48 | + } |
| 49 | + |
| 50 | + /// Constructor for the message object |
| 51 | + /** |
| 52 | + * @param _json JSON string of the message object |
| 53 | + * @param bot Pointer of core object |
| 54 | + */ |
| 55 | + messages(const std::string& _json, aegis::core* _core) noexcept |
| 56 | + : _core(_core) |
| 57 | + { |
| 58 | + from_json(nlohmann::json::parse(_json), *this); |
| 59 | + } |
| 60 | + |
| 61 | + /// Constructor for the messages object |
| 62 | + /** |
| 63 | + * @param _json JSON object of the messages array |
| 64 | + * @param _core Pointer of core object |
| 65 | + */ |
| 66 | + messages(const nlohmann::json & _json, aegis::core * _core) noexcept |
| 67 | + : _core(_core) |
| 68 | + { |
| 69 | + from_json(_json, *this); |
| 70 | + } |
| 71 | + |
| 72 | + /// Set the guild of the message object. This is mostly an internal function |
| 73 | + /// though is left public for lower level use |
| 74 | + /** |
| 75 | + * @param _guild Pointer of the guild to assign the message to |
| 76 | + */ |
| 77 | + AEGIS_DECL void set_guild(aegis::guild * _guild) |
| 78 | + { |
| 79 | + this->_guild = _guild; |
| 80 | + } |
| 81 | + |
| 82 | + messages() = default; |
| 83 | + messages& operator=(const messages&) = default; |
| 84 | + messages(const messages&) = default; |
| 85 | + messages(messages&& msg) = default; |
| 86 | + |
| 87 | + std::vector<aegis::gateway::objects::message>::iterator begin() |
| 88 | + { |
| 89 | + return _messages.begin(); |
| 90 | + } |
| 91 | + |
| 92 | + std::vector<aegis::gateway::objects::message>::iterator end() |
| 93 | + { |
| 94 | + return _messages.end(); |
| 95 | + } |
| 96 | + |
| 97 | + std::vector<aegis::gateway::objects::message>::reverse_iterator rbegin() |
| 98 | + { |
| 99 | + return _messages.rbegin(); |
| 100 | + } |
| 101 | + |
| 102 | + std::vector<aegis::gateway::objects::message>::reverse_iterator rend() |
| 103 | + { |
| 104 | + return _messages.rend(); |
| 105 | + } |
| 106 | + |
| 107 | + std::vector<aegis::gateway::objects::message> _messages; /**< array of messages */ |
| 108 | + |
| 109 | +private: |
| 110 | + friend inline void from_json(const nlohmann::json& j, objects::messages& m); |
| 111 | + friend inline void to_json(nlohmann::json& j, const objects::messages& m); |
| 112 | + friend class aegis::core; |
| 113 | + |
| 114 | + std::string _content;/**< String of the message contents */ |
| 115 | + aegis::channel * _channel = nullptr;/**< Pointer to the channel this message belongs to */ |
| 116 | + aegis::guild * _guild = nullptr;/**< Pointer to the guild this message belongs to */ |
| 117 | +#if !defined(AEGIS_DISABLE_ALL_CACHE) |
| 118 | + aegis::user * _user = nullptr;/**< Pointer to the author of this message */ |
| 119 | +#endif |
| 120 | + aegis::core * _core = nullptr; |
| 121 | + snowflake _message_id = 0; /**< snowflake of the message */ |
| 122 | + snowflake _channel_id = 0; /**< snowflake of the channel this message belongs to */ |
| 123 | + snowflake _guild_id = 0; /**< snowflake of the guild this message belongs to */ |
| 124 | + snowflake _author_id = 0; /**< snowflake of the author of this message */ |
| 125 | +}; |
| 126 | + |
| 127 | +/// \cond TEMPLATES |
| 128 | +inline void from_json(const nlohmann::json& j, objects::messages& m) |
| 129 | +{ |
| 130 | + if (j.size()) |
| 131 | + for (const auto& _message : j) |
| 132 | + m._messages.push_back(_message); |
| 133 | +} |
| 134 | + |
| 135 | +inline void to_json(nlohmann::json& j, const objects::messages& m) |
| 136 | +{ |
| 137 | + if (!m._messages.empty()) |
| 138 | + for (const auto& _message : m._messages) |
| 139 | + j.push_back(_message); |
| 140 | +} |
| 141 | +/// \endcond |
| 142 | + |
| 143 | +} |
| 144 | + |
| 145 | +} |
| 146 | + |
| 147 | +} |
0 commit comments