Esempio n. 1
0
//Topic Home Page
func (this *Topic) Get() error {
	//check topic slug
	slug := this.Params().Get(":slug")
	topic, err := models.GetTopicBySlug(slug)
	if err != nil {
		return err
	}

	//get topic category
	var category models.Category
	err = models.GetById(topic.CategoryId, &category)
	if err != nil {
		return err
	}

	//get posts by topic
	cnt, err := models.CountByExample(&models.Post{TopicId: topic.Id})
	if err != nil {
		return err
	}

	pager := this.SetPaginator(setting.PostCountPerPage, cnt)
	posts, err := models.FindPosts(setting.PostCountPerPage, pager.Offset())
	if err != nil {
		return err
	}

	this.Data["Posts"] = posts
	this.Data["Topic"] = &topic
	this.Data["Category"] = &category

	//check whether added it into favorite list
	var hasFavorite bool
	if this.IsLogin {
		hasFavorite, _ = models.HasUserFollowTopic(int64(this.User.Id), topic.Id)
	}
	this.Data["HasFavorite"] = hasFavorite

	//new best post
	var newBestPosts []models.Post
	this.setNewBestPostsOfTopic(&newBestPosts, topic)
	//most replys posts
	var mostReplysPosts []models.Post
	this.setMostReplysPostsOfTopic(&mostReplysPosts, topic)
	this.setSidebarBuilletinInfo()
	return this.Render("post/topic.html", this.Data)
}
Esempio n. 2
0
// Add this topic into favorite list
func (this *Topic) Post() {
	slug := this.Params().Get(":slug")
	result := map[string]interface{}{
		"success": false,
	}

	topic, err := models.GetTopicBySlug(slug)
	if err != nil {
		return
	}

	if this.IsAjax() {
		action := this.GetString("action")
		switch action {
		case "favorite":
			if this.IsLogin {
				has, err := models.HasUserFollowTopic(int64(this.User.Id), topic.Id)
				if err != nil {
					log.Error("get follow user error:", err)
					return
				}

				if has {
					err = models.DeleteFollowTopic(int64(this.User.Id), topic.Id)
				} else {
					fav := models.FollowTopic{UserId: int64(this.User.Id), TopicId: topic.Id}
					err = models.Insert(&fav)
				}

				if err != nil {
					return
				}

				//TODO: add back
				//topic.RefreshFollowers()
				//this.User.RefreshFavTopics()
				result["success"] = true
			}
		}
	}

	this.Data["json"] = result
	this.ServeJson(this.Data)
}