Exemplo n.º 1
0
Arquivo: presence.go Projeto: ovh/tat
// Upsert insert of update a presence (user / topic)
func Upsert(presence *tat.Presence, user tat.User, topic tat.Topic, status string) error {
	presence.Status = status
	if err := checkAndFixStatus(presence); err != nil {
		return err
	}
	//	presence.ID = bson.NewObjectId().Hex()
	presence.Topic = topic.Topic
	var userPresence = tat.UserPresence{}
	userPresence.Username = user.Username
	userPresence.Fullname = user.Fullname
	presence.UserPresence = userPresence
	now := time.Now()
	presence.DatePresence = now.Unix()
	presence.DateTimePresence = time.Now()

	//selector := ]bson.M{}
	//selector = append(selector, bson.M{"userpresence.username": userPresence.Username})
	//selector = append(selector, bson.M{"topic": topic.Topic})
	selector := bson.M{"topic": topic.Topic, "userPresence.username": userPresence.Username}
	if _, err := store.Tat().CPresences.Upsert(selector, presence); err != nil {
		log.Errorf("Error while inserting new presence for %s err:%s", userPresence.Username, err)
	}
	return nil
}
Exemplo n.º 2
0
Arquivo: presence.go Projeto: ovh/tat
// truncate to 140 characters
// if len < 1, return error
func checkAndFixStatus(presence *tat.Presence) error {
	status := strings.TrimSpace(presence.Status)
	if len(status) < 1 {
		return fmt.Errorf("Invalid Status:%s", presence.Status)
	}

	validStatus := [...]string{"online", "offline", "busy"}
	find := false
	for _, s := range validStatus {
		if s == presence.Status {
			find = true
			break
		}
	}

	if !find {
		return fmt.Errorf("Invalid Status, should be online or offline or busy :%s", presence.Status)
	}
	presence.Status = status
	return nil
}