fix_: fix unmute logic (#5720)

This commit is contained in:
Parvesh Monu 2024-08-30 17:47:43 +05:30 committed by GitHub
parent 82ba10ede7
commit 0531535ef5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 51 additions and 37 deletions

View File

@ -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()
@ -1577,34 +1577,42 @@ 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)
// Execute the check immediately upon starting
response := &MessengerResponse{}
currTime := time.Now()
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)
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
}
@ -1612,21 +1620,27 @@ func (m *Messenger) watchChatsAndCommunitiesToUnmute() {
}()
}
// 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
}

View File

@ -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)