generated from mattermost/mattermost-plugin-starter-template
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement autorespond feature (#188)
* Implement autorespond feature * Better handling for cast errors * Add tests * Fix indentation * Add empty setting for auto respond message instructions * Use read-only setting, not empty setting * Fix formatting * Address requested changes * Fix naming * Add interactive dialog * Fix naming * Remove calls to GetSetting, add FalseSkip, fix error message * Change naming, add logging, fix test code * Rename API route * Fix API path variable * Use default message as dialog placeholder * Fix diff * Change dialog title * Fix merge conflict
- Loading branch information
1 parent
5e9b8e3
commit b8aff5a
Showing
18 changed files
with
556 additions
and
13 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Copyright (c) 2019-present Mattermost, Inc. All Rights Reserved. | ||
// See License for license information. | ||
|
||
package api | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
|
||
"github.com/mattermost/mattermost-server/v5/model" | ||
|
||
"github.com/mattermost/mattermost-plugin-mscalendar/server/mscalendar" | ||
) | ||
|
||
func (api *api) setAutoRespondMessage(w http.ResponseWriter, req *http.Request) { | ||
mattermostUserID := req.Header.Get("Mattermost-User-ID") | ||
if mattermostUserID == "" { | ||
dialogResponseError(w, "Not authorized.") | ||
return | ||
} | ||
|
||
v := model.SubmitDialogRequest{} | ||
err := json.NewDecoder(req.Body).Decode(&v) | ||
if err != nil { | ||
api.Logger.Warnf("Failed to unmarshal auto-respond message dialog request. err=%v", err) | ||
dialogResponseError(w, "Failed to process submit dialog response") | ||
return | ||
} | ||
|
||
m := mscalendar.New(api.Env, mattermostUserID) | ||
message, ok := v.Submission["auto_respond_message"].(string) | ||
if !ok { | ||
dialogResponseError(w, `No recognizable value for property "auto_respond_message".`) | ||
return | ||
} | ||
|
||
err = m.SetUserAutoRespondMessage(mattermostUserID, message) | ||
if err != nil { | ||
api.Logger.Warnf("Failed to set auto-respond message. err=%v", err) | ||
dialogResponseError(w, "Failed to set auto-respond message") | ||
return | ||
} | ||
|
||
response := model.SubmitDialogResponse{} | ||
w.Header().Set("Content-Type", "application/json") | ||
w.Write(response.ToJson()) | ||
api.Env.Poster.Ephemeral(mattermostUserID, v.ChannelId, "Auto-respond message changed to: '%s'", message) | ||
} | ||
|
||
func dialogResponseError(w http.ResponseWriter, errorMessage string) { | ||
response := model.SubmitDialogResponse{ | ||
Error: errorMessage, | ||
} | ||
w.Header().Set("Content-Type", "application/json") | ||
w.Write(response.ToJson()) | ||
} |
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,46 @@ | ||
package command | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/mattermost/mattermost-server/v5/model" | ||
|
||
"github.com/mattermost/mattermost-plugin-mscalendar/server/config" | ||
"github.com/mattermost/mattermost-plugin-mscalendar/server/mscalendar" | ||
) | ||
|
||
func (c *Command) autoRespond(parameters ...string) (string, bool, error) { | ||
if len(parameters) == 0 { | ||
request := model.OpenDialogRequest{ | ||
TriggerId: c.Args.TriggerId, | ||
URL: c.Config.PluginURL + config.PathDialogs + config.PathSetAutoRespondMessage, | ||
Dialog: model.Dialog{ | ||
CallbackId: "", | ||
Title: "Microsoft Calendar", | ||
Elements: []model.DialogElement{ | ||
{ | ||
DisplayName: "Auto-Respond Message", | ||
Name: "auto_respond_message", | ||
Type: "text", | ||
Placeholder: mscalendar.DefaultAutoRespondMessage, | ||
}, | ||
}, | ||
SubmitLabel: "Submit", | ||
NotifyOnCancel: false, | ||
State: "", | ||
}, | ||
} | ||
|
||
c.MSCalendar.OpenAutoRespondDialog(request) | ||
return "", false, nil | ||
} | ||
|
||
autoRespondMessage := strings.Join(parameters, " ") | ||
err := c.MSCalendar.SetUserAutoRespondMessage(c.Args.UserId, autoRespondMessage) | ||
if err != nil { | ||
return fmt.Sprintf("Failed to set auto-respond message. err=%v", err), false, nil | ||
} | ||
|
||
return fmt.Sprintf("Auto-respond message changed to: '%s'", autoRespondMessage), false, nil | ||
} |
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,70 @@ | ||
// Copyright (c) 2019-present Mattermost, Inc. All Rights Reserved. | ||
// See License for license information. | ||
|
||
package mscalendar | ||
|
||
import ( | ||
"github.com/mattermost/mattermost-plugin-mscalendar/server/store" | ||
"github.com/mattermost/mattermost-server/v5/model" | ||
) | ||
|
||
const DefaultAutoRespondMessage = "This user is currently in a meeting." | ||
|
||
type AutoRespond interface { | ||
HandleBusyDM(post *model.Post) error | ||
SetUserAutoRespondMessage(userID string, message string) error | ||
OpenAutoRespondDialog(request model.OpenDialogRequest) error | ||
} | ||
|
||
func (m *mscalendar) HandleBusyDM(post *model.Post) error { | ||
channel, err := m.PluginAPI.GetMattermostChannel(post.ChannelId) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if channel.Type != model.CHANNEL_DIRECT { | ||
return nil | ||
} | ||
|
||
usersInChannel, err := m.PluginAPI.GetMattermostUsersInChannel(post.ChannelId, model.CHANNEL_SORT_BY_USERNAME, 0, 2) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var storedRecipient *store.User | ||
for _, u := range usersInChannel { | ||
storedUser, _ := m.Store.LoadUser(u.Id) | ||
if u.Id != post.UserId { | ||
storedRecipient = storedUser | ||
break | ||
} | ||
} | ||
|
||
if storedRecipient == nil || !storedRecipient.Settings.AutoRespond || len(storedRecipient.ActiveEvents) == 0 { | ||
return nil | ||
} | ||
|
||
recipientStatus, err := m.PluginAPI.GetMattermostUserStatus(storedRecipient.MattermostUserID) | ||
if err != nil { | ||
return err | ||
} | ||
if recipientStatus.Status == model.STATUS_ONLINE { | ||
return nil | ||
} | ||
|
||
message := storedRecipient.Settings.AutoRespondMessage | ||
if message == "" { | ||
message = DefaultAutoRespondMessage | ||
} | ||
|
||
m.Poster.Ephemeral(post.UserId, post.ChannelId, message) | ||
return nil | ||
} | ||
|
||
func (m *mscalendar) SetUserAutoRespondMessage(userID string, message string) error { | ||
return m.Store.SetSetting(userID, store.AutoRespondMessageSettingID, message) | ||
} | ||
|
||
func (m *mscalendar) OpenAutoRespondDialog(request model.OpenDialogRequest) error { | ||
return m.PluginAPI.OpenInteractiveDialog(request) | ||
} |
Oops, something went wrong.