Skip to content

Commit

Permalink
feat: add pin topic support
Browse files Browse the repository at this point in the history
  • Loading branch information
alimy committed Dec 30, 2023
1 parent 6c51c79 commit 8967359
Show file tree
Hide file tree
Showing 49 changed files with 197 additions and 59 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ All notable changes to paopao-ce are documented in this file.
### Added
- add all-in-one docker image build scripts.
- frontend: add tweets filter support use tag for home page and make it as default behavior.
- add pin topic support.

## 0.5.2
### Change
Expand Down
19 changes: 19 additions & 0 deletions auto/api/v1/priv.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions internal/core/cs/topics.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const (
TagTypeHot TagType = "hot"
TagTypeNew TagType = "new"
TagTypeFollow TagType = "follow"
TagTypePin TagType = "pin"
TagTypeHotExtral TagType = "hot_extral"
)

Expand Down Expand Up @@ -40,6 +41,7 @@ type TagItem struct {
QuoteNum int64 `json:"quote_num"`
IsFollowing int8 `json:"is_following"`
IsTop int8 `json:"is_top"`
IsPin int8 `json:"is_pin"`
}

func (t *TagInfo) Format() *TagItem {
Expand All @@ -51,5 +53,6 @@ func (t *TagInfo) Format() *TagItem {
QuoteNum: t.QuoteNum,
IsFollowing: 0,
IsTop: 0,
IsPin: 0,
}
}
3 changes: 2 additions & 1 deletion internal/core/topics.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ type TopicService interface {
TagsByKeyword(keyword string) (cs.TagInfoList, error)
GetHotTags(userId int64, limit int, offset int) (cs.TagList, error)
GetNewestTags(userId int64, limit int, offset int) (cs.TagList, error)
GetFollowTags(userId int64, limit int, offset int) (cs.TagList, error)
GetFollowTags(userId int64, is_pin bool, limit int, offset int) (cs.TagList, error)
FollowTopic(userId int64, topicId int64) error
UnfollowTopic(userId int64, topicId int64) error
StickTopic(userId int64, topicId int64) (int8, error)
PinTopic(userId int64, topicId int64) (int8, error)
}

