Esempio n. 1
0
func ArticlePermaLinkHandler(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)

	vars := mux.Vars(r)
	id, err := strconv.ParseInt(vars["id"], 10, 64)
	if err != nil {
		core.HandleError(c, w, err)
		return
	}

	article, err := GetArticleById(c, id, true)
	if err != nil {
		core.HandleNotFound(c, w)
		return
	}

	if !article.IsPublic {
		user := auth.CurrentUser(c)
		if !user.IsAdmin {
			core.HandleAuthRequired(c, w)
			return
		}
	}

	redirectTo, err := article.URL()
	if err != nil {
		core.HandleNotFound(c, w)
		return
	}
	http.Redirect(w, r, redirectTo.Path, 302)
}
Esempio n. 2
0
func ArticleHandler(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)
	user := auth.CurrentUser(c)

	vars := mux.Vars(r)
	id, err := strconv.ParseInt(vars["id"], 10, 64)
	if err != nil {
		core.HandleError(c, w, err)
		return
	}

	article, err := GetArticleById(c, id, !user.IsAdmin)
	if err != nil {
		core.HandleNotFound(c, w)
		return
	}

	if !article.IsPublic && !user.IsAdmin {
		core.HandleAuthRequired(c, w)
		return
	}

	viewedArticles := make([]string, 0)
	if cookie, err := r.Cookie("viewedArticles"); err != http.ErrNoCookie {
		viewedArticles = strings.Split(cookie.Value, ",")
	}

	articleID := strconv.FormatInt(article.Key().IntID(), 32)
	if !isViewedArticle(viewedArticles, articleID) {
		viewedArticles = append(viewedArticles, articleID)
		http.SetCookie(w, &http.Cookie{
			Name:    "viewedArticles",
			Value:   strings.Join(viewedArticles, ","),
			Path:    "/",
			Expires: time.Now().Add(time.Duration(30*24) * time.Hour),
		})

		if err := ChangeArticleViewsCount(c, article.Key(), +1); err != nil {
			core.HandleError(c, w, err)
			return
		}
	}

	context := tmplt.Context{"article": article}
	core.RenderTemplate(c, w, context, "templates/blog/article.html", LAYOUT)
}