// Collect all bribbles from all users followed func (ts *Tribserver) GetTribblesBySubscription( args *tp.GetTribblesArgs, reply *tp.GetTribblesReply) error { user_key := fmt.Sprintf("%s:U", args.Userid) foll_key := fmt.Sprintf("%s:F", args.Userid) if _, err := ts.store.Get(user_key); lsplog.CheckReport(2, err) { reply.Status = tp.ENOSUCHUSER return nil } follows, err := ts.store.GetList(foll_key) if lsplog.CheckReport(3, err) { reply.Status = tp.ENOSUCHUSER return nil } for i := 0; i < len(follows); i++ { var getargs tp.GetTribblesArgs var getreply tp.GetTribblesReply getargs.Userid = follows[i] err = ts.GetTribbles(&getargs, &getreply) if lsplog.CheckReport(1, err) { reply.Status = tp.ENOSUCHUSER return err } reply.Tribbles = append(reply.Tribbles, getreply.Tribbles...) } var tribs Tribs = reply.Tribbles sort.Sort(tribs) if len(tribs) > 100 { reply.Tribbles = tribs[:100] } reply.Status = tp.OK return nil }
// Get posted tribbles func (ts *Tribserver) GetTribbles( args *tp.GetTribblesArgs, reply *tp.GetTribblesReply) error { user_key := fmt.Sprintf("%s:U", args.Userid) trib_key := fmt.Sprintf("%s:T", args.Userid) if _, err := ts.store.Get(user_key); err != nil { reply.Status = tp.ENOSUCHUSER return nil } var ids []string var err error if ids, err = ts.store.GetList(trib_key); err != nil { reply.Status = tp.ENOSUCHUSER return nil } length := len(ids) if length > 100 { length = 100 } reply.Tribbles = make([]tp.Tribble, length) for i := 0; i < length; i++ { key := fmt.Sprintf("%s:%s:%s", args.Userid, ids[len(ids)-1-i], ts.hostport) result, err := ts.store.Get(key) if err != nil { reply.Status = tp.ENOSUCHUSER return errors.New("get tribble message error") } json.Unmarshal([]byte(result), &(reply.Tribbles[i])) } reply.Status = tp.OK return nil }