// AddFavoriteTag Add a favorite tag to user func AddFavoriteTag(user *tat.User, tag string) error { if containsFavoriteTag(user, tag) { return fmt.Errorf("AddFavoriteTag not possible, %s is already a favorite tag", tag) } cache.CleanUsernames(user.Username) return store.Tat().CUsers.Update( bson.M{"_id": user.ID}, bson.M{"$push": bson.M{"favoritesTags": tag}}) }
// DisableNotificationsTopic add topic to user list offNotificationsTopics func DisableNotificationsTopic(user *tat.User, topic string) error { if containsOffNotificationsTopic(user, topic) { return fmt.Errorf("DisableNotificationsTopic not possible, notifications are already off on topic %s", topic) } cache.CleanUsernames(user.Username) return store.Tat().CUsers.Update( bson.M{"_id": user.ID}, bson.M{"$push": bson.M{"offNotificationsTopics": topic}}) }
// RemoveContact removes a contact from user func RemoveContact(user *tat.User, contactUsername string) error { l, err := getContact(user, contactUsername) if err != nil { return fmt.Errorf("Remove Contact is not possible, %s is not a contact of this user", contactUsername) } cache.CleanUsernames(user.Username) return store.Tat().CUsers.Update( bson.M{"_id": user.ID}, bson.M{"$pull": bson.M{"contacts": l}}) }
// AddContact add a contact to user func AddContact(user *tat.User, contactUsername string, contactFullname string) error { if containsContact(user, contactUsername) { return fmt.Errorf("AddContact not possible, %s is already a contact of this user", contactUsername) } var newContact = &tat.Contact{Username: contactUsername, Fullname: contactFullname} cache.CleanUsernames(user.Username) return store.Tat().CUsers.Update( bson.M{"_id": user.ID}, bson.M{"$push": bson.M{"contacts": newContact}}) }
// RemoveFavoriteTag remove a favorite tag from user func RemoveFavoriteTag(user *tat.User, tag string) error { t, err := getFavoriteTag(user, tag) if err != nil { return fmt.Errorf("Remove favorite tag is not possible, %s is not a favorite of this user", tag) } cache.CleanUsernames(user.Username) return store.Tat().CUsers.Update( bson.M{"_id": user.ID}, bson.M{"$pull": bson.M{"favoritesTags": t}}) }
// AddFavoriteTopic add a favorite topic to user func AddFavoriteTopic(user *tat.User, topic string) error { if containsFavoriteTopic(user, topic) { return fmt.Errorf("AddFavoriteTopic not possible, %s is already a favorite topic", topic) } err := store.Tat().CUsers.Update( bson.M{"_id": user.ID}, bson.M{"$push": bson.M{"favoritesTopics": topic}}) if err != nil { log.Errorf("Error while add favorite topic to user %s: %s", user.Username, err) return err } cache.CleanUsernames(user.Username) return nil }
// EnableNotificationsTopic remove topic from user list offNotificationsTopics func EnableNotificationsTopic(user *tat.User, topic string) error { topicName, err := tat.CheckAndFixNameTopic(topic) if err != nil { return err } t, err := getOffNotificationsTopic(user, topicName) if err != nil { return fmt.Errorf("Enable notifications on topic %s is not possible, notifications are already enabled", topicName) } cache.CleanUsernames(user.Username) return store.Tat().CUsers.Update( bson.M{"_id": user.ID}, bson.M{"$pull": bson.M{"offNotificationsTopics": t}}) }
// DisableNotificationsAllTopics add all topics to user list offNotificationsTopics, except /Private/* func DisableNotificationsAllTopics(user *tat.User) error { criteria := &tat.TopicCriteria{ Skip: 0, Limit: 9000000, } _, topics, err := topic.ListTopics(criteria, user, false, false, false) if err != nil { return err } topicsToSet := []string{} for _, topic := range topics { if !strings.HasPrefix(topic.Topic, "/Private") { topicsToSet = append(topicsToSet, topic.Topic) } } cache.CleanUsernames(user.Username) return store.Tat().CUsers.Update( bson.M{"_id": user.ID}, bson.M{"$set": bson.M{"offNotificationsTopics": topicsToSet}}) }
// RemoveFavoriteTopic removes a favorite topic from user func RemoveFavoriteTopic(user *tat.User, topic string) error { topicName, err := tat.CheckAndFixNameTopic(topic) if err != nil { return err } t, err := getFavoriteTopic(user, topicName) if err != nil { return fmt.Errorf("Remove favorite topic is not possible, %s is not a favorite of this user", topicName) } err = store.Tat().CUsers.Update( bson.M{"_id": user.ID}, bson.M{"$pull": bson.M{"favoritesTopics": t}}) if err != nil { log.Errorf("Error while remove favorite topic from user %s: %s", user.Username, err) return err } cache.CleanUsernames(user.Username) return nil }
// Update changes fullname and email of user func Update(user *tat.User, newFullname, newEmail string) error { userCheck := tat.User{} found, err := FindByEmail(&userCheck, newEmail) if err != nil { return err } if user.Email != newEmail && found { return fmt.Errorf("Email %s already exists", newEmail) } found2, err2 := FindByFullname(&userCheck, newFullname) if err2 != nil { return err2 } if user.Fullname != newFullname && found2 { return fmt.Errorf("Fullname %s already exists", newFullname) } cache.CleanUsernames(user.Username) return store.Tat().CUsers.Update( bson.M{"_id": user.ID}, bson.M{"$set": bson.M{"fullname": newFullname, "email": newEmail}}) }
// Rename changes username of one user func Rename(user *tat.User, newUsername string) error { var userCheck = tat.User{} found, errCheck := FindByUsername(&userCheck, newUsername) if errCheck != nil { return fmt.Errorf("Rename> Error with DB Backend:%s", errCheck) } else if found { return fmt.Errorf("Rename> Username %s already exists", newUsername) } err := store.Tat().CUsers.Update( bson.M{"_id": user.ID}, bson.M{"$set": bson.M{"username": newUsername}}) if err != nil { return err } topic.ChangeUsernameOnTopics(user.Username, newUsername) group.ChangeUsernameOnGroups(user.Username, newUsername) presence.ChangeAuthorUsernameOnPresences(user.Username, newUsername) cache.CleanUsernames(user.Username) return nil }
// EnableNotificationsAllTopics removes all topics from user list offNotificationsTopics func EnableNotificationsAllTopics(user *tat.User) error { cache.CleanUsernames(user.Username) return store.Tat().CUsers.Update( bson.M{"_id": user.ID}, bson.M{"$set": bson.M{"offNotificationsTopics": []bson.M{}}}) }