Пример #1
0
Файл: topics.go Проект: ovh/tat
// Delete deletes requested topic only if user is Tat admin, or admin on topic
func (t *TopicsController) Delete(ctx *gin.Context) {
	topicRequest, err := GetParam(ctx, "topic")
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{"error": "Invalid Topic"})
		return
	}

	var user = tat.User{}
	found, err := userDB.FindByUsername(&user, getCtxUsername(ctx))
	if !found {
		ctx.JSON(http.StatusInternalServerError, gin.H{"error": "User unknown"})
		return
	} else if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{"error": "Error while fetching user"})
		return
	}

	paramJSON := tat.ParamTopicUserJSON{
		Topic:     topicRequest,
		Username:  user.Username,
		Recursive: false,
	}

	topic, e := t.preCheckUser(ctx, &paramJSON)
	if e != nil {
		return
	}
	// If user delete a Topic under /Private/username, no check or RW to delete
	if !strings.HasPrefix(topic.Topic, "/Private/"+user.Username) {
		// check if user is Tat admin or admin on this topic
		hasRW := topicDB.IsUserAdmin(topic, &user)
		if !hasRW {
			ctx.JSON(http.StatusForbidden, gin.H{"error": fmt.Errorf("No RW access to topic %s (to delete it)", topic.Topic)})
			return
		}
	}

	c := &tat.MessageCriteria{Topic: topic.Topic, OnlyCount: "true"}
	count, err := messageDB.CountMessages(c, user.Username, *topic)
	if err != nil {
		log.Errorf("Error while list Messages in Delete %s", err)
		ctx.JSON(http.StatusInternalServerError, gin.H{"error": "Error while list Messages in Delete topic"})
		return
	}

	if count > 0 {
		ctx.JSON(http.StatusInternalServerError, gin.H{"error": "Could not delete this topic, this topic have messages"})
		return
	}

	if err = topicDB.Delete(topic, &user); err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}
	ctx.JSON(http.StatusOK, gin.H{"info": fmt.Sprintf("Topic %s is deleted", topic.Topic)})
}
Пример #2
0
Файл: stats.go Проект: ovh/tat
// DistributionTopics returns total number of messages
func (*StatsController) DistributionTopics(ctx *gin.Context) {
	c := &tat.TopicCriteria{}
	skip, e := strconv.Atoi(ctx.DefaultQuery("skip", "0"))
	if e != nil {
		skip = 0
	}
	c.Skip = skip
	limit, e2 := strconv.Atoi(ctx.DefaultQuery("limit", "20"))
	if e2 != nil {
		limit = 20
	}
	c.Limit = limit

	count, topics, err := topic.ListTopics(c, nil, true, false, false)
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Error while listing topics %s", err)})
		return
	}

	info := ""
	t := []tat.TopicDistributionJSON{}
	for _, topic := range topics {
		countMsg, err := message.CountMessages(&tat.MessageCriteria{Topic: topic.Topic}, "internal", topic)
		if err != nil {
			info += fmt.Sprintf("Error on topic %s: %s", topic.Topic, err)
		}
		t = append(t, tat.TopicDistributionJSON{
			ID:         topic.ID,
			Topic:      topic.Topic,
			Count:      countMsg,
			Dedicated:  topic.Collection != "",
			Collection: topic.Collection,
		})
	}

	ctx.JSON(http.StatusOK, gin.H{
		"total":  count,
		"info":   info,
		"topics": t,
	})
}
Пример #3
0
// List messages on one topic, with given criteria
func (m *MessagesController) List(ctx *gin.Context) {
	out, user, topic, criteria, httpCode, err := m.innerList(ctx)

	if err != nil {
		ctx.JSON(httpCode, gin.H{"error": err.Error()})
		return
	}

	if criteria.OnlyCount == tat.True {
		count, e := messageDB.CountMessages(criteria, user.Username, topic)
		if e != nil {
			ctx.JSON(http.StatusInternalServerError, gin.H{"error": e.Error()})
			return
		}
		ctx.JSON(http.StatusOK, &tat.MessagesCountJSON{Count: count})
		return
	}

	// send presence
	presenceArg := ctx.Query("presence")
	if presenceArg != "" && !user.IsSystem {
		go func() {
			var presence = tat.Presence{}
			if e := presenceDB.Upsert(&presence, user, topic, presenceArg); e != nil {
				log.Errorf("Error while InsertPresence %s", e)
			}
		}()
	}

	messages, err := messageDB.ListMessages(criteria, user.Username, topic)
	if err != nil {
		ctx.JSON(http.StatusForbidden, gin.H{"error": err.Error()})
	}
	out.Messages = messages
	ctx.JSON(http.StatusOK, out)

}