-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomment.go
48 lines (41 loc) · 1.54 KB
/
comment.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package server
import (
"context"
"net/http"
"time"
"github.com/devnull-twitch/brainslurp/internal/server/components/pages"
"github.com/devnull-twitch/brainslurp/internal/server/components/shared"
"github.com/devnull-twitch/brainslurp/lib/issues"
"github.com/devnull-twitch/brainslurp/lib/proto/issue"
pb_user "github.com/devnull-twitch/brainslurp/lib/proto/user"
"github.com/dgraph-io/badger/v4"
"github.com/sirupsen/logrus"
)
func HandleNewIssueComment(db *badger.DB) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
walkChain(
db, w, r,
authUserWithProjectNo,
checkIssueNumber,
func(ctx context.Context, w http.ResponseWriter, r *http.Request, next nextCall) {
projectObj := getProjectFromContext(ctx)
issueObj := getIssueFromContext(ctx)
userObj := getUserFromContext(ctx)
r.ParseForm()
comment := r.Form.Get("comment")
newCommentActivity := &issue.IssueActivity{
CreatedAt: time.Now().Unix(),
TriggerUser: userObj.GetNumber(),
Body: comment,
}
issueObj.Activities = append(issueObj.Activities, newCommentActivity)
if _, _, err := issues.Update(db, projectObj.GetNumber(), issueObj); err != nil {
logrus.WithError(err).Warn("error saving issue with new comment")
w.WriteHeader(http.StatusInternalServerError)
pages.Error("Error saving issue with comment").Render(r.Context(), w)
}
shared.CommentBox(newCommentActivity, map[uint64]*pb_user.User{userObj.GetNumber(): userObj}).Render(r.Context(), w)
},
)
}
}