Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fb8fbd8

Browse files
author
xuxinhua
committed
@的bugfix;每周精选邮件bugfix
1 parent 4b5fd94 commit fb8fbd8

File tree

10 files changed

+67
-36
lines changed

10 files changed

+67
-36
lines changed

websites/code/studygolang/conf/config.json.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
"data": "data/max_online_num",
1414
"pid": "pid/studygolang.pid",
1515

16+
"unsubscribe_token_key": "xxxxx",
17+
1618
"qiniu_access_key": "xxxxxxxxxxxxx",
1719
"qiniu_secret_key": "xxxxxxxxxxxxxx",
1820
"qiniu_bucket_name": "xxxxxxxxx",

websites/code/studygolang/src/controller/account.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ func RegisterHandler(rw http.ResponseWriter, req *http.Request) {
6565

6666
func sendWelcomeMail(email []string) {
6767
content := `Welcome to Study Golang.<br><br>
68-
欢迎您,成功注册成为 Golang中文社区 | Go语言学习园地 会员<br><br>
68+
欢迎您,成功注册成为 Go语言中文网 | Go语言学习园地 会员<br><br>
6969
Golang中文社区是一个Go语言技术社区,完全用Go语言开发。我们为gopher们提供一个好的学习交流场所。加入到社区中来,参与分享,学习,不断提高吧。前往 <a href="http://studygolang.com">Golang中文社区 | Go语言学习园地</a><br>
70-
<div style="text-align:right;">&copy;2012-2014 studygolang.com Golang中文社区 | Go语言学习园地</div>`
70+
<div style="text-align:right;">&copy;2012-2015 studygolang.com Go语言中文网 | Golang中文社区 | Go语言学习园地</div>`
7171
service.SendMail("Golang中文社区 | Go语言学习园地 注册成功通知", content, email)
7272
}
7373

websites/code/studygolang/src/controller/user.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func EmailUnsubHandler(rw http.ResponseWriter, req *http.Request) {
7777
return
7878
}
7979

80-
realToken := service.GenUnsubscribeToken(user.Username, user.Email)
80+
realToken := service.GenUnsubscribeToken(user)
8181
if token != realToken {
8282
util.Redirect(rw, req, "/")
8383
return

websites/code/studygolang/src/model/user.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ package model
88

99
import (
1010
"fmt"
11-
"logger"
1211
"math/rand"
12+
13+
"logger"
1314
"util"
1415
)
1516

@@ -120,9 +121,6 @@ func (this *UserLogin) GetPasscode() string {
120121
return this.passcode
121122
}
122123

123-
// 产生退订邮件的 token key
124-
const UNSUBSCRIBE_TOKEN_KEY = "sg_#fuol$ew"
125-
126124
// 用户基本信息
127125
type User struct {
128126
Uid int `json:"uid"`
@@ -246,6 +244,13 @@ func (this *User) colFieldMap() map[string]interface{} {
246244
}
247245
}
248246

247+
func (this *User) String() string {
248+
buffer := util.NewBuffer()
249+
buffer.Append(this.Username).Append(this.Email).AppendInt(this.Uid).Append(this.Mtime)
250+
251+
return buffer.String()
252+
}
253+
249254
// 活跃用户信息
250255
// 活跃度规则:
251256
// 1、注册成功后 +2

websites/code/studygolang/src/server/studygolang/background.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func ServeBackGround() {
3737
c.AddFunc("@daily", service.GenSitemap)
3838

3939
// 给用户发邮件,如通知网站最近的动态,每周的晨读汇总等
40-
c.AddFunc("0 0 10 * * 0", service.EmailNotice)
40+
c.AddFunc("0 0 9 * * 1", service.EmailNotice)
4141

4242
c.Start()
4343
}

websites/code/studygolang/src/service/comment.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,10 @@ func decodeCmtContent(comment *model.Comment) string {
6969
// 安全过滤
7070
content := template.HTMLEscapeString(comment.Content)
7171
// @别人
72-
reg := regexp.MustCompile(`@([^\s@]{4,20})`)
73-
content = reg.ReplaceAllString(content, `<a href="/user/$1" title="@$1">@$1</a>`)
72+
content = parseAtUser(content)
7473

7574
// 回复某一楼层
76-
reg = regexp.MustCompile(`#(\d+)楼`)
75+
reg := regexp.MustCompile(`#(\d+)楼`)
7776
url := fmt.Sprintf("%s%d#comment", model.PathUrlMap[comment.Objtype], comment.Objid)
7877
content = reg.ReplaceAllString(content, `<a href="`+url+`$1" title="$1">#$1<span>楼</span></a>`)
7978

websites/code/studygolang/src/service/common.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package service
22

33
import (
4+
"fmt"
45
"net/url"
6+
"regexp"
7+
8+
"model"
59
"util"
610
)
711

@@ -36,3 +40,24 @@ func GenSetClause(form url.Values, fields []string) string {
3640
}
3741
return ""
3842
}
43+
44+
// @某人
45+
func parseAtUser(content string) string {
46+
user := model.NewUser()
47+
48+
reg := regexp.MustCompile(`@([^\s@]{4,20})`)
49+
return reg.ReplaceAllStringFunc(content, func(matched string) string {
50+
username := matched[1:]
51+
52+
// 校验 username 是否存在
53+
err := user.Where("username=?", username).Find()
54+
if err != nil {
55+
return matched
56+
}
57+
58+
if user.Username != username {
59+
return matched
60+
}
61+
return fmt.Sprintf(`<a href="/user/%s" title="%s">%s</a>`, username, matched, matched)
62+
})
63+
}

websites/code/studygolang/src/service/mail.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@ var emailTpl = template.Must(template.New("email.html").Funcs(emailFuncMap).Pars
5656
// 订阅邮件通知
5757
func EmailNotice() {
5858

59-
beginTime := time.Now().Add(-7*24*time.Hour).Format("2006-01-02") + " 00:00:00"
59+
beginDate := time.Now().Add(-7 * 24 * time.Hour).Format("2006-01-02")
60+
endDate := time.Now().Add(-24 * time.Hour).Format("2006-01-02")
61+
62+
beginTime := beginDate + " 00:00:00"
6063

6164
// 本周晨读(过去 7 天)
6265
readings, err := model.NewMorningReading().Where("ctime>? AND rtype=0", beginTime).Order("id DESC").FindAll()
@@ -77,9 +80,11 @@ func EmailNotice() {
7780
}
7881

7982
data := map[string]interface{}{
80-
"readings": readings,
81-
"articles": articles,
82-
"topics": topics,
83+
"readings": readings,
84+
"articles": articles,
85+
"topics": topics,
86+
"beginDate": beginDate,
87+
"endDate": endDate,
8388
}
8489

8590
// 给所有用户发送邮件
@@ -108,7 +113,7 @@ func EmailNotice() {
108113
}
109114

110115
data["email"] = user.Email
111-
data["token"] = GenUnsubscribeToken(user.Username, user.Email)
116+
data["token"] = GenUnsubscribeToken(user)
112117

113118
content, err := genEmailContent(data)
114119
if err != nil {
@@ -117,18 +122,16 @@ func EmailNotice() {
117122
}
118123

119124
go func(content, email string) {
120-
SendMail("每周精选", content, []string{"[email protected]"})
125+
SendMail("每周精选", content, []string{email})
121126
}(content, user.Email)
122-
break
123127
}
124-
break
125128
}
126129

127130
}
128131

129132
// 生成 退订 邮件的 token
130-
func GenUnsubscribeToken(username, email string) string {
131-
return util.Md5(username + email + model.UNSUBSCRIBE_TOKEN_KEY)
133+
func GenUnsubscribeToken(user *model.User) string {
134+
return util.Md5(user.String() + Config["unsubscribe_token_key"])
132135
}
133136

134137
func genEmailContent(data map[string]interface{}) (string, error) {

websites/code/studygolang/src/service/topic.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"errors"
1111
"html/template"
1212
"net/url"
13-
"regexp"
1413
"strconv"
1514
"strings"
1615

@@ -195,8 +194,7 @@ func decodeTopicContent(topic *model.Topic) string {
195194
content = util.EmbedWide(content)
196195

197196
// @别人
198-
reg := regexp.MustCompile(`@([^\s@]{4,20})`)
199-
return reg.ReplaceAllString(content, `<a href="/user/$1" title="@$1">@$1</a>`)
197+
return parseAtUser(content)
200198
}
201199

202200
// 获得主题列表页需要的数据

websites/code/studygolang/template/email.html

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<td style="padding:30px 0 5px 0;">
1212
<table align="center" border="0" cellpadding="0" cellspacing="0" width="600" style="background-color: white; padding: 10px;">
1313
<tr>
14-
<td style="font-size: 20px;"><a href="http://studygolang.com" target="_blank" style="text-decoration: none;">Go语言中文网</a>&nbsp;每周精选</td>
14+
<td style="font-size: 20px;"><a href="http://studygolang.com?fr=email" target="_blank" style="text-decoration: none;">Go语言中文网</a>&nbsp;每周精选</td>
1515
<td align="right">{{.beginDate}}至{{.endDate}}</td>
1616
</tr>
1717
</table>
@@ -24,15 +24,15 @@
2424
<table align="center" border="0" cellpadding="0" cellspacing="0" width="600" style="background-color: white; padding: 10px;">
2525
<tr>
2626
<td style="padding: 10px 0 20px 0;font-size: 20px;" width="85%">本周每日晨读</td>
27-
<td align="right" style="padding: 10px 0 20px 0;"><a href="http://studygolang.com/readings" target="_blank">历史晨读>></a></td>
27+
<td align="right" style="padding: 10px 0 20px 0;"><a href="http://studygolang.com/readings?fr=email" target="_blank">历史晨读>></a></td>
2828
</tr>
2929
<tr>
3030
<td colspan="2">
3131
<table border="0" cellpadding="0" cellspacing="0" width="100%">
3232
{{range .readings}}
3333
<tr>
3434
<td width="10%" align="center" style="background-color: #DB6D4C; color: white;">{{time_format .Ctime}}</td>
35-
<td style="padding-left: 10px;">{{.Content}}<a href="http://studygolang.com/readings/{{.Id}}" target="_blank">我要阅读>></a>{{if .Urls}}&nbsp;&nbsp;相关阅读:{{range .Urls}}<a href="http://studygolang.com/wr?u={{.}}" target="_blank">{{end}}{{end}}</td>
35+
<td style="padding-left: 10px;">{{.Content}}<a href="http://studygolang.com/readings/{{.Id}}?fr=email" target="_blank">我要阅读>></a>{{if .Urls}}&nbsp;&nbsp;相关阅读:{{range .Urls}}<a href="http://studygolang.com/wr?u={{.}}&fr=email" target="_blank">{{end}}{{end}}</td>
3636
</tr>
3737
<tr><td>&nbsp;</td><td></td></tr>
3838
{{end}}
@@ -50,7 +50,7 @@
5050
<table align="center" border="0" cellpadding="0" cellspacing="0" width="600" style="background-color: white; padding: 10px;">
5151
<tr>
5252
<td style="padding: 10px 0 20px 0;font-size: 20px;" width="85%">本周精彩文章</td>
53-
<td align="right" style="padding: 10px 0 20px 0;"><a href="http://studygolang.com/articles" target="_blank">更多文章>></a></td>
53+
<td align="right" style="padding: 10px 0 20px 0;"><a href="http://studygolang.com/articles?fr=email" target="_blank">更多文章>></a></td>
5454
</tr>
5555
<tr>
5656
<td colspan="2">
@@ -60,11 +60,11 @@
6060
<td>
6161
<table>
6262
<tr>
63-
<td style="padding-bottom: 5px;"><a href="http://studygolang.com/articles/{{.Id}}" target="_blank" title="{{.Title}}">{{.Title}}</a></td>
63+
<td style="padding-bottom: 5px;"><a href="http://studygolang.com/articles/{{.Id}}?fr=email" target="_blank" title="{{.Title}}">{{.Title}}</a></td>
6464
</tr>
6565
<tr>
6666
<td style="font-size: 13px; color: #aaa;">
67-
{{substring .Txt 250 "..."}}<a href="http://studygolang.com/articles/{{.Id}}" target="_blank">阅读全文</a>
67+
{{substring .Txt 250 "..."}}<a href="http://studygolang.com/articles/{{.Id}}?fr=email" target="_blank">阅读全文</a>
6868
</td>
6969
</tr>
7070
</table>
@@ -86,7 +86,7 @@
8686
<table align="center" border="0" cellpadding="0" cellspacing="0" width="600" style="background-color: white; padding: 10px;">
8787
<tr>
8888
<td style="padding: 10px 0 20px 0;font-size: 20px;" width="85%">本周热门主题</td>
89-
<td align="right" style="padding: 10px 0 20px 0;"><a href="http://studygolang.com/topics" target="_blank">更多主题>></a></td>
89+
<td align="right" style="padding: 10px 0 20px 0;"><a href="http://studygolang.com/topics?fr=email" target="_blank">更多主题>></a></td>
9090
</tr>
9191
<tr>
9292
<td colspan="2">
@@ -96,14 +96,14 @@
9696
<td>
9797
<table>
9898
<tr>
99-
<td style="padding-bottom: 5px;"><a href="http://studygolang.com/topics/{{.Tid}}" target="_blank" title="{{.Title}}">
99+
<td style="padding-bottom: 5px;"><a href="http://studygolang.com/topics/{{.Tid}}?fr=email" target="_blank" title="{{.Title}}">
100100
{{.Title}}
101101
</a></td>
102102
</tr>
103103
<tr>
104104
<td style="font-size: 13px; color: #aaa;">
105105
{{substring .Content 250 "..."}}
106-
<a href="http://studygolang.com/topics/{{.Tid}}">阅读全文</a></td>
106+
<a href="http://studygolang.com/topics/{{.Tid}}?fr=email">阅读全文</a></td>
107107
</tr>
108108
</table>
109109
</td>
@@ -125,13 +125,12 @@
125125
<td align="center" style="font-family: Arial, sans-serif; font-size: 13px;">这封邮件的收件地址是 {{.email}};点击 <a href="http://studygolang.com/user/email/unsubscribe?u={{.token}}&email={{.email}}" target="_blank">退订</a></td>
126126
</tr>
127127
<tr>
128-
<td align="center" style="font-family: Arial, sans-serif; font-size: 13px; padding: 5px 0 10px 0;">© 2015 Go语言中文网 studygolang.com</td>
128+
<td align="center" style="font-family: Arial, sans-serif; font-size: 13px; padding: 5px 0 10px 0;">© 2012-2015 Go语言中文网 studygolang.com</td>
129129
</tr>
130130
</table>
131131
</td>
132132
</tr>
133133
</table>
134-
<script type="text/javascript">
135-
</script>
134+
<a href="http://www.51.la/?17656270" target="_blank" style="display: none;"><img alt="&#x6211;&#x8981;&#x5566;&#x514D;&#x8D39;&#x7EDF;&#x8BA1;" src="http://img.users.51.la/17656270.asp" style="border:none" /></a>
136135
</body>
137136
</html>

0 commit comments

Comments
 (0)