generated from mattermost/mattermost-plugin-starter-template
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Add a way to handle subscriptions from the settings panel * Change notification setting id name, getCalendar function name to getCal, and return notification setting directly Co-authored-by: Lev <1187448+levb@users.noreply.github.com> Co-authored-by: mattermod <mattermod@users.noreply.github.com>
- Loading branch information
1 parent
c252ee0
commit 95ad6b1
Showing
3 changed files
with
140 additions
and
4 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,135 @@ | ||
package mscalendar | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/mattermost/mattermost-plugin-mscalendar/server/utils/settingspanel" | ||
"github.com/mattermost/mattermost-server/v5/model" | ||
) | ||
|
||
type notificationSetting struct { | ||
title string | ||
description string | ||
id string | ||
dependsOn string | ||
getCal func(string) MSCalendar | ||
} | ||
|
||
func NewNotificationsSetting(getCal func(string) MSCalendar) settingspanel.Setting { | ||
return ¬ificationSetting{ | ||
title: "Receive notifications of new events", | ||
description: "Do you want to subscribe to new events and receive a message when they are created?", | ||
id: "new_or_updated_event_setting", | ||
dependsOn: "", | ||
getCal: getCal, | ||
} | ||
} | ||
|
||
func (s *notificationSetting) Set(userID string, value interface{}) error { | ||
boolValue := false | ||
if value == "true" { | ||
boolValue = true | ||
} | ||
|
||
cal := s.getCal(userID) | ||
|
||
if boolValue { | ||
_, err := cal.LoadMyEventSubscription() | ||
if err != nil { | ||
_, err := cal.CreateMyEventSubscription() | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
_, err := cal.LoadMyEventSubscription() | ||
if err == nil { | ||
return cal.DeleteMyEventSubscription() | ||
} | ||
return nil | ||
} | ||
|
||
func (s *notificationSetting) Get(userID string) (interface{}, error) { | ||
cal := s.getCal(userID) | ||
_, err := cal.LoadMyEventSubscription() | ||
if err == nil { | ||
return "true", nil | ||
} | ||
|
||
return "false", nil | ||
} | ||
|
||
func (s *notificationSetting) GetID() string { | ||
return s.id | ||
} | ||
|
||
func (s *notificationSetting) GetTitle() string { | ||
return s.title | ||
} | ||
|
||
func (s *notificationSetting) GetDescription() string { | ||
return s.description | ||
} | ||
|
||
func (s *notificationSetting) GetDependency() string { | ||
return s.dependsOn | ||
} | ||
|
||
func (s *notificationSetting) GetSlackAttachments(userID, settingHandler string, disabled bool) (*model.SlackAttachment, error) { | ||
title := fmt.Sprintf("Setting: %s", s.title) | ||
currentValueMessage := "Disabled" | ||
|
||
actions := []*model.PostAction{} | ||
if !disabled { | ||
currentValue, err := s.Get(userID) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
currentTextValue := "No" | ||
if currentValue == "true" { | ||
currentTextValue = "Yes" | ||
} | ||
currentValueMessage = fmt.Sprintf("Current value: %s", currentTextValue) | ||
|
||
actionTrue := model.PostAction{ | ||
Name: "Yes", | ||
Integration: &model.PostActionIntegration{ | ||
URL: settingHandler, | ||
Context: map[string]interface{}{ | ||
settingspanel.ContextIDKey: s.id, | ||
settingspanel.ContextButtonValueKey: "true", | ||
}, | ||
}, | ||
} | ||
|
||
actionFalse := model.PostAction{ | ||
Name: "No", | ||
Integration: &model.PostActionIntegration{ | ||
URL: settingHandler, | ||
Context: map[string]interface{}{ | ||
settingspanel.ContextIDKey: s.id, | ||
settingspanel.ContextButtonValueKey: "false", | ||
}, | ||
}, | ||
} | ||
actions = []*model.PostAction{&actionTrue, &actionFalse} | ||
|
||
} | ||
|
||
text := fmt.Sprintf("%s\n%s", s.description, currentValueMessage) | ||
sa := model.SlackAttachment{ | ||
Title: title, | ||
Text: text, | ||
Actions: actions, | ||
} | ||
|
||
return &sa, nil | ||
} | ||
|
||
func (s *notificationSetting) IsDisabled(foreignValue interface{}) bool { | ||
return foreignValue == "false" | ||
} |
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