Skip to content

Commit 7a93227

Browse files
Use the real payload format not the wrong mock one
Signed-off-by: eternal-flame-AD <yume@yumechi.jp>
1 parent c45f590 commit 7a93227

File tree

4 files changed

+115
-175
lines changed

4 files changed

+115
-175
lines changed

README.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ sources:
2020
2121
2. Copy the Webhook URL and secret to Misskey (Settings -> Webhook)
2222
23-
a. Set the base URL to receive any of 'replied to', 'renoted', 'mentioned' (reactions seem to have bugs upstream and nothing is received)
24-
25-
b. Set the base URL plus `/follow` to receive 'followed' events
23+
a. Set the base URL to receive any of 'replied to', 'renoted', 'mentioned', 'followed' (reactions seem to have bugs upstream and nothing is received)
2624
2725
3. Done!
2826

plugin.go

+101-124
Original file line numberDiff line numberDiff line change
@@ -100,163 +100,140 @@ func (c *MisskeyHookPlugin) RegisterWebhook(basePath string, g *gin.RouterGroup)
100100
return
101101
}
102102

103-
var payload WebhookPayload[NoteRelatedWebhookPayloadBody]
103+
var payload WebhookPayload[UserPayload]
104104

105105
if err := ctx.BindJSON(&payload); err != nil {
106106
ctx.JSON(400, gin.H{"error": "Invalid JSON"})
107107
return
108108
}
109109