// TopicServantA 话题服务(版本A)
Expand Down
1 change: 1 addition & 0 deletions internal/dao/jinzhu/dbr/topic.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type TopicUser struct {
Remark string `json:"-"`
QuoteNum int64 `json:"quote_num"`
IsTop int8 `json:"is_top"`
IsPin int8 `json:"is_pin"`
ReserveA string `json:"-"`
ReserveB string `json:"-"`
}
Expand Down
44 changes: 35 additions & 9 deletions internal/dao/jinzhu/topics.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type topicSrvA struct {
type topicInfo struct {
TopicId int64
IsTop int8
IsPin int8
}

func newTopicService(db *gorm.DB) core.TopicService {
Expand Down Expand Up @@ -110,17 +111,18 @@ func (s *topicSrv) GetNewestTags(userId int64, limit int, offset int) (cs.TagLis
return s.tagsFormatA(userId, tags)
}

func (s *topicSrv) GetFollowTags(userId int64, limit int, offset int) (cs.TagList, error) {
func (s *topicSrv) GetFollowTags(userId int64, is_pin bool, limit int, offset int) (cs.TagList, error) {
if userId < 0 {
return nil, nil
}
userTopics := []*topicInfo{}
err := s.db.Model(&dbr.TopicUser{}).
Where("user_id=?", userId).
Order("is_top DESC").
Limit(limit).
Offset(offset).
Find(&userTopics).Error
db := s.db.Model(&dbr.TopicUser{}).Order("is_top DESC").Limit(limit).Offset(offset)
if is_pin {
db = db.Where("user_id=? AND is_pin=1", userId)
} else {
db = db.Where("user_id=?", userId)
}
err := db.Find(&userTopics).Error
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -207,7 +209,7 @@ func (s *topicSrv) tagsFormatA(userId int64, tags cs.TagList) (cs.TagList, error
}
for _, tag := range tags {
if info, exist := userTopicsMap[tag.ID]; exist {
tag.IsFollowing, tag.IsTop = 1, info.IsTop
tag.IsFollowing, tag.IsTop, tag.IsPin = 1, info.IsTop, info.IsPin
}
}
}
Expand Down Expand Up @@ -238,7 +240,7 @@ func (s *topicSrv) tagsFormatB(userTopicsMap map[int64]*topicInfo, tags cs.TagIn
if len(userTopicsMap) > 0 {
for _, tag := range tagList {
if info, exist := userTopicsMap[tag.ID]; exist {
tag.IsFollowing, tag.IsTop = 1, info.IsTop
tag.IsFollowing, tag.IsTop, tag.IsPin = 1, info.IsTop, info.IsPin
}
}
}
Expand Down Expand Up @@ -409,3 +411,27 @@ func (s *topicSrv) StickTopic(userId int64, topicId int64) (status int8, err err
db.Commit()
return
}

func (s *topicSrv) PinTopic(userId int64, topicId int64) (status int8, err error) {
db := s.db.Begin()
defer db.Rollback()

m := &dbr.TopicUser{}
err = db.Model(m).
Where("user_id=? and topic_id=?", userId, topicId).
UpdateColumn("is_pin", gorm.Expr("1-is_pin")).Error
if err != nil {
return
}
status = -1
err = db.Model(m).Where("user_id=? and topic_id=?", userId, topicId).Select("is_pin").Scan(&status).Error
if err != nil {
return
}
if status < 0 {
return -1, errors.New("topic not exist")
}

db.Commit()
return
}
1 change: 1 addition & 0 deletions internal/model/web/loose.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const (
TagTypeHot = cs.TagTypeHot
TagTypeNew = cs.TagTypeNew
TagTypeFollow = cs.TagTypeFollow
TagTypePin = cs.TagTypePin
TagTypeHotExtral = cs.TagTypeHotExtral
)

Expand Down
9 changes: 9 additions & 0 deletions internal/model/web/priv.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,15 @@ type StickTopicResp struct {
StickStatus int8 `json:"top_status"`
}

type PinTopicReq struct {
SimpleInfo `json:"-" binding:"-"`
TopicId int64 `json:"topic_id" binding:"required"`
}

type PinTopicResp struct {
PinStatus int8 `json:"pin_status"`
}

type FollowTopicReq struct {
SimpleInfo `json:"-" binding:"-"`
TopicId int64 `json:"topic_id" binding:"required"`
Expand Down
1 change: 1 addition & 0 deletions internal/model/web/xerror.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ var (
ErrFollowTopicFailed = xerror.NewError(90001, "关注话题失败")
ErrUnfollowTopicFailed = xerror.NewError(90002, "取消关注话题失败")
ErrStickTopicFailed = xerror.NewError(90003, "更行话题置顶状态失败")
ErrPinTopicFailed = xerror.NewError(90005, "更行话题钉住状态失败")
ErrThumbsUpTweetComment = xerror.NewError(90101, "评论点赞失败")
ErrThumbsDownTweetComment = xerror.NewError(90102, "评论点踩失败")
ErrThumbsUpTweetReply = xerror.NewError(90103, "评论回复点赞失败")
Expand Down
6 changes: 4 additions & 2 deletions internal/servants/web/loose.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,15 +384,17 @@ func (s *looseSrv) TopicList(req *web.TopicListReq) (*web.TopicListResp, mir.Err
case web.TagTypeNew:
tags, err = s.Ds.GetNewestTags(req.Uid, num, 0)
case web.TagTypeFollow:
tags, err = s.Ds.GetFollowTags(req.Uid, num, 0)
tags, err = s.Ds.GetFollowTags(req.Uid, false, num, 0)
case web.TagTypePin:
tags, err = s.Ds.GetFollowTags(req.Uid, true, num, 0)
case web.TagTypeHotExtral:
extralNum := req.ExtralNum
if extralNum <= 0 {
extralNum = num
}
tags, err = s.Ds.GetHotTags(req.Uid, num, 0)
if err == nil {
extralTags, err = s.Ds.GetFollowTags(req.Uid, extralNum, 0)
extralTags, err = s.Ds.GetFollowTags(req.Uid, false, extralNum, 0)
}
default:
// TODO: return good error
Expand Down
11 changes: 11 additions & 0 deletions internal/servants/web/priv.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,17 @@ func (s *privSrv) StickTopic(req *web.StickTopicReq) (*web.StickTopicResp, mir.E
}, nil
}

func (s *privSrv) PinTopic(req *web.PinTopicReq) (*web.PinTopicResp, mir.Error) {
status, err := s.Ds.PinTopic(req.Uid, req.TopicId)
if err != nil {
logrus.Errorf("user(%d) pin topic(%d) failed: %s", req.Uid, req.TopicId, err)
return nil, web.ErrPinTopicFailed
}
return &web.PinTopicResp{
PinStatus: status,
}, nil
}

func (s *privSrv) UploadAttachment(req *web.UploadAttachmentReq) (*web.UploadAttachmentResp, mir.Error) {
defer req.File.Close()

Expand Down
5 changes: 4 additions & 1 deletion mirc/web/v1/priv.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,12 @@ type Priv struct {
// ThumbsDownTweetReply 点踩评论回复
ThumbsDownTweetReply func(Post, web.TweetReplyThumbsReq) `mir:"/tweet/reply/thumbsdown"`

// StickTopic 置顶动态
// StickTopic 置顶话题
StickTopic func(Post, web.StickTopicReq) web.StickTopicResp `mir:"/topic/stick"`

// PinTopic 钉住话题
PinTopic func(Post, web.PinTopicReq) web.PinTopicResp `mir:"/topic/pin"`

// FollowTopic 关注话题
FollowTopic func(Post, web.FollowTopicReq) `mir:"/topic/follow"`

Expand Down
2 changes: 2 additions & 0 deletions scripts/migration/mysql/0015_topic_user_pin.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE `p_topic_user` DROP COLUMN `is_pin`;
DROP INDEX IF EXISTS `idx_topic_user_uid_ispin`;
2 changes: 2 additions & 0 deletions scripts/migration/mysql/0015_topic_user_pin.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE `p_topic_user` ADD COLUMN `is_pin` TINYINT NOT NULL DEFAULT 0 COMMENT '是否钉住 0 为未钉住、1 为已钉住';
CREATE INDEX `idx_topic_user_uid_ispin` ON `p_topic_user` (`user_id`, `is_pin`) USING BTREE;
2 changes: 2 additions & 0 deletions scripts/migration/postgres/0014_topic_user_pin.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE p_topic_user DROP COLUMN is_pin;
DROP INDEX IF EXISTS idx_topic_user_uid_ispin;
2 changes: 2 additions & 0 deletions scripts/migration/postgres/0014_topic_user_pin.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE p_topic_user ADD COLUMN is_pin SMALLINT NOT NULL DEFAULT 0; -- 是否钉住 0 为未钉住、1 为已钉住
CREATE INDEX idx_topic_user_uid_ispin ON p_topic_user USING btree ( user_id, is_pin );
2 changes: 2 additions & 0 deletions scripts/migration/sqlite3/0015_topic_user_pin.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE "p_topic_user" DROP COLUMN "is_pin";
DROP INDEX IF EXISTS "idx_topic_user_uid_ispin";
6 changes: 6 additions & 0 deletions scripts/migration/sqlite3/0015_topic_user_pin.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
ALTER TABLE "p_topic_user" ADD COLUMN "is_pin" integer NOT NULL DEFAULT 0;
CREATE INDEX "main"."idx_topic_user_uid_ispin"
ON "p_topic_user" (
"user_id" ASC,
"is_pin" ASC
);
4 changes: 3 additions & 1 deletion scripts/paopao-mysql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -324,14 +324,16 @@ CREATE TABLE `p_topic_user` (
`remark` VARCHAR ( 512 ) COMMENT '备注',
`quote_num` BIGINT COMMENT '引用数',
`is_top` TINYINT NOT NULL DEFAULT '0' COMMENT '是否置顶 0 为未置顶、1 为已置顶',
`is_pin` TINYINT NOT NULL DEFAULT '0' COMMENT '是否钉住 0 为未钉住、1 为已钉住',
`created_on` BIGINT NOT NULL DEFAULT '0' COMMENT '创建时间',
`modified_on` BIGINT NOT NULL DEFAULT '0' COMMENT '修改时间',
`deleted_on` BIGINT NOT NULL DEFAULT '0' COMMENT '删除时间',
`is_del` TINYINT NOT NULL DEFAULT '0' COMMENT '是否删除 0 为未删除、1 为已删除',
`reserve_a` VARCHAR ( 255 ) COMMENT '保留字段a',
`reserve_b` VARCHAR ( 255 ) COMMENT '保留字段b',
PRIMARY KEY ( `id` ) USING BTREE,
UNIQUE KEY `idx_topic_user_uid_tid` ( `topic_id`, `user_id` ) USING BTREE
UNIQUE KEY `idx_topic_user_uid_tid` ( `topic_id`, `user_id` ) USING BTREE,
KEY `idx_topic_user_uid_ispin` ( `user_id`, `is_pin`) USING BTREE
) ENGINE = INNODB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '用户话题';

-- ----------------------------
Expand Down
2 changes: 2 additions & 0 deletions scripts/paopao-postgres.sql
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ CREATE TABLE p_topic_user (
remark VARCHAR ( 512 ),-- 备注
quote_num BIGINT,-- 引用数
is_top SMALLINT NOT NULL DEFAULT 0,-- 是否置顶 0 为未置顶、1 为已置顶
is_pin SMALLINT NOT NULL DEFAULT 0,-- 是否钉住 0 为未钉住、1 为已钉住
created_on BIGINT NOT NULL DEFAULT 0,-- 创建时间
modified_on BIGINT NOT NULL DEFAULT 0,-- 修改时间
deleted_on BIGINT NOT NULL DEFAULT 0,-- 删除时间
Expand All @@ -273,6 +274,7 @@ CREATE TABLE p_topic_user (
reserve_b VARCHAR ( 255 ) -- 保留字段b
);
CREATE UNIQUE INDEX idx_topic_user_uid_tid ON p_topic_user USING btree ( topic_id, user_id );
CREATE INDEX idx_topic_user_uid_ispin ON p_topic_user USING btree ( user_id, is_pin );

CREATE SEQUENCE IF NOT EXISTS user_id_seq AS BIGINT MINVALUE 100058 NO MAXVALUE;
DROP TABLE IF EXISTS p_user;
Expand Down
6 changes: 6 additions & 0 deletions scripts/paopao-sqlite3.sql
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ CREATE TABLE "p_topic_user" (
"remark" text ( 512 ),-- 备注
"quote_num" integer,-- 引用数
"is_top" integer NOT NULL DEFAULT 0,-- 是否置顶 0 为未置顶、1 为已置顶
"is_pin" integer NOT NULL DEFAULT 0,-- 是否钉住 0 为未钉住、1 为已钉住
"created_on" integer NOT NULL DEFAULT 0,-- 创建时间
"modified_on" integer NOT NULL DEFAULT 0,-- 修改时间
"deleted_on" integer NOT NULL DEFAULT 0,-- 删除时间
Expand Down Expand Up @@ -680,6 +681,11 @@ ON "p_topic_user" (
"topic_id",
"user_id"
);
CREATE INDEX "main"."idx_topic_user_uid_ispin"
ON "p_topic_user" (
"user_id" ASC,
"is_pin" ASC
);

-- ----------------------------
-- Indexes structure for table p_user
Expand Down
1 change: 0 additions & 1 deletion web/dist/assets/404-krUgO4p7.js

This file was deleted.

1 change: 0 additions & 1 deletion web/dist/assets/Anouncement-UvikhvqM.js

This file was deleted.

1 change: 0 additions & 1 deletion web/dist/assets/Collection-Ve_FHe8v.js

This file was deleted.

Loading

0 comments on commit 8967359

Please sign in to comment.