Beispiel #1
0
func (c Account) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	t := c.App.Templates.Lookup("account.html")
	p := app.TmplParams(c.App, r, "Account")

	if len(p.User.Email) == 0 {
		http.Redirect(w, r, "/signin", 302)
	}

	t.Execute(w, accountParams{
		p,
	})
}
Beispiel #2
0
func (c Audiences) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	t := c.App.Templates.Lookup("audiences.html")

	vars := mux.Vars(r)
	subreddit := vars["subreddit"]

	// test if known subreddit
	subreddit = strings.ToLower(strings.Trim(subreddit, " "))
	if len(subreddit) == 0 {
		http.Redirect(w, r, "/", 302)
		return
	}

	t.Execute(w, audiencesParams{
		Params:    app.TmplParams(c.App, r, "Audiences"),
		Subreddit: subreddit,
	})
}
Beispiel #3
0
func (c Index) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	t := c.App.Templates.Lookup("index.html")
	t.Execute(w, indexParams{
		app.TmplParams(c.App, r, "Index"),
	})
}
func (c TodayHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)

	subreddit := vars["subreddit"]
	if len(subreddit) == 0 {
		w.WriteHeader(400)
		return
	}

	r.ParseForm()
	t := r.Form.Get("t")
	hours := 36

	// ensure the right to retrieve more than 36h
	// ----------------------
	tmplParams := app.TmplParams(c.App, r, "ApiToday")

	demoModeMessage := false
	if len(t) > 0 && t != "36h" && len(tmplParams.User.Email) == 0 {
		// not auth user, demo message and stay on 36h
		demoModeMessage = true
	} else {
		// auth user, test if he wants 7 days of data
		if t == "7d" {
			hours = 24 * 7
		}
	}

	// retrieve data
	// ----------------------

	dataAudiences, dataRankings, dataArticles, err := c.getData(subreddit, hours)
	if err != nil {
		log.Println("err:", err.Error())
		w.WriteHeader(500)
		return
	}

	audiences := object.AudiencesFromApp(dataAudiences)
	lowest, highest := app.LowestHighest(dataAudiences)
	articles := object.ArticlesFromApp(dataArticles, dataRankings)

	// If the user is logged in, we need to
	// retrieve its annotations.
	// ----------------------
	user := app.GetUser(c.App.DB(), r)

	annotations := make([]object.Annotation, 0)
	if len(user.Email) > 0 {
		after := time.Now().Add(-time.Hour * time.Duration(hours))
		values, err := c.App.DB().FindAnnotations(subreddit, user.Uuid, after)
		if err != nil {
			log.Println("err:", err.Error())
			w.WriteHeader(500)
			return
		}
		for _, a := range values {
			annotations = append(annotations, object.Annotation{
				Message: a.Message,
				Time:    a.Time,
			})
		}
	}

	// serialize and send response
	// ----------------------

	render(w, 200, todayHandlerResp{
		Audiences:       audiences,
		Average:         app.Average(dataAudiences),
		Articles:        articles,
		DemoModeMessage: demoModeMessage,
		Annotations:     annotations,
		LowestAudience:  object.AudienceFromApp(lowest),
		HighestAudience: object.AudienceFromApp(highest),
	})
}