Skip to content

Commit

Permalink
fix_: fix unmute logic (#5720)
Browse files Browse the repository at this point in the history
  • Loading branch information
Parveshdhull authored Aug 30, 2024
1 parent 82ba10e commit 0531535
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 38 deletions.
86 changes: 50 additions & 36 deletions protocol/messenger.go
Original file line number Diff line number Diff line change
Expand Up @@ -827,7 +827,7 @@ func (m *Messenger) Start() (*MessengerResponse, error) {
m.schedulePublishGrantsForControlledCommunities()
m.handleENSVerificationSubscription(ensSubscription)
m.watchConnectionChange()
m.watchChatsAndCommunitiesToUnmute()
m.watchChatsToUnmute()
m.watchCommunitiesToUnmute()
m.watchExpiredMessages()
m.watchIdentityImageChanges()
Expand Down Expand Up @@ -1577,56 +1577,70 @@ func (m *Messenger) watchConnectionChange() {
go subscribedConnectionStatus(subscription)
}

// watchChatsAndCommunitiesToUnmute regularly checks for chats and communities that should be unmuted
func (m *Messenger) watchChatsAndCommunitiesToUnmute() {
m.logger.Debug("watching unmuted chats")
// watchChatsToUnmute checks every minute to identify and unmute chats that should no longer be muted.
func (m *Messenger) watchChatsToUnmute() {
m.logger.Debug("Checking for chats to unmute every minute")
go func() {
for {
select {
case <-time.After(1 * time.Minute):
response := &MessengerResponse{}
m.allChats.Range(func(chatID string, c *Chat) bool {
chatMuteTill := c.MuteTill.Truncate(time.Second)
currTime := time.Now().Truncate(time.Second)

if currTime.After(chatMuteTill) && !chatMuteTill.Equal(time.Time{}) && c.Muted {
err := m.persistence.UnmuteChat(c.ID)
if err != nil {
m.logger.Info("err", zap.Any("Couldn't unmute chat", err))
return false
}
c.Muted = false
c.MuteTill = time.Time{}
response.AddChat(c)
// Execute the check immediately upon starting
response := &MessengerResponse{}
currTime := time.Now()

m.allChats.Range(func(chatID string, c *Chat) bool {
chatMuteTill := c.MuteTill
if currTime.After(chatMuteTill) && !chatMuteTill.Equal(time.Time{}) && c.Muted {
err := m.persistence.UnmuteChat(c.ID)
if err != nil {
m.logger.Warn("watchChatsToUnmute error", zap.Any("Couldn't unmute chat", err))
return false
}
return true
})

if !response.IsEmpty() {
signal.SendNewMessages(response)
c.Muted = false
c.MuteTill = time.Time{}
response.AddChat(c)
}
return true
})

if !response.IsEmpty() {
signal.SendNewMessages(response)
}

// Calculate the time until the next whole minute
now := time.Now()
waitDuration := time.Until(now.Truncate(time.Minute).Add(time.Minute))

// Wait until the next minute
select {
case <-time.After(waitDuration):
// Continue to next iteration
case <-m.quit:
return
}
}
}()
}

// watchCommunitiesToUnmute regularly checks for communities that should be unmuted
// watchCommunitiesToUnmute checks every minute to identify and unmute communities that should no longer be muted.
func (m *Messenger) watchCommunitiesToUnmute() {
m.logger.Debug("watching unmuted communities")
m.logger.Debug("Checking for communities to unmute every minute")
go func() {
for {
select {
case <-time.After(1 * time.Minute):
response, err := m.CheckCommunitiesToUnmute()
if err != nil {
return
}
// Execute the check immediately upon starting
response, err := m.CheckCommunitiesToUnmute()
if err != nil {
m.logger.Warn("watchCommunitiesToUnmute error", zap.Any("Couldn't unmute communities", err))
} else if !response.IsEmpty() {
signal.SendNewMessages(response)
}

if !response.IsEmpty() {
signal.SendNewMessages(response)
}
// Calculate the time until the next whole minute
now := time.Now()
waitDuration := time.Until(now.Truncate(time.Minute).Add(time.Minute))

// Wait until the next minute
select {
case <-time.After(waitDuration):
// Continue to next iteration
case <-m.quit:
return
}
Expand Down
4 changes: 2 additions & 2 deletions protocol/messenger_communities.go
Original file line number Diff line number Diff line change
Expand Up @@ -838,12 +838,12 @@ func (m *Messenger) CheckCommunitiesToUnmute() (*MessengerResponse, error) {
m.logger.Debug("watching communities to unmute")
response := &MessengerResponse{}
communities, err := m.communitiesManager.All()
currTime := time.Now()
if err != nil {
return nil, fmt.Errorf("couldn't get all communities: %v", err)
}
for _, community := range communities {
communityMuteTill := community.MuteTill().Truncate(time.Second)
currTime := time.Now().Truncate(time.Second)
communityMuteTill := community.MuteTill()

if currTime.After(communityMuteTill) && !communityMuteTill.Equal(time.Time{}) && community.Muted() {
err := m.communitiesManager.SetMuted(community.ID(), false)
Expand Down

0 comments on commit 0531535

Please sign in to comment.