Exemplo n.º 1
0
func (ts *tribServer) RemoveSubscription(args *tribrpc.SubscriptionArgs, reply *tribrpc.SubscriptionReply) error {
	userkey := util.FormatUserKey(args.UserID)
	targetkey := util.FormatUserKey(args.TargetUserID)

	usersubs := util.FormatSubListKey(args.UserID)
	target := args.TargetUserID

	// Make sure user exists
	if _, eu := ts.ls.Get(userkey); eu != nil {
		reply.Status = tribrpc.NoSuchUser
		return nil
	}
	// Make sure target exists
	if _, et := ts.ls.Get(targetkey); et != nil {
		reply.Status = tribrpc.NoSuchTargetUser
		return nil
	}
	// Make sure user is subscribed to target
	if err := ts.ls.RemoveFromList(usersubs, target); err != nil {
		reply.Status = tribrpc.NoSuchTargetUser
	} else {
		reply.Status = tribrpc.OK
	}
	return nil
}
// AddSubscription adds TargerUserID to UserID's list of subscriptions.
// Replies with status NoSuchUser if the specified UserID does not exist, and NoSuchTargerUser
// if the specified TargerUserID does not exist.
func (ts *tribServer) AddSubscription(args *tribrpc.SubscriptionArgs, reply *tribrpc.SubscriptionReply) error {

	userId := args.UserID
	targetId := args.TargetUserID

	userIdKey := util.FormatUserKey(userId)
	targetIdKey := util.FormatUserKey(targetId)

	_, ok_user := ts.libstore.Get(userIdKey)
	_, ok_target := ts.libstore.Get(targetIdKey)
	if ok_user != nil {
		reply.Status = tribrpc.NoSuchUser
	} else if ok_target != nil {
		reply.Status = tribrpc.NoSuchTargetUser
	} else {
		subKey := util.FormatSubListKey(userId)

		err := ts.libstore.AppendToList(subKey, targetId)
		if err != nil {
			reply.Status = tribrpc.Exists
		} else {
			reply.Status = tribrpc.OK
		}
	}
	return nil
}
Exemplo n.º 3
0
func (ts *tribServer) AddSubscription(args *tribrpc.SubscriptionArgs, reply *tribrpc.SubscriptionReply) error {
	userkey := util.FormatUserKey(args.UserID)
	targetkey := util.FormatUserKey(args.TargetUserID)

	usersubs := util.FormatSubListKey(args.UserID)
	target := args.TargetUserID

	// Make sure user exists
	if _, eu := ts.ls.Get(userkey); eu != nil {
		reply.Status = tribrpc.NoSuchUser
		return nil
	}
	// Make sure targetUser exists
	if _, et := ts.ls.Get(targetkey); et != nil {
		reply.Status = tribrpc.NoSuchTargetUser
		return nil
	}
	// Make sure user isn't already subscribed to target
	err := ts.ls.AppendToList(usersubs, target)
	if err == nil {
		reply.Status = tribrpc.OK
	} else {
		reply.Status = tribrpc.Exists
	}
	return nil
}
Exemplo n.º 4
0
func (ts *tribServer) AddSubscription(args *tribrpc.SubscriptionArgs, reply *tribrpc.SubscriptionReply) error {
	userid := args.UserID
	targetid := args.TargetUserID
	useridkey := util.FormatUserKey(userid)
	targetidkey := util.FormatUserKey(targetid)

	_, userExists := ts.ls.Get(useridkey)

	if userExists != nil {
		reply.Status = tribrpc.NoSuchUser
		return nil
	}

	_, targetExists := ts.ls.Get(targetidkey)

	if targetExists != nil {
		reply.Status = tribrpc.NoSuchTargetUser
		return nil
	}

	userSubListKey := util.FormatSubListKey(userid)
	appendErr := ts.ls.AppendToList(userSubListKey, targetid)

	if appendErr != nil {
		reply.Status = tribrpc.Exists
		return nil
	}

	reply.Status = tribrpc.OK
	return nil
}
Exemplo n.º 5
0
/*
-format sublist
*/
func (ts *tribServer) GetTribblesBySubscription(args *tribrpc.GetTribblesArgs, reply *tribrpc.GetTribblesReply) error {

	if DBG {
		fmt.Println("-----> GetTribblesBySubscription")
	}
	var usrID string = args.UserID

	//check if user present in server
	_, err := ts.libStore.Get(util.FormatUserKey(usrID))

	if err != nil {
		switch err.Error() {
		case WRONG_SERVER:
			fmt.Println("ERROR: WRONG SERVER in tribserver")
			return errors.New("Wrong server contacted!")

		case KEY_NOT_FOUND:
			reply.Status = tribrpc.NoSuchUser
			return nil

		default:
			fmt.Println("ERROR in tribserver: wrong error message received")
		}
	}

	//get list of subscribers
	subscrList, err := ts.libStore.GetList(util.FormatSubListKey(usrID))

	if err != nil {
		//return err
		fmt.Println("No subscribers, or error getting list of subscribers")
		reply.Status = tribrpc.OK
		return nil
	}

	//initialise empty slice of all postKeys from subscribers
	var allPostKeys []string = make([]string, 0)

	//populate allPostKeys
	for _, currUser := range subscrList {
		currPostKeys, err := ts.libStore.GetList(util.FormatTribListKey(currUser))

		if err == nil {
			allPostKeys = append(allPostKeys, currPostKeys...)

		} else {
			//fmt.Println("0 tribs for user detected")
		}
	}

	sort.Sort(PostByTime(allPostKeys))

	//choose most recent posts, and get tribbles
	var tribList []tribrpc.Tribble = ts.getTribbleList(allPostKeys)

	reply.Tribbles = tribList
	reply.Status = tribrpc.OK
	return nil

}
Exemplo n.º 6
0
func (ts *tribServer) GetTribblesBySubscription(args *tribrpc.GetTribblesArgs, reply *tribrpc.GetTribblesReply) error {
	if ts.userExist(args.UserID) == false {
		reply.Status = tribrpc.NoSuchUser
		return nil
	}
	subscriptions, err := ts.ls.GetList(util.FormatSubListKey(args.UserID))
	if err != nil {
		reply.Status = tribrpc.OK
		reply.Tribbles = make([]tribrpc.Tribble, 0)
		return nil
	}
	reply.Tribbles = make([]tribrpc.Tribble, 0)
	mychan := make(chan []tribrpc.Tribble, 1000)
	go func() {
		for i := 0; i < len(subscriptions); i++ {
			user := subscriptions[i]
			go func() {
				tribbles, _ := ts.GetAllTribblesByUser(user)
				mychan <- tribbles
			}()
		}
	}()
	for i := 0; i < len(subscriptions); i++ {
		tribbles := <-mychan
		reply.Tribbles = append(reply.Tribbles, tribbles...)
	}
	reply.Status = tribrpc.OK
	reply.Tribbles = prepareTribbles(reply.Tribbles)
	return nil
}
Exemplo n.º 7
0
func (ts *tribServer) AddSubscription(args *tribrpc.SubscriptionArgs, reply *tribrpc.SubscriptionReply) error {
	user_id := util.FormatUserKey(args.UserID)
	target_user_id := util.FormatUserKey(args.TargetUserID)

	// If the user_id doesn't exist
	// Return error
	_, err := ts.lib_store.Get(user_id)
	if err != nil {
		reply.Status = tribrpc.NoSuchUser
		return nil
	}

	// If the target_user_id doesn't exist
	// Return error
	_, err = ts.lib_store.Get(target_user_id)
	if err != nil {
		reply.Status = tribrpc.NoSuchTargetUser
		return nil
	}

	user_sub := util.FormatSubListKey(args.UserID)
	err = ts.lib_store.AppendToList(user_sub, args.TargetUserID)
	if err != nil {
		reply.Status = tribrpc.Exists
		return nil
	}
	reply.Status = tribrpc.OK
	return nil
}
func (ts *tribServer) GetTribblesBySubscription(args *tribrpc.GetTribblesArgs, reply *tribrpc.GetTribblesReply) error {
	userkey := util.FormatUserKey(args.UserID)
	_, userErr := ts.libstore.Get(userkey)

	if userErr != nil {
		reply.Status = tribrpc.NoSuchUser
	} else {
		reply.Status = tribrpc.OK
		subskey := util.FormatSubListKey(args.UserID)
		subsList, _ := ts.libstore.GetList(subskey)

		totalTribList := make(Tribbles, 0)
		for i := 0; i < len(subsList); i++ {
			curUser := subsList[i]
			curtribList := ts.GetTribblesOneUser(curUser)

			for j := 0; j < len(curtribList); j++ {
				totalTribList = append(totalTribList, curtribList[j])
			}
			//totalTribList = append(totalTribList, curtribList)
		}

		sort.Sort(totalTribList)
		totalLen := len(totalTribList)
		if totalLen > 100 {
			totalLen = 100
		}
		reply.Tribbles = make(Tribbles, totalLen)
		for j := 0; j < totalLen; j++ {
			reply.Tribbles[j] = totalTribList[j]
		}
	}
	return nil

}
Exemplo n.º 9
0
func (ts *tribServer) GetTribblesBySubscription(args *tribrpc.GetTribblesArgs, reply *tribrpc.GetTribblesReply) error {
	if args == nil {
		return errors.New("ts: Can't getSubscription nil")
	}
	if reply == nil {
		return errors.New("ts: Can't reply with nil in getSubscription")
	}

	_, err := ts.ls.Get(util.FormatUserKey(args.UserID))
	if err != nil {
		reply.Status = tribrpc.NoSuchUser
		reply.Tribbles = make([]tribrpc.Tribble, 0, 0)
		return nil
	}

	users, err := ts.ls.GetList(util.FormatSubListKey(args.UserID))
	if err != nil {
		reply.Status = tribrpc.OK
		reply.Tribbles = make([]tribrpc.Tribble, 0, 0)
		return nil
	}

	postIDs := make([]string, 0)
	for _, userID := range users {
		lst, err := ts.ls.GetList(util.FormatTribListKey(userID))
		if err == nil {
			postIDs = append(postIDs, lst...)
		}
	}

	tmp := tribbleIDs(postIDs)

	sort.Sort(tmp)
	tribbleIdsChrono := []string(tmp)

	length := 100
	if len(tribbleIdsChrono) < 100 {
		length = len(tribbleIdsChrono)
	}

	tribbles := make([]tribrpc.Tribble, length)
	var tribTmp tribrpc.Tribble
	i := 0
	for i < length {
		trib, err := ts.ls.Get(tribbleIdsChrono[i])
		if err == nil {
			json.Unmarshal([]byte(trib), &tribTmp)
			tribbles[i] = tribTmp
			i += 1 // If error occurs, we do NOT increment i
		}
	}
	reply.Status = tribrpc.OK
	reply.Tribbles = tribbles
	return nil
}
Exemplo n.º 10
0
func (ts *tribServer) GetTribblesBySubscription(args *tribrpc.GetTribblesArgs, reply *tribrpc.GetTribblesReply) error {
	// Getsublist by key
	// Get tribble list by user_ids in sublist
	// sort the tribble list by the timestamp
	// retrive the first 100 tribble (Be careful about the delete operation)
	user_id := util.FormatUserKey(args.UserID)

	// If the user_id doesn't exist
	// Reture NoSuchUser
	_, err := ts.lib_store.Get(user_id)
	if err != nil {
		reply.Status = tribrpc.NoSuchUser
		return nil
	}

	// Get user's subscription_list
	user_id_list := util.FormatSubListKey(args.UserID)
	// list of IDs
	user_list, _ := ts.lib_store.GetList(user_id_list)

	if user_list == nil {
		reply.Status = tribrpc.OK
		return nil
	}

	// Get tribble_ids from the user_ids in the subscription list
	tribble_id_list := make([]string, 0)
	for _, usr_id := range user_list {
		t_id_list, err := ts.lib_store.GetList(util.FormatTribListKey(usr_id))
		if err == nil {
			tribble_id_list = append(tribble_id_list, t_id_list...)
		}
	}

	sort.Sort(SortTribbleByTimestamp(tribble_id_list))

	counter := 0
	tribble_list := make([]tribrpc.Tribble, 100)
	for _, tid := range tribble_id_list {
		tribble, err := ts.lib_store.Get(tid)
		if err == nil {
			json.Unmarshal([]byte(tribble), &tribble_list[counter])
			counter += 1
		}
		if counter >= 100 {
			break
		}
	}

	reply.Status = tribrpc.OK
	reply.Tribbles = tribble_list[:counter]
	return nil
}
func (ts *tribServer) GetSubscriptions(args *tribrpc.GetSubscriptionsArgs, reply *tribrpc.GetSubscriptionsReply) error {
	subkey := util.FormatSubListKey(args.UserID)
	userkey := util.FormatUserKey(args.UserID)
	_, userErr := ts.libstore.Get(userkey)
	if userErr != nil {
		reply.Status = tribrpc.NoSuchUser
	} else {
		list, _ := ts.libstore.GetList(subkey)
		reply.Status = tribrpc.OK
		reply.UserIDs = list
	}
	return nil
}
Exemplo n.º 12
0
func (ts *tribServer) GetSubscriptions(args *tribrpc.GetSubscriptionsArgs, reply *tribrpc.GetSubscriptionsReply) error {
	if ts.userExist(args.UserID) == false {
		reply.Status = tribrpc.NoSuchUser
		return nil
	}
	UserIDs, err := ts.ls.GetList(util.FormatSubListKey(args.UserID))
	if err == nil {
		reply.Status = tribrpc.OK
		reply.UserIDs = UserIDs
	} else {
		reply.Status = tribrpc.OK
		reply.UserIDs = make([]string, 0)
	}
	return nil
}
Exemplo n.º 13
0
func (ts *tribServer) GetSubscriptions(args *tribrpc.GetSubscriptionsArgs, reply *tribrpc.GetSubscriptionsReply) error {
	key := util.FormatUserKey(args.UserID)
	if ts.checkIfKnown(key) {
		sublist_key := util.FormatSubListKey(args.UserID)
		sub_ids, err := ts.storage.GetList(sublist_key)
		if err != nil {
			sub_ids = []string{}
		}
		reply.UserIDs = sub_ids
		reply.Status = tribrpc.OK
	} else {
		reply.Status = tribrpc.NoSuchUser
	}
	return nil
}
Exemplo n.º 14
0
func (ts *tribServer) AddSubscription(args *tribrpc.SubscriptionArgs, reply *tribrpc.SubscriptionReply) error {
	if ts.userExist(args.UserID) == false {
		reply.Status = tribrpc.NoSuchUser
		return nil
	}
	if ts.userExist(args.TargetUserID) == false {
		reply.Status = tribrpc.NoSuchTargetUser
		return nil
	}
	err := ts.ls.AppendToList(util.FormatSubListKey(args.UserID), args.TargetUserID)
	if err == nil {
		reply.Status = tribrpc.OK
	} else {
		reply.Status = tribrpc.Exists
	}
	return nil
}
Exemplo n.º 15
0
func (ts *tribServer) AddSubscription(args *tribrpc.SubscriptionArgs, reply *tribrpc.SubscriptionReply) error {
	user_key := util.FormatUserKey(args.UserID)
	target_key := util.FormatUserKey(args.TargetUserID)
	if !ts.checkIfKnown(user_key) {
		reply.Status = tribrpc.NoSuchUser
	} else if !ts.checkIfKnown(target_key) {
		reply.Status = tribrpc.NoSuchTargetUser
	} else {
		sublist_key := util.FormatSubListKey(args.UserID)
		err := ts.storage.AppendToList(sublist_key, args.TargetUserID)
		if err != nil {
			reply.Status = tribrpc.Exists
		} else {
			reply.Status = tribrpc.OK
		}
	}
	return nil
}
Exemplo n.º 16
0
func (ts *tribServer) GetSubscriptions(args *tribrpc.GetSubscriptionsArgs, reply *tribrpc.GetSubscriptionsReply) error {
	user_id := util.FormatUserKey(args.UserID)

	// If the user_id doesn't exist
	// Reture NoSuchUser
	_, err := ts.lib_store.Get(user_id)
	if err != nil {
		fmt.Println("===========", err, "============")
		reply.Status = tribrpc.NoSuchUser
		return nil
	}

	user_id_list := util.FormatSubListKey(args.UserID)
	user_list, err := ts.lib_store.GetList(user_id_list)
	//if err != nil {
	//		reply.Status = tribrpc.NoSuchUser
	//		return nil
	//	}

	reply.UserIDs = user_list
	reply.Status = tribrpc.OK
	return nil
}
Exemplo n.º 17
0
func (ts *tribServer) GetSubscriptions(args *tribrpc.GetSubscriptionsArgs, reply *tribrpc.GetSubscriptionsReply) error {

	userkey := util.FormatUserKey(args.UserID)

	usersubs := util.FormatSubListKey(args.UserID)

	// Make sure user exists
	if _, eu := ts.ls.Get(userkey); eu != nil {
		reply.Status = tribrpc.NoSuchUser
		return nil
	}

	if lst, err := ts.ls.GetList(usersubs); err == nil {
		reply.Status = tribrpc.OK
		reply.UserIDs = lst
		return nil
	} else {
		// The user has no subscriptions yet
		reply.Status = tribrpc.OK
		reply.UserIDs = make([]string, 0)
		return nil
	}
}
Exemplo n.º 18
0
func (ts *tribServer) GetTribblesBySubscription(args *tribrpc.GetTribblesArgs, reply *tribrpc.GetTribblesReply) error {
	key := util.FormatUserKey(args.UserID)
	if ts.checkIfKnown(key) {
		sublist_key := util.FormatSubListKey(args.UserID)
		sublist_ids, err := ts.storage.GetList(sublist_key)
		if err != nil {
			sublist_ids = []string{}
		}

		post_ids := []string{}
		for _, userid := range sublist_ids {
			triblist_key := util.FormatTribListKey(userid)
			local_post_ids, err := ts.storage.GetList(triblist_key)
			if err != nil {
				sublist_ids = []string{}
			}
			post_ids = append(post_ids, local_post_ids...)
		}

		sort.Sort(ByChrono(post_ids))
		tribs := []tribrpc.Tribble{}
		for _, v := range post_ids[0:min(100, len(post_ids))] {
			postStr, err := ts.storage.Get(v)
			if err != nil {
				return err
			}
			trib := tribrpc.Tribble{}
			json.Unmarshal([]byte(postStr), &trib)
			tribs = append(tribs, trib)
		}
		reply.Tribbles = tribs
		reply.Status = tribrpc.OK
	} else {
		reply.Status = tribrpc.NoSuchUser
	}
	return nil
}
Exemplo n.º 19
0
func (ts *tribServer) GetSubscriptions(args *tribrpc.GetSubscriptionsArgs, reply *tribrpc.GetSubscriptionsReply) error {
	userid := args.UserID
	useridkey := util.FormatUserKey(userid)

	_, userExists := ts.ls.Get(useridkey)

	if userExists != nil {
		reply.Status = tribrpc.NoSuchUser
		return nil
	}

	userSubListKey := util.FormatSubListKey(userid)
	nodes, err := ts.ls.GetList(userSubListKey)

	if err != nil {
		reply.Status = tribrpc.OK
		reply.UserIDs = nil
		return nil
	}

	reply.Status = tribrpc.OK
	reply.UserIDs = nodes
	return nil
}
Exemplo n.º 20
0
func (ts *tribServer) AddSubscription(args *tribrpc.SubscriptionArgs, reply *tribrpc.SubscriptionReply) error {
	if DBG {
		fmt.Println("-----> AddSubscription")
	}
	/*
		-Check if user present
		-Check if user being subscribed to is present
		-append to list: sublist key formatter,
	*/

	var thisUsrId string = args.UserID
	var subscrId string = args.TargetUserID

	//check if this user present in server
	_, err := ts.libStore.Get(util.FormatUserKey(thisUsrId))

	if err != nil {

		switch err.Error() {
		case WRONG_SERVER:
			fmt.Println("ERROR: WRONG SERVER in tribserver")
			return errors.New("Wrong server contacted!")

		case KEY_NOT_FOUND:
			reply.Status = tribrpc.NoSuchUser
			return nil

		default:
			fmt.Println("ERROR in tribserver: wrong error message received")
			return nil
		}
	}

	//check if subscribe user is present
	_, err = ts.libStore.Get(util.FormatUserKey(subscrId))

	if err != nil {
		switch err.Error() {
		case WRONG_SERVER:
			fmt.Println("ERROR: WRONG SERVER in tribserver")
			return errors.New("Wrong server contacted!")

		case KEY_NOT_FOUND:
			reply.Status = tribrpc.NoSuchTargetUser
			return nil

		default:
			fmt.Println("ERROR in tribserver: wrong error message received")
		}
	}

	//add to list of subscribers
	err = ts.libStore.AppendToList(util.FormatSubListKey(thisUsrId), subscrId)

	if err != nil {
		//TODO after checkpoint, case on err
		reply.Status = tribrpc.Exists
		return nil
	}

	/* no error */
	reply.Status = tribrpc.OK
	return nil
}
Exemplo n.º 21
0
func (ts *tribServer) GetSubscriptions(args *tribrpc.GetSubscriptionsArgs, reply *tribrpc.GetSubscriptionsReply) error {
	if DBG {
		fmt.Println("-----> GetSubscriptions")
	}
	/*
		formatsublistkey
	*/
	var thisUsrId string = args.UserID

	//check if user present in server
	_, err := ts.libStore.Get(util.FormatUserKey(thisUsrId))

	if err != nil {
		switch err.Error() {
		case KEY_NOT_FOUND:
			reply.Status = tribrpc.NoSuchUser
			return nil

		case WRONG_SERVER:
			fmt.Println("ERROR: WRONG SERVER in tribserver")
			return errors.New("Wrong server contacted!")

		default:
			fmt.Println("ERROR in tribserver: wrong error message received")
			return errors.New("WRONG error message")
		}

		//user not found
		reply.Status = tribrpc.NoSuchUser
		//return err
		return nil
	}

	subscrList, err := ts.libStore.GetList(util.FormatSubListKey(thisUsrId))

	if err == nil {
		reply.Status = tribrpc.OK
		reply.UserIDs = subscrList
		return nil
	}

	//error not nil

	switch err.Error() {
	case KEY_NOT_FOUND:
		//User present, no subscriptions
		reply.Status = tribrpc.OK
		//Empty list of user IDs
		reply.UserIDs = make([]string, 0)
		return nil
		break

	case WRONG_SERVER:
		fmt.Println("ERROR: WRONG SERVER in tribserver")
		return nil
		break
	default:
		fmt.Println("ERROR in tribserver: wrong error message received")
	}

	return nil
}
Exemplo n.º 22
0
/*
check if both values exist, then delete the sub
*/
func (ts *tribServer) RemoveSubscription(args *tribrpc.SubscriptionArgs, reply *tribrpc.SubscriptionReply) error {
	if DBG {
		fmt.Println("-----> RemoveSubscription")
	}
	var thisUsrId string = args.UserID
	var subscrId string = args.TargetUserID

	//check if this user present in server
	_, err := ts.libStore.Get(util.FormatUserKey(thisUsrId))

	if err != nil {

		switch err.Error() {
		case WRONG_SERVER:
			fmt.Println("ERROR: WRONG SERVER in tribserver")
			return errors.New("Wrong server contacted!")

		case KEY_NOT_FOUND:
			reply.Status = tribrpc.NoSuchUser
			return nil

		default:
			fmt.Println("ERROR in tribserver: wrong error message received")
		}
	}

	//check if subscribe user is present
	_, err = ts.libStore.Get(util.FormatUserKey(subscrId))

	if err != nil {
		switch err.Error() {
		case WRONG_SERVER:
			fmt.Println("ERROR: WRONG SERVER in tribserver")
			return errors.New("Wrong server contacted!")

		case KEY_NOT_FOUND:
			reply.Status = tribrpc.NoSuchTargetUser
			return nil

		default:
			fmt.Println("ERROR in tribserver: wrong error message received")
			return nil
		}
	}

	//retrieve list of subsriber user IDs
	err = ts.libStore.RemoveFromList(util.FormatSubListKey(thisUsrId), subscrId)

	if err == nil {
		reply.Status = tribrpc.OK
		return nil
	}

	switch err.Error() {
	case ITEM_NOT_FOUND:
		//User present, no subscriptions
		reply.Status = tribrpc.NoSuchTargetUser
		//Empty list of user IDs
		return nil

	case WRONG_SERVER:
		fmt.Println("ERROR: WRONG SERVER in tribserver")
		return errors.New("Wrong server contacted!")

	case KEY_NOT_FOUND:
		fmt.Println("TribServ: Key was not found!")
		reply.Status = tribrpc.NoSuchTargetUser
		return nil

	default:
		fmt.Println("ERROR in tribserver: wrong error message received")
	}

	if err != nil {
		//TODO after checkpoint, case on err
		reply.Status = tribrpc.NoSuchTargetUser
		return nil
	}

	return nil
}
Exemplo n.º 23
0
func (ts *tribServer) GetTribblesBySubscription(args *tribrpc.GetTribblesArgs, reply *tribrpc.GetTribblesReply) error {
	userkey := util.FormatUserKey(args.UserID)

	// Make sure user exists
	if _, eu := ts.ls.Get(userkey); eu != nil {
		reply.Status = tribrpc.NoSuchUser
		return nil
	}

	// Get list of subscriptions.  return empty list if user has no subscriptions.
	var subs []string
	var esubs error
	if subs, esubs = ts.ls.GetList(util.FormatSubListKey(args.UserID)); esubs != nil {
		reply.Status = tribrpc.OK
		reply.Tribbles = make([]tribrpc.Tribble, 0)
		return nil
	}

	numTribbles := 0

	// Get the last 100 tribble IDs from each subscribed user
	tribIDs := make([][]string, len(subs))
	for i := 0; i < len(subs); i++ {
		if lst, err := ts.ls.GetList(util.FormatTribListKey(subs[i])); err == nil {
			if len(lst) > 100 {
				tribIDs[i] = lst[len(lst)-100:]
			} else {
				tribIDs[i] = lst
			}
			numTribbles += len(lst)
		}
	}

	// lastTribs is the most recent tribble seen from each subscribed user
	lastTribs := make([]tribrpc.Tribble, len(subs))
	// index is the list of indexes into tribIDs for each subscribed user
	index := make([]int, len(subs))

	if numTribbles > 100 {
		numTribbles = 100
	}

	finalTribbles := make([]tribrpc.Tribble, numTribbles)
	var mtribble string
	var tribble tribrpc.Tribble

	// valid[i] marks if an unused Tribble is located at lastTribs[i]
	valid := make([]bool, len(lastTribs))

	for j := 0; j < numTribbles; j++ {
		minIndex := -1
		for check := 0; check < len(lastTribs); check++ {
			// If this user has more tribbles left
			if index[check] < len(tribIDs[check]) {
				// If we need to get a new tribble for this user
				if valid[check] == false {
					mtribble, _ = ts.ls.Get(tribIDs[check][len(tribIDs[check])-1-index[check]])
					json.Unmarshal([]byte(mtribble), &tribble)
					lastTribs[check] = tribble
					valid[check] = true
				}
				// This tribble is more
				if minIndex == -1 || cmpLessTribble(lastTribs[check], lastTribs[minIndex]) {
					minIndex = check
				}
			}
		}
		copyTribble := lastTribs[minIndex]
		finalTribbles[j] = copyTribble
		valid[minIndex] = false
		index[minIndex] += 1

	}

	reply.Status = tribrpc.OK
	reply.Tribbles = finalTribbles
	return nil
}