-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhaiken.go
194 lines (179 loc) · 4.88 KB
/
haiken.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package main
import (
"encoding/json"
"fmt"
"log"
"strings"
"github.com/aji-su/haiken/ikku-go"
strip "github.com/grokify/html-strip-tags-go"
"github.com/pkg/errors"
)
const (
FindMessage = "俳句を発見しました!"
DetailMessage = "俳句解析結果"
StopMessage = "俳句検出を停止してください"
VisibilityHome = "home"
)
type Haiken struct {
reviewer *ikku.Reviewer
account *Account
rest *RestClient
homeStreamID string
mainStreamID string
}
func NewHaiken(r *ikku.Reviewer, a *Account, rc *RestClient, h, m string) *Haiken {
return &Haiken{
reviewer: r,
account: a,
rest: rc,
homeStreamID: h,
mainStreamID: m,
}
}
func (h *Haiken) Handle(message []byte) error {
var m Message
if err := json.Unmarshal(message, &m); err != nil {
log.Printf("failed to unmarshal message: %s", message)
return err
}
switch m.Body.ID {
case h.homeStreamID:
var s *Status
if err := json.Unmarshal([]byte(m.Body.Body), &s); err != nil {
return err
}
if s.Account.ID == h.account.ID {
log.Printf("skip own post")
return nil
}
if s.Renote != nil {
log.Printf("skip reblog")
return nil
}
for _, id := range s.Mentions {
if id == h.account.ID {
if err := h.handleMention(s); err != nil {
log.Printf("failed to handle mention: %v, message=%#v", err, m)
}
return nil
}
}
if s.Visibility != "public" && s.Visibility != "home" {
log.Printf("skip private post")
return nil
}
if err := h.review(s, false); err != nil {
log.Printf("review err: %v, message=%#v", err, m)
return nil
}
case h.mainStreamID:
if m.Body.Type != "followed" {
log.Printf("Ignoring unknown type: %s", m.Body.Type)
return nil
}
var a *Account
if err := json.Unmarshal([]byte(m.Body.Body), &a); err != nil {
log.Printf("failed to unmarshal account: %s, message=%#v", m.Body.Body, m)
return err
}
log.Printf("Followed by the account: %#v", a)
if err := h.rest.Follow(a.ID, true); err != nil {
log.Printf("failed to follow account: %s, message=%#v", m.Body.Body, m)
return nil
}
default:
log.Printf("Unknown streamID: %s, message=%#v", m.Body.ID, m)
return nil
}
return nil
}
func (h *Haiken) review(s *Status, force bool) error {
log.Printf("reviewing: %#v", s)
con := strip.StripTags(s.Text)
nodes, songs, err := h.reviewer.Search(con)
if err != nil {
return errors.Wrap(err, "failed to review")
}
if force && len(songs) < 1 {
if err := h.sendDetail(nodes, s.Account, s.ID, s.LocalOnly); err != nil {
return errors.Wrap(err, "sendDetail err")
}
}
if len(songs) > 0 {
resBody, err := h.sendReport(nodes, songs, s)
if err != nil {
return errors.Wrap(err, "sendReport err")
}
var resp struct {
CreatedNote *Status `json:"createdNote"`
}
if err := json.Unmarshal(resBody, &resp); err != nil {
return errors.Wrap(err, "resBody unmarshal err")
}
log.Printf("result id: %v", resp.CreatedNote.ID)
if err := h.sendDetail(nodes, s.Account, s.ID, s.LocalOnly); err != nil {
return errors.Wrap(err, "sendDetail err")
}
}
return nil
}
func (h *Haiken) sendReport(nodes []*ikku.Node, songs []*ikku.Song, s *Status) ([]byte, error) {
var sSongs []string
for _, song := range songs {
var s []string
for _, phrases := range song.Phrases() {
var sPhrase []string
for _, phrase := range phrases {
sPhrase = append(sPhrase, phrase.Surface())
}
s = append(s, strings.Join(sPhrase, ""))
}
sSongs = append(sSongs, strings.Join(s, " "))
}
report := fmt.Sprintf("『%s』", strings.Join(sSongs, "』\n\n『"))
if s.Cw != nil {
return h.post(
message(s.Account.Username, s.Account.Host, report),
stringP(s.ID),
stringP(FindMessage),
s.LocalOnly,
)
} else {
return h.post(
message(s.Account.Username, s.Account.Host, fmt.Sprintf("%s\n%s", FindMessage, report)),
stringP(s.ID),
nil,
s.LocalOnly,
)
}
}
func message(username string, host *string, message string) string {
if host == nil {
return fmt.Sprintf("@%s\n%s", username, message)
}
return fmt.Sprintf("@%s@%s\n%s", username, *host, message)
}
func (h *Haiken) sendDetail(nodes []*ikku.Node, act Account, replyID string, localOnly bool) error {
var ds []string
for _, node := range nodes {
ds = append(ds, fmt.Sprintf("[%s:%d]", node.Pronunciation(), node.PronunciationLength()))
}
details := strings.Join(ds, ",")
if act.Username != "" {
details = message(act.Username, act.Host, details)
}
_, err := h.post(details, stringP(replyID), stringP(DetailMessage), localOnly)
return err
}
func (h *Haiken) handleMention(s *Status) error {
if strings.Contains(s.Text, StopMessage) {
return h.rest.Follow(s.Account.ID, false)
}
return h.review(s, true)
}
func (h *Haiken) post(text string, replyID *string, cw *string, localOnly bool) ([]byte, error) {
return h.rest.Post(text, replyID, cw, VisibilityHome, localOnly)
}
func stringP(s string) *string {
return &s
}