Improve add friend logging/dialogs
This commit is contained in:
@@ -1467,7 +1467,7 @@ namespace rpcn
|
|||||||
return error;
|
return error;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool rpcn_client::add_friend(const std::string& friend_username)
|
std::optional<ErrorType> rpcn_client::add_friend(const std::string& friend_username)
|
||||||
{
|
{
|
||||||
std::vector<u8> data;
|
std::vector<u8> data;
|
||||||
std::copy(friend_username.begin(), friend_username.end(), std::back_inserter(data));
|
std::copy(friend_username.begin(), friend_username.end(), std::back_inserter(data));
|
||||||
@@ -1478,19 +1478,18 @@ namespace rpcn
|
|||||||
std::vector<u8> packet_data;
|
std::vector<u8> packet_data;
|
||||||
if (!forge_send_reply(CommandType::AddFriend, req_id, data, packet_data))
|
if (!forge_send_reply(CommandType::AddFriend, req_id, data, packet_data))
|
||||||
{
|
{
|
||||||
return false;
|
return std::nullopt;
|
||||||
}
|
}
|
||||||
|
|
||||||
vec_stream reply(packet_data);
|
vec_stream reply(packet_data);
|
||||||
auto error = static_cast<ErrorType>(reply.get<u8>());
|
const auto error = static_cast<ErrorType>(reply.get<u8>());
|
||||||
|
|
||||||
if (error != rpcn::ErrorType::NoError)
|
if (error == ErrorType::NoError)
|
||||||
{
|
rpcn_log.success("add_friend(\"%s\") succeeded", friend_username);
|
||||||
return false;
|
else
|
||||||
}
|
rpcn_log.error("add_friend(\"%s\") failed with error: %s", error);
|
||||||
|
|
||||||
rpcn_log.success("You have successfully added \"%s\" as a friend", friend_username);
|
return error;
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool rpcn_client::remove_friend(const std::string& friend_username)
|
bool rpcn_client::remove_friend(const std::string& friend_username)
|
||||||
|
|||||||
@@ -293,7 +293,7 @@ namespace rpcn
|
|||||||
ErrorType send_reset_token(std::string_view npid, std::string_view email);
|
ErrorType send_reset_token(std::string_view npid, std::string_view email);
|
||||||
ErrorType reset_password(std::string_view npid, std::string_view token, std::string_view password);
|
ErrorType reset_password(std::string_view npid, std::string_view token, std::string_view password);
|
||||||
ErrorType delete_account();
|
ErrorType delete_account();
|
||||||
bool add_friend(const std::string& friend_username);
|
std::optional<ErrorType> add_friend(const std::string& friend_username);
|
||||||
bool remove_friend(const std::string& friend_username);
|
bool remove_friend(const std::string& friend_username);
|
||||||
|
|
||||||
u32 get_num_friends();
|
u32 get_num_friends();
|
||||||
|
|||||||
@@ -1262,13 +1262,10 @@ rpcn_friends_dialog::rpcn_friends_dialog(QWidget* parent)
|
|||||||
|
|
||||||
connect(accept_request_action, &QAction::triggered, this, [this, str_sel_friend]()
|
connect(accept_request_action, &QAction::triggered, this, [this, str_sel_friend]()
|
||||||
{
|
{
|
||||||
if (!m_rpcn->add_friend(str_sel_friend))
|
if (add_friend_with_error_dialog(str_sel_friend))
|
||||||
{
|
|
||||||
QMessageBox::critical(this, tr("Error adding a friend!"), tr("An error occurred while trying to add a friend!"), QMessageBox::Ok);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
QMessageBox::information(this, tr("Friend added!"), tr("You've successfully added a friend!"), QMessageBox::Ok);
|
QMessageBox::information(this, tr("Friend added!"), tr("You've successfully added a friend!"), QMessageBox::Ok);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -1304,11 +1301,8 @@ rpcn_friends_dialog::rpcn_friends_dialog(QWidget* parent)
|
|||||||
|
|
||||||
connect(send_friend_request_action, &QAction::triggered, this, [this, str_sel_friend]()
|
connect(send_friend_request_action, &QAction::triggered, this, [this, str_sel_friend]()
|
||||||
{
|
{
|
||||||
if (!m_rpcn->add_friend(str_sel_friend))
|
if (!add_friend_with_error_dialog(str_sel_friend))
|
||||||
{
|
|
||||||
QMessageBox::critical(this, tr("Error sending a friend request!"), tr("An error occurred while trying to send a friend request!"), QMessageBox::Ok);
|
|
||||||
return;
|
return;
|
||||||
}
|
|
||||||
|
|
||||||
QString qstr_friend = QString::fromStdString(str_sel_friend);
|
QString qstr_friend = QString::fromStdString(str_sel_friend);
|
||||||
add_update_list(m_lst_requests, qstr_friend, m_icon_request_sent, QVariant(false));
|
add_update_list(m_lst_requests, qstr_friend, m_icon_request_sent, QVariant(false));
|
||||||
@@ -1341,11 +1335,7 @@ rpcn_friends_dialog::rpcn_friends_dialog(QWidget* parent)
|
|||||||
QMessageBox::critical(this, tr("Error validating username!"), tr("The username you entered is invalid!"), QMessageBox::Ok);
|
QMessageBox::critical(this, tr("Error validating username!"), tr("The username you entered is invalid!"), QMessageBox::Ok);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!m_rpcn->add_friend(str_friend_username))
|
if (add_friend_with_error_dialog(str_friend_username))
|
||||||
{
|
|
||||||
QMessageBox::critical(this, tr("Error adding friend!"), tr("An error occurred while adding a friend!"), QMessageBox::Ok);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
add_update_list(m_lst_requests, QString::fromStdString(str_friend_username), m_icon_request_sent, QVariant(false));
|
add_update_list(m_lst_requests, QString::fromStdString(str_friend_username), m_icon_request_sent, QVariant(false));
|
||||||
QMessageBox::information(this, tr("Friend added!"), tr("Friend was successfully added!"), QMessageBox::Ok);
|
QMessageBox::information(this, tr("Friend added!"), tr("Friend was successfully added!"), QMessageBox::Ok);
|
||||||
@@ -1360,6 +1350,42 @@ rpcn_friends_dialog::~rpcn_friends_dialog()
|
|||||||
m_rpcn->remove_friend_cb(friend_callback, this);
|
m_rpcn->remove_friend_cb(friend_callback, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool rpcn_friends_dialog::add_friend_with_error_dialog(const std::string& friend_username)
|
||||||
|
{
|
||||||
|
QString err_msg;
|
||||||
|
const auto opt_error = m_rpcn->add_friend(friend_username);
|
||||||
|
|
||||||
|
if (opt_error.has_value())
|
||||||
|
{
|
||||||
|
const auto error = opt_error.value();
|
||||||
|
|
||||||
|
if (error != rpcn::ErrorType::NoError)
|
||||||
|
{
|
||||||
|
switch (error)
|
||||||
|
{
|
||||||
|
case rpcn::ErrorType::NotFound: err_msg = tr("The specified username does not exist."); break;
|
||||||
|
case rpcn::ErrorType::InvalidInput: err_msg = tr("You cannot add yourself as a friend."); break;
|
||||||
|
case rpcn::ErrorType::Blocked: err_msg = tr("You or the other user have the other blocked."); break;
|
||||||
|
case rpcn::ErrorType::AlreadyFriend: err_msg = tr("You are already friends with this user."); break;
|
||||||
|
case rpcn::ErrorType::DbFail: err_msg = tr("A database error occurred. Please try again later."); break;
|
||||||
|
default: err_msg = tr("An unexpected error occurred."); break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
err_msg = tr("Failed to send the friend request.");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!err_msg.isEmpty())
|
||||||
|
{
|
||||||
|
QMessageBox::critical(this, tr("Friend Request Failed"), err_msg, QMessageBox::Ok);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
bool rpcn_friends_dialog::is_ok() const
|
bool rpcn_friends_dialog::is_ok() const
|
||||||
{
|
{
|
||||||
return m_rpcn_ok;
|
return m_rpcn_ok;
|
||||||
|
|||||||
@@ -121,6 +121,7 @@ public:
|
|||||||
private:
|
private:
|
||||||
void add_update_list(QListWidget* list, const QString& name, const QIcon& icon, const QVariant& data);
|
void add_update_list(QListWidget* list, const QString& name, const QIcon& icon, const QVariant& data);
|
||||||
void remove_list(QListWidget* list, const QString& name);
|
void remove_list(QListWidget* list, const QString& name);
|
||||||
|
bool add_friend_with_error_dialog(const std::string& friend_username);
|
||||||
|
|
||||||
private Q_SLOTS:
|
private Q_SLOTS:
|
||||||
void add_update_friend(const QString& name, bool status);
|
void add_update_friend(const QString& name, bool status);
|
||||||
|
|||||||
Reference in New Issue
Block a user