Exemplo n.º 1
0
func (this *NewPost) Post() error {
	if this.CheckActiveRedirect() {
		return nil
	}

	var err error
	form := post.PostForm{Locale: this.Locale}
	topicSlug := this.GetString("topic")
	if len(topicSlug) > 0 {
		topic, err := models.GetTopicBySlug(topicSlug)
		if err == nil {
			form.Category = topic.CategoryId
			form.Topic = topic.Id
			this.Data["Topic"] = topic
		} else {
			log.Error("Can not find topic by slug:", topicSlug)
		}
	} else {
		topicId, err := this.GetInt("Topic")
		if err == nil {
			topic, err := models.GetTopicById(topicId)
			if err == nil {
				form.Category = topic.CategoryId
				form.Topic = topic.Id
				this.Data["Topic"] = topic
			} else {
				log.Error("Can not find topic by id:", topicId)
			}
		} else {
			log.Error("Parse param Topic from request failed", err)
		}
	}
	if categorySlug := this.GetString("category"); categorySlug != "" {
		log.Debug("Find category slug:", categorySlug)
		category, err := models.GetCategoryBySlug(categorySlug)
		if err != nil {
			log.Error("Get category error", err)
		}
		this.Data["Category"] = &category
	}
	err = models.FindTopics(&form.Topics)
	if err != nil {
		return err
	}
	if !this.ValidFormSets(&form) {
		this.Redirect("/new")
		return nil
	}

	var post models.Post
	if err := form.SavePost(&post, &this.User); err == nil {
		this.JsStorage("deleteKey", "post/new")
		this.Redirect(post.Link())
		return nil
	}
	return this.Render("post/new.html", this.Data)
}
Exemplo n.º 2
0
func (this *NewPost) Get() error {
	if this.CheckActiveRedirect() {
		return nil
	}

	form := post.PostForm{Locale: this.Locale}
	topicSlug := this.GetString("topic")
	if len(topicSlug) > 0 {
		topic, err := models.GetTopicBySlug(topicSlug)
		if err != nil {
			this.Redirect(setting.AppUrl)
			return nil
		}

		form.Topic = topic.Id
		form.Category = topic.CategoryId

		err = models.FindTopicsByCategoryId(&form.Topics, topic.CategoryId)
		if err != nil {
			this.Redirect(setting.AppUrl)
			return nil
		}

		this.Data["Topic"] = topic
	} else {
		catSlug := this.GetString("category")
		if len(catSlug) > 0 {
			category, err := models.GetCategoryBySlug(catSlug)
			if err != nil {
				return err
			}

			form.Category = category.Id
			err = models.FindTopicsByCategoryId(&form.Topics, category.Id)
			if err != nil {
				return err
			}
			this.Data["Category"] = category
		} else {
			this.Redirect(setting.AppUrl)
			return nil
		}
	}

	this.SetFormSets(&form)
	return this.Render("post/new.html", this.Data)
}
Exemplo n.º 3
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)
}
Exemplo n.º 4
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)
}