mirror of
https://github.com/celisej567/abaddon.git
synced 2026-09-04 11:33:27 +03:00
friends: send friend requests
This commit is contained in:
@@ -2,6 +2,8 @@
|
||||
#include "../abaddon.hpp"
|
||||
#include "lazyimage.hpp"
|
||||
|
||||
using namespace std::string_literals;
|
||||
|
||||
FriendsList::FriendsList()
|
||||
: Gtk::Box(Gtk::ORIENTATION_VERTICAL)
|
||||
, m_filter_mode(FILTER_FRIENDS) {
|
||||
@@ -167,9 +169,12 @@ FriendsListAddComponent::FriendsListAddComponent()
|
||||
m_box.add(m_add);
|
||||
m_box.add(m_status);
|
||||
|
||||
m_add.signal_clicked().connect(sigc::mem_fun(*this, &FriendsListAddComponent::Submit));
|
||||
|
||||
m_label.set_halign(Gtk::ALIGN_CENTER);
|
||||
|
||||
m_entry.set_placeholder_text("Enter a Username#1234");
|
||||
m_entry.signal_key_press_event().connect(sigc::mem_fun(*this, &FriendsListAddComponent::OnKeyPress), false);
|
||||
|
||||
add(m_label);
|
||||
add(m_box);
|
||||
@@ -177,6 +182,41 @@ FriendsListAddComponent::FriendsListAddComponent()
|
||||
show_all_children();
|
||||
}
|
||||
|
||||
void FriendsListAddComponent::Submit() {
|
||||
if (m_requesting) return;
|
||||
|
||||
auto text = m_entry.get_text();
|
||||
m_label.set_text("Invalid input"); // cheeky !!
|
||||
m_entry.set_text("");
|
||||
const auto hashpos = text.find("#");
|
||||
if (hashpos == Glib::ustring::npos) return;
|
||||
const auto username = text.substr(0, hashpos);
|
||||
const auto discriminator = text.substr(hashpos + 1);
|
||||
if (username.size() == 0 || discriminator.size() != 4) return;
|
||||
if (discriminator.find_first_not_of("0123456789") != Glib::ustring::npos) return;
|
||||
|
||||
m_requesting = true;
|
||||
m_label.set_text("Hang on...");
|
||||
|
||||
const auto cb = [this](bool success, DiscordError code) {
|
||||
m_requesting = false;
|
||||
if (success) {
|
||||
m_label.set_text("Success!");
|
||||
} else {
|
||||
m_label.set_text("Failed: "s + GetDiscordErrorDisplayString(code));
|
||||
}
|
||||
};
|
||||
Abaddon::Get().GetDiscordClient().SendFriendRequest(username, std::stoul(discriminator), sigc::track_obj(cb, *this));
|
||||
}
|
||||
|
||||
bool FriendsListAddComponent::OnKeyPress(GdkEventKey *e) {
|
||||
if (e->keyval == GDK_KEY_Return) {
|
||||
Submit();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
FriendsListFriendRow::FriendsListFriendRow(RelationshipType type, const UserData &data)
|
||||
: Name(data.Username + "#" + data.Discriminator)
|
||||
, Type(type)
|
||||
|
||||
@@ -7,11 +7,16 @@ public:
|
||||
FriendsListAddComponent();
|
||||
|
||||
private:
|
||||
void Submit();
|
||||
bool OnKeyPress(GdkEventKey *e);
|
||||
|
||||
Gtk::Label m_label;
|
||||
Gtk::Label m_status;
|
||||
Gtk::Entry m_entry;
|
||||
Gtk::Button m_add;
|
||||
Gtk::Box m_box;
|
||||
|
||||
bool m_requesting = false;
|
||||
};
|
||||
|
||||
class FriendsListFriendRow;
|
||||
|
||||
@@ -697,6 +697,25 @@ void DiscordClient::RemoveRelationship(Snowflake id, sigc::slot<void(bool succes
|
||||
});
|
||||
}
|
||||
|
||||
void DiscordClient::SendFriendRequest(const Glib::ustring &username, int discriminator, sigc::slot<void(bool success, DiscordError code)> callback) {
|
||||
FriendRequestObject obj;
|
||||
obj.Username = username;
|
||||
obj.Discriminator = discriminator;
|
||||
m_http.MakePOST("/users/@me/relationships", nlohmann::json(obj).dump(), [this, callback](const http::response_type &response) {
|
||||
if (CheckCode(response, 204)) {
|
||||
callback(true, DiscordError::NONE);
|
||||
} else {
|
||||
auto code = DiscordError::GENERIC;
|
||||
try {
|
||||
// pull me somewhere else?
|
||||
const auto data = nlohmann::json::parse(response.text);
|
||||
data.at("code").get_to(code);
|
||||
} catch (...) {}
|
||||
callback(false, code);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
bool DiscordClient::CanModifyRole(Snowflake guild_id, Snowflake role_id, Snowflake user_id) const {
|
||||
const auto guild = *GetGuild(guild_id);
|
||||
if (guild.OwnerID == user_id) return true;
|
||||
|
||||
@@ -139,6 +139,7 @@ public:
|
||||
void DeleteEmoji(Snowflake guild_id, Snowflake emoji_id, sigc::slot<void(bool success)> callback);
|
||||
std::optional<GuildApplicationData> GetGuildApplication(Snowflake guild_id) const;
|
||||
void RemoveRelationship(Snowflake id, sigc::slot<void(bool success)> callback);
|
||||
void SendFriendRequest(const Glib::ustring &username, int discriminator, sigc::slot<void(bool success, DiscordError code)> callback);
|
||||
|
||||
bool CanModifyRole(Snowflake guild_id, Snowflake role_id) const;
|
||||
bool CanModifyRole(Snowflake guild_id, Snowflake role_id, Snowflake user_id) const;
|
||||
|
||||
36
discord/errors.hpp
Normal file
36
discord/errors.hpp
Normal file
@@ -0,0 +1,36 @@
|
||||
#pragma once
|
||||
|
||||
enum class DiscordError {
|
||||
GENERIC = 0,
|
||||
INVALID_FORM_BODY = 50035,
|
||||
RELATIONSHIP_INCOMING_DISABLED = 80000,
|
||||
RELATIONSHIP_INCOMING_BLOCKED = 80001,
|
||||
RELATIONSHIP_INVALID_USER_BOT = 80002, // this is misspelled in discord's source lul
|
||||
RELATIONSHIP_INVALID_SELF = 80003,
|
||||
RELATIONSHIP_INVALID_DISCORD_TAG = 80004,
|
||||
RELATIONSHIP_ALREADY_FRIENDS = 80007,
|
||||
|
||||
NONE = -1,
|
||||
};
|
||||
|
||||
constexpr const char *GetDiscordErrorDisplayString(DiscordError error) {
|
||||
switch (error) {
|
||||
case DiscordError::INVALID_FORM_BODY:
|
||||
return "Something's wrong with your input";
|
||||
case DiscordError::RELATIONSHIP_INCOMING_DISABLED:
|
||||
return "This user isn't accepting friend requests";
|
||||
case DiscordError::RELATIONSHIP_INCOMING_BLOCKED:
|
||||
return "You are blocked by this user";
|
||||
case DiscordError::RELATIONSHIP_INVALID_USER_BOT:
|
||||
return "You can't send a request to a bot";
|
||||
case DiscordError::RELATIONSHIP_INVALID_SELF:
|
||||
return "You can't send a request to yourself";
|
||||
case DiscordError::RELATIONSHIP_INVALID_DISCORD_TAG:
|
||||
return "No users with that tag exist";
|
||||
case DiscordError::RELATIONSHIP_ALREADY_FRIENDS:
|
||||
return "You are already friends with that user";
|
||||
case DiscordError::GENERIC:
|
||||
default:
|
||||
return "An error occurred";
|
||||
}
|
||||
}
|
||||
@@ -463,3 +463,8 @@ void from_json(const nlohmann::json &j, RelationshipAddData &m) {
|
||||
JS_D("type", m.Type);
|
||||
JS_D("user", m.User);
|
||||
}
|
||||
|
||||
void to_json(nlohmann::json &j, const FriendRequestObject &m) {
|
||||
j["username"] = m.Username;
|
||||
j["discriminator"] = m.Discriminator;
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
#include "ban.hpp"
|
||||
#include "auditlog.hpp"
|
||||
#include "relationship.hpp"
|
||||
#include "errors.hpp"
|
||||
|
||||
// most stuff below should just be objects that get processed and thrown away immediately
|
||||
|
||||
@@ -645,3 +646,10 @@ struct RelationshipAddData {
|
||||
|
||||
friend void from_json(const nlohmann::json &j, RelationshipAddData &m);
|
||||
};
|
||||
|
||||
struct FriendRequestObject {
|
||||
std::string Username;
|
||||
int Discriminator;
|
||||
|
||||
friend void to_json(nlohmann::json &j, const FriendRequestObject &m);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user