Skip to content

Commit

Permalink
refactor: split dpp::thread from channel.h into thread.h
Browse files Browse the repository at this point in the history
fixes `dpp::message`'s copy constructor calling `std::vector<dpp::channel>`'s copy constructor with incomplete `dpp::channel`
  • Loading branch information
Mishura4 committed Dec 4, 2023
1 parent 60b0d08 commit 50d5746
Show file tree
Hide file tree
Showing 8 changed files with 364 additions and 295 deletions.
206 changes: 2 additions & 204 deletions include/dpp/channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
#include <dpp/managed.h>
#include <dpp/utility.h>
#include <dpp/voicestate.h>
#include <dpp/message.h>
#include <dpp/json_fwd.h>
#include <dpp/permissions.h>
#include <dpp/json_interface.h>
Expand Down Expand Up @@ -243,38 +242,6 @@ struct DPP_EXPORT permission_overwrite {
permission_overwrite(snowflake id, uint64_t allow, uint64_t deny, overwrite_type type);
};


/**
* @brief metadata for threads
*/
struct DPP_EXPORT thread_metadata {
/**
* @brief Timestamp when the thread's archive status was last changed, used for calculating recent activity.
*/
time_t archive_timestamp;

/**
* @brief The duration in minutes to automatically archive the thread after recent activity (60, 1440, 4320, 10080).
*/
uint16_t auto_archive_duration;

/**
* @brief Whether a thread is archived
*/
bool archived;

/**
* @brief Whether a thread is locked. When a thread is locked,
* only users with `MANAGE_THREADS` can un-archive it.
*/
bool locked;

/**
* @brief Whether non-moderators can add other non-moderators. Only for private threads.
*/
bool invitable;
};

/**
* @brief Auto archive duration of threads which will stop showing in the channel list after the specified period of inactivity.
* Defined as an enum to fit into 1 byte. Internally it'll be translated to minutes to match the API
Expand All @@ -301,42 +268,6 @@ enum auto_archive_duration_t : uint8_t {
arc_1_week = 4,
};

/**
* @brief represents membership of a user with a thread
*/
struct DPP_EXPORT thread_member : public json_interface<thread_member> {
protected:
friend struct json_interface<thread_member>;

/**
* @brief Read struct values from a json object
* @param j json to read values from
* @return A reference to self
*/
thread_member& fill_from_json_impl(nlohmann::json* j);

public:
/**
* @brief ID of the thread member is part of.
*/
snowflake thread_id;

/**
* @brief ID of the member.
*/
snowflake user_id;

/**
* @brief The time when user last joined the thread.
*/
time_t joined;

/**
* @brief Any user-thread settings, currently only used for notifications.
*/
uint32_t flags;
};

/**
* @brief Represents a tag that is able to be applied to a thread in a forum or media channel
*/
Expand Down Expand Up @@ -401,11 +332,6 @@ struct DPP_EXPORT forum_tag : public managed, public json_interface<forum_tag> {
forum_tag& set_name(const std::string& name);
};

/**
* @brief A group of thread member objects. the key is the user_id of the dpp::thread_member
*/
typedef std::unordered_map<snowflake, thread_member> thread_member_map;

/**
* @brief A definition of a discord channel.
* There are one of these for every channel type except threads. Threads are
Expand All @@ -429,6 +355,8 @@ class DPP_EXPORT channel : public managed, public json_interface<channel> {
*/
virtual json to_json_impl(bool with_id = false) const;

static constexpr uint16_t CHANNEL_TYPE_MASK = 0b0000000000001111;

public:
/**
* @brief Channel name (1-100 characters).
Expand Down Expand Up @@ -937,111 +865,6 @@ class DPP_EXPORT channel : public managed, public json_interface<channel> {

};

/** @brief A definition of a discord thread.
* A thread is a superset of a channel. Not to be confused with `std::thread`!
*/
class DPP_EXPORT thread : public channel, public json_interface<thread> {
protected:
friend struct json_interface<thread>;

/** Read class values from json object
* @param j A json object to read from
* @return A reference to self
*/
thread& fill_from_json_impl(nlohmann::json* j);

/**
* @brief Build json for this thread object
*
* @param with_id include the ID in the json
* @return std::string JSON string
*/
json to_json_impl(bool with_id = false) const override;

public:
using json_interface<thread>::fill_from_json;
using json_interface<thread>::build_json;
using json_interface<thread>::to_json;

/**
* @brief Thread member of current user if joined to the thread.
* Note this is only set by certain api calls otherwise contains default data
*/
thread_member member;

/**
* @brief Thread metadata (threads)
*/
thread_metadata metadata;

/**
* @brief Created message. Only filled within the cluster::thread_create_in_forum() method
*/
message msg;

/**
* @brief A list of dpp::forum_tag IDs that have been applied to a thread in a forum or media channel.
*/
std::vector<snowflake> applied_tags;

/**
* @brief Number of messages ever sent in the thread.
* It's similar to thread::message_count on message creation, but will not decrement the number when a message is deleted
*/
uint32_t total_messages_sent;

/**
* @brief Number of messages (not including the initial message or deleted messages) of the thread.
* For threads created before July 1, 2022, the message count is inaccurate when it's greater than 50.
*/
uint8_t message_count;

/**
* @brief Approximate count of members in a thread (stops counting at 50)
*/
uint8_t member_count;

/**
* @brief Construct a new thread object
*/
thread();

/**
* @brief Returns true if the thread is within an announcement channel
*
* @return true if announcement thread
*/
bool is_news_thread() const;

/**
* @brief Returns true if the channel is a public thread
*
* @return true if public thread
*/
bool is_public_thread() const;

/**
* @brief Returns true if the channel is a private thread
*
* @return true if private thread
*/
bool is_private_thread() const;

/**
* @brief Destroy the thread object
*/
virtual ~thread() = default;
};


