-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessage.go
67 lines (58 loc) · 1.68 KB
/
message.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
package main
import (
"regexp"
"github.com/goadesign/goa"
"github.com/smousa/chatty/app"
)
var (
mentionsRe = regexp.MustCompile(`@(\w+)`) // TODO: emails?
emoticonsRe = regexp.MustCompile(`\(([0-9A-z]{1,15})\)`)
linksRe = regexp.MustCompile(`(https?://\S+)`) // TODO: less strict for urls
)
// MessageController implements the message resource.
type MessageController struct {
*goa.Controller
URLReader URLReader
}
// NewMessageController creates a message controller.
func NewMessageController(service *goa.Service) *MessageController {
return &MessageController{Controller: service.NewController("MessageController")}
}
// Parse runs the parse action.
func (c *MessageController) Parse(ctx *app.ParseMessageContext) error {
res := &app.ChattyMessageInfo{
Mentions: c.getMentions(ctx.Payload.Input),
Emoticons: c.getEmoticons(ctx.Payload.Input),
Links: c.getLinks(ctx.Payload.Input),
}
return ctx.OK(res)
}
func (c *MessageController) getMentions(input string) (mentions []string) {
out := mentionsRe.FindAllStringSubmatch(input, -1)
mentions = make([]string, len(out))
for i, o := range out {
mentions[i] = o[1]
}
return
}
func (c *MessageController) getEmoticons(input string) (emoticons []string) {
out := emoticonsRe.FindAllStringSubmatch(input, -1)
emoticons = make([]string, len(out))
for i, o := range out {
emoticons[i] = o[1]
}
return
}
func (c *MessageController) getLinks(input string) (links app.ChattyLinkCollection) {
out := linksRe.FindAllStringSubmatch(input, -1)
for _, o := range out {
title, err := c.URLReader.PageTitle(o[1])
if err == nil {
links = append(links, &app.ChattyLink{
URL: o[1],
Title: title,
})
}
}
return
}