Skip to content

Commit

Permalink
fixed whole example
Browse files Browse the repository at this point in the history
  • Loading branch information
Xavrax committed Jul 29, 2024
1 parent dd87160 commit 11acdfe
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 63 deletions.
8 changes: 8 additions & 0 deletions core/pubnub_objects_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,10 @@ enum pubnub_res pubnub_get_memberships_ex(pubnub_t* pb,
{
enum pubnub_res rslt;

if (NULL == opts.uuid) {
opts.uuid = pb->core.user_id;
}

PUBNUB_ASSERT(pb_valid_ctx_ptr(pb));

pubnub_mutex_lock(pb->monitor);
Expand Down Expand Up @@ -602,6 +606,10 @@ enum pubnub_res pubnub_remove_memberships_ex(pubnub_t* pb,
{
enum pubnub_res rslt;
char obj_buffer[PUBNUB_BUF_MAXLEN];

if (NULL == opts.uuid) {
opts.uuid = pb->core.user_id;
}

PUBNUB_ASSERT(pb_valid_ctx_ptr(pb));

Expand Down
31 changes: 14 additions & 17 deletions core/pubnub_objects_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -536,13 +536,13 @@ PUBNUB_EXTERN enum pubnub_res pubnub_remove_memberships(pubnub_t* pb,
to perform those operations on one, or more memberships.
An example for @channels:
[
{
"id": "main-channel-id"
},
{
"id": "channel-0"
}
]
{
"channel":{ "id": "main-channel-id" }
},
{
"channel":{ "id": "channel-0" }
}
]
Use `pubnub_memberships_defopts()` to get the default options.
Expand Down Expand Up @@ -733,16 +733,13 @@ PUBNUB_EXTERN enum pubnub_res pubnub_set_members_ex(pubnub_t* pb,
property to perform the operation on one or more members.
An example for @update_obj:
[
{
"id": "some-user-id",
"custom": {
"starred": true
}
},
{
"id": "user-0-id"
}
]
{
"uuid":{ "id": "main-user-id" }
},
{
"uuid":{ "id": "user-0" }
}
]
@param pb The pubnub context. Can't be NULL
@param channel_metadataid The Channel ID.
Expand Down
63 changes: 17 additions & 46 deletions core/samples/pubnub_objects_api_sample.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,8 @@ static void sync_sample_free(pubnub_t* p);

int main(void) {
// No demo keysets as app context is not applicable to it
// char* publish_key = getenv("PUBNUB_PUBLISH_KEY");
// char* subscribe_key = getenv("PUBNUB_SUBSCRIBE_KEY");
char* publish_key = "pub-c-2451c2cf-9f04-446b-8f85-e06564095e55";
char* subscribe_key = "sub-c-d16ff59f-b415-4ef9-8c29-ddda64fa2b43";
char* publish_key = getenv("PUBNUB_PUBLISH_KEY");
char* subscribe_key = getenv("PUBNUB_SUBSCRIBE_KEY");

enum pubnub_res result;

Expand Down Expand Up @@ -288,43 +286,15 @@ int main(void) {
pubnub_res_2_string(result));
}

sleep(1);
printf("Get users' memberships\n");

struct pubnub_membership_opts getall_memberships_opts = pubnub_memberships_defopts();
getall_memberships_opts.limit = 1;
getall_memberships_opts.include = "custom";
getall_memberships_opts.filter = "custom.key=='value'";
getall_memberships_opts.count = pbccTrue;
getall_memberships_opts.sort = "channel.name:desc";

result = pubnub_set_memberships_ex(pb, "", getall_memberships_opts);

if (PNR_STARTED == result) {
result = pubnub_await(pb);
}

if (PNR_OK == result) {
printf("Get memberships successful!\n");

for (const char* response = pubnub_get(pb); response != NULL; response = pubnub_get(pb)) {
printf("Response: %s\n", response);
}
} else {
printf("Get memberships failed with code: %d('%s')\n",
result,
pubnub_res_2_string(result));
}

sleep(1);
printf("Get users' memberships by UUID\n");

struct pubnub_membership_opts get_memberships_opts = pubnub_memberships_defopts();
get_memberships_opts.limit = 1;
get_memberships_opts.include = "custom";
get_memberships_opts.filter = "custom.key=='value'";
get_memberships_opts.filter = "custom.starred==true";
get_memberships_opts.count = pbccTrue;
get_memberships_opts.sort = "channel.name:desc";
get_memberships_opts.sort = "channel.id:desc";

result = pubnub_get_memberships_ex(pb, get_memberships_opts);

Expand All @@ -349,19 +319,20 @@ int main(void) {
struct pubnub_membership_opts remove_memberships_opts = pubnub_memberships_defopts();
remove_memberships_opts.include = "custom";
remove_memberships_opts.limit = 1;
remove_memberships_opts.sort = "channel.name:desc";
remove_memberships_opts.filter = "custom.key=='value'";
remove_memberships_opts.sort = "channel.id:desc";
remove_memberships_opts.filter = "custom.starred==true";
remove_memberships_opts.count = pbccTrue;

const char* channels_meta_remove = "["
"{"
"\"id\": \"main-channel-id\""
"\"channel\":{ \"id\": \"main-channel-id\" }"
"},"
"{"
"\"id\": \"channel-0\""
"\"channel\":{ \"id\": \"channel-0\" }"
"}"
"]";


result = pubnub_remove_memberships_ex(pb, channels_meta_remove, remove_memberships_opts);

if (PNR_STARTED == result) {
Expand All @@ -383,8 +354,8 @@ int main(void) {
struct pubnub_members_opts set_members_opts = pubnub_members_defopts();
set_members_opts.include = "custom";
set_members_opts.limit = 1;
set_members_opts.sort = "uuid.name:desc";
set_members_opts.filter = "custom.key=='value'";
set_members_opts.sort = "uuid.id:desc";
set_members_opts.filter = "custom.starred==true";
set_members_opts.count = pbccTrue;

const char* members_meta = "["
Expand Down Expand Up @@ -422,9 +393,9 @@ int main(void) {
struct pubnub_members_opts get_members_opts = pubnub_members_defopts();
get_members_opts.limit = 1;
get_members_opts.include = "custom";
get_members_opts.filter = "custom.key=='value'";
get_members_opts.filter = "custom.starred==true";
get_members_opts.count = pbccTrue;
get_members_opts.sort = "uuid.name:desc";
get_members_opts.sort = "uuid.id:desc";

result = pubnub_get_members_ex(pb, "channel_id", get_members_opts);

Expand All @@ -450,16 +421,16 @@ int main(void) {
struct pubnub_members_opts remove_members_opts = pubnub_members_defopts();
remove_members_opts.include = "custom";
remove_members_opts.limit = 1;
remove_members_opts.sort = "uuid.name:desc";
remove_members_opts.filter = "custom.key=='value'";
remove_members_opts.sort = "uuid.id:desc";
remove_members_opts.filter = "custom.starred==true";
remove_members_opts.count = pbccTrue;

const char* members_meta_remove = "["
"{"
"\"id\": \"main-user-id\""
"\"uuid\":{ \"id\": \"main-user-id\" }"
"},"
"{"
"\"id\": \"user-0\""
"\"uuid\":{ \"id\": \"user-0\" }"
"}"
"]";

Expand Down

0 comments on commit 11acdfe

Please sign in to comment.