Skip to content

Commit 3ac2ca0

Browse files
committed
breaking: rewrote GetChannelPins
1 parent c2118a7 commit 3ac2ca0

File tree

4 files changed

+123
-4
lines changed

4 files changed

+123
-4
lines changed

include/dpp/message.h

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2334,7 +2334,7 @@ template <typename T> struct message_snapshot {
23342334
std::vector<T> messages;
23352335
};
23362336

2337-
/**
2337+
/**
23382338
* @brief Represents messages sent and received on Discord
23392339
*/
23402340
struct DPP_EXPORT message : public managed, json_interface<message> {
@@ -2998,11 +2998,88 @@ struct DPP_EXPORT message : public managed, json_interface<message> {
29982998
[[nodiscard]] bool has_poll() const noexcept;
29992999
};
30003000

3001+
/**
3002+
* @brief Represents a discord webhook
3003+
*/
3004+
class DPP_EXPORT message_pin : public json_interface<message_pin> {
3005+
protected:
3006+
friend struct json_interface<message_pin>;
3007+
3008+
/**
3009+
* @brief Fill in object from json data
3010+
*
3011+
* @param j JSON data
3012+
* @return message_pin& Reference to self
3013+
*/
3014+
message_pin& fill_from_json_impl(nlohmann::json* j);
3015+
3016+
/** Build JSON from this object.
3017+
* @return The JSON text of the message
3018+
*/
3019+
virtual json to_json() const;
3020+
3021+
public:
3022+
3023+
message_pin();
3024+
3025+
message_pin(time_t _pinned_at, const message& _pinned_message);
3026+
3027+
/*
3028+
* @brief Construct a new message_pin object
3029+
* @param msg_pin message_pin to copy
3030+
*/
3031+
message_pin(const message_pin& msg_pin) = default;
3032+
3033+
/*
3034+
* @brief Construct a new message_pin object
3035+
* @param msg_pin message_pin to move
3036+
*/
3037+
message_pin(message_pin&& msg_pin) = default;
3038+
3039+
/**
3040+
* @brief Destroy the message_pin object
3041+
*/
3042+
~message_pin() = default;
3043+
3044+
/**
3045+
* @brief Copy a message_pin object
3046+
*
3047+
* @param msg_pin message_pin to copy
3048+
* @return message_pin& Reference to self
3049+
*/
3050+
message_pin &operator=(const message_pin& msg_pin) = default;
3051+
3052+
/**
3053+
* @brief Move a message_pin object
3054+
*
3055+
* @param msg_pin message_pin to move
3056+
* @return message_pin& Reference to self
3057+
*/
3058+
message_pin &operator=(message_pin&& msg_pin) = default;
3059+
3060+
public:
3061+
3062+
/**
3063+
* @brief The time the message was pinned.
3064+
*/
3065+
time_t pinned_at;
3066+
3067+
/**
3068+
* @brief The pinned message.
3069+
*/
3070+
message pinned_message;
3071+
};
3072+
30013073
/**
30023074
* @brief A group of messages
30033075
*/
30043076
typedef std::unordered_map<snowflake, message> message_map;
30053077

3078+
/**
3079+
* @brief A group of pinned messages
3080+
*/
3081+
typedef std::unordered_map<snowflake, message_pin> message_pin_map;
3082+
30063083
/**
30073084
* @brief A group of stickers
30083085
*/

include/dpp/restresults.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ typedef std::variant<
135135
confirmation,
136136
message,
137137
message_map,
138+
message_pin_map,
138139
user,
139140
user_identified,
140141
user_map,

src/dpp/cluster/message.cpp

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ void cluster::message_get_reactions(snowflake message_id, snowflake channel_id,
153153

154154

155155
void cluster::message_pin(snowflake channel_id, snowflake message_id, command_completion_event_t callback) {
156-
rest_request<confirmation>(this, API_PATH "/channels", std::to_string(channel_id), "/messages/pins/" + std::to_string(message_id), m_put, "", std::move(callback));
156+
rest_request<confirmation>(this, API_PATH "/channels", std::to_string(channel_id), "messages/pins/" + std::to_string(message_id), m_put, "", std::move(callback));
157157
}
158158

159159
void cluster::messages_get(snowflake channel_id, snowflake around, snowflake before, snowflake after, uint64_t limit, command_completion_event_t callback) {
@@ -168,7 +168,7 @@ void cluster::messages_get(snowflake channel_id, snowflake around, snowflake bef
168168

169169

170170
void cluster::message_unpin(snowflake channel_id, snowflake message_id, command_completion_event_t callback) {
171-
rest_request<confirmation>(this, API_PATH "/channels", std::to_string(channel_id), "/messages/pins/" + std::to_string(message_id), m_delete, "", std::move(callback));
171+
rest_request<confirmation>(this, API_PATH "/channels", std::to_string(channel_id), "messages/pins/" + std::to_string(message_id), m_delete, "", std::move(callback));
172172
}
173173

174174

@@ -205,7 +205,26 @@ void cluster::poll_end(snowflake message_id, snowflake channel_id, command_compl
205205

206206

207207
void cluster::channel_pins_get(snowflake channel_id, command_completion_event_t callback) {
208-
rest_request_list<message>(this, API_PATH "/channels", std::to_string(channel_id), "/messages/pins", m_get, "", std::move(callback));
208+
/* Have to do it like this because this now returns more than just a list.
209+
* It's now a structure containing the pinned items in "items" and then a boolean called "has_more".
210+
*/
211+
this->post_rest_multipart(API_PATH "/channels", std::to_string(channel_id), "messages/pins", m_get, "", [this, callback](json &j, const http_request_completion_t& http) {
212+
std::unordered_map<snowflake, dpp::message_pin> list;
213+
214+
confirmation_callback_t e(this, confirmation(), http);
215+
if (!e.is_error()) {
216+
for (auto & curr_item : j["items"]) {
217+
dpp::message_pin pinned_msg{};
218+
pinned_msg.pinned_at = ts_not_null(&curr_item, "pinned_at");
219+
pinned_msg.pinned_message = message().fill_from_json(&curr_item["message"]);
220+
221+
list[pinned_msg.pinned_message.id] = pinned_msg;
222+
}
223+
}
224+
if (callback) {
225+
callback(confirmation_callback_t(this, list, http));
226+
}
227+
});
209228
}
210229

211230
}

src/dpp/message.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1783,5 +1783,27 @@ sticker& sticker::set_file_content(std::string_view fc) {
17831783
return *this;
17841784
}
17851785

1786+
message_pin::message_pin() : pinned_at(-1), pinned_message() {
1787+
}
1788+
1789+
message_pin::message_pin(time_t _pinned_at, const message& _pinned_message) : pinned_at(_pinned_at), pinned_message(_pinned_message) {
1790+
}
1791+
1792+
1793+
message_pin& message_pin::fill_from_json_impl(nlohmann::json *j) {
1794+
this->pinned_at = ts_not_null(j, "pinned_at");
1795+
json& j_message = (*j)["message"];
1796+
this->pinned_message = message().fill_from_json(&j_message);
1797+
1798+
return *this;
1799+
}
17861800

1801+
json message_pin::to_json() const {
1802+
json j;
1803+
j["pinned_at"] = pinned_at;
1804+
j["message"] = pinned_message.to_json();
1805+
return j;
17871806
}
1807+
1808+
1809+
}// namespace dpp

0 commit comments

Comments
 (0)