-
Notifications
You must be signed in to change notification settings - Fork 0
/
posts.go
106 lines (98 loc) · 2.49 KB
/
posts.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
package blogplus
import (
"bytes"
"log"
"net/url"
"path"
"regexp"
"strings"
)
var (
anchorTagRe = regexp.MustCompile("<a .*?</a>")
htmlTagRe = regexp.MustCompile("<.*?>")
)
func isMeaningfulContent(content string) bool {
return len(content) > 200
}
func IsMeaningfulPost(post Activity) bool {
// nothing for reshares
if post.Verb == "share" {
return false
}
return isMeaningfulContent(htmlTagRe.ReplaceAllString(post.Object.Content, ""))
}
type TextAttachmentContext struct {
VisualAttachments []Attachment
TextAttachments []Attachment
}
func extractSubject(post *Activity) {
lines := strings.Split(post.Object.Content, "<br />")
for _, line := range lines {
line = anchorTagRe.ReplaceAllString(line, "")
if len(line) == 0 {
continue
}
idx := strings.IndexAny(line, "\u3002.")
if idx > 0 {
if strings.HasPrefix(line[idx:], "\u3002") {
idx += len("\u3002")
} else if line[idx] == '.' {
idx += len(".")
} else {
panic("unexpected character at " + line[idx:])
}
post.Object.Subject = line[:idx]
} else {
post.Object.Subject = line
}
return
}
// if it fails to extract the subject, use "title" instead
post.Object.Subject = post.Title
}
func formAttachments(post *Activity) {
attachments := post.Object.Attachments
if len(attachments) == 0 {
post.FormedAttachment = ""
} else if len(attachments) == 1 {
attachment := attachments[0]
buf := bytes.NewBuffer([]byte{})
var err error
switch attachment.ObjectType {
case "video", "photo":
err = ImageAttachmentTempl.Execute(buf, attachment)
case "article":
err = TextAttachmentTempl.Execute(buf,
TextAttachmentContext{
TextAttachments: []Attachment{attachment}})
default:
log.Println("unknown attachment type:", attachment.ObjectType)
}
if err != nil {
log.Println("template error:", err)
}
post.FormedAttachment = buf.String()
} else {
var tc TextAttachmentContext
for _, attachment := range attachments {
if attachment.ObjectType == "article" {
tc.TextAttachments = append(tc.TextAttachments, attachment)
} else {
tc.VisualAttachments = append(tc.VisualAttachments, attachment)
}
}
buf := bytes.NewBuffer([]byte{})
err := TextAttachmentTempl.Execute(buf, tc)
if err != nil {
log.Println("template error:", err)
}
post.FormedAttachment = buf.String()
}
}
func processPost(post *Activity, postUrl *url.URL) {
u := *postUrl
u.Path = path.Join(u.Path, post.Id)
post.Permalink = u.String()
formAttachments(post)
extractSubject(post)
}