|
| 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 | + |
0 commit comments