package topic import ( "errors" "live/app/lib" "live/app/lib/aliyun" "live/app/lib/config" "live/app/model/topic" ) type Topic struct { } // @Title 根据关键字获取话题列表 func (t *Topic) List(keyword string, page lib.Page) (result *[]topic.Topic, count int64) { result = &[]topic.Topic{} topicModel := &topic.Topic{} start := (page.GetPage() - 1) * page.GetLimit() count = topicModel.GetlistCount(keyword) if count <= int64(start) { // 没有数据,不需要查库 return } *result = topicModel.Getlist(keyword, start, page.GetLimit()) return } // @Title 添加话题 func (t *Topic) Add(title string) (uint, error) { topicModel := &topic.Topic{} topicInfo := topicModel.GetByTitle(title) if topicInfo.Id > 0 { return topicInfo.Id, nil } text, err := aliyun.Text.CheckText(title) if err != nil { return 0, err } if len(text.Politics) > 0 || len(text.Contraband) > 0 || len(text.Salacity) > 0 || len(text.Abuse) > 0 { return 0, errors.New("包含敏感词汇") } data := topic.Topic{ Title: title, } topicModel.Create(&data) if data.Id <= 0 { return 0, errors.New("网络异常") } return data.Id, nil } // @Title 获取热门话题 func (t *Topic) Hot() []topic.Topic { topicModel := &topic.Topic{} return topicModel.GetHotlist(config.Config.Section("topic").Key("hotCount").MustInt(10)) } // @Title 根据ids获取列表 func (t *Topic) GetListByIds(topicIds []uint) (result *[]topic.Topic) { result = &[]topic.Topic{} if len(topicIds) == 0 { return } topicModel := &topic.Topic{} *result = topicModel.GetlistByIds(topicIds) return } type replyTopicInfo struct { topic.Topic IsFollow int `json:"isFollow"` } // @Title 根据id获取单条信息 func (t *Topic) GetInfo(userId, topicId uint) (result *replyTopicInfo) { result = &replyTopicInfo{} if topicId <= 0 { return } topicModel := &topic.Topic{} result.Topic = topicModel.GetById(topicId) if result.Id > 0 { // 获取关注相关 topicFollow := &topic.TopicFollow{} followInfo := topicFollow.GetByUserIdAndTopicId(userId, topicId) if followInfo.Id > 0 && followInfo.IsDel == 0 { result.IsFollow = 1 } } return }