Skip to content

Commit

Permalink
Support getting batches of chat events via LocalUserIndex (#4848)
Browse files Browse the repository at this point in the history
  • Loading branch information
hpeebles authored Nov 27, 2023
1 parent b95121b commit 29194e3
Show file tree
Hide file tree
Showing 39 changed files with 564 additions and 30 deletions.
4 changes: 4 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions backend/canisters/community/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## [unreleased]

### Added

- Support getting batches of chat events via LocalUserIndex ([#4848](https://github.com/open-chat-labs/open-chat/pull/4848))

### Changed

- Make events private for payment gated chats ([#4843](https://github.com/open-chat-labs/open-chat/pull/4843))
Expand Down
4 changes: 4 additions & 0 deletions backend/canisters/community/api/src/queries/c2c_events.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
use types::RelayedArgs;

pub type Args = RelayedArgs<crate::events::Args>;
pub type Response = crate::EventsResponse;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
use types::RelayedArgs;

pub type Args = RelayedArgs<crate::events_by_index::Args>;
pub type Response = crate::EventsResponse;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
use types::RelayedArgs;

pub type Args = RelayedArgs<crate::events_window::Args>;
pub type Response = crate::EventsResponse;
3 changes: 3 additions & 0 deletions backend/canisters/community/api/src/queries/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
pub mod c2c_events;
pub mod c2c_events_by_index;
pub mod c2c_events_window;
pub mod c2c_summary;
pub mod channel_summary;
pub mod channel_summary_updates;
Expand Down
1 change: 1 addition & 0 deletions backend/canisters/community/c2c_client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
candid = { workspace = true }
canister_client = { path = "../../../libraries/canister_client" }
community_canister = { path = "../api" }
ic-cdk = { workspace = true }
Expand Down
3 changes: 3 additions & 0 deletions backend/canisters/community/c2c_client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ use canister_client::generate_c2c_call;
use community_canister::*;

// Queries
generate_c2c_call!(c2c_events);
generate_c2c_call!(c2c_events_by_index);
generate_c2c_call!(c2c_events_window);

// Updates
generate_c2c_call!(c2c_create_proposals_channel);
Expand Down
15 changes: 12 additions & 3 deletions backend/canisters/community/impl/src/queries/events.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
use crate::guards::caller_is_local_user_index;
use crate::queries::check_replica_up_to_date;
use crate::{read_state, RuntimeState};
use candid::Principal;
use canister_api_macros::query_msgpack;
use community_canister::c2c_events::Args as C2CArgs;
use community_canister::events::{Response::*, *};
use group_chat_core::EventsResult;
use ic_cdk_macros::query;

#[query]
fn events(args: Args) -> Response {
read_state(|state| events_impl(args, state))
read_state(|state| events_impl(args, None, state))
}

fn events_impl(args: Args, state: &RuntimeState) -> Response {
#[query_msgpack(guard = "caller_is_local_user_index")]
fn c2c_events(args: C2CArgs) -> Response {
read_state(|state| events_impl(args.args, Some(args.caller), state))
}

fn events_impl(args: Args, on_behalf_of: Option<Principal>, state: &RuntimeState) -> Response {
if let Err(now) = check_replica_up_to_date(args.latest_known_update, state) {
return ReplicaNotUpToDateV2(now);
}

let caller = state.env.caller();
let caller = on_behalf_of.unwrap_or_else(|| state.env.caller());
let user_id = state.data.members.get(caller).map(|m| m.user_id);

if user_id.is_none() && (!state.data.is_public || state.data.has_payment_gate()) {
Expand Down
15 changes: 12 additions & 3 deletions backend/canisters/community/impl/src/queries/events_by_index.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
use crate::guards::caller_is_local_user_index;
use crate::queries::check_replica_up_to_date;
use crate::{read_state, RuntimeState};
use candid::Principal;
use canister_api_macros::query_msgpack;
use community_canister::c2c_events_by_index::Args as C2CArgs;
use community_canister::events_by_index::{Response::*, *};
use group_chat_core::EventsResult;
use ic_cdk_macros::query;

#[query]
fn events_by_index(args: Args) -> Response {
read_state(|state| events_by_index_impl(args, state))
read_state(|state| events_by_index_impl(args, None, state))
}

fn events_by_index_impl(args: Args, state: &RuntimeState) -> Response {
#[query_msgpack(guard = "caller_is_local_user_index")]
fn c2c_events_by_index(args: C2CArgs) -> Response {
read_state(|state| events_by_index_impl(args.args, Some(args.caller), state))
}

fn events_by_index_impl(args: Args, on_behalf_of: Option<Principal>, state: &RuntimeState) -> Response {
if let Err(now) = check_replica_up_to_date(args.latest_known_update, state) {
return ReplicaNotUpToDateV2(now);
}

let caller = state.env.caller();
let caller = on_behalf_of.unwrap_or_else(|| state.env.caller());
let user_id = state.data.members.get(caller).map(|m| m.user_id);

if user_id.is_none() && (!state.data.is_public || state.data.has_payment_gate()) {
Expand Down
15 changes: 12 additions & 3 deletions backend/canisters/community/impl/src/queries/events_window.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
use crate::guards::caller_is_local_user_index;
use crate::queries::check_replica_up_to_date;
use crate::{read_state, RuntimeState};
use candid::Principal;
use canister_api_macros::query_msgpack;
use community_canister::c2c_events_window::Args as C2CArgs;
use community_canister::events_window::{Response::*, *};
use group_chat_core::EventsResult;
use ic_cdk_macros::query;

#[query]
fn events_window(args: Args) -> Response {
read_state(|state| events_window_impl(args, state))
read_state(|state| events_window_impl(args, None, state))
}

fn events_window_impl(args: Args, state: &RuntimeState) -> Response {
#[query_msgpack(guard = "caller_is_local_user_index")]
fn c2c_events_window(args: C2CArgs) -> Response {
read_state(|state| events_window_impl(args.args, Some(args.caller), state))
}

fn events_window_impl(args: Args, on_behalf_of: Option<Principal>, state: &RuntimeState) -> Response {
if let Err(now) = check_replica_up_to_date(args.latest_known_update, state) {
return ReplicaNotUpToDateV2(now);
}

let caller = state.env.caller();
let caller = on_behalf_of.unwrap_or_else(|| state.env.caller());
let user_id = state.data.members.get(caller).map(|m| m.user_id);

if user_id.is_none() && (!state.data.is_public || state.data.has_payment_gate()) {
Expand Down
4 changes: 4 additions & 0 deletions backend/canisters/group/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## [unreleased]

### Added

- Support getting batches of chat events via LocalUserIndex ([#4848](https://github.com/open-chat-labs/open-chat/pull/4848))

### Changed

- Make events private for payment gated chats ([#4843](https://github.com/open-chat-labs/open-chat/pull/4843))
Expand Down
4 changes: 4 additions & 0 deletions backend/canisters/group/api/src/queries/c2c_events.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
use types::RelayedArgs;

pub type Args = RelayedArgs<crate::events::Args>;
pub type Response = crate::EventsResponse;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
use types::RelayedArgs;

pub type Args = RelayedArgs<crate::events_by_index::Args>;
pub type Response = crate::EventsResponse;
4 changes: 4 additions & 0 deletions backend/canisters/group/api/src/queries/c2c_events_window.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
use types::RelayedArgs;

pub type Args = RelayedArgs<crate::events_window::Args>;
pub type Response = crate::EventsResponse;
3 changes: 3 additions & 0 deletions backend/canisters/group/api/src/queries/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
pub mod c2c_events;
pub mod c2c_events_by_index;
pub mod c2c_events_internal;
pub mod c2c_events_window;
pub mod c2c_name_and_members;
pub mod c2c_summary;
pub mod c2c_summary_updates;
Expand Down
3 changes: 3 additions & 0 deletions backend/canisters/group/c2c_client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ use canister_client::{generate_c2c_call, generate_candid_c2c_call};
use group_canister::*;

// Queries
generate_c2c_call!(c2c_events);
generate_c2c_call!(c2c_events_by_index);
generate_c2c_call!(c2c_events_internal);
generate_c2c_call!(c2c_events_window);
generate_c2c_call!(c2c_name_and_members);
generate_c2c_call!(c2c_summary);
generate_c2c_call!(c2c_summary_updates);
Expand Down
8 changes: 8 additions & 0 deletions backend/canisters/group/impl/src/guards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ pub fn caller_is_group_index_or_local_group_index() -> Result<(), String> {
}
}

pub fn caller_is_local_user_index() -> Result<(), String> {
if read_state(|state| state.is_caller_local_user_index()) {
Ok(())
} else {
Err("Caller is not the local_user_index".to_string())
}
}

pub fn caller_is_local_group_index() -> Result<(), String> {
if read_state(|state| state.is_caller_local_group_index()) {
Ok(())
Expand Down
15 changes: 12 additions & 3 deletions backend/canisters/group/impl/src/queries/events.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
use crate::guards::caller_is_local_user_index;
use crate::queries::check_replica_up_to_date;
use crate::{read_state, RuntimeState};
use candid::Principal;
use canister_api_macros::query_msgpack;
use group_canister::c2c_events::Args as C2CArgs;
use group_canister::events::{Response::*, *};
use group_chat_core::EventsResult;
use ic_cdk_macros::query;

#[query]
fn events(args: Args) -> Response {
read_state(|state| events_impl(args, state))
read_state(|state| events_impl(args, None, state))
}

fn events_impl(args: Args, state: &RuntimeState) -> Response {
#[query_msgpack(guard = "caller_is_local_user_index")]
fn c2c_events(args: C2CArgs) -> Response {
read_state(|state| events_impl(args.args, Some(args.caller), state))
}

fn events_impl(args: Args, on_behalf_of: Option<Principal>, state: &RuntimeState) -> Response {
if let Err(now) = check_replica_up_to_date(args.latest_known_update, state) {
return ReplicaNotUpToDateV2(now);
}

let caller = state.env.caller();
let caller = on_behalf_of.unwrap_or_else(|| state.env.caller());
let user_id = state.data.lookup_user_id(caller);

match state.data.chat.events(
Expand Down
15 changes: 12 additions & 3 deletions backend/canisters/group/impl/src/queries/events_by_index.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
use crate::guards::caller_is_local_user_index;
use crate::queries::check_replica_up_to_date;
use crate::{read_state, RuntimeState};
use candid::Principal;
use canister_api_macros::query_msgpack;
use group_canister::c2c_events_by_index::Args as C2CArgs;
use group_canister::events_by_index::{Response::*, *};
use group_chat_core::EventsResult;
use ic_cdk_macros::query;

#[query]
fn events_by_index(args: Args) -> Response {
read_state(|state| events_by_index_impl(args, state))
read_state(|state| events_by_index_impl(args, None, state))
}

fn events_by_index_impl(args: Args, state: &RuntimeState) -> Response {
#[query_msgpack(guard = "caller_is_local_user_index")]
fn c2c_events_by_index(args: C2CArgs) -> Response {
read_state(|state| events_by_index_impl(args.args, Some(args.caller), state))
}

fn events_by_index_impl(args: Args, on_behalf_of: Option<Principal>, state: &RuntimeState) -> Response {
if let Err(now) = check_replica_up_to_date(args.latest_known_update, state) {
return ReplicaNotUpToDateV2(now);
}

let caller = state.env.caller();
let caller = on_behalf_of.unwrap_or_else(|| state.env.caller());
let user_id = state.data.lookup_user_id(caller);

match state
Expand Down
15 changes: 12 additions & 3 deletions backend/canisters/group/impl/src/queries/events_window.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
use crate::guards::caller_is_local_user_index;
use crate::queries::check_replica_up_to_date;
use crate::{read_state, RuntimeState};
use candid::Principal;
use canister_api_macros::query_msgpack;
use group_canister::c2c_events_window::Args as C2CArgs;
use group_canister::events_window::{Response::*, *};
use group_chat_core::EventsResult;
use ic_cdk_macros::query;

#[query]
fn events_window(args: Args) -> Response {
read_state(|state| events_window_impl(args, state))
read_state(|state| events_window_impl(args, None, state))
}

fn events_window_impl(args: Args, state: &RuntimeState) -> Response {
#[query_msgpack(guard = "caller_is_local_user_index")]
fn c2c_events_window(args: C2CArgs) -> Response {
read_state(|state| events_window_impl(args.args, Some(args.caller), state))
}

fn events_window_impl(args: Args, on_behalf_of: Option<Principal>, state: &RuntimeState) -> Response {
if let Err(now) = check_replica_up_to_date(args.latest_known_update, state) {
return ReplicaNotUpToDateV2(now);
}

let caller = state.env.caller();
let caller = on_behalf_of.unwrap_or_else(|| state.env.caller());
let user_id = state.data.lookup_user_id(caller);

match state.data.chat.events_window(
Expand Down
4 changes: 4 additions & 0 deletions backend/canisters/local_user_index/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## [unreleased]

### Added

- Support getting batches of chat events via LocalUserIndex ([#4848](https://github.com/open-chat-labs/open-chat/pull/4848))

### Changed

- Add `local_user_index_canister_id` to group/community summaries ([#4857](https://github.com/open-chat-labs/open-chat/pull/4857))
Expand Down
7 changes: 5 additions & 2 deletions backend/canisters/local_user_index/api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ edition = "2021"
[dependencies]
candid = { workspace = true }
candid_gen = { path = "../../../libraries/candid_gen" }
community_canister = { path = "../../community/api" }
group_canister = { path = "../../group/api" }
ic-ledger-types = { workspace = true }
serde = { workspace = true }
serde_bytes = "0.11"
types = { path = "../../../libraries/types" }
serde_bytes = { workspace = true }
types = { path = "../../../libraries/types" }
user_canister = { path = "../../user/api" }
Loading

0 comments on commit 29194e3

Please sign in to comment.