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

Skip to content

Commit 383c6df

Browse files
committed
支持用户自己置顶
1 parent c9659f7 commit 383c6df

File tree

14 files changed

+187
-3
lines changed

14 files changed

+187
-3
lines changed

config/db.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ CREATE TABLE IF NOT EXISTS `topics` (
3434
`flag` tinyint NOT NULL DEFAULT 0 COMMENT '审核标识,0-未审核;1-已审核;2-审核删除;3-用户自己删除',
3535
`editor_uid` int unsigned NOT NULL DEFAULT 0 COMMENT '最后编辑人',
3636
`top` tinyint unsigned NOT NULL DEFAULT '0' COMMENT '置顶,0否,1置顶',
37+
`top_time` int unsigned NOT NULL DEFAULT 0 COMMENT '置顶时间',
3738
`tags` varchar(63) NOT NULL DEFAULT '' COMMENT 'tag,逗号分隔',
3839
`permission` tinyint unsigned NOT NULL DEFAULT 0 COMMENT '访问权限:0-公开;1-登录用户可见;2-关注的人可见',
3940
`ctime` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
@@ -616,7 +617,6 @@ CREATE TABLE `view_record` (
616617
INDEX `idx_uid` (`uid`)
617618
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT '用户浏览记录表';
618619

619-
620620
CREATE TABLE `gift` (
621621
`id` int unsigned NOT NULL AUTO_INCREMENT,
622622
`name` varchar(63) NOT NULL DEFAULT '' COMMENT '物品名称',

src/http/controller/topic.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ func (self TopicController) RegisterRoute(g *echo.Group) {
4242
g.Match([]string{"GET", "POST"}, "/topics/new", self.Create, middleware.NeedLogin(), middleware.Sensivite(), middleware.BalanceCheck(), middleware.PublishNotice())
4343
g.Match([]string{"GET", "POST"}, "/topics/modify", self.Modify, middleware.NeedLogin(), middleware.Sensivite())
4444

45+
g.POST("/topics/set_top", self.SetTop, middleware.NeedLogin())
46+
4547
g.Match([]string{"GET", "POST"}, "/append/topic/:tid", self.Append, middleware.NeedLogin(), middleware.Sensivite(), middleware.BalanceCheck())
4648
}
4749

@@ -311,3 +313,22 @@ func (TopicController) Nodes(ctx echo.Context) error {
311313

312314
return render(ctx, "topics/nodes.html", data)
313315
}
316+
317+
func (TopicController) SetTop(ctx echo.Context) error {
318+
tid := goutils.MustInt(ctx.FormValue("tid"))
319+
if tid == 0 {
320+
return ctx.Redirect(http.StatusSeeOther, "/topics")
321+
}
322+
323+
me := ctx.Get("user").(*model.Me)
324+
err := logic.DefaultTopic.SetTop(ctx, me, tid)
325+
if err != nil {
326+
if err == logic.NotFoundErr {
327+
return ctx.Redirect(http.StatusSeeOther, "/topics")
328+
}
329+
330+
return fail(ctx, 1, "出错了:"+err.Error())
331+
}
332+
333+
return success(ctx, nil)
334+
}

src/logic/common.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ func init() {
2626
schemaDecoder.IgnoreUnknownKeys(true)
2727
}
2828

29-
var NotModifyAuthorityErr = errors.New("没有修改权限")
29+
var (
30+
NotModifyAuthorityErr = errors.New("没有修改权限")
31+
NotFoundErr = errors.New("Not Found")
32+
)
3033

3134
func GetLogger(ctx context.Context) *logger.Logger {
3235
if ctx == nil {

src/logic/feed.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515

1616
. "db"
1717

18+
"github.com/go-xorm/xorm"
1819
"github.com/polaris1119/set"
1920
)
2021

@@ -107,6 +108,16 @@ func (FeedLogic) publish(object interface{}, objectExt interface{}) {
107108
go model.PublishFeed(object, objectExt)
108109
}
109110

111+
// setTop 置顶或取消置顶
112+
func (FeedLogic) setTop(session *xorm.Session, objid, objtype int, top int) error {
113+
_, err := session.Table(new(model.Feed)).Where("objid=? AND objtype=?", objid, objtype).
114+
Update(map[string]interface{}{
115+
"top": top,
116+
})
117+
118+
return err
119+
}
120+
110121
// updateComment 更新动态评论数据
111122
func (FeedLogic) updateComment(objid, objtype, uid int, cmttime time.Time) {
112123
go func() {

src/logic/observer.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ var (
1818
commentObservable Observable
1919
ViewObservable Observable
2020
appendObservable Observable
21+
topObservable Observable
2122
)
2223

2324
func init() {
@@ -44,6 +45,11 @@ func init() {
4445
appendObservable.AddObserver(&UserWeightObserver{})
4546
appendObservable.AddObserver(&TodayActiveObserver{})
4647
appendObservable.AddObserver(&UserRichObserver{})
48+
49+
topObservable = NewConcreteObservable(actionTop)
50+
topObservable.AddObserver(&UserWeightObserver{})
51+
topObservable.AddObserver(&TodayActiveObserver{})
52+
topObservable.AddObserver(&UserRichObserver{})
4753
}
4854

4955
type Observer interface {
@@ -65,6 +71,7 @@ const (
6571
actionComment = "comment"
6672
actionView = "view"
6773
actionAppend = "append"
74+
actionTop = "top" // 置顶
6875
)
6976

7077
type ConcreteObservable struct {
@@ -123,6 +130,8 @@ func (this *UserWeightObserver) Update(action string, uid, objtype, objid int) {
123130
weight = 1
124131
case actionAppend:
125132
weight = 15
133+
case actionTop:
134+
weight = 5
126135
}
127136

128137
DefaultUser.IncrUserWeight("uid", uid, weight)
@@ -144,6 +153,8 @@ func (*TodayActiveObserver) Update(action string, uid, objtype, objid int) {
144153
weight = 1
145154
case actionAppend:
146155
weight = 15
156+
case actionTop:
157+
weight = 5
147158
}
148159

149160
DefaultRank.GenDAURank(uid, weight)
@@ -351,6 +362,22 @@ func (UserRichObserver) Update(action string, uid, objtype, objid int) {
351362
desc = fmt.Sprintf(`为主题 › <a href="/topics/%d">%s</a> 增加附言`,
352363
topic.Tid,
353364
topic.Title)
365+
} else if action == actionTop {
366+
typ = model.MissionTypeTop
367+
award = -200
368+
369+
switch objtype {
370+
case model.TypeTopic:
371+
topic := DefaultTopic.findByTid(objid)
372+
desc = fmt.Sprintf(`将主题 › <a href="/topics/%d">%s</a> 置顶`,
373+
topic.Tid,
374+
topic.Title)
375+
case model.TypeArticle:
376+
article, _ := DefaultArticle.FindById(nil, objid)
377+
desc = fmt.Sprintf(`将文章 › <a href="/articles/%d">%s</a> 置顶`,
378+
article.Id,
379+
article.Title)
380+
}
354381
}
355382

356383
DefaultUserRich.IncrUserRich(user, typ, award, desc)

src/logic/topic.go

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ func (self TopicLogic) Publish(ctx context.Context, me *model.Me, form url.Value
8787

8888
session := MasterDB.NewSession()
8989
defer session.Close()
90+
session.Begin()
9091

9192
_, err = session.Insert(topic)
9293
if err != nil {
@@ -209,6 +210,94 @@ func (self TopicLogic) Append(ctx context.Context, uid, tid int, content string)
209210
return nil
210211
}
211212

213+
// SetTop 置顶
214+
func (self TopicLogic) SetTop(ctx context.Context, me *model.Me, tid int) error {
215+
objLog := GetLogger(ctx)
216+
217+
if !me.IsAdmin {
218+
topic := self.findByTid(tid)
219+
if topic.Tid == 0 || topic.Uid != me.Uid {
220+
return NotFoundErr
221+
}
222+
}
223+
224+
session := MasterDB.NewSession()
225+
defer session.Close()
226+
session.Begin()
227+
228+
_, err := session.Table(new(model.Topic)).Id(tid).Update(map[string]interface{}{
229+
"top": 1,
230+
"top_time": time.Now().Unix(),
231+
})
232+
if err != nil {
233+
objLog.Errorln("TopicLogic SetTop error:", err)
234+
session.Rollback()
235+
return err
236+
}
237+
238+
err = DefaultFeed.setTop(session, tid, model.TypeTopic, 1)
239+
if err != nil {
240+
objLog.Errorln("TopicLogic SetTop feed error:", err)
241+
session.Rollback()
242+
return err
243+
}
244+
245+
session.Commit()
246+
247+
go topObservable.NotifyObservers(me.Uid, model.TypeTopic, tid)
248+
249+
return nil
250+
}
251+
252+
// UnsetTop 取消置顶
253+
func (self TopicLogic) UnsetTop(ctx context.Context, tid int) error {
254+
objLog := GetLogger(ctx)
255+
256+
session := MasterDB.NewSession()
257+
defer session.Close()
258+
session.Begin()
259+
260+
_, err := session.Table(new(model.Topic)).Id(tid).Update(map[string]interface{}{
261+
"top": 0,
262+
})
263+
if err != nil {
264+
objLog.Errorln("TopicLogic UnsetTop error:", err)
265+
session.Rollback()
266+
return err
267+
}
268+
269+
err = DefaultFeed.setTop(session, tid, model.TypeTopic, 0)
270+
if err != nil {
271+
objLog.Errorln("TopicLogic UnsetTop feed error:", err)
272+
session.Rollback()
273+
return err
274+
}
275+
276+
session.Commit()
277+
278+
return nil
279+
}
280+
281+
// AutoUnsetTop 自动取消置顶
282+
func (self TopicLogic) AutoUnsetTop() error {
283+
topics := make([]*model.Topic, 0)
284+
err := MasterDB.Where("top=1").Find(&topics)
285+
if err != nil {
286+
logger.Errorln("TopicLogic AutoUnsetTop error:", err)
287+
return err
288+
}
289+
290+
for _, topic := range topics {
291+
if topic.TopTime == 0 || topic.TopTime+86400 > time.Now().Unix() {
292+
continue
293+
}
294+
295+
self.UnsetTop(nil, topic.Tid)
296+
}
297+
298+
return nil
299+
}
300+
212301
// FindAll 支持多页翻看
213302
func (self TopicLogic) FindAll(ctx context.Context, paginator *Paginator, orderBy string, querystring string, args ...interface{}) []map[string]interface{} {
214303
objLog := GetLogger(ctx)

src/logic/user.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,8 @@ func (UserLogic) doCreateUser(ctx context.Context, session *xorm.Session, user *
638638
}
639639
user.Open = 0
640640

641+
user.DauAuth = model.DefaultAuth
642+
641643
_, err := session.Insert(user)
642644
if err != nil {
643645
return err

src/model/mission.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ const (
2525
MissionTypeBook = 57
2626

2727
MissionTypeAppend = 60
28+
// 置顶
29+
MissionTypeTop = 61
2830

2931
MissionTypeModify = 65
3032
// 被回复

src/model/topic.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ type Topic struct {
3838
Lastreplytime OftenTime `json:"lastreplytime"`
3939
EditorUid int `json:"editor_uid"`
4040
Top uint8 `json:"top"`
41+
TopTime int64 `json:"top_time"`
4142
Tags string `json:"tags"`
4243
Permission int `json:"permission"`
4344
Ctime OftenTime `json:"ctime" xorm:"created"`

src/model/user.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ const (
6767
TypeTop = 101
6868
)
6969

70+
const DefaultAuth = DauAuthTopic | DauAuthArticle | DauAuthResource | DauAuthProject | DauAuthComment
71+
7072
// 用户基本信息
7173
type User struct {
7274
Uid int `json:"uid" xorm:"pk autoincr"`

src/model/user_rich.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ var BalanceTypeMap = map[int]string{
2525
MissionTypeProject: "发布项目",
2626
MissionTypeBook: "分享图书",
2727
MissionTypeAppend: "增加附言",
28+
MissionTypeTop: "置顶",
2829
MissionTypeModify: "修改",
2930
MissionTypeReplied: "回复收益",
3031
MissionTypeAward: "额外赠予",

src/server/studygolang/background.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ func ServeBackGround() {
6060
c.AddFunc("0 0 0 * * *", logic.DefaultEmail.EmailNotice)
6161
}
6262

63+
// 取消置顶
64+
c.AddFunc("0 * * * * *", unsetTop)
65+
6366
// 每天对活跃用户奖励铜币
6467
c.AddFunc("@daily", logic.DefaultUserRich.AwardCooper)
6568
}
@@ -141,3 +144,7 @@ func genViewRank() {
141144
logic.DefaultRank.GenMonthRank(objtype)
142145
}
143146
}
147+
148+
func unsetTop() {
149+
logic.DefaultTopic.AutoUnsetTop()
150+
}

template/admin/user/detail.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ <h3>基本信息</h3>
2222
<p>
2323
<label for="name">用户名</label>
2424
<span class="field txt">
25-
{{.user.Name}}
25+
{{.user.Username}}
2626
</span>
2727
</p>
2828
</div>

template/topics/detail.html

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ <h1>
3030
{{else if and (eq .me.Uid .topic.user.Uid) (lt (len .appends) 3) }}
3131
<a class="op" href="/append/topic/{{.topic.tid}}" title="附言">附言</a>
3232
{{end}}
33+
{{if and (canPublish .me.DauAuth 101) (not .topic.top)}}
34+
<a id="set-top" class="op" href="/topics/set_top?tid={{.topic.tid}}" title="置顶">置顶</a>
35+
{{end}}
3336
</small>
3437
</div>
3538
{{if gt (distanceDay .topic.ctime) 100 }}
@@ -178,6 +181,21 @@ <h1>
178181
var url = $(this).attr('href');
179182
$(this).attr('target', '_blank');
180183
});
184+
185+
$('#set-top').on('click', function(evt) {
186+
evt.preventDefault();
187+
188+
var url = $(this).attr('href');
189+
$.post(url, {}, function(result) {
190+
if (result.ok) {
191+
comTip("置顶成功!");
192+
} else {
193+
comTip(result.msg);
194+
}
195+
});
196+
197+
return false;
198+
});
181199
});
182200
</script>
183201

0 commit comments

Comments
 (0)