// Create creates a new topic func (*TopicsController) Create(ctx *gin.Context) { var topicIn tat.TopicCreateJSON ctx.Bind(&topicIn) var user = tat.User{} found, err := userDB.FindByUsername(&user, getCtxUsername(ctx)) if !found { ctx.JSON(http.StatusUnauthorized, gin.H{"error": "User unknown"}) return } else if err != nil { ctx.JSON(http.StatusInternalServerError, gin.H{"error": "Error while fetching user."}) return } var topic tat.Topic topic.Topic = topicIn.Topic topic.Description = topicIn.Description err = topicDB.Insert(&topic, &user) if err != nil { log.Errorf("Error while InsertTopic %s", err) ctx.JSON(tat.Error(err)) return } ctx.JSON(http.StatusCreated, topic) }
// AddRoGroup add a readonly group on selected topic func (t *TopicsController) AddRoGroup(ctx *gin.Context) { var paramJSON tat.ParamTopicGroupJSON ctx.Bind(¶mJSON) topic, e := t.preCheckGroup(ctx, ¶mJSON) if e != nil { ctx.JSON(tat.Error(e)) return } if err := topicDB.AddRoGroup(topic, getCtxUsername(ctx), paramJSON.Groupname, paramJSON.Recursive); err != nil { log.Errorf("Error while adding admin read only group: %s", err) ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } ctx.JSON(http.StatusCreated, "") }
// TruncateLabels clear labels on one topic func (t *TopicsController) TruncateLabels(ctx *gin.Context) { var paramJSON tat.TopicNameJSON ctx.Bind(¶mJSON) topic, e := t.preCheckUserAdminOnTopic(ctx, paramJSON.Topic) if e != nil { ctx.JSON(tat.Error(e)) return } if err := topicDB.TruncateLabels(topic); err != nil { log.Errorf("Error while clear labels on topic %s: %s", topic.Topic, err) ctx.JSON(http.StatusInternalServerError, gin.H{"error": "error while clear labels on topic " + topic.Topic}) return } ctx.JSON(http.StatusOK, gin.H{"info": fmt.Sprintf("%d labels cleared", len(topic.Labels))}) }
// RemoveRwGroup removes a read write group on selected topic func (t *TopicsController) RemoveRwGroup(ctx *gin.Context) { var paramJSON tat.ParamTopicGroupJSON ctx.Bind(¶mJSON) topic, e := t.preCheckGroup(ctx, ¶mJSON) if e != nil { ctx.JSON(tat.Error(e)) return } err := topicDB.RemoveRwGroup(topic, getCtxUsername(ctx), paramJSON.Groupname, paramJSON.Recursive) if err != nil { log.Errorf("Error while removing read write group: %s", err) ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } ctx.JSON(http.StatusOK, "") }
// ComputeTags computes tags on one topic func (t *TopicsController) ComputeTags(ctx *gin.Context) { var paramJSON tat.TopicNameJSON ctx.Bind(¶mJSON) topic, e := t.preCheckUserAdminOnTopic(ctx, paramJSON.Topic) if e != nil { ctx.JSON(tat.Error(e)) return } nbComputed, err := topicDB.ComputeTags(topic) if err != nil { log.Errorf("Error while compute tags on topic %s: %s", topic.Topic, err) ctx.JSON(http.StatusInternalServerError, gin.H{"error": "error while compute tags on topic " + topic.Topic}) return } ctx.JSON(http.StatusCreated, gin.H{"info": fmt.Sprintf("%d tags computed", nbComputed)}) }
// AddParameter add a parameter on selected topic func (t *TopicsController) AddParameter(ctx *gin.Context) { var topicParameterBind tat.TopicParameterJSON ctx.Bind(&topicParameterBind) topic, e := t.preCheckUserAdminOnTopic(ctx, topicParameterBind.Topic) if e != nil { ctx.JSON(tat.Error(e)) return } err := topicDB.AddParameter(topic, getCtxUsername(ctx), topicParameterBind.Key, topicParameterBind.Value, topicParameterBind.Recursive) if err != nil { log.Errorf("Error while adding parameter: %s", err) ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } ctx.JSON(http.StatusCreated, "") }
// Truncate deletes all messages in a topic only if user is Tat admin, or admin on topic func (t *TopicsController) Truncate(ctx *gin.Context) { var paramJSON tat.TopicNameJSON ctx.Bind(¶mJSON) topic, e := t.preCheckUserAdminOnTopic(ctx, paramJSON.Topic) if e != nil { ctx.JSON(tat.Error(e)) return } nbRemoved, err := topicDB.Truncate(topic) if err != nil { log.Errorf("Error while truncate topic %s", err) ctx.JSON(http.StatusInternalServerError, gin.H{"error": "error while truncate topic " + topic.Topic}) return } // 201 returns ctx.JSON(http.StatusCreated, gin.H{"info": fmt.Sprintf("%d messages removed", nbRemoved)}) }
// SetParam update Topic Parameters : MaxLength, CanForeceDate, CanUpdateMsg, CanDeleteMsg, CanUpdateAllMsg, CanDeleteAllMsg, AdminCanDeleteAllMsg // admin only, except on Private topic func (t *TopicsController) SetParam(ctx *gin.Context) { var paramsBind paramsJSON ctx.Bind(¶msBind) topic := &tat.Topic{} var errFind, err error if strings.HasPrefix(paramsBind.Topic, "/Private/"+getCtxUsername(ctx)) { topic, errFind = topicDB.FindByTopic(paramsBind.Topic, false, false, false, nil) if errFind != nil { ctx.JSON(http.StatusInternalServerError, gin.H{"error": "Error while fetching topic /Private/" + getCtxUsername(ctx)}) return } } else { topic, err = t.preCheckUserAdminOnTopic(ctx, paramsBind.Topic) if err != nil { ctx.JSON(tat.Error(err)) return } } for _, p := range paramsBind.Parameters { if strings.HasPrefix(p.Key, tat.HookTypeKafka) && !isTatAdmin(ctx) { ctx.JSON(http.StatusInternalServerError, gin.H{"error": "Only Tat Admin can use tathook-kafka"}) return } } err = topicDB.SetParam(topic, getCtxUsername(ctx), paramsBind.Recursive, paramsBind.MaxLength, paramsBind.CanForceDate, paramsBind.CanUpdateMsg, paramsBind.CanDeleteMsg, paramsBind.CanUpdateAllMsg, paramsBind.CanDeleteAllMsg, paramsBind.AdminCanUpdateAllMsg, paramsBind.AdminCanDeleteAllMsg, paramsBind.IsAutoComputeTags, paramsBind.IsAutoComputeLabels, paramsBind.Parameters) // add tat2xmpp_username RO or RW on this topic if a key is xmpp for _, p := range paramsBind.Parameters { if strings.HasPrefix(p.Key, tat.HookTypeXMPPOut) { found := false for _, u := range topic.ROUsers { if u == viper.GetString("tat2xmpp_username") { found = true } } for _, u := range topic.RWUsers { if u == viper.GetString("tat2xmpp_username") { found = true } } if !found { if errf := topicDB.AddRoUser(topic, getCtxUsername(ctx), viper.GetString("tat2xmpp_username"), false); errf != nil { log.Errorf("Error while adding read only user tat2xmpp_username: %s", errf) ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } } } else if strings.HasPrefix(p.Key, tat.HookTypeXMPP) { found := false for _, u := range topic.RWUsers { if u == viper.GetString("tat2xmpp_username") { found = true } } if !found { if errf := topicDB.AddRwUser(topic, getCtxUsername(ctx), viper.GetString("tat2xmpp_username"), false); errf != nil { log.Errorf("Error while adding read write user tat2xmpp_username: %s", errf) ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } } } } if err != nil { log.Errorf("Error while setting parameters: %s", err) ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } ctx.JSON(http.StatusCreated, gin.H{"info": fmt.Sprintf("Topic %s updated", topic.Topic)}) }