// User handles requests to query some user's info func (uc *User) User(w *json.Encoder, r *json.Decoder, h *jsonapi.HTTP) { res := new(Response) var args map[string]interface{} if err := r.Decode(&args); err != nil { res.Fail("Arguments not in JSON format.") return } ids, ok := args["userid"] if !ok { res.Fail("No user id passed.").Do(w) return } uids, ok := ids.([]interface{}) if !ok || len(uids) < 1 { res.Fail("No user id passed.").Do(w) return } ret := make([]*model.User, 0, len(uids)) for _, uid := range uids { u, err := model.LoadUser(int(uid.(float64))) // json numbers converts to float64 in go if err != nil { log.Printf("Error loading user from db: %s", err) res.Fail("Error loading user from db").Do(w) return } ret = append(ret, u) } res.Ok(ret).Do(w) }
// Me returns current user info to user func Me(sess common.Session) (u *model.User, err error) { uidStr := sess.Get("uid") if err = sess.Err(); err != nil { log.Printf("Failed to read session data: %s", err) return } uid, err := strconv.Atoi(uidStr) if err != nil { log.Printf("uid (%s) not integer: %s", uidStr, err) return } if u, err = model.LoadUser(uid); err != nil { log.Printf("Failed to load user#%d from db: %s", uid, err) } return }