Skip to content

Commit 907611b

Browse files
authored
add creation of restriction channel (#13)
1 parent 116e941 commit 907611b

File tree

4 files changed

+133
-5
lines changed

4 files changed

+133
-5
lines changed

CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ if(${COMPILE_EXAMPLES})
206206
example(thread_channel)
207207
example(message_reactions)
208208
example(delete_message)
209+
example(restrictions)
209210
endif()
210211

211212
if(${RUN_TESTS})

example/restrictions.cpp

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
#include "restrictions.hpp"
2+
#include <cstdlib>
3+
#include "channel.hpp"
4+
#include "pubnub_chat/chat.hpp"
5+
#include "string.hpp"
6+
7+
void print_restriction(const Pubnub::Restriction& restriction);
8+
void prepare_user(Pubnub::Chat& chat, const Pubnub::String& user_id);
9+
void prepare_channel(Pubnub::Chat& chat, const Pubnub::String& channel_id);
10+
11+
int main() {
12+
auto subscribe_key = std::getenv("PUBNUB_SUBSCRIBE_KEY");
13+
auto publish_key = std::getenv("PUBNUB_PUBLISH_KEY");
14+
auto user_id = std::getenv("USER_ID");
15+
16+
// Create a chat instance
17+
auto chat = Pubnub::Chat::init(publish_key, subscribe_key, user_id, Pubnub::ChatConfig{});
18+
19+
// Chat Object restrictions
20+
prepare_user(chat, "user1");
21+
prepare_channel(chat, "channel1");
22+
23+
auto chat_restricitons = Pubnub::Restriction{};
24+
chat_restricitons.mute = true;
25+
chat_restricitons.reason = "mute for some reason";
26+
27+
chat.set_restrictions("user1", "channel1", chat_restricitons);
28+
29+
std::cout << "|Chat Object restrictions:|" << std::endl;
30+
auto user1 = chat.get_user("user1");
31+
auto channel1 = chat.get_channel("channel1");
32+
33+
std::cout << "user1 restrictions for channel1: " << user1.user_id() << std::endl;
34+
print_restriction(user1.get_channel_restrictions(channel1));
35+
36+
std::cout << "channel1 restrictions for user1: " << channel1.channel_id() << std::endl;
37+
print_restriction(channel1.get_user_restrictions(user1));
38+
39+
// Channel Object restrictions
40+
prepare_user(chat, "user2");
41+
prepare_channel(chat, "channel2");
42+
43+
auto channel = chat.get_channel("channel2");
44+
45+
auto channel_restricitons = Pubnub::Restriction{};
46+
chat_restricitons.ban = true;
47+
chat_restricitons.reason = "ban for some reason";
48+
49+
channel.set_restrictions("user2", channel_restricitons);
50+
51+
std::cout << "|Channel Object restrictions:|" << std::endl;
52+
auto user2 = chat.get_user("user2");
53+
54+
std::cout << "user2 restrictions for channel2: " << std::endl;
55+
print_restriction(user2.get_channel_restrictions(channel));
56+
57+
std::cout << "channel2 restrictions for user2: " << std::endl;
58+
print_restriction(channel.get_user_restrictions(user2));
59+
60+
// User Object restrictions
61+
prepare_user(chat, "user3");
62+
prepare_channel(chat, "channel3");
63+
64+
auto user = chat.get_user("user3");
65+
66+
auto user_restricitons = Pubnub::Restriction{};
67+
user_restricitons.mute = true;
68+
user_restricitons.reason = "mute for some reason";
69+
70+
user.set_restrictions("channel3", user_restricitons);
71+
72+
std::cout << "|User Object restrictions:|" << std::endl;
73+
auto channel3 = chat.get_channel("channel3");
74+
75+
std::cout << "user3 restrictions for channel3: " << std::endl;
76+
print_restriction(user.get_channel_restrictions(channel3));
77+
78+
std::cout << "channel3 restrictions for user3: " << std::endl;
79+
print_restriction(channel3.get_user_restrictions(user));
80+
81+
return 0;
82+
}
83+
84+
void print_restriction(const Pubnub::Restriction& restriction) {
85+
std::cout << "Restrctions: {" << std::endl;
86+
std::cout << " ban: " << restriction.ban << std::endl;
87+
std::cout << " mute: " << restriction.mute << std::endl;
88+
std::cout << " reason: " << restriction.reason << std::endl;
89+
std::cout << "}" << std::endl;
90+
}
91+
92+
void prepare_user(Pubnub::Chat& chat, const Pubnub::String& user_id) {
93+
try {
94+
chat.create_user(user_id, Pubnub::ChatUserData{});
95+
} catch (const std::exception& e) {
96+
// User already exists
97+
}
98+
}
99+
100+
void prepare_channel(Pubnub::Chat& chat, const Pubnub::String& channel_id) {
101+
try {
102+
chat.create_public_conversation(channel_id, Pubnub::ChatChannelData{});
103+
} catch (const std::exception& e) {
104+
// Channel already exists
105+
}
106+
}
107+

src/application/restrictions_service.cpp

+24-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
#include "restrictions_service.hpp"
22
#include "application/chat_service.hpp"
3+
#include "channel.hpp"
4+
#include "domain/channel_entity.hpp"
35
#include "infra/pubnub.hpp"
46
#include "infra/entity_repository.hpp"
57
#include "const_values.hpp"
68
#include "message.hpp"
79
#include "chat_helpers.hpp"
810
#include "domain/json.hpp"
11+
#include "application/channel_service.hpp"
12+
#include "application/user_service.hpp"
13+
#include "domain/restrictions.hpp"
914

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

39+
try {
40+
if (auto chat_service_shared = chat_service.lock())
41+
{
42+
chat_service_shared->channel_service->create_channel(restrictions_channel, ChannelEntity{});
43+
} else {
44+
throw std::runtime_error("Chat service is not available");
45+
}
46+
}
47+
catch (const std::invalid_argument& e)
48+
{
49+
// TODO: better error handling
50+
if (String(e.what()).find("already exists") == String::npos) {
51+
throw e;
52+
}
53+
// else ignore - moderation channel already exist
54+
}
55+
3456
//Lift restrictions
3557
if(!restrictions.ban && !restrictions.mute)
3658
{
37-
String remove_member_string = String("[{\"uuid\": {\"id\": \"") + user_id + String("\"}}]");
3859
{
3960
auto pubnub_handle = this->pubnub->lock();
40-
pubnub_handle->remove_members(restrictions_channel, remove_member_string);
61+
pubnub_handle->remove_members(restrictions_channel, Restrictions::remove_member_payload(user_id));
4162
}
42-
String event_payload_string = String("{\"channelId\": \"") + restrictions_channel + String("\", \"restriction\": \"lifted\", \"reason\": \"") + restrictions.reason + String("\"}");
63+
String event_payload_string = Restrictions::lift_restrictions_payload(restrictions_channel, restrictions.reason);
4364
chat_service_shared->emit_chat_event(pubnub_chat_event_type::PCET_MODERATION, user_id, event_payload_string);
4465
return;
4566
}

src/domain/restrictions.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ Pubnub::String Restrictions::remove_member_payload(const Pubnub::String &user_id
66
}
77

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

1312
Pubnub::String Restrictions::restrict_member_payload(const Pubnub::String &user_id) {

0 commit comments

Comments
 (0)