Skip to content

Commit

Permalink
notification feature
Browse files Browse the repository at this point in the history
  • Loading branch information
girish17 committed Sep 25, 2023
1 parent 6c47d5e commit 6748cfc
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 4 deletions.
2 changes: 2 additions & 0 deletions server/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ func (p *Plugin) ServeHTTP(_ *plugin.Context, w http.ResponseWriter, r *http.Req
SaveWP(p.MattermostPlugin, r)
case "/delWP":
DeleteWorkPackage(p.MattermostPlugin, w, r)
case "/subscribe":
NotificationSubscribe(p.MattermostPlugin, w, r)
case "/notifyChannel":
NotifyChannel(p.MattermostPlugin, w, r)
case "/bye":
Expand Down
2 changes: 2 additions & 0 deletions server/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ var messages = struct {
WPFetchFailMsg string
CnfWPLogMsg string
UnknownStatusCode string
SubscribeMsg string
}{
OpAuthFailMsg: "OpenProject authentication failed. Please try again.",
ProjectSelMsg: "*Please select a project*",
Expand Down Expand Up @@ -84,4 +85,5 @@ var messages = struct {
WPFetchFailMsg: "**That didn't work :pensive: Couldn't fetch work packages from OP**",
CnfWPLogMsg: "**Confirm work package deletion?**",
UnknownStatusCode: "**Unknown status code - error occurred** :pensive:",
SubscribeMsg: "**Subscribed to OpenProject notifications :sunglasses: **",
}
9 changes: 9 additions & 0 deletions server/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,15 @@ func GetAttachmentJSON(pluginURL string) string {
}
}
},
{
"name": "Subscribe to notifications",
"integration": {
"url": "` + pluginURL + `/subscribe",
"context": {
"action": ""
}
}
},
{
"name": "Bye :wave:",
"integration": {
Expand Down
1 change: 1 addition & 0 deletions server/projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type Links struct {
Versions Categories `json:"versions"`
Projects Categories `json:"projects"`
Status Self `json:"status"`
User Self `json:"user"`
}

type CreateWorkPackage struct {
Expand Down
1 change: 1 addition & 0 deletions server/responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type Action struct {

type Attachment struct {
Actions []Action `json:"actions"`
Links Links `json:"_links"`
}

type OptAttachments struct {
Expand Down
9 changes: 6 additions & 3 deletions server/timeEntries.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,12 @@ type EmbeddedTimeEntry struct {
}

type LinksTimeEntry struct {
Self Self `json:"self"`
Validate Self `json:"validate"`
Commit Self `json:"commit"`
Self Self `json:"self"`
Validate Self `json:"validate"`
Commit Self `json:"commit"`
Project Self `json:"project"`
User Self `json:"user"`
WorkPackage Self `json:"workPackage"`
}

type TimeEntries struct {
Expand Down
40 changes: 39 additions & 1 deletion server/uiActions.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ var customFieldForBillableHours string

var timeEntriesSchema map[string]interface{}

var subscribedChannelID string

func OpAuth(p plugin.MattermostPlugin, r *http.Request, pluginURL string) {
body, _ := io.ReadAll(r.Body)
var jsonBody map[string]interface{}
Expand Down Expand Up @@ -714,12 +716,48 @@ func showTimeLogSel(p plugin.MattermostPlugin, w http.ResponseWriter, channelID
_, _ = p.API.UpdatePost(post)
}

func NotificationSubscribe(p plugin.MattermostPlugin, w http.ResponseWriter, r *http.Request) {
body, _ := io.ReadAll(r.Body)
var jsonBody map[string]interface{}
_ = json.Unmarshal(body, &jsonBody)
subscribedChannelID = jsonBody["channel_id"].(string)
user, _ := p.API.GetUserByUsername(opBot)
post := getUpdatePostMsg(user.Id, subscribedChannelID, messages.SubscribeMsg)
_, _ = p.API.UpdatePost(post)
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(200)
_, _ = w.Write(body)
_ = respHTTP.Write(w)
}

func NotifyChannel(p plugin.MattermostPlugin, w http.ResponseWriter, r *http.Request) {
body, _ := io.ReadAll(r.Body)
p.API.LogDebug("Request body from OpenProject Webhooks: ", string(body))
defer r.Body.Close()
var opJSONRes Notification
user, _ := p.API.GetUserByUsername(opBot)
_ = json.Unmarshal(body, &opJSONRes)
p.API.LogDebug("Request body from OpenProject Webhooks: ", opJSONRes)
if subscribedChannelID == "" {
p.API.LogDebug("Not subscribed to OpenProject notification webhook")
} else {
notificationType := strings.Split(opJSONRes.Action, ":")[0]
var post *model.Post
notificationMsg := "**OpenProject notification** "
switch notificationType {
case "time_entry":
notificationMsg += opJSONRes.Action + " by " + opJSONRes.TimeEntry.Links.User.Title + " for " + opJSONRes.TimeEntry.Links.WorkPackage.Title + " in " + opJSONRes.TimeEntry.Links.Project.Title
case "project":
notificationMsg += opJSONRes.Action + " : " + opJSONRes.Project.Links.Self.Title
case "work_package":
notificationMsg += opJSONRes.Action + " : " + opJSONRes.WorkPackage.Links.Self.Title
case "attachment":
notificationMsg += opJSONRes.Action + " : " + opJSONRes.Attachment.Links.Self.Title
default:
notificationMsg += opJSONRes.Action
}
post = getUpdatePostMsg(user.Id, subscribedChannelID, notificationMsg)
_, _ = p.API.UpdatePost(post)
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(200)
_, _ = w.Write(body)
Expand Down

0 comments on commit 6748cfc

Please sign in to comment.