Example #1
0
File: topics.go Project: ovh/tat
// List returns the list of topics that can be viewed by user
func (t *TopicsController) List(ctx *gin.Context) {
	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
	}
	criteria := t.buildCriteria(ctx, user)
	count, topics, err := topicDB.ListTopics(criteria, user, false, false, false)
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{"error": "Error while fetching topics."})
		return
	}

	out := &tat.TopicsJSON{Topics: topics, Count: count}

	if criteria.GetNbMsgUnread == "true" {
		c := &tat.PresenceCriteria{
			Username: user.Username,
		}
		count, presences, err := presenceDB.ListPresencesAllFields(c)
		if err != nil {
			ctx.AbortWithError(http.StatusInternalServerError, err)
			return
		}

		unread := make(map[string]int)
		knownPresence := false
		for _, topic := range topics {
			if tat.ArrayContains(user.OffNotificationsTopics, topic.Topic) {
				continue
			}
			knownPresence = false
			for _, presence := range presences {
				if topic.Topic != presence.Topic {
					continue
				}
				knownPresence = true
				if topic.DateLastMessage > presence.DatePresence {
					unread[presence.Topic] = 1
				}
				break
			}
			if !knownPresence {
				unread[topic.Topic] = -1
			}
		}
		out.TopicsMsgUnread = unread
		out.CountTopicsMsgUnread = count
	}
	ctx.JSON(http.StatusOK, out)
}
Example #2
0
File: groups.go Project: ovh/tat
// Delete deletes requested group
// only for Tat admin
func (g *GroupsController) Delete(ctx *gin.Context) {
	groupName, err := GetParam(ctx, "group")
	if err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{"error": "Invalid Group in query"})
		return
	}

	groupToDelete, err := groupDB.FindByName(groupName)
	if err != nil {
		ctx.JSON(http.StatusNotFound, gin.H{"error": "Invalid Group"})
		return
	}

	user, err := PreCheckUser(ctx)
	if err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{"error": "Error while fetching user"})
		return
	}

	c := tat.TopicCriteria{}
	c.Skip = 0
	c.Limit = 10
	c.Group = groupToDelete.Name

	count, topics, err := topicDB.ListTopics(&c, &user, false, false, false)
	if err != nil {
		log.Errorf("Error while getting topics associated to group %s:%s", groupToDelete.Name, err.Error())
		ctx.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Errorf("Error while getting topics associated to group")})
		return
	}

	if len(topics) > 0 {
		e := fmt.Sprintf("Group %s associated to %d topic, you can't delete it", groupToDelete.Name, count)
		log.Errorf(e)
		ctx.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Errorf(e)})
		return
	}

	if err = groupDB.Delete(groupToDelete, &user); err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Error while deleting Group: %s", err.Error())})
		return
	}
	ctx.JSON(http.StatusOK, "")
}
Example #3
0
File: stats.go Project: 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,
	})
}
Example #4
0
File: user.go Project: ovh/tat
// DisableNotificationsAllTopics add all topics to user list offNotificationsTopics, except /Private/*
func DisableNotificationsAllTopics(user *tat.User) error {
	criteria := &tat.TopicCriteria{
		Skip:  0,
		Limit: 9000000,
	}
	_, topics, err := topic.ListTopics(criteria, user, false, false, false)
	if err != nil {
		return err
	}

	topicsToSet := []string{}
	for _, topic := range topics {
		if !strings.HasPrefix(topic.Topic, "/Private") {
			topicsToSet = append(topicsToSet, topic.Topic)
		}
	}

	cache.CleanUsernames(user.Username)
	return store.Tat().CUsers.Update(
		bson.M{"_id": user.ID},
		bson.M{"$set": bson.M{"offNotificationsTopics": topicsToSet}})
}