Skip to content

Commit

Permalink
Adds demonstration of reaction hooks (#123)
Browse files Browse the repository at this point in the history
  • Loading branch information
shieldsjared authored Oct 31, 2020
1 parent 5f04050 commit b5c592e
Show file tree
Hide file tree
Showing 16 changed files with 634 additions and 127 deletions.
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@ require (
github.com/mattermost/mattermost-server/v5 v5.25.1
github.com/mholt/archiver/v3 v3.3.0
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.5.1
github.com/stretchr/testify v1.6.1
)

replace github.com/mattermost/mattermost-server/v5 v5.25.1 => github.com/shieldsjared/mattermost-server/v5 v5.0.0-20201008141002-56ee0d27d54b
380 changes: 336 additions & 44 deletions go.sum

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion server/activate_hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/mattermost/mattermost-plugin-api/cluster"
)

const minimumServerVersion = "5.12.0"
const minimumServerVersion = "5.30.0"

func (p *Plugin) checkServerVersion() error {
serverVersion, err := semver.Parse(p.API.GetServerVersion())
Expand Down
30 changes: 23 additions & 7 deletions server/channel_hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (p *Plugin) ChannelHasBeenCreated(c *plugin.Context, channel *model.Channel
msg := fmt.Sprintf("ChannelHasBeenCreated: ~%s", channel.Name)
if err := p.postPluginMessage(channel.TeamId, msg); err != nil {
p.API.LogError(
"failed to post ChannelHasBeenCreated message",
"Failed to post ChannelHasBeenCreated message",
"channel_id", channel.Id,
"error", err.Error(),
)
Expand All @@ -40,20 +40,28 @@ func (p *Plugin) UserHasJoinedChannel(c *plugin.Context, channelMember *model.Ch

user, err := p.API.GetUser(channelMember.UserId)
if err != nil {
p.API.LogError("failed to query user", "user_id", channelMember.UserId)
p.API.LogError(
"Failed to query user",
"user_id", channelMember.UserId,
"error", err.Error(),
)
return
}

channel, err := p.API.GetChannel(channelMember.ChannelId)
if err != nil {
p.API.LogError("failed to query channel", "channel_id", channelMember.ChannelId)
p.API.LogError(
"Failed to query channel",
"channel_id", channelMember.ChannelId,
"error", err.Error(),
)
return
}

msg := fmt.Sprintf("UserHasJoinedChannel: @%s, ~%s", user.Username, channel.Name)
if err := p.postPluginMessage(channel.TeamId, msg); err != nil {
p.API.LogError(
"failed to post UserHasJoinedChannel message",
"Failed to post UserHasJoinedChannel message",
"user_id", channelMember.UserId,
"error", err.Error(),
)
Expand All @@ -74,20 +82,28 @@ func (p *Plugin) UserHasLeftChannel(c *plugin.Context, channelMember *model.Chan

user, err := p.API.GetUser(channelMember.UserId)
if err != nil {
p.API.LogError("failed to query user", "user_id", channelMember.UserId)
p.API.LogError(
"Failed to query user",
"user_id", channelMember.UserId,
"error", err.Error(),
)
return
}

channel, err := p.API.GetChannel(channelMember.ChannelId)
if err != nil {
p.API.LogError("failed to query channel", "channel_id", channelMember.ChannelId)
p.API.LogError(
"Failed to query channel",
"channel_id", channelMember.ChannelId,
"error", err.Error(),
)
return
}

msg := fmt.Sprintf("UserHasLeftChannel: @%s, ~%s", user.Username, channel.Name)
if err := p.postPluginMessage(channel.TeamId, msg); err != nil {
p.API.LogError(
"failed to post UserHasLeftChannel message",
"Failed to post UserHasLeftChannel message",
"user_id", channelMember.UserId,
"error", err.Error(),
)
Expand Down
8 changes: 4 additions & 4 deletions server/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func (p *Plugin) diffConfiguration(newConfiguration *configuration) {

teams, err := p.API.GetTeams()
if err != nil {
p.API.LogWarn("failed to query teams OnConfigChange", "err", err)
p.API.LogWarn("Failed to query teams OnConfigChange", "err", err)
return
}

Expand All @@ -166,13 +166,13 @@ func (p *Plugin) diffConfiguration(newConfiguration *configuration) {

newConfigurationData, jsonErr := json.Marshal(newConfiguration)
if jsonErr != nil {
p.API.LogWarn("failed to marshal new configuration", "err", err)
p.API.LogWarn("Failed to marshal new configuration", "err", err)
return
}

fileInfo, err := p.API.UploadFile(newConfigurationData, demoChannelID, "configuration.json")
if err != nil {
p.API.LogWarn("failed to attach new configuration", "err", err)
p.API.LogWarn("Failed to attach new configuration", "err", err)
return
}

Expand All @@ -184,7 +184,7 @@ func (p *Plugin) diffConfiguration(newConfiguration *configuration) {
Props: configurationDiff,
FileIds: model.StringArray{fileInfo.Id},
}); err != nil {
p.API.LogWarn("failed to post OnConfigChange message", "err", err)
p.API.LogWarn("Failed to post OnConfigChange message", "err", err)
return
}
}
Expand Down
4 changes: 2 additions & 2 deletions server/file_hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func (p *Plugin) FileWillBeUploaded(c *plugin.Context, fileInfo *model.FileInfo,
teams, err := p.API.GetTeams()
if err != nil {
p.API.LogError(
"failed to query teams FileWillBeUploaded",
"Failed to query teams FileWillBeUploaded",
"error", err.Error(),
)
return
Expand All @@ -40,7 +40,7 @@ func (p *Plugin) FileWillBeUploaded(c *plugin.Context, fileInfo *model.FileInfo,
msg := fmt.Sprintf("FileName @%s has been created in", fileInfo.Name)
if err := p.postPluginMessage(team.Id, msg); err != nil {
p.API.LogError(
"failed to post FileWillBeUploaded message",
"Failed to post FileWillBeUploaded message",
"channel_id", configuration.demoChannelIDs[team.Id],
"error", err.Error(),
)
Expand Down
30 changes: 15 additions & 15 deletions server/http_hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,28 +56,28 @@ func (p *Plugin) handleStatus(w http.ResponseWriter, r *http.Request) {

w.Header().Set("Content-Type", "application/json")
if _, err := w.Write(responseJSON); err != nil {
p.API.LogError("failed to write status", "err", err.Error())
p.API.LogError("Failed to write status", "err", err.Error())
}
}

func (p *Plugin) handleHello(w http.ResponseWriter, r *http.Request) {
if _, err := w.Write([]byte("Hello World!")); err != nil {
p.API.LogError("failed to write hello world", "err", err.Error())
p.API.LogError("Failed to write hello world", "err", err.Error())
}
}

func (p *Plugin) handleDialog1(w http.ResponseWriter, r *http.Request) {
request := model.SubmitDialogRequestFromJson(r.Body)
if request == nil {
p.API.LogError("failed to decode SubmitDialogRequest")
p.API.LogError("Failed to decode SubmitDialogRequest")
w.WriteHeader(http.StatusBadRequest)
return
}

if !request.Cancelled {
number, ok := request.Submission[dialogElementNameNumber].(float64)
if !ok {
p.API.LogError("request is missing field", "field", dialogElementNameNumber)
p.API.LogError("Request is missing field", "field", dialogElementNameNumber)
w.WriteHeader(http.StatusOK)
return
}
Expand All @@ -95,7 +95,7 @@ func (p *Plugin) handleDialog1(w http.ResponseWriter, r *http.Request) {

user, appErr := p.API.GetUser(request.UserId)
if appErr != nil {
p.API.LogError("failed to get user for dialog", "err", appErr.Error())
p.API.LogError("Failed to get user for dialog", "err", appErr.Error())
w.WriteHeader(http.StatusOK)
return
}
Expand All @@ -111,7 +111,7 @@ func (p *Plugin) handleDialog1(w http.ResponseWriter, r *http.Request) {
Message: fmt.Sprintf(msg, user.Username),
})
if appErr != nil {
p.API.LogError("failed to post handleDialog1 message", "err", appErr.Error())
p.API.LogError("Failed to post handleDialog1 message", "err", appErr.Error())
return
}

Expand All @@ -127,7 +127,7 @@ func (p *Plugin) handleDialog1(w http.ResponseWriter, r *http.Request) {
Type: "custom_demo_plugin",
Props: request.Submission,
}); appErr != nil {
p.API.LogError("failed to post handleDialog1 message", "err", appErr.Error())
p.API.LogError("Failed to post handleDialog1 message", "err", appErr.Error())
return
}
}
Expand All @@ -138,14 +138,14 @@ func (p *Plugin) handleDialog1(w http.ResponseWriter, r *http.Request) {
func (p *Plugin) handleDialog2(w http.ResponseWriter, r *http.Request) {
request := model.SubmitDialogRequestFromJson(r.Body)
if request == nil {
p.API.LogError("failed to decode SubmitDialogRequest")
p.API.LogError("Failed to decode SubmitDialogRequest")
w.WriteHeader(http.StatusBadRequest)
return
}

user, appErr := p.API.GetUser(request.UserId)
if appErr != nil {
p.API.LogError("failed to get user for dialog", "err", appErr.Error())
p.API.LogError("Failed to get user for dialog", "err", appErr.Error())
w.WriteHeader(http.StatusOK)
return
}
Expand All @@ -160,7 +160,7 @@ func (p *Plugin) handleDialog2(w http.ResponseWriter, r *http.Request) {
ChannelId: request.ChannelId,
Message: fmt.Sprintf("@%v confirmed an Interactive Dialog %v", user.Username, suffix),
}); appErr != nil {
p.API.LogError("failed to post handleDialog2 message", "err", appErr.Error())
p.API.LogError("Failed to post handleDialog2 message", "err", appErr.Error())
return
}

Expand Down Expand Up @@ -241,14 +241,14 @@ func (p *Plugin) handleInteractiveAction(w http.ResponseWriter, r *http.Request)

user, appErr := p.API.GetUser(request.UserId)
if appErr != nil {
p.API.LogError("failed to get user for interactive action", "err", appErr.Error())
p.API.LogError("Failed to get user for interactive action", "err", appErr.Error())
w.WriteHeader(http.StatusInternalServerError)
return
}

post, postErr := p.API.GetPost(request.PostId)
if postErr != nil {
p.API.LogError("failed to get post for interactive action", "err", postErr.Error())
p.API.LogError("Failed to get post for interactive action", "err", postErr.Error())
w.WriteHeader(http.StatusInternalServerError)
return
}
Expand All @@ -259,7 +259,7 @@ func (p *Plugin) handleInteractiveAction(w http.ResponseWriter, r *http.Request)

requestJSON, jsonErr := json.MarshalIndent(request, "", " ")
if jsonErr != nil {
p.API.LogError("failed to marshal json for interactive action", "err", jsonErr.Error())
p.API.LogError("Failed to marshal json for interactive action", "err", jsonErr.Error())
w.WriteHeader(http.StatusInternalServerError)
return
}
Expand All @@ -271,7 +271,7 @@ func (p *Plugin) handleInteractiveAction(w http.ResponseWriter, r *http.Request)
RootId: rootID,
Message: fmt.Sprintf(msg, user.Username, string(requestJSON)),
}); appErr != nil {
p.API.LogError("failed to post handleInteractiveAction message", "err", appErr.Error())
p.API.LogError("Failed to post handleInteractiveAction message", "err", appErr.Error())
w.WriteHeader(http.StatusInternalServerError)
return
}
Expand All @@ -285,7 +285,7 @@ func (p *Plugin) writeSubmitDialogResponse(w http.ResponseWriter, response *mode
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
if _, err := w.Write(response.ToJson()); err != nil {
p.API.LogError("failed to write DialogResponse", "err", err.Error())
p.API.LogError("Failed to write DialogResponse", "err", err.Error())
}
}

Expand Down
2 changes: 1 addition & 1 deletion server/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func (p *Plugin) BackgroundJob() {
})
if err != nil {
p.API.LogError(
"failed to post BackgroundJob message",
"Failed to post BackgroundJob message",
"channel_id", channelID,
"error", err.Error(),
)
Expand Down
4 changes: 2 additions & 2 deletions server/login_hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (p *Plugin) UserHasLoggedIn(c *plugin.Context, user *model.User) {
teams, err := p.API.GetTeams()
if err != nil {
p.API.LogError(
"failed to query teams UserHasLoggedIn",
"Failed to query teams UserHasLoggedIn",
"error", err.Error(),
)
return
Expand All @@ -40,7 +40,7 @@ func (p *Plugin) UserHasLoggedIn(c *plugin.Context, user *model.User) {
msg := fmt.Sprintf("User @%s has logged in", user.Username)
if err := p.postPluginMessage(team.Id, msg); err != nil {
p.API.LogError(
"failed to post UserHasLoggedIn message",
"Failed to post UserHasLoggedIn message",
"channel_id", configuration.demoChannelIDs[team.Id],
"error", err.Error(),
)
Expand Down
Loading

0 comments on commit b5c592e

Please sign in to comment.