Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions core/common/BaseClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,17 @@ BaseClient::kickGroupUsersAsync(NSessionPtr session, const std::string& groupId,
return promise->get_future();
}

std::future<void>
BaseClient::banGroupUsersAsync(NSessionPtr session, const std::string& groupId, const std::vector<std::string>& ids) {
auto promise = std::make_shared<std::promise<void>>();

banGroupUsers(
session, groupId, ids, [=]() { promise->set_value(); },
[=](const NError& error) { promise->set_exception(std::make_exception_ptr<NException>(error)); });

return promise->get_future();
}

std::future<void> BaseClient::joinGroupAsync(NSessionPtr session, const std::string& groupId) {
auto promise = std::make_shared<std::promise<void>>();

Expand Down
3 changes: 3 additions & 0 deletions core/common/BaseClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,9 @@ class BaseClient : public NClientInterface {
std::future<void>
kickGroupUsersAsync(NSessionPtr session, const std::string& groupId, const std::vector<std::string>& ids) override;

std::future<void>
banGroupUsersAsync(NSessionPtr session, const std::string& groupId, const std::vector<std::string>& ids) override;

std::future<void> joinGroupAsync(NSessionPtr session, const std::string& groupId) override;

std::future<void> leaveGroupAsync(NSessionPtr session, const std::string& groupId) override;
Expand Down
25 changes: 25 additions & 0 deletions core/core-rest/RestClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1588,6 +1588,31 @@ void RestClient::kickGroupUsers(
}
}

void RestClient::banGroupUsers(
NSessionPtr session,
const std::string& groupId,
const std::vector<std::string>& ids,
std::function<void()> successCallback,
ErrorCallback errorCallback) {
try {
RestReqContext* ctx = createReqContext(nullptr);
setSessionAuth(ctx, session);

ctx->successCallback = successCallback;
ctx->errorCallback = errorCallback;

NHttpQueryArgs args;

for (auto& id : ids) {
args.emplace("user_ids", id);
}

sendReq(ctx, NHttpReqMethod::POST, "/v2/group/" + encodeURIComponent(groupId) + "/ban", "", std::move(args));
} catch (exception& e) {
NLOG_ERROR("exception: " + string(e.what()));
}
}

void RestClient::joinGroup(
NSessionPtr session,
const std::string& groupId,
Expand Down
7 changes: 7 additions & 0 deletions core/core-rest/RestClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,13 @@ class RestClient : public BaseClient {
std::function<void()> successCallback,
ErrorCallback errorCallback) override;

void banGroupUsers(
NSessionPtr session,
const std::string& groupId,
const std::vector<std::string>& ids,
std::function<void()> successCallback,
ErrorCallback errorCallback) override;

void joinGroup(
NSessionPtr session,
const std::string& groupId,
Expand Down
21 changes: 21 additions & 0 deletions integrationtests/src/test_groups.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,26 @@ void test_addAndKickGroupUser() {
}
}

void test_addAndBanGroupUser() {
NTest test(__func__, true);
test.runTest();

try {
auto session1 = test.client->authenticateCustomAsync(TestGuid::newGuid(), "", true).get();
test.addSession(session1);
auto session2 = test.client->authenticateCustomAsync(TestGuid::newGuid(), "", true).get();
test.addSession(session2);
NGroup group =
test.client->createGroupAsync(session1, "BanTest-" + TestGuid::newGuid(), "", "", "", true).get();
test.client->addGroupUsersAsync(session1, group.id, {session2->getUserId()}).get();
test.client->banGroupUsersAsync(session1, group.id, {session2->getUserId()}).get();
test.stopTest(true);
} catch (const std::exception& e) {
NLOG_INFO("test failed: " + std::string(e.what()));
test.stopTest(false);
}
}

void test_createClosedGroup() {
NTest test(__func__, true);
test.runTest();
Expand Down Expand Up @@ -474,6 +494,7 @@ void test_groups() {
test_deleteGroup();
test_joinAndLeaveGroup();
test_addAndKickGroupUser();
test_addAndBanGroupUser();
test_createClosedGroup();
test_createGroup_withDescription();
test_updateGroup();
Expand Down
17 changes: 17 additions & 0 deletions interface/include/nakama-cpp/NClientInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,20 @@ class NAKAMA_API NClientInterface {
std::function<void()> successCallback = nullptr,
ErrorCallback errorCallback = nullptr) = 0;

/**
* Ban one or more users from the group.
*
* @param session The session of the user.
* @param groupId The id of the group.
* @param ids The ids of the users to ban.
*/
virtual void banGroupUsers(
NSessionPtr session,
const std::string& groupId,
const std::vector<std::string>& ids,
std::function<void()> successCallback = nullptr,
ErrorCallback errorCallback = nullptr) = 0;

/**
* Join a group if it has open membership or request to join it.
*
Expand Down Expand Up @@ -1616,6 +1630,9 @@ class NAKAMA_API NClientInterface {
virtual std::future<void>
kickGroupUsersAsync(NSessionPtr session, const std::string& groupId, const std::vector<std::string>& ids) = 0;

virtual std::future<void>
banGroupUsersAsync(NSessionPtr session, const std::string& groupId, const std::vector<std::string>& ids) = 0;

/**
* Join a group if it has open membership or request to join it.
*
Expand Down
Loading