From 91f687fd038636977a65c71697a7f62706596189 Mon Sep 17 00:00:00 2001 From: Matt Grogan Date: Thu, 21 Nov 2024 19:45:57 +0200 Subject: [PATCH] Remove any spurious video calls in progress (#6872) --- backend/canisters/community/CHANGELOG.md | 1 + .../impl/src/lifecycle/post_upgrade.rs | 9 ++++- backend/canisters/group/CHANGELOG.md | 1 + .../group/impl/src/lifecycle/post_upgrade.rs | 7 +++- backend/canisters/user/CHANGELOG.md | 1 + .../user/impl/src/lifecycle/post_upgrade.rs | 9 ++++- .../libraries/chat_events/src/chat_events.rs | 37 +++++++++++++++++-- 7 files changed, 58 insertions(+), 7 deletions(-) diff --git a/backend/canisters/community/CHANGELOG.md b/backend/canisters/community/CHANGELOG.md index b9fe6c2417..6fd7c89d52 100644 --- a/backend/canisters/community/CHANGELOG.md +++ b/backend/canisters/community/CHANGELOG.md @@ -34,6 +34,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). - Remove code to migrate events to stable memory now that it is complete ([#6769](https://github.com/open-chat-labs/open-chat/pull/6769)) - Remove code to migrate to the new thread summary format ([#6862](https://github.com/open-chat-labs/open-chat/pull/6862)) +- Remove any spurious video calls in progress ([#6872](https://github.com/open-chat-labs/open-chat/pull/6872)) ## [[2.0.1433](https://github.com/open-chat-labs/open-chat/releases/tag/v2.0.1433-community)] - 2024-11-07 diff --git a/backend/canisters/community/impl/src/lifecycle/post_upgrade.rs b/backend/canisters/community/impl/src/lifecycle/post_upgrade.rs index 5b2484633b..fc59961ff4 100644 --- a/backend/canisters/community/impl/src/lifecycle/post_upgrade.rs +++ b/backend/canisters/community/impl/src/lifecycle/post_upgrade.rs @@ -1,7 +1,7 @@ use crate::jobs::import_groups::finalize_group_import; use crate::lifecycle::{init_env, init_state}; use crate::memory::{get_chat_events_memory, get_upgrades_memory}; -use crate::{read_state, Data}; +use crate::{mutate_state, read_state, Data}; use canister_logger::LogEntry; use canister_tracing_macros::trace; use community_canister::post_upgrade::Args; @@ -54,4 +54,11 @@ fn post_upgrade(args: Args) { .data .record_instructions_count(InstructionCountFunctionId::PostUpgrade, now) }); + + mutate_state(|state| { + let now = state.env.now(); + for channel in state.data.channels.iter_mut() { + channel.chat.events.remove_spurious_video_call_in_progress(now); + } + }); } diff --git a/backend/canisters/group/CHANGELOG.md b/backend/canisters/group/CHANGELOG.md index d8bbbab191..26f3c3811f 100644 --- a/backend/canisters/group/CHANGELOG.md +++ b/backend/canisters/group/CHANGELOG.md @@ -29,6 +29,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). - Remove code to migrate events to stable memory ([#6837](https://github.com/open-chat-labs/open-chat/pull/6837)) - Remove code to migrate to the new thread summary format ([#6862](https://github.com/open-chat-labs/open-chat/pull/6862)) +- Remove any spurious video calls in progress ([#6872](https://github.com/open-chat-labs/open-chat/pull/6872)) ### Fixed diff --git a/backend/canisters/group/impl/src/lifecycle/post_upgrade.rs b/backend/canisters/group/impl/src/lifecycle/post_upgrade.rs index 989d848d88..0993d8ded0 100644 --- a/backend/canisters/group/impl/src/lifecycle/post_upgrade.rs +++ b/backend/canisters/group/impl/src/lifecycle/post_upgrade.rs @@ -1,6 +1,6 @@ use crate::lifecycle::{init_env, init_state}; use crate::memory::{get_chat_events_memory, get_upgrades_memory}; -use crate::{read_state, Data}; +use crate::{mutate_state, read_state, Data}; use canister_logger::LogEntry; use canister_tracing_macros::trace; use group_canister::post_upgrade::Args; @@ -45,4 +45,9 @@ fn post_upgrade(args: Args) { .data .record_instructions_count(InstructionCountFunctionId::PostUpgrade, now) }); + + mutate_state(|state| { + let now = state.env.now(); + state.data.chat.events.remove_spurious_video_call_in_progress(now); + }); } diff --git a/backend/canisters/user/CHANGELOG.md b/backend/canisters/user/CHANGELOG.md index 054f6e94ae..0b88c987c3 100644 --- a/backend/canisters/user/CHANGELOG.md +++ b/backend/canisters/user/CHANGELOG.md @@ -47,6 +47,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). - Remove events from being stored on the heap ([#6758](https://github.com/open-chat-labs/open-chat/pull/6758)) - Removed a bunch of unwanted achievements ([#6794](https://github.com/open-chat-labs/open-chat/pull/6794)) - Remove code to migrate events to stable memory ([#6837](https://github.com/open-chat-labs/open-chat/pull/6837)) +- Remove any spurious video calls in progress ([#6872](https://github.com/open-chat-labs/open-chat/pull/6872)) ## [[2.0.1412](https://github.com/open-chat-labs/open-chat/releases/tag/v2.0.1414-user)] - 2024-10-24 diff --git a/backend/canisters/user/impl/src/lifecycle/post_upgrade.rs b/backend/canisters/user/impl/src/lifecycle/post_upgrade.rs index cccda9c674..9e96c1d4a4 100644 --- a/backend/canisters/user/impl/src/lifecycle/post_upgrade.rs +++ b/backend/canisters/user/impl/src/lifecycle/post_upgrade.rs @@ -1,6 +1,6 @@ use crate::lifecycle::{init_env, init_state}; use crate::memory::{get_chat_events_memory, get_upgrades_memory}; -use crate::Data; +use crate::{mutate_state, Data}; use canister_logger::LogEntry; use canister_tracing_macros::trace; use ic_cdk::post_upgrade; @@ -34,4 +34,11 @@ fn post_upgrade(args: Args) { init_state(env, data, args.wasm_version); info!(version = %args.wasm_version, "Post-upgrade complete"); + + mutate_state(|state| { + let now = state.env.now(); + for chat in state.data.direct_chats.iter_mut() { + chat.events.remove_spurious_video_call_in_progress(now); + } + }); } diff --git a/backend/libraries/chat_events/src/chat_events.rs b/backend/libraries/chat_events/src/chat_events.rs index 8a52122542..1cb26c4bf7 100644 --- a/backend/libraries/chat_events/src/chat_events.rs +++ b/backend/libraries/chat_events/src/chat_events.rs @@ -23,14 +23,15 @@ use types::{ AcceptP2PSwapResult, CallParticipant, CancelP2PSwapResult, CanisterId, Chat, ChatType, CompleteP2PSwapResult, CompletedCryptoTransaction, Cryptocurrency, DirectChatCreated, EventContext, EventIndex, EventWrapper, EventWrapperInternal, EventsTimeToLiveUpdated, GroupCanisterThreadDetails, GroupCreated, GroupFrozen, GroupUnfrozen, Hash, - HydratedMention, Mention, Message, MessageContentInitial, MessageEditedEventPayload, MessageEventPayload, MessageId, - MessageIndex, MessageMatch, MessageReport, MessageTippedEventPayload, Milliseconds, MultiUserChat, P2PSwapAccepted, - P2PSwapCompleted, P2PSwapCompletedEventPayload, P2PSwapContent, P2PSwapStatus, PendingCryptoTransaction, PollVotes, - ProposalUpdate, PushEventResult, Reaction, ReactionAddedEventPayload, RegisterVoteResult, ReserveP2PSwapResult, + HydratedMention, Mention, Message, MessageContent, MessageContentInitial, MessageEditedEventPayload, MessageEventPayload, + MessageId, MessageIndex, MessageMatch, MessageReport, MessageTippedEventPayload, Milliseconds, MultiUserChat, + P2PSwapAccepted, P2PSwapCompleted, P2PSwapCompletedEventPayload, P2PSwapContent, P2PSwapStatus, PendingCryptoTransaction, + PollVotes, ProposalUpdate, PushEventResult, Reaction, ReactionAddedEventPayload, RegisterVoteResult, ReserveP2PSwapResult, ReserveP2PSwapSuccess, TimestampMillis, TimestampNanos, Timestamped, Tips, UserId, VideoCall, VideoCallEndedEventPayload, VideoCallParticipants, VideoCallPresence, VoteOperation, }; use utils::consts::OPENCHAT_BOT_USER_ID; +use utils::time::HOUR_IN_MS; #[derive(Serialize, Deserialize)] pub struct ChatEvents { @@ -49,6 +50,34 @@ pub struct ChatEvents { } impl ChatEvents { + pub fn remove_spurious_video_call_in_progress(&mut self, now: TimestampMillis) { + // IF any direct chats have video calls in progress where either: + // 1. The message cannot be found + // 2. The message is not a video call + // 3. More than 2 hours have passed since the call was started + // THEN remove the video call in progress indicator + + if self.video_call_is_spurious(now) { + self.video_call_in_progress = Timestamped::new(None, now); + } + } + + fn video_call_is_spurious(&self, now: TimestampMillis) -> bool { + if let Some(video_call) = &self.video_call_in_progress.value { + if now - self.video_call_in_progress.timestamp > 2 * HOUR_IN_MS { + return true; + } + + if let Some(message) = self.main_events_reader().message(video_call.message_index.into(), None) { + return !matches!(message.content, MessageContent::VideoCall(_)); + } else { + return true; + } + } + + false + } + pub fn init_stable_storage(memory: Memory) { stable_storage::init(memory) }