-
Notifications
You must be signed in to change notification settings - Fork 25
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
Changes from 4 commits
6c76941
3089056
6e12f49
4fbe800
585eb4b
18d40cc
d151b09
a3ae95c
4ba8776
b2f4f5c
189be08
3b40dc1
f92f794
e91f44b
0607f52
a9c482b
5528545
18d48bd
dfbd7e0
d67c0e3
7ead072
cd3cc6d
fd5c45d
78d13e9
e268fcf
fd149c7
5a698c8
6db9aee
7b2a6fe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,8 @@ import ( | |
"fmt" | ||
"time" | ||
|
||
"github.com/mattermost/mattermost-plugin-mscalendar/server/config" | ||
"github.com/mattermost/mattermost-plugin-mscalendar/server/mscalendar/views" | ||
"github.com/mattermost/mattermost-plugin-mscalendar/server/remote" | ||
"github.com/mattermost/mattermost-plugin-mscalendar/server/store" | ||
"github.com/mattermost/mattermost-plugin-mscalendar/server/utils" | ||
|
@@ -115,7 +117,7 @@ func (m *mscalendar) setUserStatuses(filteredUsers store.UserIndex, schedules [] | |
continue | ||
} | ||
|
||
res = m.setStatusFromAvailability(mattermostUserID, status, s.AvailabilityView) | ||
res = m.setStatusFromAvailability(mattermostUserID, status, s) | ||
} | ||
if res != "" { | ||
return res, nil | ||
|
@@ -136,31 +138,58 @@ func (m *mscalendar) GetAvailabilities(remoteUserID string, scheduleIDs []string | |
return client.GetSchedule(remoteUserID, scheduleIDs, start, end, availabilityTimeWindowSize) | ||
} | ||
|
||
func (m *mscalendar) setStatusFromAvailability(mattermostUserID, currentStatus string, av remote.AvailabilityView) string { | ||
currentAvailability := av[0] | ||
func (m *mscalendar) setStatusFromAvailability(mattermostUserID, currentStatus string, s *remote.ScheduleInformation) string { | ||
currentAvailability := s.AvailabilityView[0] | ||
u := NewUser(mattermostUserID) | ||
|
||
if !m.ShouldChangeStatus(u) { | ||
return "Status not changed by user configuration." | ||
} | ||
|
||
if !m.HasAvailabilityChanged(u, currentAvailability) { | ||
ok, newEventTime := m.HasNewEventStarted(u, s) | ||
mickmister marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if !ok { | ||
return "Status not changed because there is no update since last status change." | ||
} | ||
u.LastStatusUpdateEventTime = *newEventTime | ||
} | ||
|
||
u.LastStatusUpdateAvailability = currentAvailability | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we need to store more information here to take into account for the user manually changing their status. Here's a proposal When the user manually changes their status, we take all events that overlap with I think this solves the one-off issue when the user wants to cancel out their most recent status change. If they want it to remain untouched by the automation, then they can disable it in their settings. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One of the main problems is that there is no eventID that I can use to handle this, I can only work with times and availability. I can do the following:
I will implement this and keep thinking about it. Probably in code make more sense. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm well if we don't have an event id we're kind of at a disadvantage in general. Maybe we shouldn't even be using
@larkox Let me know what you think about the shortcomings of using |
||
m.Store.StoreUser(u.User) | ||
|
||
url := fmt.Sprintf("%s%s%s", m.Config.PluginURLPath, config.PathPostAction, config.PathConfirmStatusChange) | ||
|
||
switch currentAvailability { | ||
case remote.AvailabilityViewFree: | ||
if currentStatus == "dnd" { | ||
if m.ShouldNotifyStatusChange(u) { | ||
m.Poster.DMWithAttachments(mattermostUserID, views.RenderStatusChangeNotificationView(s.ScheduleItems, "Online", url)) | ||
return fmt.Sprintf("User asked for status change from %s to online", currentStatus) | ||
} | ||
m.PluginAPI.UpdateMattermostUserStatus(mattermostUserID, "online") | ||
return fmt.Sprintf("User is free. Setting user from %s to online.", currentStatus) | ||
} else { | ||
return fmt.Sprintf("User is free, and is already set to %s.", currentStatus) | ||
} | ||
return fmt.Sprintf("User is free, and is already set to %s.", currentStatus) | ||
case remote.AvailabilityViewTentative, remote.AvailabilityViewBusy: | ||
if currentStatus != "dnd" { | ||
if m.ShouldNotifyStatusChange(u) { | ||
m.Poster.DMWithAttachments(mattermostUserID, views.RenderStatusChangeNotificationView(s.ScheduleItems, "Do Not Disturb", url)) | ||
return fmt.Sprintf("User asked for status change from %s to dnd.", currentStatus) | ||
} | ||
m.PluginAPI.UpdateMattermostUserStatus(mattermostUserID, "dnd") | ||
return fmt.Sprintf("User is busy. Setting user from %s to dnd.", currentStatus) | ||
} else { | ||
return fmt.Sprintf("User is busy, and is already set to %s.", currentStatus) | ||
} | ||
return fmt.Sprintf("User is busy, and is already set to %s.", currentStatus) | ||
case remote.AvailabilityViewOutOfOffice: | ||
if currentStatus != "offline" { | ||
if m.ShouldNotifyStatusChange(u) { | ||
m.Poster.DMWithAttachments(mattermostUserID, views.RenderStatusChangeNotificationView(s.ScheduleItems, "Offline", url)) | ||
return fmt.Sprintf("User asked for status change fom %s to offline", currentStatus) | ||
} | ||
m.PluginAPI.UpdateMattermostUserStatus(mattermostUserID, "offline") | ||
return fmt.Sprintf("User is out of office. Setting user from %s to offline", currentStatus) | ||
} else { | ||
return fmt.Sprintf("User is out of office, and is already set to %s.", currentStatus) | ||
} | ||
return fmt.Sprintf("User is out of office, and is already set to %s.", currentStatus) | ||
case remote.AvailabilityViewWorkingElsewhere: | ||
return fmt.Sprintf("User is working elsewhere. Pending implementation.") | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package views | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/mattermost/mattermost-plugin-mscalendar/server/remote" | ||
"github.com/mattermost/mattermost-server/v5/model" | ||
) | ||
|
||
func RenderStatusChangeNotificationView(events []*remote.ScheduleItem, status, url string) *model.SlackAttachment { | ||
for _, e := range events { | ||
if e.Start.Time().After(time.Now()) { | ||
return statusChangeAttachments(e, status, url) | ||
} | ||
} | ||
return statusChangeAttachments(nil, status, url) | ||
} | ||
|
||
func renderScheduleItem(si *remote.ScheduleItem, status string) string { | ||
if si == nil { | ||
return fmt.Sprintf("You have no upcoming events.\n Shall I change your status to %s?", status) | ||
} | ||
|
||
resp := fmt.Sprintf("Your event with subject `%s` will start soon.", si.Subject) | ||
if si.Subject == "" { | ||
resp = "An event with no subject will start soon." | ||
} | ||
|
||
resp += "\nShall I change your status to %s?" | ||
return resp | ||
} | ||
|
||
func statusChangeAttachments(event *remote.ScheduleItem, status, url string) *model.SlackAttachment { | ||
actionYes := &model.PostAction{ | ||
Name: "Yes", | ||
Integration: &model.PostActionIntegration{ | ||
URL: url, | ||
Context: map[string]interface{}{ | ||
"value": true, | ||
"change_to": status, | ||
}, | ||
}, | ||
} | ||
|
||
actionNo := &model.PostAction{ | ||
Name: "No", | ||
Integration: &model.PostActionIntegration{ | ||
URL: url, | ||
Context: map[string]interface{}{ | ||
"value": false, | ||
}, | ||
}, | ||
} | ||
|
||
sa := &model.SlackAttachment{ | ||
Title: "Status change", | ||
Text: renderScheduleItem(event, status), | ||
Actions: []*model.PostAction{actionYes, actionNo}, | ||
} | ||
|
||
return sa | ||
} |
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.
Nit: can you change this to match the type assertion strategy above?
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.
Sorry, forgot about that.