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

Add status change confirmation #73

Merged
merged 29 commits into from
May 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
6c76941
Add status change confirmation
larkox Apr 3, 2020
3089056
Improve error handling and messages
larkox Apr 6, 2020
6e12f49
Improve detection of meetings by checking the start time
larkox Apr 7, 2020
4fbe800
Add event information to status change notification
larkox Apr 8, 2020
585eb4b
Merge branch 'master' into GH60StatusChangeConfirmation
levb Apr 8, 2020
18d40cc
Change start and end times types to pointers and be more clear with h…
larkox Apr 13, 2020
d151b09
Merge branch 'GH60StatusChangeConfirmation' of /~https://github.com/lar…
larkox Apr 13, 2020
a3ae95c
Merge branch 'master' into GH60StatusChangeConfirmation
levb Apr 13, 2020
4ba8776
Merge branch 'master' into GH60StatusChangeConfirmation
mickmister Apr 14, 2020
b2f4f5c
Use GetCalendarView to determine user's current status
mickmister Apr 15, 2020
189be08
Merge branch 'master' into GH60StatusChangeConfirmation
mickmister Apr 16, 2020
3b40dc1
Test status sync job
mickmister Apr 17, 2020
f92f794
treat bot as admin
mickmister Apr 21, 2020
e91f44b
Merge branch 'master' into GH60StatusChangeConfirmation
larkox Apr 22, 2020
0607f52
Use pretty statuses on slack action response
larkox Apr 22, 2020
a9c482b
Take in mind cancelled events for notifications and status change
larkox Apr 22, 2020
5528545
Merge branch 'master' into GH60StatusChangeConfirmation
larkox Apr 22, 2020
18d48bd
Use GetSchedule for status and GetCalendarView for reminders
mickmister Apr 23, 2020
dfbd7e0
Revert "Use GetSchedule for status and GetCalendarView for reminders"
larkox Apr 23, 2020
d67c0e3
Merge branch 'master' into GH60StatusChangeConfirmation
larkox Apr 23, 2020
7ead072
Fix panic due to error handling and change check to include events am…
larkox Apr 23, 2020
cd3cc6d
Add GetReminder setting and fix issue where a user that decided not t…
larkox Apr 23, 2020
fd5c45d
short circuit when no users want status, same with reminders. fix tests
mickmister Apr 24, 2020
78d13e9
Improve error testing
mickmister Apr 24, 2020
e268fcf
Name changes refactoring, and changes on how upcoming events and cale…
larkox Apr 24, 2020
fd149c7
Fix test
larkox Apr 24, 2020
5a698c8
Remove unneeded botUser references
larkox Apr 24, 2020
6db9aee
Merge branch 'master' into GH60StatusChangeConfirmation
mickmister Apr 29, 2020
7b2a6fe
Handle mistaken string when the availability handles an ongoing event…
larkox Apr 30, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions server/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ func Init(h *httputils.Handler, env mscalendar.Env, notificationProcessor mscale
postActionRouter.HandleFunc(config.PathDecline, api.postActionDecline).Methods("POST")
postActionRouter.HandleFunc(config.PathTentative, api.postActionTentative).Methods("POST")
postActionRouter.HandleFunc(config.PathRespond, api.postActionRespond).Methods("POST")
postActionRouter.HandleFunc(config.PathConfirmStatusChange, api.postActionConfirmStatusChange).Methods("POST")
}
95 changes: 95 additions & 0 deletions server/api/post_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@
package api

import (
"encoding/json"
"errors"
"fmt"
"net/http"
"strings"
"time"

"github.com/mattermost/mattermost-server/v5/model"

"github.com/mattermost/mattermost-plugin-mscalendar/server/config"
"github.com/mattermost/mattermost-plugin-mscalendar/server/mscalendar"
"github.com/mattermost/mattermost-plugin-mscalendar/server/mscalendar/views"
"github.com/mattermost/mattermost-plugin-mscalendar/server/utils"
)

Expand Down Expand Up @@ -134,3 +138,94 @@ func prettyOption(option string) string {
return ""
}
}

func (api *api) postActionConfirmStatusChange(w http.ResponseWriter, req *http.Request) {
mattermostUserID := req.Header.Get("Mattermost-User-ID")
if mattermostUserID == "" {
utils.SlackAttachmentError(w, "Not authorized.")
return
}

response := model.PostActionIntegrationResponse{}
post := &model.Post{}

request := model.PostActionIntegrationRequestFromJson(req.Body)
if request == nil {
utils.SlackAttachmentError(w, "Invalid request.")
return
}

value, ok := request.Context["value"].(bool)
if !ok {
utils.SlackAttachmentError(w, `No recognizable value for property "value".`)
return
}

returnText := "The status has not been changed."
if value {
changeTo, ok := request.Context["change_to"]
if !ok {
utils.SlackAttachmentError(w, "No state to change to.")
return
}
stringChangeTo := changeTo.(string)
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: can you change this to match the type assertion strategy above?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, forgot about that.

prettyChangeTo, ok := request.Context["pretty_change_to"]
if !ok {
prettyChangeTo = changeTo
}
stringPrettyChangeTo := prettyChangeTo.(string)

api.PluginAPI.UpdateMattermostUserStatus(mattermostUserID, stringChangeTo)
returnText = fmt.Sprintf("The status has been changed to %s.", stringPrettyChangeTo)
}

eventInfo, err := getEventInfo(request.Context)
if err != nil {
utils.SlackAttachmentError(w, err.Error())
return
}

if eventInfo != "" {
returnText = eventInfo + "\n" + returnText
}

sa := &model.SlackAttachment{
Title: "Status Change",
Text: returnText,
}

model.ParseSlackAttachment(post, []*model.SlackAttachment{sa})

response.Update = post
w.Header().Set("Content-Type", "application/json")
w.Write(response.ToJson())
}