110-
post := payload.Body
110+
if payload.Body.Note != nil {
111+
post := payload.Body.Note
111112

112-
title := fmt.Sprintf("[%s] [%s] %s", payload.Type, src.Name, post.ID)
113+
title := fmt.Sprintf("[%s] [%s] %s", payload.Type, src.Name, post.ID)
113114

114-
var url string
115-
if payload.Server != "" {
116-
url = strings.TrimRight(payload.Server, "/") + "/notes/" + post.ID
117-
}
118-
119-
var message bytes.Buffer
120-
121-
message.WriteString(fmt.Sprintf("User: %s (%s)\n\n", escapeMarkdown(post.User.Name), escapeMarkdown(post.User.Username)))
122-
123-
if post.Cw != nil {
124-
message.WriteString(fmt.Sprintf("CW: %s\n\n", escapeMarkdown(*post.Cw)))
125-
} else if post.Text != nil {
126-
message.WriteString(escapeMarkdown(*post.Text))
127-
} else {
128-
message.WriteString("<missing content>")
129-
}
130-
131-
if post.Reply != nil {
132-
message.WriteString("\n\n---\n\n")
133-
134-
message.WriteString(fmt.Sprintf("Parent: %s\n\n", escapeMarkdown(post.Reply.User.Name)))
135-
136-
if post.Reply.Cw != nil {
137-
message.WriteString(fmt.Sprintf("CW: %s\n\n", escapeMarkdown(*post.Reply.Cw)))
138-
} else if post.Reply.Text != nil {
139-
message.WriteString(escapeMarkdown(*post.Reply.Text))
140-
} else {
141-
message.WriteString("<missing content>")
115+
var url string
116+
if payload.Server != "" {
117+
url = strings.TrimRight(payload.Server, "/") + "/notes/" + post.ID
142118
}
143119

144-
message.WriteString("\n\n---\n\n")
145-
}
146-
147-
if post.Renote != nil {
148-
message.WriteString("\n\n---\n\n")
120+
var message bytes.Buffer
149121

150-
message.WriteString(fmt.Sprintf("Renote of: %s\n\n", escapeMarkdown(post.Renote.User.Name)))
122+
message.WriteString(fmt.Sprintf("User: %s (%s)\n\n", escapeMarkdown(post.User.Name), escapeMarkdown(post.User.Username)))
151123

152-
if post.Renote.Cw != nil {
153-
message.WriteString(fmt.Sprintf("CW: %s\n\n", escapeMarkdown(*post.Renote.Cw)))
154-
} else if post.Renote.Text != nil {
155-
message.WriteString(escapeMarkdown(*post.Renote.Text))
124+
if post.Cw != nil {
125+
message.WriteString(fmt.Sprintf("CW: %s\n\n", escapeMarkdown(*post.Cw)))
126+
} else if post.Text != nil {
127+
message.WriteString(escapeMarkdown(*post.Text))
156128
} else {
157129
message.WriteString("<missing content>")
158130
}
159131

160-
message.WriteString("\n\n---\n\n")
161-
}
132+
if post.Reply != nil {
133+
message.WriteString("\n\n---\n\n")
162134

163-
msg := plugin.Message{
164-
Title: title,
165-
Message: message.String(),
166-
Priority: 0,
167-
Extras: map[string]interface{}{
168-
"misskey::payload": map[string]interface{}{
169-
"note": post,
170-
},
171-
"client::display": map[string]interface{}{
172-
"contentType": "text/markdown",
173-
},
174-
},
175-
}
176-
177-
if url != "" {
178-
msg.Extras["client::notification"] = map[string]interface{}{
179-
"click": map[string]interface{}{
180-
"url": url,
181-
},
182-
}
183-
}
135+
message.WriteString(fmt.Sprintf("Parent: %s\n\n", escapeMarkdown(post.Reply.User.Name)))
184136

185-
if err := c.msgHandler.SendMessage(msg); err != nil {
186-
ctx.JSON(500, gin.H{"error": "Failed to send message"})
187-
return
188-
}
189-
})
190-
191-
g.HEAD("/push/misskey/:slug/follow", func(ctx *gin.Context) {
192-
ctx.SetAccepted("application/json")
193-
ctx.Status(200)
194-
})
195-
196-
g.GET("/push/misskey/:slug/follow", func(ctx *gin.Context) {
197-
ctx.JSON(405, gin.H{"error": "Method Not Allowed"})
198-
})
137+
if post.Reply.Cw != nil {
138+
message.WriteString(fmt.Sprintf("CW: %s\n\n", escapeMarkdown(*post.Reply.Cw)))
139+
} else if post.Reply.Text != nil {
140+
message.WriteString(escapeMarkdown(*post.Reply.Text))
141+
} else {
142+
message.WriteString("<missing content>")
143+
}
199144

200-
g.POST("/push/misskey/:slug/follow", func(ctx *gin.Context) {
201-
202-
secret := ctx.GetHeader("X-Misskey-Hook-Secret")
145+
message.WriteString("\n\n---\n\n")
146+
}
203147

204-
if secret == "" {
205-
ctx.JSON(400, gin.H{"error": "Missing secret"})
206-
return
207-
}
148+
if post.Renote != nil {
149+
message.WriteString("\n\n---\n\n")
208150

209-
src := c.config.GetSource(ctx.Param("slug"))
151+
message.WriteString(fmt.Sprintf("Renote of: %s\n\n", escapeMarkdown(post.Renote.User.Name)))
210152

211-
if src == nil || src.Secret == DummySecret || src.Secret != secret {
212-
ctx.JSON(404, gin.H{"error": "Source not found or secret mismatch"})
213-
return
214-
}
153+
if post.Renote.Cw != nil {
154+
message.WriteString(fmt.Sprintf("CW: %s\n\n", escapeMarkdown(*post.Renote.Cw)))
155+
} else if post.Renote.Text != nil {
156+
message.WriteString(escapeMarkdown(*post.Renote.Text))
157+
} else {
158+
message.WriteString("<missing content>")
159+
}
215160

216-
var payload WebhookPayload[WebhookUser]
161+
message.WriteString("\n\n---\n\n")
162+
}
217163

218-
if err := ctx.BindJSON(&payload); err != nil {
219-
ctx.JSON(400, gin.H{"error": "Invalid JSON"})
220-
return
221-
}
164+
msg := plugin.Message{
165+
Title: title,
166+
Message: message.String(),
167+
Priority: 0,
168+
Extras: map[string]interface{}{
169+
"misskey::payload": map[string]interface{}{
170+
"note": post,
171+
},
172+
"client::display": map[string]interface{}{
173+
"contentType": "text/markdown",
174+
},
175+
},
176+
}
222177

223-
user := payload.Body
178+
if url != "" {
179+
msg.Extras["client::notification"] = map[string]interface{}{
180+
"click": map[string]interface{}{
181+
"url": url,
182+
},
183+
}
184+
}
224185

225-
title := fmt.Sprintf("[%s] [%s] %s", payload.Type, src.Name, user.Name)
186+
if err := c.msgHandler.SendMessage(msg); err != nil {
187+
ctx.JSON(500, gin.H{"error": "Failed to send message"})
188+
return
189+
}
190+
} else if payload.Body.User != nil {
191+
user := payload.Body.User
226192

227-
var url string
228-
if payload.Server != "" {
229-
url = strings.TrimRight(payload.Server, "/") + "/users/" + user.ID
230-
}
193+
title := fmt.Sprintf("[%s] [%s] %s", payload.Type, src.Name, user.UserNameFull())
231194

232-
var message bytes.Buffer
195+
var url string
233196

234-
message.WriteString(fmt.Sprintf("User: %s (%s)\n\n", escapeMarkdown(user.Name), escapeMarkdown(user.Username)))
197+
if payload.Server != "" {
198+
url = strings.TrimRight(payload.Server, "/") + "/" + user.UserNameFull()
199+
}
235200

236-
msg := plugin.Message{
237-
Title: title,
238-
Message: message.String(),
239-
Priority: 0,
240-
Extras: map[string]interface{}{
241-
"misskey::payload": map[string]interface{}{
242-
"user": user,
243-
},
244-
"client::display": map[string]interface{}{
245-
"contentType": "text/markdown",
201+
var message bytes.Buffer
202+
203+
message.WriteString(fmt.Sprintf("User: %s (%s)\n\n", escapeMarkdown(user.Name), escapeMarkdown(user.Username)))
204+
205+
message.WriteString(fmt.Sprintf("Followers: %d\n", user.FollowersCount))
206+
message.WriteString(fmt.Sprintf("Following: %d\n", user.FollowingCount))
207+
message.WriteString(fmt.Sprintf("Notes: %d\n", user.NotesCount))
208+
209+
msg := plugin.Message{
210+
Title: title,
211+
Message: message.String(),
212+
Priority: 0,
213+
Extras: map[string]interface{}{
214+
"misskey::payload": map[string]interface{}{
215+
"user": user,
216+
},
217+
"client::display": map[string]interface{}{
218+
"contentType": "text/markdown",
219+
},
246220
},
247-
},
248-
}
221+
}
249222

250-
if url != "" {
251-
msg.Extras["client::notification"] = map[string]interface{}{
252-
"click": map[string]interface{}{
253-
"url": url,
254-
},
223+
if url != "" {
224+
msg.Extras["client::notification"] = map[string]interface{}{
225+
"click": map[string]interface{}{
226+
"url": url,
227+
},
228+
}
255229
}
256-
}
257230

258-
if err := c.msgHandler.SendMessage(msg); err != nil {
259-
ctx.JSON(500, gin.H{"error": "Failed to send message"})
231+
if err := c.msgHandler.SendMessage(msg); err != nil {
232+
ctx.JSON(500, gin.H{"error": "Failed to send message"})
233+
return
234+
}
235+
} else {
236+
ctx.JSON(400, gin.H{"error": "Invalid payload, unknown body"})
260237
return
261238
}
262239
})
@@ -356,7 +333,7 @@ Enabled: {{ .State.Enabled }}
356333
### **{{ .Name }}** ({{ .Slug }})
357334
358335
- Secret: {{ .Secret }}
359-
- URL: [{{ .URL }}]({{ .URL }}) (Append /abuse to receive abuse reports, /follow to receive user follow events)
336+
- URL: [{{ .URL }}]({{ .URL }}) (Append /abuse to receive abuse reports)
360337
361338
{{ end }}
362339
`)))

receiving.go

+13
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,19 @@ type WebhookUser struct {
99
Username string `json:"username,omitempty"`
1010
UsernameLower string `json:"usernameLower,omitempty"`
1111
Name string `json:"name,omitempty"`
12+
Host string `json:"host,omitempty"`
1213
FollowersCount int `json:"followersCount,omitempty"`
1314
FollowingCount int `json:"followingCount,omitempty"`
1415
NotesCount int `json:"notesCount,omitempty"`
1516
}
1617

18+
func (u *WebhookUser) UserNameFull() string {
19+
if u.Host != "" {
20+
return "@" + u.Username + "@" + u.Host
21+
}
22+
return "@" + u.Username
23+
}
24+
1725
type WebhookPayload[T any] struct {
1826
Server string `json:"server,omitempty"`
1927
Type string `json:"type,omitempty"`
@@ -26,6 +34,11 @@ type WebhookPayload[T any] struct {
2634
Body T `json:"body,omitempty"`
2735
}
2836

37+
type UserPayload struct {
38+
User *WebhookUser `json:"user,omitempty"`
39+
Note *NoteRelatedWebhookPayloadBody `json:"note,omitempty"`
40+
}
41+
2942
func (p *WebhookPayload[T]) CreatedAtUnix() int64 {
3043
return int64(p.CreatedAt / 1000)
3144
}

receiving_test.go

-48
This file was deleted.

0 commit comments

Comments
 (0)