Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add creation of moderation channel for restrictions #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,9 @@ target_link_libraries(pubnub-chat-example PUBLIC pubnub-chat)
if(${COMPILE_EXAMPLES})
add_executable(pubnub-message-draft-example example/message_draft.cpp)
target_link_libraries(pubnub-message-draft-example PRIVATE pubnub-chat)
add_executable(pubnub-restrictions-example example/restrictions.cpp)
target_link_libraries(pubnub-restrictions-example PRIVATE pubnub-chat)

endif()

if(${RUN_TESTS})
Expand Down
107 changes: 107 additions & 0 deletions example/restrictions.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#include "restrictions.hpp"
#include <cstdlib>
#include "channel.hpp"
#include "pubnub_chat/chat.hpp"
#include "string.hpp"

void print_restriction(const Pubnub::Restriction& restriction);
void prepare_user(Pubnub::Chat& chat, const Pubnub::String& user_id);
void prepare_channel(Pubnub::Chat& chat, const Pubnub::String& channel_id);

int main() {
auto subscribe_key = std::getenv("PUBNUB_SUBSCRIBE_KEY");
auto publish_key = std::getenv("PUBNUB_PUBLISH_KEY");
auto user_id = std::getenv("USER_ID");

// Create a chat instance
auto chat = Pubnub::Chat::init(publish_key, subscribe_key, user_id, Pubnub::ChatConfig{});

// Chat Object restrictions
prepare_user(chat, "user1");
prepare_channel(chat, "channel1");

auto chat_restricitons = Pubnub::Restriction{};
chat_restricitons.mute = true;
chat_restricitons.reason = "mute for some reason";

chat.set_restrictions("user1", "channel1", chat_restricitons);

std::cout << "|Chat Object restrictions:|" << std::endl;
auto user1 = chat.get_user("user1");
auto channel1 = chat.get_channel("channel1");

std::cout << "user1 restrictions for channel1: " << user1.user_id() << std::endl;
print_restriction(user1.get_channel_restrictions(channel1));

std::cout << "channel1 restrictions for user1: " << channel1.channel_id() << std::endl;
print_restriction(channel1.get_user_restrictions(user1));

// Channel Object restrictions
prepare_user(chat, "user2");
prepare_channel(chat, "channel2");

auto channel = chat.get_channel("channel2");

auto channel_restricitons = Pubnub::Restriction{};
chat_restricitons.ban = true;
chat_restricitons.reason = "ban for some reason";

channel.set_restrictions("user2", channel_restricitons);

std::cout << "|Channel Object restrictions:|" << std::endl;
auto user2 = chat.get_user("user2");

std::cout << "user2 restrictions for channel2: " << std::endl;
print_restriction(user2.get_channel_restrictions(channel));

std::cout << "channel2 restrictions for user2: " << std::endl;
print_restriction(channel.get_user_restrictions(user2));

// User Object restrictions
prepare_user(chat, "user3");
prepare_channel(chat, "channel3");

auto user = chat.get_user("user3");

auto user_restricitons = Pubnub::Restriction{};
user_restricitons.mute = true;
user_restricitons.reason = "mute for some reason";

user.set_restrictions("channel3", user_restricitons);

std::cout << "|User Object restrictions:|" << std::endl;
auto channel3 = chat.get_channel("channel3");

std::cout << "user3 restrictions for channel3: " << std::endl;
print_restriction(user.get_channel_restrictions(channel3));

std::cout << "channel3 restrictions for user3: " << std::endl;
print_restriction(channel3.get_user_restrictions(user));

return 0;
}

void print_restriction(const Pubnub::Restriction& restriction) {
std::cout << "Restrctions: {" << std::endl;
std::cout << " ban: " << restriction.ban << std::endl;
std::cout << " mute: " << restriction.mute << std::endl;
std::cout << " reason: " << restriction.reason << std::endl;
std::cout << "}" << std::endl;
}

void prepare_user(Pubnub::Chat& chat, const Pubnub::String& user_id) {
try {
chat.create_user(user_id, Pubnub::ChatUserData{});
} catch (const std::exception& e) {
// User already exists
}
}

void prepare_channel(Pubnub::Chat& chat, const Pubnub::String& channel_id) {
try {
chat.create_public_conversation(channel_id, Pubnub::ChatChannelData{});
} catch (const std::exception& e) {
// Channel already exists
}
}

27 changes: 24 additions & 3 deletions src/application/restrictions_service.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
#include "restrictions_service.hpp"
#include "application/chat_service.hpp"
#include "channel.hpp"
#include "domain/channel_entity.hpp"
#include "infra/pubnub.hpp"
#include "infra/entity_repository.hpp"
#include "const_values.hpp"
#include "message.hpp"
#include "chat_helpers.hpp"
#include "domain/json.hpp"
#include "application/channel_service.hpp"
#include "application/user_service.hpp"
#include "domain/restrictions.hpp"

using namespace Pubnub;
using json = nlohmann::json;
Expand All @@ -31,15 +36,31 @@ void RestrictionsService::set_restrictions(const String& user_id, const String&
//Restrictions are held in new channel with ID: PUBNUB_INTERNAL_MODERATION_{ChannelName}
String restrictions_channel = INTERNAL_MODERATION_PREFIX + channel_id;

try {
if (auto chat_service_shared = chat_service.lock())
{
chat_service_shared->channel_service->create_channel(restrictions_channel, ChannelEntity{});
} else {
throw std::runtime_error("Chat service is not available");
}
}
catch (const std::invalid_argument& e)
{
// TODO: better error handling
if (String(e.what()).find("already exists") == String::npos) {
throw e;
}
// else ignore - moderation channel already exist
}

//Lift restrictions
if(!restrictions.ban && !restrictions.mute)
{
String remove_member_string = String("[{\"uuid\": {\"id\": \"") + user_id + String("\"}}]");
{
auto pubnub_handle = this->pubnub->lock();
pubnub_handle->remove_members(restrictions_channel, remove_member_string);
pubnub_handle->remove_members(restrictions_channel, Restrictions::remove_member_payload(user_id));
}
String event_payload_string = String("{\"channelId\": \"") + restrictions_channel + String("\", \"restriction\": \"lifted\", \"reason\": \"") + restrictions.reason + String("\"}");
String event_payload_string = Restrictions::lift_restrictions_payload(restrictions_channel, restrictions.reason);
chat_service_shared->emit_chat_event(pubnub_chat_event_type::PCET_MODERATION, user_id, event_payload_string);
return;
}
Expand Down
3 changes: 1 addition & 2 deletions src/domain/restrictions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ Pubnub::String Restrictions::remove_member_payload(const Pubnub::String &user_id
}

Pubnub::String Restrictions::lift_restrictions_payload(const Pubnub::String &channel_id, const Pubnub::String &reason) {
auto restrictions_channel = Pubnub::INTERNAL_MODERATION_PREFIX + channel_id;
return "{\"channelId\": \"" + restrictions_channel + "\", \"restrictions\": \"lifted\", \"reason\": \"" + reason + "\"}";
return "{\"channelId\": \"" + channel_id + "\", \"restrictions\": \"lifted\", \"reason\": \"" + reason + "\"}";
}

Pubnub::String Restrictions::restrict_member_payload(const Pubnub::String &user_id) {
Expand Down