-
-
Notifications
You must be signed in to change notification settings - Fork 121
/
service.go
211 lines (172 loc) · 4.6 KB
/
service.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package dao
import (
"fmt"
"io"
"net/http"
"net/url"
"regexp"
"strings"
"time"
"github.com/ArtalkJS/Artalk/internal/entity"
"github.com/ArtalkJS/Artalk/internal/log"
"github.com/ArtalkJS/Artalk/internal/utils"
)
// ===============
// Comment
// ===============
// 获取评论回复链接
func (dao *Dao) GetLinkToReplyByComment(c *entity.Comment, notifyKey ...string) string {
page := dao.FetchPageForComment(c)
rawURL := dao.GetPageAccessibleURL(&page)
// 请求 query
queryMap := map[string]string{
"atk_comment": fmt.Sprintf("%d", c.ID),
}
// atk_notify_key
if len(notifyKey) > 0 {
queryMap["atk_notify_key"] = notifyKey[0]
}
return utils.AddQueryToURL(rawURL, queryMap)
}
// ===============
// Page
// ===============
// 获取可访问链接
func (dao *Dao) GetPageAccessibleURL(p *entity.Page, s ...*entity.Site) string {
if p.AccessibleURL == "" {
acURL := p.Key
// 若 pageKey 为相对路径,生成相对于 site.FirstUrl 配置的 URL
if !utils.ValidateURL(p.Key) {
var site *entity.Site
if len(s) > 0 && s[0] != nil {
site = s[0]
} else {
findSite := dao.FetchSiteForPage(p)
site = &findSite
}
if site != nil {
u1, e1 := url.Parse(dao.CookSite(site).FirstUrl)
u2, e2 := url.Parse(p.Key)
if e1 == nil && e2 == nil {
acURL = u1.ResolveReference(u2).String()
}
}
}
p.AccessibleURL = acURL
}
return p.AccessibleURL
}
func (dao *Dao) FetchPageFromURL(p *entity.Page) error {
cookedPage := dao.CookPage(p)
url := cookedPage.URL
if url == "" {
return fmt.Errorf("URL cannot be null")
}
// 获取 URL 页面 title
title, err := GetTitleByURL(url)
if err == nil && title != "" {
p.Title = title
}
if err := dao.UpdatePage(p); err != nil {
log.Error("Failed to save in FetchPage")
return err
}
return nil
}
func GetTitleByURL(url string) (string, error) {
if !utils.ValidateURL(url) {
log.Error("Invalid URL: " + url)
return "", fmt.Errorf("invalid URL")
}
// Request the HTML page.
res, err := http.Get(url)
if err != nil {
log.Error(err)
return "", err
}
defer res.Body.Close()
if res.StatusCode != 200 {
log.Error(fmt.Sprintf("status code error: %d '%s' '%s'", res.StatusCode, res.Status, url))
return "", fmt.Errorf("status code error")
}
body, err := io.ReadAll(res.Body)
if err != nil {
return "", err
}
data := pageExtractFromHTML(body)
if data.RedirectURL != "" {
return GetTitleByURL(data.RedirectURL)
}
return data.Title, nil
}
// the data extracted from a page html
type pageExtractData struct {
Title string
RedirectURL string
}
func pageExtractFromHTML(html []byte) (data pageExtractData) {
// 读取页面 title
titleTagReg := regexp.MustCompile(`(?i)<title>(.*?)</title>`)
titleTagMatch := titleTagReg.FindSubmatch(html)
if len(titleTagMatch) > 1 {
data.Title = strings.TrimSpace(string(titleTagMatch[1]))
}
// 如果页面有跳转
redirectTagReg := regexp.MustCompile(`(?i)<meta.*?http-equiv="refresh.*?content="(.*?)".*?>`)
redirectTagMatch := redirectTagReg.FindSubmatch(html)
if len(redirectTagMatch) > 1 {
urlReg := regexp.MustCompile(`https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)`)
match := urlReg.FindSubmatch(redirectTagMatch[1])
if len(match) > 0 {
data.RedirectURL = strings.TrimSpace(string(match[0]))
}
}
return data
}
// ===============
// Notify
// ===============
func (dao *Dao) NotifySetInitial(n *entity.Notify) error {
n.IsRead = false
n.IsEmailed = false
return dao.DB().Save(n).Error
}
func (dao *Dao) NotifySetRead(n *entity.Notify) error {
n.IsRead = true
nowTime := time.Now()
n.ReadAt = &nowTime
return dao.DB().Save(n).Error
}
func (dao *Dao) NotifySetEmailed(n *entity.Notify) error {
n.IsEmailed = true
nowTime := time.Now()
n.EmailAt = &nowTime
return dao.DB().Save(n).Error
}
func (dao *Dao) GetReadLinkByNotify(n *entity.Notify) string {
c := dao.FetchCommentForNotify(n)
return dao.GetLinkToReplyByComment(&c, n.Key)
}
// ===============
// Vote
// ===============
func (dao *Dao) VoteSync() {
var comments []entity.Comment
dao.DB().Find(&comments)
for _, c := range comments {
voteUp := dao.GetVoteNum(c.ID, string(entity.VoteTypeCommentUp))
voteDown := dao.GetVoteNum(c.ID, string(entity.VoteTypeCommentDown))
c.VoteUp = int(voteUp)
c.VoteDown = int(voteDown)
dao.UpdateComment(&c)
}
var pages []entity.Page
dao.DB().Find(&pages)
for _, p := range pages {
voteUp := dao.GetVoteNum(p.ID, string(entity.VoteTypePageUp))
voteDown := dao.GetVoteNum(p.ID, string(entity.VoteTypePageDown))
p.VoteUp = voteUp
p.VoteDown = voteDown
dao.UpdatePage(&p)
}
}