Synchronize muted channel state
This commit is contained in:
committed by
Brandon McGinty
parent
118799d112
commit
20c6997ab0
+23
-8
@@ -54,6 +54,7 @@ type Barnard struct {
|
|||||||
|
|
||||||
// Added for channel muting
|
// Added for channel muting
|
||||||
MutedChannels map[uint32]bool
|
MutedChannels map[uint32]bool
|
||||||
|
MutedChannelsMutex sync.RWMutex
|
||||||
userChannels map[uint32]*gumble.Channel
|
userChannels map[uint32]*gumble.Channel
|
||||||
|
|
||||||
// Added for noise suppression
|
// Added for noise suppression
|
||||||
@@ -116,6 +117,25 @@ func (b *Barnard) updateUserGain(user *gumble.User) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (b *Barnard) isChannelMuted(channelID uint32) bool {
|
||||||
|
b.MutedChannelsMutex.RLock()
|
||||||
|
defer b.MutedChannelsMutex.RUnlock()
|
||||||
|
return b.MutedChannels[channelID]
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *Barnard) setChannelMuted(channelID uint32, muted bool) {
|
||||||
|
b.MutedChannelsMutex.Lock()
|
||||||
|
defer b.MutedChannelsMutex.Unlock()
|
||||||
|
if b.MutedChannels == nil {
|
||||||
|
b.MutedChannels = make(map[uint32]bool)
|
||||||
|
}
|
||||||
|
if muted {
|
||||||
|
b.MutedChannels[channelID] = true
|
||||||
|
} else {
|
||||||
|
delete(b.MutedChannels, channelID)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (b *Barnard) StopTransmission() {
|
func (b *Barnard) StopTransmission() {
|
||||||
if b.Tx {
|
if b.Tx {
|
||||||
b.Notify("micdown", "me", "")
|
b.Notify("micdown", "me", "")
|
||||||
@@ -159,7 +179,7 @@ func (b *Barnard) TreeItemKeyPress(ui *uiterm.Ui, tree *uiterm.Tree, item uiterm
|
|||||||
if treeItem.Channel != nil {
|
if treeItem.Channel != nil {
|
||||||
if key == *b.Hotkeys.MuteToggle {
|
if key == *b.Hotkeys.MuteToggle {
|
||||||
// Determine new channel mute state
|
// Determine new channel mute state
|
||||||
channelWillBeMuted := !b.MutedChannels[treeItem.Channel.ID]
|
channelWillBeMuted := !b.isChannelMuted(treeItem.Channel.ID)
|
||||||
|
|
||||||
// Set all users in channel to the same mute state
|
// Set all users in channel to the same mute state
|
||||||
users := makeUsersArray(treeItem.Channel.Users)
|
users := makeUsersArray(treeItem.Channel.Users)
|
||||||
@@ -175,15 +195,10 @@ func (b *Barnard) TreeItemKeyPress(ui *uiterm.Ui, tree *uiterm.Tree, item uiterm
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Update channel mute state
|
// Update channel mute state
|
||||||
if channelWillBeMuted {
|
b.setChannelMuted(treeItem.Channel.ID, channelWillBeMuted)
|
||||||
b.MutedChannels[treeItem.Channel.ID] = true
|
if channelWillBeMuted && b.Client.Self.Channel.ID == treeItem.Channel.ID && b.Tx {
|
||||||
// If this is the current channel, stop transmission
|
|
||||||
if b.Client.Self.Channel.ID == treeItem.Channel.ID && b.Tx {
|
|
||||||
b.StopTransmission()
|
b.StopTransmission()
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
delete(b.MutedChannels, treeItem.Channel.ID)
|
|
||||||
}
|
|
||||||
|
|
||||||
b.RebuildUserChannelTreePreservingSelection()
|
b.RebuildUserChannelTreePreservingSelection()
|
||||||
b.Ui.Refresh()
|
b.Ui.Refresh()
|
||||||
|
|||||||
@@ -123,7 +123,9 @@ func (b *Barnard) OnConnect(e *gumble.ConnectEvent) {
|
|||||||
b.Client = e.Client
|
b.Client = e.Client
|
||||||
|
|
||||||
// Reset muted channels state on connect
|
// Reset muted channels state on connect
|
||||||
|
b.MutedChannelsMutex.Lock()
|
||||||
b.MutedChannels = make(map[uint32]bool)
|
b.MutedChannels = make(map[uint32]bool)
|
||||||
|
b.MutedChannelsMutex.Unlock()
|
||||||
b.userChannels = make(map[uint32]*gumble.Channel)
|
b.userChannels = make(map[uint32]*gumble.Channel)
|
||||||
b.RecordingMutex.Lock()
|
b.RecordingMutex.Lock()
|
||||||
b.recordingAllowed = nil
|
b.recordingAllowed = nil
|
||||||
@@ -279,7 +281,7 @@ func (b *Barnard) OnUserChange(e *gumble.UserChangeEvent) {
|
|||||||
// Check if user is joining a muted channel
|
// Check if user is joining a muted channel
|
||||||
if e.Type.Has(gumble.UserChangeConnected) || e.Type.Has(gumble.UserChangeChannel) {
|
if e.Type.Has(gumble.UserChangeConnected) || e.Type.Has(gumble.UserChangeChannel) {
|
||||||
// If the channel is muted, ensure the user is muted
|
// If the channel is muted, ensure the user is muted
|
||||||
if b.MutedChannels[e.User.Channel.ID] {
|
if b.isChannelMuted(e.User.Channel.ID) {
|
||||||
// Only mute if not already muted
|
// Only mute if not already muted
|
||||||
if !e.User.LocallyMuted() {
|
if !e.User.LocallyMuted() {
|
||||||
b.UserConfig.ToggleMute(e.User)
|
b.UserConfig.ToggleMute(e.User)
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
"unicode/utf8"
|
"unicode/utf8"
|
||||||
@@ -83,6 +84,20 @@ func TestServerAddressDefaultsPortWithoutBreakingIPv6(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestConcurrentMutedChannelAccess(t *testing.T) {
|
||||||
|
b := &Barnard{MutedChannels: make(map[uint32]bool)}
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
for i := 0; i < 20; i++ {
|
||||||
|
wg.Add(1)
|
||||||
|
go func(i int) {
|
||||||
|
defer wg.Done()
|
||||||
|
b.setChannelMuted(uint32(i%3), i%2 == 0)
|
||||||
|
_ = b.isChannelMuted(uint32((i + 1) % 3))
|
||||||
|
}(i)
|
||||||
|
}
|
||||||
|
wg.Wait()
|
||||||
|
}
|
||||||
|
|
||||||
func TestPublicTextMessageTargetsChannelIDsAndTrees(t *testing.T) {
|
func TestPublicTextMessageTargetsChannelIDsAndTrees(t *testing.T) {
|
||||||
root := &gumble.Channel{ID: 1, Name: "Root"}
|
root := &gumble.Channel{ID: 1, Name: "Root"}
|
||||||
current := &gumble.Channel{ID: 2, Name: "Room", Parent: root}
|
current := &gumble.Channel{ID: 2, Name: "Room", Parent: root}
|
||||||
|
|||||||
@@ -334,7 +334,7 @@ func (b *Barnard) setTransmit(ui *uiterm.Ui, val int) {
|
|||||||
b.Notify("error", "me", "no tx while disconnected")
|
b.Notify("error", "me", "no tx while disconnected")
|
||||||
b.Tx = false
|
b.Tx = false
|
||||||
b.UpdateGeneralStatus("no tx while disconnected", true)
|
b.UpdateGeneralStatus("no tx while disconnected", true)
|
||||||
} else if b.MutedChannels[b.Client.Self.Channel.ID] {
|
} else if b.isChannelMuted(b.Client.Self.Channel.ID) {
|
||||||
// Check if current channel is muted
|
// Check if current channel is muted
|
||||||
b.Notify("error", "me", "cannot transmit in muted channel")
|
b.Notify("error", "me", "cannot transmit in muted channel")
|
||||||
b.Tx = false
|
b.Tx = false
|
||||||
|
|||||||
+1
-1
@@ -131,7 +131,7 @@ func (b *Barnard) TreeItemBuild(item uiterm.TreeItem) []uiterm.TreeItem {
|
|||||||
})
|
})
|
||||||
for _, subchannel := range cl {
|
for _, subchannel := range cl {
|
||||||
displayName := subchannel.Name
|
displayName := subchannel.Name
|
||||||
if b.MutedChannels[subchannel.ID] {
|
if b.isChannelMuted(subchannel.ID) {
|
||||||
displayName = "[MUTED] #" + displayName
|
displayName = "[MUTED] #" + displayName
|
||||||
} else {
|
} else {
|
||||||
displayName = "#" + displayName
|
displayName = "#" + displayName
|
||||||
|
|||||||
Reference in New Issue
Block a user