Skip to content

Commit 4fa33cf

Browse files
authored
Merge pull request #14 from Brightscout/MI-806
MI-806 move logic to open edit subscription modal from server to webapp
2 parents 109343a + a70e6ac commit 4fa33cf

File tree

16 files changed

+137
-69
lines changed

16 files changed

+137
-69
lines changed

server/command/command.go

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ var ConfluenceCommandHandler = Handler{
3333
handlers: map[string]HandlerFunc{
3434
"list": listChannelSubscription,
3535
"unsubscribe": deleteSubscription,
36-
"edit": editSubscription,
3736
"help": confluenceHelp,
3837
},
3938
defaultHandler: executeConfluenceDefault,
@@ -109,18 +108,6 @@ func listChannelSubscription(context *model.CommandArgs, args ...string) *model.
109108
return &model.CommandResponse{}
110109
}
111110

112-
func editSubscription(context *model.CommandArgs, args ...string) *model.CommandResponse {
113-
if len(args) == 0 {
114-
postCommandResponse(context, specifyAlias)
115-
return &model.CommandResponse{}
116-
}
117-
alias := args[0]
118-
if err := service.OpenSubscriptionEditModal(context.ChannelId, context.UserId, alias); err != nil {
119-
postCommandResponse(context, err.Error())
120-
}
121-
return &model.CommandResponse{}
122-
}
123-
124111
func confluenceHelp(context *model.CommandArgs, args ...string) *model.CommandResponse {
125112
postCommandResponse(context, helpText)
126113
return &model.CommandResponse{}

server/controller/edit_subscription.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ import (
99
"github.com/Brightscout/mattermost-plugin-confluence/server/service"
1010
)
1111

12-
var EditChannelSubscription = &Endpoint{
12+
var editChannelSubscription = &Endpoint{
1313
RequiresAuth: true,
1414
Path: "/subscription",
1515
Method: http.MethodPut,
16-
Execute: editChannelSubscription,
16+
Execute: handleEditChannelSubscription,
1717
}
1818

19-
func editChannelSubscription(w http.ResponseWriter, r *http.Request) {
19+
func handleEditChannelSubscription(w http.ResponseWriter, r *http.Request) {
2020
body := json.NewDecoder(r.Body)
2121
subscription := serializer.Subscription{}
2222
if err := body.Decode(&subscription); err != nil {

server/controller/get_subscription.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package controller
2+
3+
import (
4+
"net/http"
5+
6+
"github.com/Brightscout/mattermost-plugin-confluence/server/service"
7+
)
8+
9+
var getChannelSubscription = &Endpoint{
10+
RequiresAuth: true,
11+
Path: "/subscription",
12+
Method: http.MethodGet,
13+
Execute: handleGetChannelSubscription,
14+
}
15+
16+
func handleGetChannelSubscription(w http.ResponseWriter, r *http.Request) {
17+
channelID := r.FormValue("channelID")
18+
alias := r.FormValue("alias")
19+
subscription, errCode, err := service.GetChannelSubscription(channelID, alias)
20+
if err != nil {
21+
http.Error(w, err.Error(), errCode)
22+
return
23+
}
24+
w.Header().Set("Content-Type", "application/json")
25+
_, _ = w.Write([]byte(subscription.ToJSON()))
26+
}

server/controller/main.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@ type Endpoint struct {
2525
// Usage: getEndpointKey(GetMetadata): GetMetadata
2626
var Endpoints = map[string]*Endpoint{
2727
getEndpointKey(confluenceCloudWebhook): confluenceCloudWebhook,
28-
getEndpointKey(SaveChannelSubscription): SaveChannelSubscription,
29-
getEndpointKey(EditChannelSubscription): EditChannelSubscription,
28+
getEndpointKey(saveChannelSubscription): saveChannelSubscription,
29+
getEndpointKey(editChannelSubscription): editChannelSubscription,
3030
getEndpointKey(confluenceServerWebhook): confluenceServerWebhook,
31+
getEndpointKey(getChannelSubscription): getChannelSubscription,
3132
}
3233

3334
// Uniquely identifies an endpoint using path and method

server/controller/save_subscription.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ import (
99
"github.com/Brightscout/mattermost-plugin-confluence/server/service"
1010
)
1111

12-
var SaveChannelSubscription = &Endpoint{
12+
var saveChannelSubscription = &Endpoint{
1313
RequiresAuth: true,
1414
Path: "/subscription",
1515
Method: http.MethodPost,
16-
Execute: saveChannelSubscription,
16+
Execute: handleSaveChannelSubscription,
1717
}
1818

19-
func saveChannelSubscription(w http.ResponseWriter, r *http.Request) {
19+
func handleSaveChannelSubscription(w http.ResponseWriter, r *http.Request) {
2020
body := json.NewDecoder(r.Body)
2121
subscription := serializer.Subscription{}
2222
if err := body.Decode(&subscription); err != nil {

server/serializer/channel_config.go renamed to server/serializer/channel_subscription.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package serializer
22

33
import (
4+
"encoding/json"
45
"fmt"
56
url2 "net/url"
67
"strings"
@@ -66,3 +67,11 @@ func FormattedSubscriptionList(channelSubscriptions map[string]Subscription) str
6667
}
6768
return list
6869
}
70+
71+
func (s *Subscription) ToJSON() string {
72+
b, err := json.Marshal(s)
73+
if err != nil {
74+
return ""
75+
}
76+
return string(b)
77+
}

server/service/edit_subscription.go

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package service
22

33
import (
4-
"encoding/json"
5-
"fmt"
64
"net/http"
75

86
"github.com/mattermost/mattermost-server/model"
@@ -13,11 +11,7 @@ import (
1311
"github.com/Brightscout/mattermost-plugin-confluence/server/store"
1412
)
1513

16-
const (
17-
openEditSubscriptionModalWebsocketEvent = "open_edit_subscription_modal"
18-
generalError = "Some error occurred. Please try again after sometime."
19-
subscriptionEditSuccess = "Subscription updated successfully."
20-
)
14+
const subscriptionEditSuccess = "Subscription updated successfully."
2115

2216
func EditSubscription(subscription serializer.Subscription, userID string) (int, error) {
2317
channelSubscriptions, cKey, gErr := GetChannelSubscriptions(subscription.ChannelID)
@@ -46,28 +40,3 @@ func EditSubscription(subscription serializer.Subscription, userID string) (int,
4640

4741
return http.StatusOK, nil
4842
}
49-
50-
func OpenSubscriptionEditModal(channelID, userID, alias string) error {
51-
channelSubscriptions, _, gErr := GetChannelSubscriptions(channelID)
52-
if gErr != nil {
53-
return errors.New(generalError)
54-
}
55-
if subscription, ok := channelSubscriptions[alias]; ok {
56-
bytes, err := json.Marshal(subscription)
57-
if err != nil {
58-
return errors.New(generalError)
59-
}
60-
config.Mattermost.PublishWebSocketEvent(
61-
openEditSubscriptionModalWebsocketEvent,
62-
map[string]interface{}{
63-
"subscription": string(bytes),
64-
},
65-
&model.WebsocketBroadcast{
66-
UserId: userID,
67-
},
68-
)
69-
return nil
70-
}
71-
72-
return errors.New(fmt.Sprintf(subscriptionNotFound, alias))
73-
}

server/service/get_subscription.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package service
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
7+
"github.com/pkg/errors"
8+
9+
"github.com/Brightscout/mattermost-plugin-confluence/server/serializer"
10+
)
11+
12+
const generalError = "Some error occurred. Please try again after sometime."
13+
14+
func GetChannelSubscription(channelID, alias string) (serializer.Subscription, int, error) {
15+
channelSubscriptions, _, gErr := GetChannelSubscriptions(channelID)
16+
if gErr != nil {
17+
return serializer.Subscription{}, http.StatusInternalServerError, errors.New(generalError)
18+
}
19+
subscription, found := channelSubscriptions[alias]
20+
if !found {
21+
return serializer.Subscription{}, http.StatusBadRequest, errors.New(fmt.Sprintf(subscriptionNotFound, alias))
22+
}
23+
return subscription, http.StatusOK, nil
24+
}

webapp/src/actions/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import {
2-
closeSubscriptionModal, openSubscriptionModal, saveChannelSubscription, receivedSubscription, editChannelSubscription,
2+
closeSubscriptionModal, openSubscriptionModal, saveChannelSubscription, editChannelSubscription, getChannelSubscription,
33
} from './subscription_modal';
44

55
export {
66
saveChannelSubscription,
77
openSubscriptionModal,
88
closeSubscriptionModal,
9-
receivedSubscription,
109
editChannelSubscription,
10+
getChannelSubscription,
1111
};

0 commit comments

Comments
 (0)