Skip to content
This repository was archived by the owner on Dec 1, 2021. It is now read-only.

Commit e988752

Browse files
committed
Added channel:get_messages() endpoint
1 parent c2ab49f commit e988752

File tree

5 files changed

+216
-0
lines changed

5 files changed

+216
-0
lines changed

.gitattributes

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Set the default behavior, in case people don't have core.autocrlf set.
2+
* text=auto
3+
4+
# Explicitly declare text files you want to always be normalized and converted
5+
# to native line endings on checkout.
6+
*.c text
7+
*.h text
8+
9+
# Declare files that will always have CRLF line endings on checkout.
10+
*.sln text eol=crlf

include/aegis/channel.hpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,23 @@ struct edit_message_t
6464
json _embed;
6565
};
6666

67+
struct get_messages_t
68+
{
69+
enum class get_messages_type {
70+
AROUND,
71+
BEFORE,
72+
AFTER
73+
};
74+
get_messages_t & message_id(snowflake param) { _message_id = param; return *this; }
75+
get_messages_t & around() { _type = get_messages_type::AROUND; return *this; }
76+
get_messages_t & before() { _type = get_messages_type::BEFORE; return *this; }
77+
get_messages_t & after() { _type = get_messages_type::AFTER; return *this; }
78+
get_messages_t & limit(int16_t param) { _limit = param; return *this; }
79+
get_messages_type _type = get_messages_type::AFTER;
80+
snowflake _message_id;
81+
int16_t _limit;
82+
};
83+
6784
struct modify_channel_t
6885
{
6986
modify_channel_t & name(const std::string & param) { _name = param; return *this; }
@@ -241,6 +258,15 @@ class channel
241258
*/
242259
AEGIS_DECL aegis::future<gateway::objects::message> get_message(snowflake message_id);
243260

261+
/// Get multiple messages from this channel
262+
/**
263+
* @see aegis::get_messages_t
264+
* @param obj Struct of the request
265+
* @returns aegis::future<gateway::objects::messages>
266+
*/
267+
AEGIS_DECL aegis::future<gateway::objects::messages> get_messages(get_messages_t obj);
268+
269+
244270
/// Edit a message in this channel
245271
/**
246272
* @param message_id Snowflake of the message to replace. Must be your own message

include/aegis/fwd.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ namespace objects
4141
{
4242
struct user;
4343
class message;
44+
class messages;
4445
struct channel;
4546
struct guild;
4647
struct emoji;
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
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+
}

include/aegis/impl/channel.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "aegis/rest/rest_reply.hpp"
1919
#include "aegis/ratelimit/ratelimit.hpp"
2020
#include "aegis/gateway/objects/message.hpp"
21+
#include "aegis/gateway/objects/messages.hpp"
2122

2223
namespace aegis
2324
{
@@ -177,6 +178,37 @@ AEGIS_DECL aegis::future<gateway::objects::message> channel::get_message(snowfla
177178
return _ratelimit.post_task<gateway::objects::message>({ _endpoint, rest::Get });
178179
}
179180

181+
AEGIS_DECL aegis::future<gateway::objects::messages> channel::get_messages(get_messages_t obj)
182+
{
183+
#if !defined(AEGIS_DISABLE_ALL_CACHE)
184+
if (_guild && !perms().can_read_history())
185+
return aegis::make_exception_future<gateway::objects::messages>(error::no_permission);
186+
#endif
187+
188+
std::shared_lock<shared_mutex> l(_m);
189+
190+
std::string type_, query_params{ "?" }, limit;
191+
192+
if (obj._limit >= 1 && obj._limit <= 100)
193+
limit = fmt::format("&limit={}", obj._limit);
194+
195+
switch (obj._type)
196+
{
197+
case get_messages_t::get_messages_type::AFTER:
198+
query_params += fmt::format("after={}{}", obj._message_id, limit);
199+
break;
200+
case get_messages_t::get_messages_type::AROUND:
201+
query_params += fmt::format("after={}{}", obj._message_id, limit);
202+
break;
203+
case get_messages_t::get_messages_type::BEFORE:
204+
query_params += fmt::format("after={}{}", obj._message_id, limit);
205+
break;
206+
}
207+
208+
std::string _endpoint = fmt::format("/channels/{}/messages", channel_id);
209+
return _ratelimit.post_task<gateway::objects::messages>({ _endpoint, rest::Get, {}, {}, {}, {}, query_params });
210+
}
211+
180212
AEGIS_DECL aegis::future<aegis::gateway::objects::message> channel::create_message(create_message_t obj)
181213
{
182214
if (obj._file.has_value())

0 commit comments

Comments
 (0)