-
Notifications
You must be signed in to change notification settings - Fork 41
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
GH-42 #81
GH-42 #81
Conversation
Hello @CalebMaftei, Thanks for your pull request! A Core Committer will review your pull request soon. For code contributions, you can learn more about the review process here. |
Issue #42 looks to be completed! Disregard this pull request. |
@CalebMaftei I don't see a PR that addresses #42. Could you please point me to that one? |
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.
That is great PR. The code looks quite good 👍
Thanks also for adding this more granular permission checks 👍
One piece of feedback: It's idiomatic in go to use err
as the variable name returned for errors. There are surely places where it's not needed, but you should stick with that guideline more.
server/hooks.go
Outdated
// We send a DM and an opportunistic ephemeral message to the channel. See | ||
// the discussion at the link below for more details: | ||
// https://github.com/mattermost/mattermost-plugin-welcomebot/pull/31#issuecomment-611691023 | ||
teamAddMessage := "# Welcome to the " + data.Team.DisplayName + " team!! \n\n" | ||
postDM := &model.Post{ | ||
UserId: p.botUserID, | ||
ChannelId: data.DirectMessage.Id, | ||
Message: teamAddMessage + string(teamMessage), | ||
} | ||
if _, appErr := p.API.CreatePost(postDM); appErr != nil { | ||
mlog.Error("failed to post welcome message to the channel", | ||
mlog.String("channelId", data.DirectMessage.Id), | ||
mlog.Err(appErr), | ||
) | ||
} | ||
|
||
postChannel := &model.Post{ | ||
UserId: p.botUserID, | ||
ChannelId: teamMember.TeamId, | ||
Message: string(teamMessage), | ||
} | ||
time.Sleep(1 * time.Second) | ||
_ = p.API.SendEphemeralPost(teamMember.UserId, postChannel) |
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.
There is a lot of duplicate code here. Can this be avoided?
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.
I can go through this piece and refactor it to be less complicated!
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.
I removed the ephemeral message. I believe we originally had it so that the user would immediately be greeted by the welcome message, however the DM with the welcome-bot is plenty sufficient.
server/hooks.go
Outdated
// We send a DM and an opportunistic ephemeral message to the channel. See | ||
// the discussion at the link below for more details: | ||
// https://github.com/mattermost/mattermost-plugin-welcomebot/pull/31#issuecomment-611691023 | ||
teamAddMessage := "# Welcome to the " + data.Team.DisplayName + " team!! \n\n" |
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.
Why is this default message needed?
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 change I believe was added to give some kind of header to the welcome bot messages. However, if this ultimately feels like we are stripping freedoms to the welcome message, it can be thrown out.
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.
I'm 1/5 we should leave the complete message to the message creator and not prefix the header.
@@ -68,10 +107,12 @@ func (p *Plugin) UserHasJoinedChannel(c *plugin.Context, channelMember *model.Ch | |||
// We send a DM and an opportunistic ephemeral message to the channel. See | |||
// the discussion at the link below for more details: | |||
// https://github.com/mattermost/mattermost-plugin-welcomebot/pull/31#issuecomment-611691023 | |||
channelAddMessage := "# Welcome to the " + channelInfo.DisplayName + " channel. \n\n" |
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.
See above. Why is this behavior change added here?
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 change I believe was added to give some kind of header to the welcome bot messages. However, if this ultimately feels like we are stripping freedoms to the welcome message, it can be thrown out.
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.
Yes, please throw it out.
server/hooks.go
Outdated
if actor.Id == channelMember.UserId { | ||
time.Sleep(3 * time.Second) | ||
} else { | ||
time.Sleep(15 * time.Second) | ||
} |
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.
What is the reasoning behind this change?
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 was added by another Dev of ours, I believe. However, I think it was due to a cluster related timing issue. I can look more into this and why it was added.
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.
So digging into this deeper, the reason this was added was to prevent a race condition between the posted ephemeral message as well as the actual welcome message. The reasoning from our Dev was: "there was some race condition involving the welcome message being sent in a specific case. I think in one instance of a user being added to a channel, the ephemeral message was sent before the user was added to the channel, so they could never see it."
However, I think this can easily be mitigated by removing the ephemeral message as this was the issue with redundancy found inside of the team welcome messages as well. What are your thoughts?
This PR has been automatically labelled "stale" because it hasn't had recent activity. |
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.
Thanks for this PR, @CalebMaftei! I have some small suggestions on top of the request from Ben.
server/command.go
Outdated
commandTriggerDeleteTeamWelcome = "delete_team_welcome" | ||
|
||
// Error Message Constants | ||
unsetMessageError = "welcome message has not been set yet" |
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.
unsetMessageError = "welcome message has not been set yet" | |
unsetMessageError = "welcome message has not been set" |
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.
The next push for this PR will have this change :)
server/command.go
Outdated
if len(parameters) > 0 { | ||
return "`delete_channel_welcome` command does not accept any extra parameters" |
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.
if len(parameters) > 0 { | |
return "`delete_channel_welcome` command does not accept any extra parameters" | |
if len(parameters) > 0 { | |
return "`" + commandTriggerDeleteChannelWelcome + "` command does not accept any extra parameters" |
Would you mind changing the channel set, get, and delete return messages to use their respective const variables like you have done for the teams?
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.
The next push for this PR will have this change :)
I did the same for set and get channel welcomes as well.
server/hooks.go
Outdated
} | ||
|
||
if teamMessage == nil { | ||
// No dynamic welcome message for the given team, so we check if one as been set in the config.json |
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.
// No dynamic welcome message for the given team, so we check if one as been set in the config.json | |
// No dynamic welcome message for the given team, so we check if one has been set in the config.json |
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.
The next push for this PR will have this change :)
server/hooks.go
Outdated
// We send a DM and an opportunistic ephemeral message to the channel. See | ||
// the discussion at the link below for more details: | ||
// https://github.com/mattermost/mattermost-plugin-welcomebot/pull/31#issuecomment-611691023 | ||
teamAddMessage := "# Welcome to the " + data.Team.DisplayName + " team!! \n\n" |
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.
I'm 1/5 we should leave the complete message to the message creator and not prefix the header.
server/command.go
Outdated
isTeamAdmin, teamAdminError := p.hasTeamAdminRole(userID, teamID) | ||
|
||
if sysAdminError != nil { | ||
p.postCommandResponse(args, "error occurred while getting the System Admin Role `%s`: `%s`", teamID, sysAdminError) |
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.
hasSysadminRole()
does not accept the teamID, so I think we can drop the teamID from the error message.
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.
I removed the team message header, as well as removed the unnecessary teamID for the hasSystemadminRole()
function. Those changes should be in with the next push to this PR :)
server/command.go
Outdated
return true | ||
} | ||
|
||
// This retrives a map of team Ids with their respective welcome message |
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 retrives a map of team Ids with their respective welcome message | |
// This retrieves a map of team Ids with their respective welcome message |
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.
The next push to this PR will have this spelling change :)
server/command.go
Outdated
return | ||
} | ||
|
||
p.postCommandResponse(args, "stored the welcome message:\n%s", message) |
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.
p.postCommandResponse(args, "stored the welcome message:\n%s", message) | |
p.postCommandResponse(args, "stored the team welcome message:\n%s", message) |
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 change as well as other places in code that didn't distinguish channel from team welcome messages has been updated!
server/command.go
Outdated
return | ||
} | ||
|
||
p.postCommandResponse(args, "welcome message has been deleted") |
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.
p.postCommandResponse(args, "welcome message has been deleted") | |
p.postCommandResponse(args, "team welcome message has been deleted") |
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 change will be in the next push for the PR :)
server/command.go
Outdated
p.postCommandResponse(args, string(data)) | ||
} | ||
|
||
func (p *Plugin) checkCommandPermission(args *model.CommandArgs) bool { |
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.
I'm inclined to change the signature to
func (p *Plugin) checkCommandPermission(args *model.CommandArgs) bool { | |
func (p *Plugin) checkCommandPermission(args *model.CommandArgs) (bool, err) { |
as it's more idiomatic to return an error instead of handling it in the function directly.
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.
New PR Will contain the signature fix, as well as the commented out section for the town square check!
I also fixed the signature of the validatePreviewPriviledges
.
Those changes should be seen with the next push to this PR! :)
|
||
validPrivileges := p.validatePreviewPrivileges(teamID, args) | ||
if !validPrivileges { | ||
return |
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.
Similar as in a comment above I think the signature should be (bool, error)
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.
Thanks for updating the PR. https://github.com/mattermost/mattermost-plugin-welcomebot/pull/81/files#r673361233 seems unresolved to me.
Sorry about that. I thought that got pushed up. I will commit and push that change now. It is commented out currently. If you would like, I can remove it all together as well. |
This PR has been automatically labelled "stale" because it hasn't had recent activity. |
<!-- ## How to Dynamically Assign Team Messages | ||
|
||
This plugin also allows system admins and team admins to change the team message of an appropriate team through a slash command, rather than adjusting the config.json. | ||
|
||
We have provided three options that are formatted identically to the channel welcomes: | ||
- `/welcomebot get_team_welcome` - prints the current team's welcome message if either the dynamic message exists, or the config.json message exists. | ||
- `/welcomebot set_team_welcome [welcome message]` - sets the current team's welcome message to the one defined in the slash command. | ||
- `/welcomebot delete_team_welcome` - deletes the current team's welcome message. This however does NOT delete any messages set inside of the config.json. | ||
|
||
For saving these messages, they are stored as KV pairs. Team message pairs have the prefix `teammsg_<team_id>` to distinguish them from the `chanmsg_<channel_id>` pairs. --> |
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.
<!-- ## How to Dynamically Assign Team Messages | |
This plugin also allows system admins and team admins to change the team message of an appropriate team through a slash command, rather than adjusting the config.json. | |
We have provided three options that are formatted identically to the channel welcomes: | |
- `/welcomebot get_team_welcome` - prints the current team's welcome message if either the dynamic message exists, or the config.json message exists. | |
- `/welcomebot set_team_welcome [welcome message]` - sets the current team's welcome message to the one defined in the slash command. | |
- `/welcomebot delete_team_welcome` - deletes the current team's welcome message. This however does NOT delete any messages set inside of the config.json. | |
For saving these messages, they are stored as KV pairs. Team message pairs have the prefix `teammsg_<team_id>` to distinguish them from the `chanmsg_<channel_id>` pairs. --> | |
## How to Dynamically Assign Team Messages | |
This plugin also allows system admins and team admins to change the team message of an appropriate team through a slash command, rather than adjusting the config.json. | |
We have provided three options that are formatted identically to the channel welcomes: | |
- `/welcomebot get_team_welcome` - prints the current team's welcome message if either the dynamic message exists, or the config.json message exists. | |
- `/welcomebot set_team_welcome [welcome message]` - sets the current team's welcome message to the one defined in the slash command. | |
- `/welcomebot delete_team_welcome` - deletes the current team's welcome message. This however does NOT delete any messages set inside of the config.json. | |
@@ -68,10 +107,12 @@ func (p *Plugin) UserHasJoinedChannel(c *plugin.Context, channelMember *model.Ch | |||
// We send a DM and an opportunistic ephemeral message to the channel. See | |||
// the discussion at the link below for more details: | |||
// https://github.com/mattermost/mattermost-plugin-welcomebot/pull/31#issuecomment-611691023 | |||
channelAddMessage := "# Welcome to the " + channelInfo.DisplayName + " channel. \n\n" |
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.
Yes, please throw it out.
// Commented out in case we do not want the user immediately greeted with an ephemeral message | ||
// postChannel := &model.Post{ | ||
// UserId: p.botUserID, | ||
// ChannelId: channelMember.ChannelId, | ||
// Message: string(data), | ||
// } | ||
|
||
// if actor.Id == channelMember.UserId { | ||
// time.Sleep(3 * time.Second) | ||
// } else { | ||
// time.Sleep(15 * time.Second) | ||
// } | ||
|
||
// _ = p.API.SendEphemeralPost(channelMember.UserId, postChannel) |
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.
Why does this PR touch this code? Is it related to the changes you are doing here?
Closing because of #143 |
Summary
This Pull Request adds new functionality that allows a team admin or system admin to adjust a dynamic team-welcome message. Previously, the only way a team message could be set was to adjust the config.json, and then redeploy the instance of mattermost. This has caused certain team admins over at Platform 1 to seek out a resolution that allows for the dynamic ability to set their team welcome message without adjusting the config.json.
With this new functionality, we have added 3 new slash commands that allow a user with the appropriate credentials to set, get, and delete what we are calling
dynamic team messages
with the following commands:get_team_welcome
- Grabs the stored dynamic team message (if it exists), or retrieves the config.json message set.set_team_welcome [new message]
- Stores internally a new KV pair where the key is the team id joined with the prefix: "teammsg_", and where the value is the message. This does not override the config.json value though.delete_team_welcome
- This deletes the KV pair set for the team without ever touching the config.json message (if it exists).If a dynamic team message is found, then the get call will retrieve that first. If a user ever wanted to resort back to the config.json message, they will just need to delete the dynamic message. All values will defer to the config.json message if no dynamic message is found.
This new functionality also allows appropriate team admins to have access to the above slash commands, rather than permitting only system admins.
Also, on line 115, I fixed a misspelling of the word 'welcome.'
Please let me know if this is the direction you would like to pursue regarding the solution to issue 42! If there is anything that I left out of this PR, I would be more than happy to add that necessary information! Thank you!
Ticket Link
Fixes #42