Example #1
0
File: topic.go Project: ovh/tat
// UpdateTopicLabels updates labels on topic
func UpdateTopicLabels(topic *tat.Topic, labels []tat.Label) {
	if !topic.IsAutoComputeLabels || len(labels) == 0 {
		return
	}

	update := false
	newLabels := topic.Labels
	for _, label := range labels {
		find := false
		for _, tlabel := range topic.Labels {
			if label.Text == tlabel.Text {
				find = true
				continue
			}
		}
		if !find {
			newLabels = append(newLabels, label)
			update = true
		}
	}

	if update {
		err := store.Tat().CTopics.Update(
			bson.M{"_id": topic.ID},
			bson.M{"$set": bson.M{"labels": newLabels}})

		if err != nil {
			log.Errorf("UpdateTopicLabels> Error while updating labels on topic")
		} else {
			log.Debugf("UpdateTopicLabels> Topic %s ", topic.Topic)
		}
		cache.CleanTopicByName(topic.Topic)
	}
}
Example #2
0
File: topic.go Project: ovh/tat
// UpdateTopicTags updates tags on topic
func UpdateTopicTags(topic *tat.Topic, tags []string) {
	if !topic.IsAutoComputeTags || len(tags) == 0 {
		return
	}

	update := false
	newTags := topic.Tags
	for _, tag := range tags {
		if !tat.ArrayContains(topic.Tags, tag) {
			update = true
			newTags = append(newTags, tag)
		}
	}

	if update {
		err := store.Tat().CTopics.Update(
			bson.M{"_id": topic.ID},
			bson.M{"$set": bson.M{"tags": newTags}})

		if err != nil {
			log.Errorf("UpdateTopicTags> Error while updating tags on topic")
		} else {
			log.Debugf("UpdateTopicTags> Topic %s ", topic.Topic)
		}
		cache.CleanTopicByName(topic.Topic)
	}
}
Example #3
0
File: topic.go Project: ovh/tat
// RemoveFilter add a user filter to the topic
func RemoveFilter(topic *tat.Topic, filter *tat.Filter) error {
	err := store.Tat().CTopics.Update(
		bson.M{"_id": topic.ID},
		bson.M{"$pull": bson.M{"filters": bson.M{"_id": filter.ID}}},
	)
	cache.CleanTopicByName(topic.Topic)
	return err
}
Example #4
0
File: topic.go Project: ovh/tat
// TruncateLabels clears "cached" labels on a topic
func TruncateLabels(topic *tat.Topic) error {
	err := store.Tat().CTopics.Update(
		bson.M{"_id": topic.ID},
		bson.M{"$unset": bson.M{"labels": ""}})

	cache.CleanTopicByName(topic.Topic)
	return err
}
Example #5
0
File: topic.go Project: ovh/tat
// UpdateFilter add a user filter to the topic
func UpdateFilter(topic *tat.Topic, filter *tat.Filter) error {
	err := store.Tat().CTopics.Update(
		bson.M{"_id": topic.ID, "filters._id": filter.ID},
		bson.M{"$set": bson.M{"filters.$": filter}},
	)
	cache.CleanTopicByName(topic.Topic)
	return err
}
Example #6
0
File: topic.go Project: ovh/tat
// ComputeLabels computes "cached" labels on a topic
// initialize labels, one entry per label (unicity with text & color)
func ComputeLabels(topic *tat.Topic) (int, error) {
	labels, err := ListLabels(*topic)
	if err != nil {
		return 0, err
	}

	err = store.Tat().CTopics.Update(
		bson.M{"_id": topic.ID},
		bson.M{"$set": bson.M{"labels": labels}})

	cache.CleanTopicByName(topic.Topic)
	return len(labels), err
}
Example #7
0
File: topic.go Project: ovh/tat
// ComputeTags computes "cached" tags in topic
// initialize tags, one entry per tag (unique)
func ComputeTags(topic *tat.Topic) (int, error) {
	tags, err := ListTags(*topic)
	if err != nil {
		return 0, err
	}

	err = store.Tat().CTopics.Update(
		bson.M{"_id": topic.ID},
		bson.M{"$set": bson.M{"tags": tags}})

	cache.CleanTopicByName(topic.Topic)
	return len(tags), err
}
Example #8
0
File: topic.go Project: ovh/tat
func setABoolParam(topic *tat.Topic, key string, value bool) error {
	if key != "isAutoComputeTags" && key != "isAutoComputeLabels" {
		return fmt.Errorf("set param %s is an invalid action", key)
	}

	err := store.Tat().CTopics.Update(
		bson.M{"_id": topic.ID},
		bson.M{"$set": bson.M{key: value}},
	)
	if err != nil {
		log.Errorf("Error while update topic %s, param %s with new value %t", topic.Topic, key, value)
	}
	cache.CleanTopicByName(topic.Topic)
	return nil
}
Example #9
0
File: topic.go Project: ovh/tat
// AddFilter add a user filter to the topic
func AddFilter(topic *tat.Topic, user *tat.User, filter *tat.Filter) error {

	filter.ID = bson.NewObjectId().Hex()
	filter.UserID = user.ID
	filter.Username = user.Username

	for _, h := range filter.Hooks {
		h.ID = bson.NewObjectId().Hex()
	}

	err := store.Tat().CTopics.Update(
		bson.M{"_id": topic.ID},
		bson.M{"$addToSet": bson.M{"filters": filter}},
	)
	cache.CleanTopicByName(topic.Topic)
	return err
}
Example #10
0
File: topic.go Project: ovh/tat
// SetParam update param maxLength, canForceDate, canUpdateMsg, canDeleteMsg,
// canUpdateAllMsg, canDeleteAllMsg, adminCanUpdateAllMsg, adminCanDeleteAllMsg, parameters on topic
func SetParam(topic *tat.Topic, username string, recursive bool, maxLength int,
	canForceDate, canUpdateMsg, canDeleteMsg, canUpdateAllMsg, canDeleteAllMsg, adminCanUpdateAllMsg, adminCanDeleteAllMsg,
	isAutoComputeTags, isAutoComputeLabels bool, parameters []tat.TopicParameter) error {

	var selector bson.M

	if recursive {
		selector = bson.M{"topic": bson.RegEx{Pattern: "^" + topic.Topic + ".*$"}}
	} else {
		selector = bson.M{"_id": topic.ID}
	}

	if maxLength <= 0 {
		maxLength = tat.DefaultMessageMaxSize
	}

	update := bson.M{
		"maxlength":            maxLength,
		"canForceDate":         canForceDate,
		"canUpdateMsg":         canUpdateMsg,
		"canDeleteMsg":         canDeleteMsg,
		"canUpdateAllMsg":      canUpdateAllMsg,
		"canDeleteAllMsg":      canDeleteAllMsg,
		"adminCanUpdateAllMsg": adminCanUpdateAllMsg,
		"adminCanDeleteAllMsg": adminCanDeleteAllMsg,
		"isAutoComputeTags":    isAutoComputeTags,
		"isAutoComputeLabels":  isAutoComputeLabels,
	}

	if parameters != nil {
		update["parameters"] = parameters
	}
	_, err := store.Tat().CTopics.UpdateAll(selector, bson.M{"$set": update})

	if err != nil {
		log.Errorf("Error while updateAll parameters : %s", err.Error())
		return err
	}
	h := fmt.Sprintf("update param to maxlength:%d, canForceDate:%t, canUpdateMsg:%t, canDeleteMsg:%t, canUpdateAllMsg:%t, canDeleteAllMsg:%t, adminCanDeleteAllMsg:%t isAutoComputeTags:%t, isAutoComputeLabels:%t",
		maxLength, canForceDate, canUpdateMsg, canDeleteMsg, canUpdateAllMsg, canDeleteAllMsg, adminCanDeleteAllMsg, isAutoComputeTags, isAutoComputeLabels)

	err = addToHistory(topic, selector, username, h)
	cache.CleanTopicByName(topic.Topic)
	return err
}
Example #11
0
File: topic.go Project: ovh/tat
// RemoveRwGroup removes a read write group from topic
func RemoveRwGroup(topic *tat.Topic, admin string, groupname string, recursive bool) error {
	err := actionOnSet(topic, "$pull", "rwGroups", groupname, admin, recursive, "remove from rw")
	cache.CleanTopicByName(topic.Topic)
	return err
}
Example #12
0
File: topic.go Project: ovh/tat
// AddAdminGroup add a admin group to topic
func AddAdminGroup(topic *tat.Topic, admin string, groupname string, recursive bool) error {
	err := actionOnSet(topic, "$addToSet", "adminGroups", groupname, admin, recursive, "add to admin")
	cache.CleanTopicByName(topic.Topic)
	return err
}