Example #1
1
File: hook.go Project: ovh/tat
func runHook(h *tat.HookJSON, f *tat.Filter, topic tat.Topic) {
	log.Debugf("runHook enter f:%+v", f)
	if !h.Hook.Enabled {
		log.Debugf("Hook not enabled on topic %s h:%+v", topic.Topic, h)
		return
	}

	if f != nil {
		if h.Hook.Errors > viper.GetInt("hooks_max_errors") {
			log.Warnf("Max errors reached on hook %s for topic %s", h.Hook.ID, topic.Topic)
			for _, fh := range f.Hooks {
				fh.Enabled = false
			}
			topicDB.UpdateFilter(&topic, f)
			return
		}
		h.Username = f.Username
	}

	if h.Hook.Action != "all" && h.Hook.Action != h.HookMessage.Action {
		log.Debugf("Skip action:%s", h.Hook.Action, h)
		return
	}

	if strings.HasPrefix(h.Hook.Type, tat.HookTypeWebHook) {
		if err := sendWebHook(h, h.Hook.Destination, topic, "", ""); err != nil {
			log.Errorf("sendHook webhook err:%s", err)
		}
	} else if strings.HasPrefix(h.Hook.Type, tat.HookTypeKafka) {
		log.Infof("sendOnKafkaTopic")
		if err := sendOnKafkaTopic(h, h.Hook.Destination, topic); err != nil {
			log.Errorf("sendHook kafka err:%s", err)
		}
	} else if h.Hook.Type == tat.HookTypeXMPP || h.Hook.Type == tat.HookTypeXMPPOut {
		if err := sendXMPP(h, h.Hook.Destination, topic); err != nil {
			log.Errorf("sendHook XMPP err:%s", err)
		}
	}
}
Example #2
0
File: topics.go Project: 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})
}