Esempio n. 1
0
func (c ArticleHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)

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

	// no extra article information in demo mode
	// ----------------------
	user := app.GetUser(c.App.DB(), r)

	if len(user.Email) == 0 {
		// demo mode
		render(w, 200, articleHandlerResp{
			Id:              articleId,
			DemoModeMessage: true,
		})
		return
	}

	// get the data
	// ----------------------

	rankings, err := c.App.DB().FindArticleRanking(subreddit, articleId)
	if err != nil {
		render(w, 500, ErrorResponse{
			Code:    500,
			Message: "can't retrieve data",
		})
		return
	}

	// compute and render
	// ----------------------

	resp := articleHandlerResp{
		Id: articleId,
	}
	computeRankings(&resp, rankings)
	render(w, 200, resp)
}
Esempio n. 2
0
func (c AnnotateHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)

	subreddit := vars["subreddit"]

	data, err := ioutil.ReadAll(r.Body)
	if err != nil {
		w.WriteHeader(500)
		log.Printf("err: while reading body of annotate: %s\n", err.Error())
		return
	}

	defer r.Body.Close()

	var body annotateBody
	if err := json.Unmarshal(data, &body); err != nil {
		w.WriteHeader(400)
		return
	}

	if len(subreddit) == 0 || body.Time.IsZero() || len(body.Message) == 0 {
		w.WriteHeader(400)
		return
	}

	u := app.GetUser(c.App.DB(), r)
	if len(u.Email) == 0 {
		w.WriteHeader(403)
		return
	}

	err = c.App.DB().InsertAnnotation(u.Uuid, subreddit, body.Time, body.Message)
	if err != nil {
		w.WriteHeader(500)
		log.Printf("err: while annotating by '%s' on '%s': %s", u.Email, subreddit, err.Error())
		return
	}
}
Esempio n. 3
0
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),
	})
}