Skip to content
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

Closed
wants to merge 7 commits into from
Closed

GH-42 #81

wants to merge 7 commits into from

Conversation

CalebMaftei
Copy link

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

@CalebMaftei CalebMaftei requested a review from jfrerich as a code owner June 29, 2021 21:02
@mattermod
Copy link
Contributor

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.

@hanzei hanzei added the 2: Dev Review Requires review by a core committer label Jun 30, 2021
@hanzei hanzei self-requested a review June 30, 2021 10:49
@CalebMaftei
Copy link
Author

Issue #42 looks to be completed! Disregard this pull request.

@CalebMaftei CalebMaftei closed this Jul 6, 2021
@hanzei
Copy link
Contributor

hanzei commented Jul 7, 2021

@CalebMaftei I don't see a PR that addresses #42. Could you please point me to that one?

@CalebMaftei CalebMaftei reopened this Jul 7, 2021
@hanzei hanzei added this to the v1.3.0 milestone Jul 16, 2021
Copy link
Contributor

@hanzei hanzei left a 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.

README.md Outdated Show resolved Hide resolved
server/command.go Outdated Show resolved Hide resolved
server/command.go Outdated Show resolved Hide resolved
server/command.go Outdated Show resolved Hide resolved
server/command.go Outdated Show resolved Hide resolved
server/hooks.go Outdated
Comment on lines 41 to 63
// 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)
Copy link
Contributor

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?

Copy link
Author

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!

Copy link
Author

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 Show resolved Hide resolved
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"
Copy link
Contributor

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?

Copy link
Author

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.

Copy link
Contributor

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"
Copy link
Contributor

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?

Copy link
Author

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.

Copy link
Contributor

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
Comment on lines 130 to 134
if actor.Id == channelMember.UserId {
time.Sleep(3 * time.Second)
} else {
time.Sleep(15 * time.Second)
}
Copy link
Contributor

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?

Copy link
Author

@CalebMaftei CalebMaftei Jul 16, 2021

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.

Copy link
Author

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?

@mattermod
Copy link
Contributor

This PR has been automatically labelled "stale" because it hasn't had recent activity.
A core team member will check in on the status of the PR to help with questions.
Thank you for your contribution!

/cc @jasonblais @jfrerich @emilyacook

Copy link
Contributor

@jfrerich jfrerich left a 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.

commandTriggerDeleteTeamWelcome = "delete_team_welcome"

// Error Message Constants
unsetMessageError = "welcome message has not been set yet"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
unsetMessageError = "welcome message has not been set yet"
unsetMessageError = "welcome message has not been set"

Copy link
Author

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

Comment on lines 122 to 123
if len(parameters) > 0 {
return "`delete_channel_welcome` command does not accept any extra parameters"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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?

Copy link
Author

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// 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

Copy link
Author

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"
Copy link
Contributor

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.

isTeamAdmin, teamAdminError := p.hasTeamAdminRole(userID, teamID)

if sysAdminError != nil {
p.postCommandResponse(args, "error occurred while getting the System Admin Role `%s`: `%s`", teamID, sysAdminError)
Copy link
Contributor

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.

Copy link
Author

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

return true
}

// This retrives a map of team Ids with their respective welcome message
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// This retrives a map of team Ids with their respective welcome message
// This retrieves a map of team Ids with their respective welcome message

Copy link
Author

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

return
}

p.postCommandResponse(args, "stored the welcome message:\n%s", message)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
p.postCommandResponse(args, "stored the welcome message:\n%s", message)
p.postCommandResponse(args, "stored the team welcome message:\n%s", message)

Copy link
Author

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!

return
}

p.postCommandResponse(args, "welcome message has been deleted")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
p.postCommandResponse(args, "welcome message has been deleted")
p.postCommandResponse(args, "team welcome message has been deleted")

Copy link
Author

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

p.postCommandResponse(args, string(data))
}

func (p *Plugin) checkCommandPermission(args *model.CommandArgs) bool {
Copy link
Contributor

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

Suggested change
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.

Copy link
Author

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! :)

server/command.go Outdated Show resolved Hide resolved

validPrivileges := p.validatePreviewPrivileges(teamID, args)
if !validPrivileges {
return
Copy link
Contributor

@hanzei hanzei Aug 16, 2021

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)

Copy link
Contributor

@hanzei hanzei left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@CalebMaftei
Copy link
Author

CalebMaftei commented Aug 17, 2021

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.

@mattermod
Copy link
Contributor

This PR has been automatically labelled "stale" because it hasn't had recent activity.
A core team member will check in on the status of the PR to help with questions.
Thank you for your contribution!

/cc @jasonblais @jfrerich @emilyacook

@hanzei hanzei self-requested a review August 28, 2021 05:39
Comment on lines +201 to +210
<!-- ## 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. -->
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<!-- ## 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"
Copy link
Contributor

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.

Comment on lines +114 to +127
// 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)
Copy link
Contributor

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?

@hanzei hanzei added the Awaiting Submitter Action Blocked on the author label Sep 21, 2021
@raghavaggarwal2308
Copy link

Closing because of #143

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2: Dev Review Requires review by a core committer Awaiting Submitter Action Blocked on the author Lifecycle/1:stale
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Ability to configure outside of the main config.json?
7 participants