Exemplo n.º 1
0
func GetContacts(db *d.MainDb, to_name string) ([]users.Contact, error) {
	resp := []users.Contact{}
	err := db.Messages.MessagesCollection.Pipe([]bson.M{
		bson.M{"$match": bson.M{"to": to_name, "is_deleted": false}},
		bson.M{"$group": bson.M{"_id": "$from", "unread_count": bson.M{"$sum": "$unread"}, "name": bson.M{"$first": "$from"}, "time": bson.M{"$max": "$time_stamp"}}}}).All(&resp)
	if err != nil {
		return resp, err
	}
	result := []users.Contact{}
	for i, cnt := range resp {
		user, _ := db.Users.GetUserById(cnt.ID)
		if user != nil {
			if user.ShowedName != "" {
				resp[i].Name = user.ShowedName
			} else {
				resp[i].Name = user.UserName
			}
			resp[i].Phone = user.Phone
			last_order, _ := db.Orders.GetByOwnerLast(resp[i].ID, to_name)
			if last_order != nil {
				resp[i].HaveActiveOrder = last_order.Active
			}
			result = append(result, resp[i])
		}
	}
	sort.Sort(users.ByContactsLastMessageTime(result))
	return result, nil
}
Exemplo n.º 2
0
func GetContacts(db *d.MainDb) ([]usrs.Contact, error) {
	resp := []usrs.Contact{}
	err := db.Messages.MessagesCollection.Pipe([]bson.M{
		bson.M{"$group": bson.M{"_id": "$from", "unread_count": bson.M{"$sum": "$unread"}, "name": bson.M{"$first": "$from"}, "time": bson.M{"$max": "$time_stamp"}}}}).All(&resp)
	if err != nil {
		return resp, err
	}
	result := []usrs.Contact{}
	for i, cnt := range resp {
		if cnt.Name == ME {
			continue
		}
		user, _ := db.Users.GetUserById(cnt.ID)
		if user != nil {
			if user.ShowedName != "" {
				resp[i].Name = user.ShowedName
			} else {
				resp[i].Name = user.UserName
			}
			resp[i].Phone = user.Phone
			result = append(result, resp[i])
		}
	}
	sort.Sort(usrs.ByContactsLastMessageTime(result))
	return result, nil
}