/**
* @brief Serialize a thread_metadata object to json
*
* @param j JSON object to serialize to
* @param tmdata object to serialize
*/
void to_json(nlohmann::json& j, const thread_metadata& tmdata);

/**
* @brief Serialize a permission_overwrite object to json
*
Expand All @@ -1055,30 +878,5 @@ void to_json(nlohmann::json& j, const permission_overwrite& po);
*/
typedef std::unordered_map<snowflake, channel> channel_map;

/**
* @brief A group of threads
*/
typedef std::unordered_map<snowflake, thread> thread_map;

/**
* @brief A thread alongside the bot's optional thread_member object tied to it
*/
struct active_thread_info {
/**
* @brief The thread object
*/
thread active_thread;

/**
* @brief The bot as a thread member, only present if the bot is in the thread
*/
std::optional<thread_member> bot_member;
};

/**
* @brief A map of threads alongside optionally the thread_member tied to the bot if it is in the thread. The map's key is the thread id. Returned from the cluster::threads_get_active method
*/
using active_threads = std::map<snowflake, active_thread_info>;

} // namespace dpp

1 change: 1 addition & 0 deletions include/dpp/dispatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <dpp/role.h>
#include <dpp/user.h>
#include <dpp/channel.h>
#include <dpp/thread.h>
#include <dpp/guild.h>
#include <dpp/invite.h>
#include <dpp/emoji.h>
Expand Down
1 change: 1 addition & 0 deletions include/dpp/dpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#include <dpp/role.h>
#include <dpp/user.h>
#include <dpp/channel.h>
#include <dpp/thread.h>
#include <dpp/guild.h>
#include <dpp/invite.h>
#include <dpp/dtemplate.h>
Expand Down
49 changes: 39 additions & 10 deletions include/dpp/message.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <dpp/snowflake.h>
#include <dpp/managed.h>
#include <dpp/user.h>
#include <dpp/channel.h>
#include <dpp/guild.h>
#include <optional>
#include <variant>
Expand Down Expand Up @@ -622,11 +623,11 @@ class DPP_EXPORT component : public json_interface<component> {
component& add_component(const component& c);

/**
* @brief Add a default value.
*
* @param id Default value. ID of a user, role, or channel
* @param type The type this default value represents
*/
* @brief Add a default value.
*
* @param id Default value. ID of a user, role, or channel
* @param type The type this default value represents
*/
component& add_default_value(const snowflake id, const component_default_value_type type);

/**
Expand Down Expand Up @@ -1979,6 +1980,18 @@ struct DPP_EXPORT message : public managed, json_interface<message> {
*/
message();

/*
* @brief Construct a new message object
* @param m Message to copy
*/
message(const message& m) = default;

/*
* @brief Construct a new message object
* @param m Message to move
*/
message(message&& m) = default;

/**
* @brief Construct a new message object
* @param o Owning cluster, passed down to various things such as dpp::attachment.
Expand All @@ -1987,11 +2000,6 @@ struct DPP_EXPORT message : public managed, json_interface<message> {
*/
message(class cluster* o);

/**
* @brief Destroy the message object
*/
virtual ~message();

/**
* @brief Construct a new message object with a channel and content
*
Expand All @@ -2017,6 +2025,27 @@ struct DPP_EXPORT message : public managed, json_interface<message> {
*/
message(const std::string &content, message_type type = mt_default);

/**
* @brief Destroy the message object
*/
~message() override = default;

/**
* @brief Copy a message object
*
* @param m Message to copy
* @return message& Reference to self
*/
message &operator=(const message& m) = default;

/**
* @brief Move a message object
*
* @param m Message to move
* @return message& Reference to self
*/
message &operator=(message&& m) = default;

/**
* @brief Set the original message reference for replies/crossposts
*
Expand Down
Loading

0 comments on commit 50d5746

Please sign in to comment.