Пример #1
0
// getTimeline returns the user's Timeline, you could insert additional things here as well.
func getTimeline(cx appengine.Context, userID, lastid string) ([]TLEntry, error) {
	conn := pool.Get(cx)
	defer conn.Close()

	list, err := redisx.Strings(conn.Do("LRANGE", "TL:"+userID, 0, -1))
	if err != nil && err != redisx.ErrNil {
		cx.Errorf("GetTimeLine %v", err)
	}
	ix := 0

	if lastid != "0" { // if we aren't the first time, search for the next batch
		for i, item := range list {
			if item == lastid {
				ix = i
				break
			}
		}
	}
	var timeline []TLEntry
	// TimeLineBatchSize is our paging mechanism, we will only return this many images.  The user
	// can ask for more.
	for i := 0; i < abelanaConfig().TimelineBatchSize && i+ix < len(list); i++ {
		photoID := list[ix+i]

		v, err := redisx.Strings(conn.Do("HMGET", "IM:"+photoID, "date", userID, "flag"))
		if err != nil && err != redisx.ErrNil {
			cx.Errorf("GetTimeLine HMGET %v", err)
		}
		if len(v) > 2 && v[2] != "" {
			flags, err := strconv.Atoi(v[2])
			if err == nil && flags > 1 {
				continue // skip flag'd images
			}
		}
		likes, err := redisx.Int(conn.Do("HLEN", "IM:"+photoID))
		if err != nil && err != redisx.ErrNil {
			cx.Errorf("GetTimeLine HLEN %v", err)
			likes = 0
		} else {
			likes = likes - 1 // offset as there is a Date as well
		}
		s := strings.Split(photoID, ".")
		dn, err := redisx.String(conn.Do("HGET", "HT:"+s[0], "dn"))
		if err != nil && err != redisx.ErrNil {
			cx.Errorf("GetTimeLine HGET %v", err)
			dn = ""
		}
		dt, err := strconv.ParseInt(v[0], 10, 64)
		if err != nil {
			dt = 1414883602 // Nov 1, 2014
		}
		te := TLEntry{dt, s[0], dn, photoID, likes, v[1] == "1"}
		timeline = append(timeline, te)
	}
	return timeline, nil
}
Пример #2
0
// getPersons finds all the display names
func getPersons(cx appengine.Context, ids []string) ([]Person, error) {
	conn := pool.Get(cx)
	defer conn.Close()
	for _, id := range ids {
		conn.Send("HGET", "HT:"+id, "dn")
	}
	conn.Flush()

	var pl []Person
	for _, id := range ids {
		n, err := redisx.String(conn.Receive())
		if err != nil {
			return nil, fmt.Errorf("getPersons: receive name: %v", err)
		}
		pl = append(pl, Person{Kind: "abelana#follower", PersonID: id, Name: n})
	}
	return pl, nil
}