Exemplo n.º 1
0
Arquivo: topic.go Projeto: 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
}
Exemplo n.º 2
0
Arquivo: topics.go Projeto: ovh/tat
// UpdateFilter add a filter on selected topic
func (t *TopicsController) UpdateFilter(ctx *gin.Context) {
	var topicFilterBind tat.Filter
	if err := ctx.Bind(&topicFilterBind); err != nil {
		log.Errorf("UpdateFilter err:%s", err)
		ctx.JSON(http.StatusBadRequest, gin.H{"error": "Invalid Post"})
		return
	}

	log.Warnf("topicFilterBind: %+v", topicFilterBind)
	if c, e := checkFilter(&topicFilterBind); e != nil {
		ctx.JSON(c, gin.H{"error": e.Error()})
		return
	}

	out, user, code, err := t.innerOneTopic(ctx, topicFilterBind.Topic)
	if err != nil {
		ctx.JSON(code, gin.H{"error": err.Error()})
		return
	}

	for _, f := range out.Topic.Filters {
		if f.ID == topicFilterBind.ID && f.UserID != user.ID {
			ctx.JSON(http.StatusForbidden, gin.H{"error": "You can not update a filter which not belong to you"})
			return
		}
	}

	topicFilterBind.UserID = user.ID // userID is not sent by UI
	topicFilterBind.Username = user.Username

	if err := topicDB.UpdateFilter(out.Topic, &topicFilterBind); err != nil {
		log.Errorf("Error while updating filter on topic %s err: %s", out.Topic.Topic, err)
		ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	ctx.JSON(http.StatusCreated, gin.H{"info": "filter updated on topic", "filter": topicFilterBind})
}