Skip to content

Commit

Permalink
Next batch of achievements (#6230)
Browse files Browse the repository at this point in the history
  • Loading branch information
megrogan authored Aug 13, 2024
1 parent ac282f1 commit 8015485
Show file tree
Hide file tree
Showing 90 changed files with 539 additions and 111 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

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

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

## [unreleased]

### Changed

- Support next batch of achievements ([#6230](https://github.com/open-chat-labs/open-chat/pull/6230))

## [[2.0.1287](https://github.com/open-chat-labs/open-chat/releases/tag/v2.0.1287-community)] - 2024-08-13

### Added

- Add `external_url` property to channel ([#6226](https://github.com/open-chat-labs/open-chat/pull/6226))
Expand Down
5 changes: 5 additions & 0 deletions backend/canisters/community/api/can.did
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ type AcceptP2PSwapArgs = record {
thread_root_message_index : opt MessageIndex;
message_id : MessageId;
pin : opt text;
new_achievement : bool;
};

type AcceptP2PSwapResponse = variant {
Expand Down Expand Up @@ -647,6 +648,7 @@ type ImportGroupResponse = variant {
type JoinVideoCallArgs = record {
channel_id : ChannelId;
message_id : MessageId;
new_achievement : bool;
};

type JoinVideoCallResponse = variant {
Expand Down Expand Up @@ -697,6 +699,7 @@ type RegisterPollVoteArgs = record {
message_index : MessageIndex;
poll_option : nat32;
operation : VoteOperation;
new_achievement : bool;
};

type RegisterPollVoteResponse = variant {
Expand Down Expand Up @@ -859,6 +862,7 @@ type SendMessageSuccess = record {

type SetMemberDisplayNameArgs = record {
display_name : opt text;
new_achievement : bool;
};

type SetMemberDisplayNameResponse = variant {
Expand All @@ -875,6 +879,7 @@ type SetVideoCallPresenceArgs = record {
channel_id : ChannelId;
message_id : MessageId;
presence : VideoCallPresence;
new_achievement : bool;
};

type SetVideoCallPresenceResponse = variant {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub struct Args {
pub thread_root_message_index: Option<MessageIndex>,
pub message_id: MessageId,
pub pin: Option<String>,
pub new_achievement: bool,
}

#[derive(CandidType, Serialize, Deserialize, Debug)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use types::{ChannelId, MessageId, VideoCallPresence};
pub struct Args {
pub channel_id: ChannelId,
pub message_id: MessageId,
pub new_achievement: bool,
}

pub type Response = crate::set_video_call_presence::Response;
Expand All @@ -16,6 +17,7 @@ impl From<Args> for crate::set_video_call_presence::Args {
channel_id: value.channel_id,
message_id: value.message_id,
presence: VideoCallPresence::Default,
new_achievement: value.new_achievement,
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub struct Args {
pub message_index: MessageIndex,
pub poll_option: u32,
pub operation: VoteOperation,
pub new_achievement: bool,
}

#[derive(CandidType, Serialize, Deserialize, Debug)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use serde::{Deserialize, Serialize};
#[derive(CandidType, Serialize, Deserialize, Debug)]
pub struct Args {
pub display_name: Option<String>,
pub new_achievement: bool,
}

#[derive(CandidType, Serialize, Deserialize, Debug)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub struct Args {
pub channel_id: ChannelId,
pub message_id: MessageId,
pub presence: VideoCallPresence,
pub new_achievement: bool,
}

#[derive(CandidType, Serialize, Deserialize, Debug)]
Expand Down
19 changes: 7 additions & 12 deletions backend/canisters/community/impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ use event_store_producer::{EventStoreClient, EventStoreClientBuilder, EventStore
use event_store_producer_cdk_runtime::CdkRuntime;
use fire_and_forget_handler::FireAndForgetHandler;
use group_chat_core::AccessRulesInternal;
use group_community_common::{PaymentReceipts, PaymentRecipient, PendingPayment, PendingPaymentReason, PendingPaymentsQueue};
use group_community_common::{
Achievements, PaymentReceipts, PaymentRecipient, PendingPayment, PendingPaymentReason, PendingPaymentsQueue,
};
use instruction_counts_log::{InstructionCountEntry, InstructionCountFunctionId, InstructionCountsLog};
use model::{events::CommunityEvents, invited_users::InvitedUsers, members::CommunityMemberInternal};
use msgpack::serialize_then_unwrap;
Expand All @@ -24,12 +26,11 @@ use std::cell::RefCell;
use std::ops::Deref;
use std::time::Duration;
use types::{
AccessGate, Achievement, BuildVersion, CanisterId, ChatMetrics, CommunityCanisterCommunitySummary, CommunityMembership,
AccessGate, BuildVersion, CanisterId, ChatMetrics, CommunityCanisterCommunitySummary, CommunityMembership,
CommunityPermissions, CommunityRole, Cryptocurrency, Cycles, Document, Empty, FrozenGroupInfo, Milliseconds, Notification,
PaymentGate, Rules, TimestampMillis, Timestamped, UserId, UserType,
};
use types::{CommunityId, SNS_FEE_SHARE_PERCENT};
use user_canister::c2c_notify_achievement;
use utils::env::Environment;
use utils::regular_jobs::RegularJobs;
use utils::time::MINUTE_IN_MS;
Expand Down Expand Up @@ -275,15 +276,6 @@ impl RuntimeState {
},
}
}

fn notify_user_of_achievements(&self, user_id: UserId, achievements: Vec<Achievement>) {
let args = c2c_notify_achievement::Args { achievements };
self.data.fire_and_forget_handler.send(
user_id.into(),
"c2c_notify_achievement_msgpack".to_string(),
serialize_then_unwrap(args),
);
}
}

fn init_instruction_counts_log() -> InstructionCountsLog {
Expand Down Expand Up @@ -333,6 +325,8 @@ struct Data {
#[serde(with = "serde_bytes")]
ic_root_key: Vec<u8>,
event_store_client: EventStoreClient<CdkRuntime>,
#[serde(default)]
achievements: Achievements,
}

impl Data {
Expand Down Expand Up @@ -429,6 +423,7 @@ impl Data {
event_store_client: EventStoreClientBuilder::new(local_group_index_canister_id, CdkRuntime::default())
.with_flush_delay(Duration::from_millis(5 * MINUTE_IN_MS))
.build(),
achievements: Achievements::default(),
}
}

Expand Down
14 changes: 13 additions & 1 deletion backend/canisters/community/impl/src/updates/accept_p2p_swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use canister_tracing_macros::trace;
use community_canister::accept_p2p_swap::{Response::*, *};
use ic_cdk::update;
use icrc_ledger_types::icrc1::transfer::TransferError;
use types::{AcceptSwapSuccess, ChannelId, Chat, MessageId, MessageIndex, P2PSwapLocation, UserId};
use types::{AcceptSwapSuccess, Achievement, ChannelId, Chat, MessageId, MessageIndex, P2PSwapLocation, UserId};

#[update]
#[trace]
Expand All @@ -15,6 +15,7 @@ async fn accept_p2p_swap(args: Args) -> Response {
let channel_id = args.channel_id;
let thread_root_message_index = args.thread_root_message_index;
let message_id = args.message_id;
let new_achievement = args.new_achievement;

let ReserveP2PSwapResult { user_id, c2c_args } = match mutate_state(|state| reserve_p2p_swap(args, state)) {
Ok(result) => result,
Expand All @@ -31,6 +32,17 @@ async fn accept_p2p_swap(args: Args) -> Response {
message_id,
transaction_index,
);

if new_achievement {
mutate_state(|state| {
state.data.achievements.notify_user(
user_id,
vec![Achievement::AcceptedP2PSwapOffer],
&mut state.data.fire_and_forget_handler,
);
});
}

Success(AcceptSwapSuccess {
token1_txn_in: transaction_index,
})
Expand Down
14 changes: 12 additions & 2 deletions backend/canisters/community/impl/src/updates/add_reaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ fn add_reaction_impl(args: Args, state: &mut RuntimeState) -> Response {
now,
&mut state.data.event_store_client,
) {
AddRemoveReactionResult::Success => {
AddRemoveReactionResult::Success(sender) => {
if let Some(message) = should_push_notification(&args, user_id, &channel.chat) {
push_notification(
args,
Expand All @@ -54,9 +54,19 @@ fn add_reaction_impl(args: Args, state: &mut RuntimeState) -> Response {
handle_activity_notification(state);

if new_achievement {
state.notify_user_of_achievements(user_id, vec![Achievement::ReactedToMessage]);
state.data.achievements.notify_user(
user_id,
vec![Achievement::ReactedToMessage],
&mut state.data.fire_and_forget_handler,
);
}

state.data.achievements.notify_user(
sender,
vec![Achievement::HadMessageReactedTo],
&mut state.data.fire_and_forget_handler,
);

Success
}
AddRemoveReactionResult::NoChange => NoChange,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use chat_events::{Reader, TipMessageArgs};
use community_canister::c2c_tip_message::{Response::*, *};
use group_chat_core::TipMessageResult;
use ledger_utils::format_crypto_amount_with_symbol;
use types::{ChannelMessageTipped, EventIndex, Notification};
use types::{Achievement, ChannelMessageTipped, EventIndex, Notification};

#[update(msgpack = true)]
#[trace]
Expand Down Expand Up @@ -69,6 +69,13 @@ fn c2c_tip_message_impl(args: Args, state: &mut RuntimeState) -> Response {
});
state.push_notification(vec![args.recipient], notification);
}

state.data.achievements.notify_user(
args.recipient,
vec![Achievement::HadMessageTipped],
&mut state.data.fire_and_forget_handler,
);

handle_activity_notification(state);
Success
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,11 @@ fn delete_messages_impl(user_id: UserId, args: Args, state: &mut RuntimeState) -
handle_activity_notification(state);

if args.new_achievement {
state.notify_user_of_achievements(user_id, vec![Achievement::DeletedMessage]);
state.data.achievements.notify_user(
user_id,
vec![Achievement::DeletedMessage],
&mut state.data.fire_and_forget_handler,
);
}

Success
Expand Down
6 changes: 5 additions & 1 deletion backend/canisters/community/impl/src/updates/edit_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ fn edit_message_impl(args: Args, state: &mut RuntimeState) -> Response {
handle_activity_notification(state);

if args.new_achievement {
state.notify_user_of_achievements(sender, vec![Achievement::EditedMessage]);
state.data.achievements.notify_user(
sender,
vec![Achievement::EditedMessage],
&mut state.data.fire_and_forget_handler,
);
}

Success
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use canister_tracing_macros::trace;
use chat_events::{RegisterPollVoteArgs, RegisterPollVoteResult};
use community_canister::register_poll_vote::{Response::*, *};
use ic_cdk::update;
use types::Achievement;

#[update]
#[trace]
Expand Down Expand Up @@ -56,6 +57,14 @@ fn register_poll_vote_impl(args: Args, state: &mut RuntimeState) -> Response {

match result {
RegisterPollVoteResult::Success(votes) => {
if args.new_achievement {
state.data.achievements.notify_user(
user_id,
vec![Achievement::VotedOnPoll],
&mut state.data.fire_and_forget_handler,
);
}

handle_activity_notification(state);
Success(votes)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fn remove_reaction_impl(args: Args, state: &mut RuntimeState) -> Response {
.chat
.remove_reaction(user_id, args.thread_root_message_index, args.message_id, args.reaction, now)
{
AddRemoveReactionResult::Success => {
AddRemoveReactionResult::Success(_) => {
handle_activity_notification(state);
Success
}
Expand Down
11 changes: 10 additions & 1 deletion backend/canisters/community/impl/src/updates/send_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,9 +247,18 @@ fn process_send_message_result(
);

if new_achievement {
state.notify_user_of_achievements(
state.data.achievements.notify_user(
sender,
Achievement::from_message(false, &result.message_event.event, thread_root_message_index.is_some()),
&mut state.data.fire_and_forget_handler,
);
}

if let MessageContent::Crypto(c) = &result.message_event.event.content {
state.data.achievements.notify_user(
c.recipient,
vec![Achievement::ReceivedCrypto],
&mut state.data.fire_and_forget_handler,
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::{activity_notifications::handle_activity_notification, mutate_state,
use canister_tracing_macros::trace;
use community_canister::set_member_display_name::{Response::*, *};
use ic_cdk::update;
use types::Achievement;
use utils::text_validation::{validate_display_name, UsernameValidationError};

#[update]
Expand Down Expand Up @@ -37,6 +38,15 @@ fn set_member_display_name_impl(args: Args, state: &mut RuntimeState) -> Respons
};

state.data.members.set_display_name(user_id, args.display_name, now);

if args.new_achievement {
state.data.achievements.notify_user(
user_id,
vec![Achievement::SetCommunityDisplayName],
&mut state.data.fire_and_forget_handler,
);
}

handle_activity_notification(state);
Success
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use canister_tracing_macros::trace;
use chat_events::SetVideoCallPresenceResult;
use community_canister::set_video_call_presence::{Response::*, *};
use ic_cdk::update;
use types::Achievement;

#[update]
#[trace]
Expand Down Expand Up @@ -37,6 +38,14 @@ pub(crate) fn set_video_call_presence_impl(args: Args, state: &mut RuntimeState)
now,
) {
SetVideoCallPresenceResult::Success => {
if args.new_achievement {
state.data.achievements.notify_user(
user_id,
vec![Achievement::JoinedCall],
&mut state.data.fire_and_forget_handler,
);
}

handle_activity_notification(state);
Success
}
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]

### Changed

- Support next batch of achievements ([#6230](https://github.com/open-chat-labs/open-chat/pull/6230))

## [[2.0.1273](https://github.com/open-chat-labs/open-chat/releases/tag/v2.0.1273-group)] - 2024-07-31

### Changed
Expand Down
Loading

0 comments on commit 8015485

Please sign in to comment.