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

Skip to content

Commit a0a6da8

Browse files
committed
首页排序规则改版
1 parent 398eec8 commit a0a6da8

File tree

13 files changed

+191
-29
lines changed

13 files changed

+191
-29
lines changed

cmd/studygolang/background.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func ServeBackGround() {
7474
c.AddFunc("@daily", logic.DefaultUserRich.AwardCooper)
7575

7676
// 首页推荐自动调整
77-
c.AddFunc("@every 2m", logic.DefaultFeed.Recommend)
77+
c.AddFunc("@daily", logic.DefaultFeed.AutoUpdateSeq)
7878

7979
}
8080

config/db.sql

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -615,8 +615,7 @@ CREATE TABLE IF NOT EXISTS `feed` (
615615
`tags` varchar(63) NOT NULL DEFAULT '' COMMENT 'tag,逗号分隔',
616616
`cmtnum` int unsigned NOT NULL DEFAULT 0 COMMENT '评论数',
617617
`top` tinyint unsigned NOT NULL DEFAULT 0 COMMENT '置顶,0否,1置顶',
618-
`recommend` tinyint unsigned NOT NULL DEFAULT 0 COMMENT '是否在首页推荐,默认不推荐,达到一定要求才推荐',
619-
`seq` int unsigned NOT NULL DEFAULT 0 COMMENT '排序用,越大越靠前',
618+
`seq` int NOT NULL DEFAULT 0 COMMENT '排序用,越大越靠前',
620619
`state` tinyint unsigned NOT NULL DEFAULT 0 COMMENT '状态:0-正常;1-下线',
621620
`created_at` timestamp NOT NULL DEFAULT '2013-03-15 14:38:09' COMMENT '创建时间',
622621
`updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '最后更新时间',

http/controller/index.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,12 @@ func (IndexController) Index(ctx echo.Context) error {
5757

5858
data["all_nodes"] = logic.GenNodes()
5959

60-
if tab == "all" {
60+
if tab == model.TabAll || tab == model.TabRecommend {
6161
pageHtml := paginator.SetTotal(logic.DefaultFeed.GetTotalCount(context.EchoContext(ctx))).GetPageHtml(ctx.Request().URL.Path)
6262

6363
data["page"] = template.HTML(pageHtml)
6464

6565
data["total"] = paginator.GetTotal()
66-
6766
}
6867

6968
return render(ctx, "index.html", data)

logic/feed.go

Lines changed: 98 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ import (
1111
"strconv"
1212
"time"
1313

14+
"github.com/polaris1119/config"
15+
"github.com/polaris1119/logger"
16+
1417
. "github.com/studygolang/studygolang/db"
1518
"github.com/studygolang/studygolang/model"
1619
"github.com/studygolang/studygolang/util"
@@ -33,11 +36,15 @@ func (self FeedLogic) GetTotalCount(ctx context.Context) int64 {
3336
return count
3437
}
3538

36-
func (self FeedLogic) FindRecentWithPaginator(ctx context.Context, paginator *Paginator) []*model.Feed {
39+
func (self FeedLogic) FindRecentWithPaginator(ctx context.Context, paginator *Paginator, tab string) []*model.Feed {
3740
objLog := GetLogger(ctx)
3841

3942
feeds := make([]*model.Feed, 0)
40-
err := MasterDB.Where("recommend=1").Desc("seq").Desc("updated_at").Limit(paginator.PerPage(), paginator.Offset()).Find(&feeds)
43+
session := MasterDB.Limit(paginator.PerPage(), paginator.Offset())
44+
if tab == model.TabRecommend {
45+
session.Desc("seq")
46+
}
47+
err := session.Desc("updated_at").Find(&feeds)
4148
if err != nil {
4249
objLog.Errorln("FeedLogic FindRecent error:", err)
4350
return nil
@@ -72,12 +79,43 @@ func (self FeedLogic) FindTop(ctx context.Context) []*model.Feed {
7279
return self.fillOtherInfo(ctx, feeds, false)
7380
}
7481

75-
// 首页按规则调整:推荐
76-
// 暂定规则:在一定时间内发布
77-
// 1. 超过 7 天,排序值置为 0;
78-
// 2.
79-
func (self FeedLogic) Recommend() {
82+
// AutoUpdateSeq 每天自动更新一次动态的排序(校准)
83+
func (self FeedLogic) AutoUpdateSeq() {
84+
feedDay := config.ConfigFile.MustInt("global", "feed_day", 7)
85+
86+
var err error
87+
offset, limit := 0, 100
88+
for {
89+
feeds := make([]*model.Feed, 0)
90+
err = MasterDB.Where("seq>0").Limit(limit, offset).Find(&feeds)
91+
if err != nil || len(feeds) == 0 {
92+
return
93+
}
94+
95+
offset += limit
96+
97+
for _, feed := range feeds {
98+
if feed.State == model.FeedOffline {
99+
continue
100+
}
101+
102+
elaspe := int(time.Now().Sub(time.Time(feed.CreatedAt)).Hours())
103+
104+
if feed.Uid > 0 {
105+
user := DefaultUser.FindOne(nil, "uid", feed.Uid)
106+
if DefaultUser.IsAdmin(user) {
107+
elaspe = int(time.Now().Sub(time.Time(feed.UpdatedAt)).Hours())
108+
}
109+
}
80110

111+
if elaspe > feedDay*24 {
112+
MasterDB.Table(new(model.Feed)).Where("id=?", feed.Id).Update(map[string]interface{}{
113+
"updated_at": time.Time(feed.UpdatedAt),
114+
"seq": 0,
115+
})
116+
}
117+
}
118+
}
81119
}
82120

83121
func (FeedLogic) fillOtherInfo(ctx context.Context, feeds []*model.Feed, filterTop bool) []*model.Feed {
@@ -140,7 +178,51 @@ func (FeedLogic) publish(object interface{}, objectExt interface{}, me *model.Me
140178
}
141179

142180
func (self FeedLogic) updateSeq(objid, objtype, cmtnum, likenum, viewnum int) {
143-
181+
go func() {
182+
feed := &model.Feed{}
183+
_, err := MasterDB.Where("objid=? AND objtype=?", objid, objtype).Get(feed)
184+
if err != nil {
185+
return
186+
}
187+
188+
if feed.State == model.FeedOffline {
189+
return
190+
}
191+
192+
feedDay := config.ConfigFile.MustInt("global", "feed_day", 7)
193+
elaspe := int(time.Now().Sub(time.Time(feed.CreatedAt)).Hours())
194+
195+
if feed.Uid > 0 {
196+
user := DefaultUser.FindOne(nil, "uid", feed.Uid)
197+
if DefaultUser.IsAdmin(user) {
198+
elaspe = int(time.Now().Sub(time.Time(feed.UpdatedAt)).Hours())
199+
}
200+
}
201+
202+
seq := 0
203+
204+
if elaspe > feedDay*24 {
205+
if feed.Seq == 0 {
206+
return
207+
}
208+
} else {
209+
if feed.Seq == 0 {
210+
seq = elaspe + (feed.Cmtnum+cmtnum)*100 + likenum*100 + viewnum*5
211+
} else {
212+
seq = feed.Seq - elaspe + cmtnum*100 + likenum*100 + viewnum*5
213+
}
214+
}
215+
216+
_, err = MasterDB.Table(new(model.Feed)).Where("objid=? AND objtype=?", objid, objtype).Update(map[string]interface{}{
217+
"updated_at": time.Time(feed.UpdatedAt),
218+
"seq": seq,
219+
})
220+
221+
if err != nil {
222+
logger.Errorln("update feed seq error:", err)
223+
return
224+
}
225+
}()
144226
}
145227

146228
// setTop 置顶或取消置顶
@@ -154,16 +236,23 @@ func (FeedLogic) setTop(session *xorm.Session, objid, objtype int, top int) erro
154236
}
155237

156238
// updateComment 更新动态评论数据
157-
func (FeedLogic) updateComment(objid, objtype, uid int, cmttime time.Time) {
239+
func (self FeedLogic) updateComment(objid, objtype, uid int, cmttime time.Time) {
158240
go func() {
159241
MasterDB.Table(new(model.Feed)).Where("objid=? AND objtype=?", objid, objtype).
160242
Incr("cmtnum", 1).Update(map[string]interface{}{
161243
"lastreplyuid": uid,
162244
"lastreplytime": cmttime,
163245
})
246+
247+
self.updateSeq(objid, objtype, 1, 0, 0)
164248
}()
165249
}
166250

251+
// updateLike 更新动态赞数据(暂时没存)
252+
func (self FeedLogic) updateLike(objid, objtype, uid int, liketime time.Time) {
253+
self.updateSeq(objid, objtype, 0, 1, 0)
254+
}
255+
167256
func (self FeedLogic) modifyTopicNode(tid, nid int) {
168257
go func() {
169258
change := map[string]interface{}{

logic/index.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func (self IndexLogic) FindData(ctx context.Context, tab string, paginator *Pagi
4141

4242
switch {
4343
case indexNav.DataSource == "feed":
44-
data["feeds"] = self.findFeeds(ctx, paginator)
44+
data["feeds"] = self.findFeeds(ctx, paginator, tab)
4545
case isNid:
4646
paginator = NewPaginator(1)
4747

@@ -126,14 +126,14 @@ func (self IndexLogic) FindData(ctx context.Context, tab string, paginator *Pagi
126126
case indexNav.DataSource == "subject":
127127
data["subjects"] = DefaultSubject.FindBy(ctx, paginator)
128128
default:
129-
data["feeds"] = self.findFeeds(ctx, paginator)
129+
data["feeds"] = self.findFeeds(ctx, paginator, tab)
130130
}
131131

132132
return data
133133
}
134134

135-
func (self IndexLogic) findFeeds(ctx context.Context, paginator *Paginator) []*model.Feed {
135+
func (self IndexLogic) findFeeds(ctx context.Context, paginator *Paginator, tab string) []*model.Feed {
136136
topFeeds := DefaultFeed.FindTop(ctx)
137-
feeds := DefaultFeed.FindRecentWithPaginator(ctx, paginator)
137+
feeds := DefaultFeed.FindRecentWithPaginator(ctx, paginator, tab)
138138
return append(topFeeds, feeds...)
139139
}

logic/like.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ package logic
99
import (
1010
"errors"
1111
"fmt"
12+
"time"
1213

1314
. "github.com/studygolang/studygolang/db"
1415

@@ -123,6 +124,8 @@ func (LikeLogic) LikeObject(ctx context.Context, uid, objid, objtype, likeFlag i
123124
if affectedRows > 0 {
124125
if liker, ok := likers[objtype]; ok {
125126
go liker.UpdateLike(objid, 1)
127+
128+
DefaultFeed.updateLike(objid, objtype, uid, time.Now())
126129
}
127130
}
128131

logic/observer.go

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ package logic
88

99
import (
1010
"fmt"
11-
"github.com/studygolang/studygolang/model"
1211
"unicode/utf8"
12+
13+
"github.com/studygolang/studygolang/model"
1314
)
1415

1516
var (
@@ -40,6 +41,7 @@ func init() {
4041
ViewObservable = NewConcreteObservable(actionView)
4142
ViewObservable.AddObserver(&UserWeightObserver{})
4243
ViewObservable.AddObserver(&TodayActiveObserver{})
44+
ViewObservable.AddObserver(&FeedSeqObserver{})
4345

4446
appendObservable = NewConcreteObservable(actionAppend)
4547
appendObservable.AddObserver(&UserWeightObserver{})
@@ -113,11 +115,15 @@ func (this *ConcreteObservable) NotifyObservers(uid, objtype, objid int) {
113115
}
114116
}
115117

116-
/////////////////////////// 具体观察者 ////////////////////////////////////////
118+
// ///////////////////////// 具体观察者 ////////////////////////////////////////
117119

118120
type UserWeightObserver struct{}
119121

120122
func (this *UserWeightObserver) Update(action string, uid, objtype, objid int) {
123+
if uid == 0 {
124+
return
125+
}
126+
121127
var weight int
122128
switch action {
123129
case actionPublish:
@@ -140,6 +146,10 @@ func (this *UserWeightObserver) Update(action string, uid, objtype, objid int) {
140146
type TodayActiveObserver struct{}
141147

142148
func (*TodayActiveObserver) Update(action string, uid, objtype, objid int) {
149+
if uid == 0 {
150+
return
151+
}
152+
143153
var weight int
144154

145155
switch action {
@@ -176,6 +186,10 @@ var objType2MissType = map[int]int{
176186

177187
// Update 如果是回复,则 objid 是 cid
178188
func (UserRichObserver) Update(action string, uid, objtype, objid int) {
189+
if uid == 0 {
190+
return
191+
}
192+
179193
user := DefaultUser.FindOne(nil, "uid", uid)
180194

181195
var (
@@ -385,3 +399,15 @@ func (UserRichObserver) Update(action string, uid, objtype, objid int) {
385399

386400
DefaultUserRich.IncrUserRich(user, typ, award, desc)
387401
}
402+
403+
type FeedSeqObserver struct{}
404+
405+
func (this *FeedSeqObserver) Update(action string, uid, objtype, objid int) {
406+
if objid == 0 {
407+
return
408+
}
409+
410+
if action == actionView {
411+
DefaultFeed.updateSeq(objid, objtype, 0, 0, 1)
412+
}
413+
}

logic/rank.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,23 @@ type RankLogic struct{}
2525
var DefaultRank = RankLogic{}
2626

2727
func (self RankLogic) GenDayRank(objtype, objid, num int) {
28+
if objtype == model.TypeTopic {
29+
topic := &model.Topic{}
30+
_, err := MasterDB.Where("tid=?", objid).Get(topic)
31+
if err != nil {
32+
return
33+
}
34+
35+
topicNode := &model.TopicNode{}
36+
_, err = MasterDB.Where("nid=?", topic.Nid).Get(topicNode)
37+
if err != nil {
38+
return
39+
}
40+
if !topicNode.ShowIndex {
41+
return
42+
}
43+
}
44+
2845
redisClient := nosql.NewRedisClient()
2946
defer redisClient.Close()
3047
key := self.getDayRankKey(objtype, times.Format("ymd"))

logic/user.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,20 @@ func (UserLogic) Total() int64 {
393393
return total
394394
}
395395

396+
func (UserLogic) IsAdmin(user *model.User) bool {
397+
if user.IsRoot {
398+
return true
399+
}
400+
401+
for _, roleId := range user.Roleids {
402+
if roleId <= model.AdminMinRoleId {
403+
return true
404+
}
405+
}
406+
407+
return false
408+
}
409+
396410
var (
397411
ErrUsername = errors.New("用户名不存在")
398412
ErrPasswd = errors.New("密码错误")

logic/view.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ func (this *views) Incr(req *http.Request, objtype, objid int, uids ...int) {
121121

122122
if len(uids) > 0 {
123123
ViewObservable.NotifyObservers(uids[0], objtype, objid)
124+
} else {
125+
ViewObservable.NotifyObservers(0, objtype, objid)
124126
}
125127
}
126128

0 commit comments

Comments
 (0)