func getEventInfo(ctx map[string]interface{}) (string, error) {
hasEvent, ok := ctx["hasEvent"].(bool)
if !ok {
return "", errors.New("Cannot check whether there is an event attached.")
}
if !hasEvent {
return "", nil
}

subject, ok := ctx["subject"].(string)
if !ok {
return "", errors.New("Cannot find the event subject.")
}

weblink, ok := ctx["weblink"].(string)
if !ok {
return "", errors.New("Cannot find the event weblink.")
}

marshalledStartTime, ok := ctx["startTime"].(string)
if !ok {
return "", errors.New("Cannot find the event start time.")
}
var startTime time.Time
json.Unmarshal([]byte(marshalledStartTime), &startTime)

return views.RenderEventWillStartLine(subject, weblink, startTime), nil
}
12 changes: 10 additions & 2 deletions server/command/availability.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,24 @@
package command

func (c *Command) availability(parameters ...string) (string, bool, error) {
authorized, err := c.MSCalendar.IsAuthorizedAdmin(c.Args.UserId)
if err != nil {
return "", false, err
}
if !authorized {
return "Not authorized", false, nil
}

switch {
case len(parameters) == 0:
resString, err := c.MSCalendar.SyncStatus(c.Args.UserId)
resString, err := c.MSCalendar.Sync(c.Args.UserId)
if err != nil {
return "", false, err
}

return resString, false, nil
case len(parameters) == 1 && parameters[0] == "all":
resString, err := c.MSCalendar.SyncStatusAll()
resString, err := c.MSCalendar.SyncAll()
if err != nil {
return "", false, err
}
Expand Down
1 change: 0 additions & 1 deletion server/command/connect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ func TestConnect(t *testing.T) {

conf := &config.Config{
PluginURL: "http://localhost",
BotUserID: "bot_user_id",
}

mscal := mock_mscalendar.NewMockMSCalendar(ctrl)
Expand Down
1 change: 0 additions & 1 deletion server/command/disconnect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ func TestDisconnect(t *testing.T) {

conf := &config.Config{
PluginURL: "http://localhost",
BotUserID: "bot_user_id",
}

mscal := mock_mscalendar.NewMockMSCalendar(ctrl)
Expand Down
1 change: 0 additions & 1 deletion server/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ type StoredConfig struct {
type Config struct {
StoredConfig

BotUserID string
BuildDate string
BuildHash string
BuildHashShort string
Expand Down
21 changes: 11 additions & 10 deletions server/config/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,17 @@ const (
Repository = "mattermost-plugin-mscalendar"
CommandTrigger = "mscalendar"

PathOAuth2 = "/oauth2"
PathComplete = "/complete"
PathAPI = "/api/v1"
PathPostAction = "/action"
PathRespond = "/respond"
PathAccept = "/accept"
PathDecline = "/decline"
PathTentative = "/tentative"
PathNotification = "/notification/v1"
PathEvent = "/event"
PathOAuth2 = "/oauth2"
PathComplete = "/complete"
PathAPI = "/api/v1"
PathPostAction = "/action"
PathRespond = "/respond"
PathAccept = "/accept"
PathDecline = "/decline"
PathTentative = "/tentative"
PathConfirmStatusChange = "/confirm"
PathNotification = "/notification/v1"
PathEvent = "/event"

FullPathEventNotification = PathNotification + PathEvent
FullPathOAuth2Redirect = PathOAuth2 + PathComplete
Expand Down
3 changes: 0 additions & 3 deletions server/jobs/job_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/stretchr/testify/require"

"github.com/mattermost/mattermost-plugin-api/cluster"
"github.com/mattermost/mattermost-plugin-mscalendar/server/config"
"github.com/mattermost/mattermost-plugin-mscalendar/server/jobs/mock_cluster"
"github.com/mattermost/mattermost-plugin-mscalendar/server/mscalendar"
"github.com/mattermost/mattermost-plugin-mscalendar/server/mscalendar/mock_plugin_api"
Expand All @@ -29,9 +28,7 @@ func newTestEnv(ctrl *gomock.Controller) mscalendar.Env {
mockPluginAPI := mock_plugin_api.NewMockPluginAPI(ctrl)

logger := &bot.NilLogger{}
conf := &config.Config{BotUserID: "bot_mm_id"}
return mscalendar.Env{
Config: conf,
Dependencies: &mscalendar.Dependencies{
Store: s,
Logger: logger,
Expand Down
8 changes: 4 additions & 4 deletions server/jobs/status_sync_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ func NewStatusSyncJob() RegisteredJob {
return RegisteredJob{
id: statusSyncJobID,
interval: mscalendar.StatusSyncJobInterval,
work: runStatusSyncJob,
work: runSyncJob,
isEnabledByConfig: isStatusSyncJobEnabled,
}
}

// runStatusSyncJob synchronizes all users' statuses between mscalendar and Mattermost.
func runStatusSyncJob(env mscalendar.Env) {
// runSyncJob synchronizes all users' statuses between mscalendar and Mattermost.
func runSyncJob(env mscalendar.Env) {
env.Logger.Debugf("User status sync job beginning")

_, err := mscalendar.New(env, "").SyncStatusAll()
_, err := mscalendar.New(env, "").SyncAll()
if err != nil {
env.Logger.Errorf("Error during user status sync job: %v", err)
}
Expand Down
Loading