Example #1
0
func taskReferralsHandler(r *http.Request, w http.ResponseWriter,
	redis *models.RedisLogger, user *models.Account) {

	if user.Taskid > len(models.NewTasks) {
		writeResponse(r.RequestURI, w, nil, errors.NewError(errors.AccessError))
		return
	}
	task := models.NewTasks[user.Taskid]
	excludes := redis.TaskSharers()
	friends := redis.Friends(models.RelFriend, user.Id)
	excludes = append(excludes, user.Id)
	excludes = append(excludes, friends...)
	users, _ := user.TaskReferrals(task.Type, excludes)
	var referrals []*referral

	for i, _ := range users {
		ref := &referral{
			Userid:   users[i].Id,
			Nickname: users[i].Nickname,
			Profile:  users[i].Profile,
			Gender:   users[i].Gender,
			Images:   users[i].Photos,
			Birthday: users[i].Birth,
			Lastlog:  users[i].LastLogin.Unix(),
			Location: users[i].Loc,
		}
		if users[i].Ratios.RunRecv > 0 {
			ref.RunRatio = float32(users[i].Ratios.RunAccept) / float32(users[i].Ratios.RunRecv)
		}
		if users[i].Ratios.PostRecv > 0 {
			ref.PostRatio = float32(users[i].Ratios.PostAccept) / float32(users[i].Ratios.PostRecv)
		}
		if users[i].Ratios.PKRecv > 0 {
			ref.PkRatio = float32(users[i].Ratios.PKAccept) / float32(users[i].Ratios.PKRecv)
		}
		if task.Type == models.TaskRunning || task.Type == models.TaskGame {
			rec, _ := users[i].LastRecord("run")
			ref.LastId = rec.Id.Hex()
			ref.LastTime = rec.PubTime.Unix()
		} else if task.Type == models.TaskPost {
			article := users[i].LatestArticle()
			ref.LastId = article.Id.Hex()
			ref.LastTime = article.PubTime.Unix()
		}
		referrals = append(referrals, ref)
	}

	respData := map[string]interface{}{"referrals": referrals}
	writeResponse(r.RequestURI, w, respData, nil)
}