-
Notifications
You must be signed in to change notification settings - Fork 1
/
line_notify.go
130 lines (107 loc) · 3.68 KB
/
line_notify.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package backend
import (
"context"
"html/template"
"net/http"
"time"
"github.com/pkg/errors"
"github.com/utahta/go-linenotify/auth"
"github.com/utahta/momoclo-channel/adapter/handler"
"github.com/utahta/momoclo-channel/container"
"github.com/utahta/momoclo-channel/domain/event"
"github.com/utahta/momoclo-channel/domain/model"
"github.com/utahta/momoclo-channel/lib/config"
"github.com/utahta/momoclo-channel/usecase"
)
// LineNotifyOn redirect to LINE Notify connection page
func LineNotifyOn(w http.ResponseWriter, req *http.Request) {
ctx := req.Context()
c, err := auth.New(config.C.LineNotify.ClientID, config.LineNotifyCallbackURL())
if err != nil {
handler.Fail(ctx, w, err, http.StatusInternalServerError)
return
}
http.SetCookie(w, &http.Cookie{Name: "state", Value: c.State, Expires: time.Now().Add(300 * time.Second), Secure: true})
container.Logger(ctx).AE().Info("Redirect to LINE Notify connection page")
err = c.Redirect(w, req)
if err != nil {
handler.Fail(ctx, w, err, http.StatusInternalServerError)
return
}
}
// LineNotifyOff redirect to LINE Notify revoking page
func LineNotifyOff(w http.ResponseWriter, req *http.Request) {
ctx := req.Context()
container.Logger(ctx).AE().Info("Redirect to LINE Notify revoking page")
// official url
http.Redirect(w, req, "https://notify-bot.line.me/my/", http.StatusFound)
}
// LineNotifyCallback stores LINE Notify token
func LineNotifyCallback(w http.ResponseWriter, req *http.Request) {
ctx := req.Context()
params, err := auth.ParseRequest(req)
if err != nil {
handler.Fail(ctx, w, err, http.StatusInternalServerError)
return
}
state, err := req.Cookie("state")
if err != nil {
handler.Fail(ctx, w, err, http.StatusInternalServerError)
return
}
if params.State != state.Value {
handler.Fail(ctx, w, errors.New("invalid csrf token"), http.StatusBadRequest)
return
}
if err := container.Usecase(ctx).AddLineNotification().Do(usecase.AddLineNotificationParams{Code: params.Code}); err != nil {
handler.Fail(ctx, w, err, http.StatusInternalServerError)
return
}
t, err := template.New("callback").Parse("<html><body><h1>通知ノフ設定オンにしました(・Θ・)</h1></body></html>")
if err != nil {
handler.Fail(ctx, w, err, http.StatusInternalServerError)
return
}
err = t.Execute(w, nil)
if err != nil {
handler.Fail(ctx, w, err, http.StatusInternalServerError)
return
}
}
// LineNotifyBroadcast invokes broadcast line notification event
func LineNotifyBroadcast(w http.ResponseWriter, req *http.Request) {
ctx := req.Context()
if err := req.ParseForm(); err != nil {
handler.Fail(ctx, w, err, http.StatusInternalServerError)
return
}
var messages []model.LineNotifyMessage
if err := event.ParseTask(req.Form, &messages); err != nil {
handler.Fail(ctx, w, err, http.StatusInternalServerError)
return
}
params := usecase.LineNotifyBroadcastParams{Messages: messages}
if err := container.Usecase(ctx).LineNotifyBroadcast().Do(params); err != nil {
handler.Fail(ctx, w, err, http.StatusInternalServerError)
return
}
}
// LineNotify notify users of messages
func LineNotify(w http.ResponseWriter, req *http.Request) {
ctx, cancel := context.WithTimeout(req.Context(), 30*time.Second)
defer cancel()
if err := req.ParseForm(); err != nil {
handler.Fail(ctx, w, err, http.StatusInternalServerError)
return
}
var request model.LineNotifyRequest
if err := event.ParseTask(req.Form, &request); err != nil {
handler.Fail(ctx, w, err, http.StatusInternalServerError)
return
}
params := usecase.LineNotifyParams{Request: request}
if err := container.Usecase(ctx).LineNotify().Do(params); err != nil {
handler.Fail(ctx, w, err, http.StatusInternalServerError)
return
}
}