-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
MM-56501 Enable/disable automuting when a user connects/disconnects #479
Merged
hmhealey
merged 15 commits into
main
from
MM-56501_automute-part-2_user-connect-disconnect
Feb 20, 2024
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
01a8567
MM-56501 Enable/disable automuting when a user connects/disconnects
hmhealey 532c041
Fix style check
hmhealey 7a31937
Explicitly check if preference isn't found with sql.ErrNoRows
hmhealey 7a5fb85
Address feedback
hmhealey 9a02045
Revert "Explicitly check if preference isn't found with sql.ErrNoRows"
hmhealey ce913b0
Have AutomuteAPIMock call hooks for join channel/change preferences
hmhealey 8fd4364
Have AutomuteAPIMock call hooks when creating a channel
hmhealey e1bd9ec
MM-56498 Handle when a user with automute enabled joins a channel
hmhealey bcef6b2
MM-56499 Handle when a user with automute enabled created a DM/GM
hmhealey e5d6971
MM-56500 Handle automuting for when a user is linked/unlinked
hmhealey c475925
Change updateAutomutingOnUserJoinedChannel to use canAutomuteChannelID
hmhealey 7887484
Address feedback
hmhealey 6a71fc9
Change mock to return an error wrapping sql.ErrNoRows in another case
hmhealey 4d2f4af
Undo error changes
hmhealey 39fef97
Add mocks to newly added tests
hmhealey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/mattermost/mattermost/server/public/model" | ||
"github.com/mattermost/mattermost/server/public/plugin" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
func (p *Plugin) UserHasJoinedChannel(c *plugin.Context, channelMember *model.ChannelMember, actor *model.User) { | ||
_, _ = p.updateAutomutingOnUserJoinedChannel(c, channelMember.UserId, channelMember.ChannelId) | ||
} | ||
|
||
func (p *Plugin) updateAutomutingOnUserJoinedChannel(c *plugin.Context, userID string, channelID string) (bool, error) { | ||
if automuteEnabled := p.getAutomuteIsEnabledForUser(userID); !automuteEnabled { | ||
return false, nil | ||
} | ||
|
||
if canAutomute, err := p.canAutomuteChannelID(channelID); err != nil { | ||
p.API.LogError( | ||
"Unable to check if channel is linked to update automuting when a user has joined the channel", | ||
"user_id", userID, | ||
"channel_id", channelID, | ||
"error", err.Error(), | ||
) | ||
return false, errors.Wrap(err, "Unable to update automuting when a user has joined a channel") | ||
} else if !canAutomute { | ||
// Only automute channels that are linked | ||
return false, nil | ||
} | ||
|
||
err := p.setChannelMembersAutomuted([]*model.ChannelMemberIdentifier{{UserId: userID, ChannelId: channelID}}, true) | ||
return err == nil, err | ||
} | ||
|
||
func (p *Plugin) ChannelHasBeenCreated(c *plugin.Context, channel *model.Channel) { | ||
_ = p.updateAutomutingOnChannelCreated(channel) | ||
} | ||
|
||
func (p *Plugin) updateAutomutingOnChannelCreated(channel *model.Channel) error { | ||
if !channel.IsGroupOrDirect() { | ||
// Assume that newly created channels can never be linked by the time this is called | ||
return nil | ||
} | ||
|
||
return p.updateAutomutingForChannelMembers(channel.Id, true) | ||
} | ||
|
||
func (p *Plugin) updateAutomutingOnChannelLinked(channelID string) error { | ||
// This simply mutes the channel for all users with automuting enabled, regardless of their settings before. It | ||
// doesn't pay attention to if the user manually muted the channel beforehand. | ||
return p.updateAutomutingForChannelMembers(channelID, true) | ||
} | ||
|
||
func (p *Plugin) updateAutomutingOnChannelUnlinked(channelID string) error { | ||
// This simply unmutes the channel for all users with automuting enabled, regardless of their settings before. It | ||
// doesn't pay attention to if the user manually muted the channel beforehand to keep it muted. | ||
return p.updateAutomutingForChannelMembers(channelID, false) | ||
} | ||
|
||
func (p *Plugin) updateAutomutingForChannelMembers(channelID string, enableAutomute bool) error { | ||
var membersToUpdate []*model.ChannelMemberIdentifier | ||
|
||
page := 0 | ||
perPage := 200 | ||
for { | ||
members, appErr := p.API.GetChannelMembers(channelID, page, perPage) | ||
if appErr != nil { | ||
return errors.Wrap(appErr, fmt.Sprintf("Unable to get all members of channel %s to update automuting", channelID)) | ||
} | ||
|
||
for _, member := range members { | ||
if p.getAutomuteIsEnabledForUser(member.UserId) { | ||
membersToUpdate = append(membersToUpdate, &model.ChannelMemberIdentifier{ChannelId: channelID, UserId: member.UserId}) | ||
} | ||
} | ||
|
||
if len(members) < perPage { | ||
break | ||
} | ||
|
||
page += 1 | ||
} | ||
|
||
if len(membersToUpdate) > 0 { | ||
if err := p.setChannelMembersAutomuted(membersToUpdate, enableAutomute); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we log
LogWarn
any error that's returned here to avoid burying it?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code is actually a bit misleading since we do log any errors inside
updateAutomutingOnUserConnect
. I wanted the method to log its own errors to keep the automute logic more self-contained, but I still wanted the method to return errors because that makes testing easier