Skip to content

Commit

Permalink
Remove any spurious video calls in progress (#6872)
Browse files Browse the repository at this point in the history
  • Loading branch information
megrogan authored Nov 21, 2024
1 parent 9717e53 commit 91f687f
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 7 deletions.
1 change: 1 addition & 0 deletions backend/canisters/community/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
}
});
}
1 change: 1 addition & 0 deletions backend/canisters/group/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
7 changes: 6 additions & 1 deletion backend/canisters/group/impl/src/lifecycle/post_upgrade.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
});
}
1 change: 1 addition & 0 deletions backend/canisters/user/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 8 additions & 1 deletion backend/canisters/user/impl/src/lifecycle/post_upgrade.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
}
});
}
37 changes: 33 additions & 4 deletions backend/libraries/chat_events/src/chat_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}
Expand Down

0 comments on commit 91f687f

Please sign in to comment.