From 044eddb6e12dc52e0868f4a22cd84905c84a0938 Mon Sep 17 00:00:00 2001 From: streamer45 Date: Fri, 4 Aug 2023 12:00:43 -0600 Subject: [PATCH 1/2] Persist list of participants on call end --- server/channel_state.go | 6 +++--- server/plugin.go | 3 ++- server/session.go | 10 ++++++---- server/utils.go | 8 ++++++++ 4 files changed, 19 insertions(+), 8 deletions(-) diff --git a/server/channel_state.go b/server/channel_state.go index f914a632d..18fe65a0b 100644 --- a/server/channel_state.go +++ b/server/channel_state.go @@ -23,8 +23,8 @@ type userState struct { } type callStats struct { - Participants int `json:"participants"` - ScreenDuration int64 `json:"screen_duration"` + Participants map[string]struct{} `json:"participants"` + ScreenDuration int64 `json:"screen_duration"` } type callState struct { @@ -313,7 +313,7 @@ func (p *Plugin) cleanCallState(channelID string, state *channelState) error { state.NodeID = "" if state.Call != nil { - if _, err := p.updateCallPostEnded(state.Call.PostID); err != nil { + if _, err := p.updateCallPostEnded(state.Call.PostID, mapKeys(state.Call.Stats.Participants)); err != nil { return err } state.Call = nil diff --git a/server/plugin.go b/server/plugin.go index 90b267a57..753a0e82a 100644 --- a/server/plugin.go +++ b/server/plugin.go @@ -325,7 +325,7 @@ func (p *Plugin) createCallStartedPost(state *channelState, userID, channelID, t return createdPost.Id, threadID, nil } -func (p *Plugin) updateCallPostEnded(postID string) (float64, error) { +func (p *Plugin) updateCallPostEnded(postID string, participants []string) (float64, error) { post, appErr := p.API.GetPost(postID) if appErr != nil { return 0, appErr @@ -342,6 +342,7 @@ func (p *Plugin) updateCallPostEnded(postID string) (float64, error) { post.DelProp("attachments") post.AddProp("attachments", []*model.SlackAttachment{&slackAttachment}) post.AddProp("end_at", time.Now().UnixMilli()) + post.AddProp("participants", participants) _, appErr = p.API.UpdatePost(post) if appErr != nil { diff --git a/server/session.go b/server/session.go index fee5376d4..e8d29e444 100644 --- a/server/session.go +++ b/server/session.go @@ -137,9 +137,11 @@ func (p *Plugin) addUserSession(state *channelState, userID, connID, channelID s JoinAt: time.Now().UnixMilli(), } state.Call.Sessions[connID] = struct{}{} - if len(state.Call.Users) > state.Call.Stats.Participants { - state.Call.Stats.Participants = len(state.Call.Users) + + if state.Call.Stats.Participants == nil { + state.Call.Stats.Participants = map[string]struct{}{} } + state.Call.Stats.Participants[userID] = struct{}{} if err := p.kvSetChannelState(channelID, state); err != nil { return nil, err @@ -322,7 +324,7 @@ func (p *Plugin) removeSession(us *session) error { // Check if call has ended. if prevState.Call != nil && currState.Call == nil { - dur, err := p.updateCallPostEnded(prevState.Call.PostID) + dur, err := p.updateCallPostEnded(prevState.Call.PostID, mapKeys(prevState.Call.Stats.Participants)) if err != nil { return err } @@ -330,7 +332,7 @@ func (p *Plugin) removeSession(us *session) error { "ChannelID": us.channelID, "CallID": prevState.Call.ID, "Duration": dur, - "Participants": prevState.Call.Stats.Participants, + "Participants": len(prevState.Call.Stats.Participants), "ScreenDuration": prevState.Call.Stats.ScreenDuration, }) } diff --git a/server/utils.go b/server/utils.go index 88ae3c231..a70d93d26 100644 --- a/server/utils.go +++ b/server/utils.go @@ -120,3 +120,11 @@ func checkMinVersion(minVersion, currVersion string) error { return nil } + +func mapKeys[K comparable, V any](m map[K]V) []K { + keys := make([]K, 0, len(m)) + for k := range m { + keys = append(keys, k) + } + return keys +} From ed1faf36e963e3f38b4920d47b2fc2bce2d3ffad Mon Sep 17 00:00:00 2001 From: streamer45 Date: Fri, 4 Aug 2023 12:04:22 -0600 Subject: [PATCH 2/2] Exclude bot from participants list --- server/session.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/server/session.go b/server/session.go index e8d29e444..f5175fffe 100644 --- a/server/session.go +++ b/server/session.go @@ -141,7 +141,10 @@ func (p *Plugin) addUserSession(state *channelState, userID, connID, channelID s if state.Call.Stats.Participants == nil { state.Call.Stats.Participants = map[string]struct{}{} } - state.Call.Stats.Participants[userID] = struct{}{} + + if userID != p.getBotID() { + state.Call.Stats.Participants[userID] = struct{}{} + } if err := p.kvSetChannelState(channelID, state); err != nil { return nil, err