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

Implement bot_updates #6934

Merged
merged 4 commits into from
Nov 29, 2024
Merged
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
1 change: 1 addition & 0 deletions backend/canisters/user_index/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

- Implement `register_bot` including bot schema ([#6928](https://github.com/open-chat-labs/open-chat/pull/6928))
- Implement `explore_bots` ([#6932](https://github.com/open-chat-labs/open-chat/pull/6932))
- Implement `bot_updates` ([#6934](https://github.com/open-chat-labs/open-chat/pull/6934))

## [[2.0.1469](https://github.com/open-chat-labs/open-chat/releases/tag/v2.0.1469-user_index)] - 2024-11-25

Expand Down
26 changes: 24 additions & 2 deletions backend/canisters/user_index/api/can.did
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,28 @@ type PublicKeyResponse = variant {
NotInitialised;
};

type BotUpdatesArgs = record {
updated_since : TimestampMillis;
};

type BotUpdatesResponse = variant {
Success : record {
added_or_updated: vec record {
id: UserId;
owner: UserId;
name: text;
avatar_id: opt nat;
endpoint: text;
description: text;
commands: vec SlashCommandSchema;
last_updated : TimestampMillis;
};
deleted: vec UserId;
timestamp: TimestampMillis;
};
SuccessNoUpdates;
};

type CheckUsernameArgs = record {
username : text;
is_bot : bool;
Expand Down Expand Up @@ -92,6 +114,7 @@ type ExploreBotsResponse = variant {
type BotMatch = record {
id : UserId;
score : nat32;
owner : UserId;
name : text;
description : text;
avatar_id : opt nat;
Expand Down Expand Up @@ -175,8 +198,6 @@ type ExternalAchievementsResponse = variant {
Success : record {
last_updated: TimestampMillis;
added_or_updated : vec ExternalAchievement;
achievements_added : vec ExternalAchievement;
achievements_removed : vec ExternalAchievement;
};
SuccessNoUpdates;
};
Expand Down Expand Up @@ -258,6 +279,7 @@ type AwardExternalAchievementResponse = variant {

service : {
// Queries
bot_updates : (BotUpdatesArgs) -> (BotUpdatesResponse) query;
check_username : (CheckUsernameArgs) -> (CheckUsernameResponse) query;
chit_leaderboard : (EmptyArgs) -> (ChitLeaderboardResponse) query;
current_user : (EmptyArgs) -> (CurrentUserResponse) query;
Expand Down
1 change: 1 addition & 0 deletions backend/canisters/user_index/api/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::env;
use ts_export::generate_ts_method;

fn main() {
generate_candid_method!(user_index, bot_updates, query);
generate_candid_method!(user_index, check_username, query);
generate_candid_method!(user_index, chit_leaderboard, query);
generate_candid_method!(user_index, current_user, query);
Expand Down
38 changes: 38 additions & 0 deletions backend/canisters/user_index/api/src/queries/bot_updates.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use candid::CandidType;
use serde::{Deserialize, Serialize};
use ts_export::ts_export;
use types::{SlashCommandSchema, TimestampMillis, UserId};

#[ts_export(user_index, bot_updates)]
#[derive(CandidType, Serialize, Deserialize, Debug)]
pub struct Args {
pub updated_since: TimestampMillis,
}

#[ts_export(user_index, bot_updates)]
#[derive(CandidType, Serialize, Deserialize, Debug)]
pub enum Response {
Success(SuccessResult),
SuccessNoUpdates,
}

#[ts_export(user_index, bot_updates)]
#[derive(CandidType, Serialize, Deserialize, Debug)]
pub struct SuccessResult {
pub added_or_updated: Vec<BotSchema>,
pub deleted: Vec<UserId>,
pub timestamp: TimestampMillis,
}

#[ts_export(user_index, bot_updates)]
#[derive(CandidType, Serialize, Deserialize, Debug)]
pub struct BotSchema {
pub id: UserId,
pub owner: UserId,
pub name: String,
pub avatar_id: Option<u128>,
pub endpoint: String,
pub description: String,
pub commands: Vec<SlashCommandSchema>,
pub last_updated: TimestampMillis,
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ use serde::{Deserialize, Serialize};
use ts_export::ts_export;
use types::BotMatch;

#[ts_export(group_index, explore_communities)]
#[ts_export(group_index, explore_bots)]
#[derive(CandidType, Serialize, Deserialize, Debug)]
pub struct Args {
pub search_term: Option<String>,
pub page_index: u32,
pub page_size: u8,
}

#[ts_export(group_index, explore_communities)]
#[ts_export(group_index, explore_bots)]
#[derive(CandidType, Serialize, Deserialize, Debug)]
pub enum Response {
Success(SuccessResult),
Expand All @@ -20,7 +20,7 @@ pub enum Response {
InvalidTerm,
}

#[ts_export(group_index, explore_communities)]
#[ts_export(group_index, explore_bots)]
#[derive(CandidType, Serialize, Deserialize, Debug)]
pub struct SuccessResult {
pub matches: Vec<BotMatch>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ pub enum Response {
pub struct SuccessResult {
pub last_updated: TimestampMillis,
pub added_or_updated: Vec<ExternalAchievement>,
// TODO: Remove after FE updated to use added_or_updated
pub achievements_added: Vec<ExternalAchievement>,
// TODO: Remove after FE updated to use added_or_updated
pub achievements_removed: Vec<ExternalAchievement>,
}

#[ts_export(user_index, external_achievements)]
Expand Down
1 change: 1 addition & 0 deletions backend/canisters/user_index/api/src/queries/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod bot_updates;
pub mod c2c_lookup_user;
pub mod check_username;
pub mod chit_leaderboard;
Expand Down
1 change: 0 additions & 1 deletion backend/canisters/user_index/api/src/queries/users.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use types::{CurrentUserSummary, TimestampMillis, UserId, UserSummaryV2};
pub struct Args {
pub user_groups: Vec<UserGroup>,
pub users_suspended_since: Option<TimestampMillis>,
//pub bots_updated_since: Option<TimestampMillis>,
}

#[ts_export(user_index, users)]
Expand Down
5 changes: 5 additions & 0 deletions backend/canisters/user_index/impl/src/model/user_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ impl Bot {
BotMatch {
id,
score,
owner: self.owner,
name: self.name.clone(),
description: self.description.clone(),
avatar_id: self.avatar.as_ref().map(|a| a.id),
Expand Down Expand Up @@ -351,6 +352,10 @@ impl UserMap {
self.users.values()
}

pub fn iter_bots(&self) -> impl Iterator<Item = (&UserId, &Bot)> {
self.bots.iter()
}

pub fn len(&self) -> usize {
self.users.len()
}
Expand Down
37 changes: 37 additions & 0 deletions backend/canisters/user_index/impl/src/queries/bot_updates.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use crate::{read_state, RuntimeState};
use canister_api_macros::query;
use user_index_canister::bot_updates::{Response::*, *};

#[query(candid = true, msgpack = true)]
fn bot_updates(args: Args) -> Response {
read_state(|state| bot_updates_impl(args, state))
}

fn bot_updates_impl(args: Args, state: &RuntimeState) -> Response {
let added_or_updated: Vec<_> = state
.data
.users
.iter_bots()
.filter(|(_, b)| b.last_updated > args.updated_since)
.map(|(id, b)| BotSchema {
id: *id,
owner: b.owner,
name: b.name.clone(),
avatar_id: b.avatar.as_ref().map(|a| a.id),
endpoint: b.endpoint.clone(),
description: b.description.clone(),
commands: b.commands.clone(),
last_updated: b.last_updated,
})
.collect();

if let Some(timestamp) = added_or_updated.iter().map(|b| b.last_updated).max() {
Success(SuccessResult {
added_or_updated,
deleted: Vec::new(),
timestamp,
})
} else {
SuccessNoUpdates
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ fn external_achievements(args: Args) -> Response {

fn external_achievements_impl(args: Args, state: &RuntimeState) -> Response {
let mut added_or_updated = Vec::new();
let mut achievements_added = Vec::new();
let mut last_updated: TimestampMillis = 0;

for (id, achievement) in state.data.external_achievements.iter() {
Expand All @@ -35,10 +34,6 @@ fn external_achievements_impl(args: Args, state: &RuntimeState) -> Response {
budget_exhausted: achievement.budget_exhausted.is_some(),
};

if add {
achievements_added.push(a.clone());
}

added_or_updated.push(a);
}
}
Expand All @@ -49,8 +44,6 @@ fn external_achievements_impl(args: Args, state: &RuntimeState) -> Response {
Success(SuccessResult {
last_updated,
added_or_updated,
achievements_added,
achievements_removed: Vec::new(),
})
}
}
1 change: 1 addition & 0 deletions backend/canisters/user_index/impl/src/queries/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod bot_updates;
pub mod c2c_lookup_user;
pub mod check_username;
pub mod chit_leaderboard;
Expand Down
1 change: 1 addition & 0 deletions backend/libraries/types/src/bots.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ pub struct BotMatch {
pub score: u32,
pub name: String,
pub description: String,
pub owner: UserId,
pub avatar_id: Option<u128>,
pub banner_id: Option<u128>,
pub commands: Vec<SlashCommandSchema>,
Expand Down
Loading