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

Skip to content

Commit 26f3676

Browse files
committed
主题或文章删除,从列表中去掉,从评论等地方去掉
fix issue: studygolang#85
1 parent 0013404 commit 26f3676

File tree

3 files changed

+41
-9
lines changed

3 files changed

+41
-9
lines changed

src/logic/article.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"strings"
1919
"time"
2020

21+
"github.com/go-xorm/xorm"
2122
"github.com/polaris1119/slices"
2223

2324
"github.com/PuerkitoBio/goquery"
@@ -556,7 +557,7 @@ func (self ArticleLogic) FindByUser(ctx context.Context, username string, limit
556557
objLog := GetLogger(ctx)
557558

558559
articles := make([]*model.Article, 0)
559-
err := MasterDB.Where("author_txt=?", username).OrderBy("id DESC").Limit(limit).Find(&articles)
560+
err := MasterDB.Where("author_txt=? AND status<?", username, model.ArticleStatusOffline).OrderBy("id DESC").Limit(limit).Find(&articles)
560561
if err != nil {
561562
objLog.Errorln("ArticleLogic FindByUser Error:", err)
562563
return nil
@@ -619,6 +620,7 @@ func (self ArticleLogic) FindAll(ctx context.Context, paginator *Paginator, orde
619620
if querystring != "" {
620621
session.Where(querystring, args...)
621622
}
623+
self.addStatusWhere(session)
622624
err := session.Limit(paginator.PerPage(), paginator.Offset()).Find(&articles)
623625
if err != nil {
624626
objLog.Errorln("ArticleLogic FindAll error:", err)
@@ -633,14 +635,15 @@ func (self ArticleLogic) FindAll(ctx context.Context, paginator *Paginator, orde
633635
func (ArticleLogic) Count(ctx context.Context, querystring string, args ...interface{}) int64 {
634636
objLog := GetLogger(ctx)
635637

638+
session := MasterDB.Where("status<?", model.ArticleStatusOffline)
636639
var (
637640
total int64
638641
err error
639642
)
640643
if querystring == "" {
641-
total, err = MasterDB.Count(new(model.Article))
644+
total, err = session.Count(new(model.Article))
642645
} else {
643-
total, err = MasterDB.Where(querystring, args...).Count(new(model.Article))
646+
total, err = session.Where(querystring, args...).Count(new(model.Article))
644647
}
645648

646649
if err != nil {
@@ -1057,6 +1060,10 @@ func (ArticleLogic) getOwner(id int) int {
10571060
return 0
10581061
}
10591062

1063+
func (ArticleLogic) addStatusWhere(session *xorm.Session) {
1064+
session.Where("status<?", model.ArticleStatusOffline)
1065+
}
1066+
10601067
// 博文评论
10611068
type ArticleComment struct{}
10621069

@@ -1086,6 +1093,9 @@ func (self ArticleComment) SetObjinfo(ids []int, commentMap map[int][]*model.Com
10861093
}
10871094

10881095
for _, article := range articles {
1096+
if article.Status < model.ArticleStatusOffline {
1097+
continue
1098+
}
10891099
objinfo := make(map[string]interface{})
10901100
objinfo["title"] = article.Title
10911101
objinfo["uri"] = model.PathUrlMap[model.TypeArticle]

src/logic/comment.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ func (self CommentLogic) FindRecent(ctx context.Context, uid, objtype, limit int
192192
self.fillObjinfos(cmts, cmtObjs[cmtType])
193193
}
194194

195-
return comments
195+
return self.filterDelObjectCmt(comments)
196196
}
197197

198198
// Publish 发表评论(或回复)。
@@ -417,7 +417,8 @@ func (self CommentLogic) FindAll(ctx context.Context, paginator *Paginator, orde
417417
for cmtType, cmts := range cmtMap {
418418
self.fillObjinfos(cmts, cmtObjs[cmtType])
419419
}
420-
return comments
420+
421+
return self.filterDelObjectCmt(comments)
421422
}
422423

423424
// Count 获取用户全部评论数
@@ -440,3 +441,13 @@ func (CommentLogic) Count(ctx context.Context, querystring string, args ...inter
440441

441442
return total
442443
}
444+
445+
func (CommentLogic) filterDelObjectCmt(comments []*model.Comment) []*model.Comment {
446+
resultCmts := make([]*model.Comment, 0, len(comments))
447+
for _, comment := range comments {
448+
if comment.Objinfo != nil && len(comment.Objinfo) > 0 {
449+
resultCmts = append(resultCmts, comment)
450+
}
451+
}
452+
return resultCmts
453+
}

src/logic/topic.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
. "db"
2020

2121
"github.com/fatih/structs"
22+
"github.com/go-xorm/xorm"
2223
"github.com/polaris1119/goutils"
2324
"github.com/polaris1119/logger"
2425
"github.com/polaris1119/set"
@@ -308,6 +309,7 @@ func (self TopicLogic) FindAll(ctx context.Context, paginator *Paginator, orderB
308309
if querystring != "" {
309310
session.Where(querystring, args...)
310311
}
312+
self.addFlagWhere(session)
311313
err := session.OrderBy(orderBy).Limit(paginator.PerPage(), paginator.Offset()).Find(&topicInfos)
312314
if err != nil {
313315
objLog.Errorln("TopicLogic FindAll error:", err)
@@ -326,11 +328,12 @@ func (TopicLogic) FindLastList(beginTime string, limit int) ([]*model.Topic, err
326328
}
327329

328330
// FindRecent 获得最近的主题(uids[0],则获取某个用户最近的主题)
329-
func (TopicLogic) FindRecent(limit int, uids ...int) []*model.Topic {
331+
func (self TopicLogic) FindRecent(limit int, uids ...int) []*model.Topic {
330332
dbSession := MasterDB.OrderBy("ctime DESC").Limit(limit)
331333
if len(uids) > 0 {
332334
dbSession.Where("uid=?", uids[0])
333335
}
336+
self.addFlagWhere(dbSession)
334337

335338
topics := make([]*model.Topic, 0)
336339
if err := dbSession.Find(&topics); err != nil {
@@ -347,7 +350,7 @@ func (TopicLogic) FindByNid(ctx context.Context, nid, curTid string) []*model.To
347350
objLog := GetLogger(ctx)
348351

349352
topics := make([]*model.Topic, 0)
350-
err := MasterDB.Where("nid=? AND tid!=?", nid, curTid).Limit(10).Find(&topics)
353+
err := MasterDB.Where("nid=? AND tid!=? AND flag<?", nid, curTid, model.FlagAuditDelete).Limit(10).Find(&topics)
351354
if err != nil {
352355
objLog.Errorln("TopicLogic FindByNid Error:", err)
353356
}
@@ -628,14 +631,15 @@ func (TopicLogic) JSEscape(topics []*model.Topic) []*model.Topic {
628631
func (TopicLogic) Count(ctx context.Context, querystring string, args ...interface{}) int64 {
629632
objLog := GetLogger(ctx)
630633

634+
session := MasterDB.Where("flag<?", model.FlagAuditDelete)
631635
var (
632636
total int64
633637
err error
634638
)
635639
if querystring == "" {
636-
total, err = MasterDB.Count(new(model.Topic))
640+
total, err = session.Count(new(model.Topic))
637641
} else {
638-
total, err = MasterDB.Where(querystring, args...).Count(new(model.Topic))
642+
total, err = session.Where(querystring, args...).Count(new(model.Topic))
639643
}
640644

641645
if err != nil {
@@ -664,6 +668,10 @@ func (TopicLogic) decodeTopicContent(ctx context.Context, topic *model.Topic) st
664668
return parseAtUser(ctx, content)
665669
}
666670

671+
func (TopicLogic) addFlagWhere(session *xorm.Session) {
672+
session.Where("flag<?", model.FlagAuditDelete)
673+
}
674+
667675
// 话题回复(评论)
668676
type TopicComment struct{}
669677

@@ -710,6 +718,9 @@ func (self TopicComment) SetObjinfo(ids []int, commentMap map[int][]*model.Comme
710718
}
711719

712720
for _, topic := range topics {
721+
if topic.Flag > model.FlagNormal {
722+
continue
723+
}
713724
objinfo := make(map[string]interface{})
714725
objinfo["title"] = topic.Title
715726
objinfo["uri"] = model.PathUrlMap[model.TypeTopic]

0 commit comments

Comments
 (0)