Example #1
0
func Random(w http.ResponseWriter, req *http.Request, ctx *models.Context) error {
	page := 1
	page, err := strconv.Atoi(req.URL.Query().Get(":page"))
	if err != nil && page == 0 {
		page = 1
	}
	skip := ITEMS_PER_PAGE * (page - 1)
	query := bson.M{"active": true}
	if f, ok := ctx.Session.Values["filter"]; ok {
		f.(*models.Filter).AddQuery(query)
	}

	//execute the query
	var photos []*models.Photo
	if err := ctx.C("photos").Find(query).Skip(skip).Limit(ITEMS_PER_PAGE).Sort("-_id").All(&photos); err != nil {
		return internal_error(w, req, err.Error())
	}
	data := ""
	var layer bytes.Buffer
	for _, i := range rand.Perm(len(photos)) {
		p := photos[i]
		err := layerTemplate.Execute(&layer, map[string]interface{}{"p": p, "ctx": ctx})
		if err != nil {
			models.Log("layer template: ", err.Error())
		}
		data += fmt.Sprintf(`{"image":"%s","thumb":"%s","title":"%s","description":"%s","layer":"%s"},`,
			models.ImageUrl(p.Id.Hex(), ""),
			models.ImageUrl(p.Id.Hex(), "thumb"),
			p.Title,
			p.Description,
			strings.Replace(layer.String(), "\n", "", -1),
		)
		layer.Reset()
	}

	data = strings.TrimRight(data, ",")
	w.Header().Set("Content-Type", "application/json")
	fmt.Fprintf(w, "["+data+"]")
	return nil
}
Example #2
0
func SetAvatar(w http.ResponseWriter, req *http.Request, ctx *models.Context) error {
	if ctx.User == nil {
		return perform_status(w, req, http.StatusForbidden)
	}
	if req.URL.Query().Get(":csrf_token") != ctx.Session.Values["csrf_token"] {
		return perform_status(w, req, http.StatusForbidden)
	}
	photoId := req.URL.Query().Get(":photo")
	if bson.IsObjectIdHex(photoId) {
		newAvatar := models.ImageUrl(photoId, "thumb")
		ctx.User.Avatar = newAvatar
		ctx.C(U).UpdateId(ctx.User.Id, bson.M{"$set": bson.M{"avatar": newAvatar}})
		//ctx.C(P).Update(bson.M{"comments.user": ctx.User.Id}, bson.M{"comments.$.avatar": bson.M{"$set": ctx.User.Gravatar(80)}})
	}
	return nil
}
Example #3
0
func TopVoted(w http.ResponseWriter, req *http.Request, ctx *models.Context) error {
	page := 1
	page, err := strconv.Atoi(req.URL.Query().Get(":page"))
	if err != nil && page == 0 {
		page = 1
	}
	skip := ITEMS_PER_PAGE * (page - 1)
	match := bson.M{"active": true}
	if f, ok := ctx.Session.Values["filter"]; ok {
		f.(*models.Filter).AddQuery(match)
	}
	var results models.WilsonSorter
	pipe := ctx.C(V).Pipe([]bson.M{
		{"$match": match},
		{"$group": bson.M{
			"_id":         "$photo",
			"scores":      bson.M{"$push": "$score"},
			"title":       bson.M{"$addToSet": "$title"},
			"description": bson.M{"$addToSet": "$description"},
			"user":        bson.M{"$addToSet": "$photouser"},
		}},
		{"$skip": skip},
		{"$limit": ITEMS_PER_PAGE},
		{"$unwind": "$user"},
		{"$unwind": "$title"},
		{"$unwind": "$description"},
	})
	pipe.All(&results)

	// calculate wilson rating http://www.goproblems.com/test/wilson/wilson-new.php
	vc := make([]int, 5)
	for _, r := range results {
		scores := r["scores"].([]interface{})
		for _, s := range scores {
			index := int(s.(float64) - 1)
			vc[index] += 1
		}
		sum := 0.0
		for i, c := range vc {
			w := float64(i) / 4.0
			sum += float64(c) * w
		}
		r["wilson"] = models.Wilson(len(scores), sum)
		vc[0], vc[1], vc[2], vc[3], vc[4] = 0, 0, 0, 0, 0
	}
	sort.Sort(results)

	data := ""
	var layer bytes.Buffer
	for _, r := range results {
		p := &models.Photo{
			Id:          r["_id"].(bson.ObjectId),
			User:        r["user"].(bson.ObjectId),
			Title:       r["title"].(string),
			Description: r["description"].(string),
		}
		err := layerTemplate.Execute(&layer, map[string]interface{}{"p": p, "ctx": ctx})
		if err != nil {
			models.Log("layer template: ", err.Error())
		}
		data += fmt.Sprintf(`{"image":"%s","thumb":"%s","title":"%s","description":"%s", "layer":"%s"},`,
			models.ImageUrl(p.Id.Hex(), ""),
			models.ImageUrl(p.Id.Hex(), "thumb"),
			p.Title,
			p.Description,
			strings.Replace(layer.String(), "\n", "", -1),
		)
		layer.Reset()
	}

	data = strings.TrimRight(data, ",")
	w.Header().Set("Content-Type", "application/json")
	fmt.Fprintf(w, "["+data+"]")
	return nil
}