コード例 #1
0
ファイル: presences.go プロジェクト: vmalguy/tat
func (m *PresencesController) create(ctx *gin.Context) {

	presenceIn, topic, e := m.preCheckTopic(ctx)
	if e != nil {
		return
	}

	user, e := m.preCheckUser(ctx)
	if e != nil {
		return
	}

	isReadAccess := topic.IsUserReadAccess(user)
	if !isReadAccess {
		e := errors.New("No Read Access to topic " + presenceIn.Topic + " for user " + user.Username)
		ctx.AbortWithError(http.StatusForbidden, e)
		ctx.JSON(http.StatusForbidden, e)
		return
	}

	var presence = models.Presence{}
	err := presence.Upsert(user, topic, presenceIn.Status)
	if err != nil {
		log.Errorf("Error while InsertPresence %s", err)
		ctx.AbortWithError(http.StatusInternalServerError, err)
		ctx.JSON(http.StatusInternalServerError, err)
		return
	}

	go models.WSPresence(&models.WSPresenceJSON{Action: "create", Presence: presence})

	//out := &presenceJSONOut{Presence: presence}
	//ctx.JSON(http.StatusCreated, nil)
}
コード例 #2
0
ファイル: messages.go プロジェクト: vmalguy/tat
// List messages on one topic, with given criterias
func (m *MessagesController) List(ctx *gin.Context) {
	var criteria = m.buildCriteria(ctx)
	presenceArg := ctx.Query("presence")
	topicIn, err := GetParam(ctx, "topic")
	if err != nil {
		return
	}
	criteria.Topic = topicIn

	// add / if search on topic
	// as topic is in path, it can't start with a /
	if criteria.Topic != "" && string(criteria.Topic[0]) != "/" {
		criteria.Topic = "/" + criteria.Topic
	}

	var topic = models.Topic{}
	err = topic.FindByTopic(criteria.Topic, true)
	if err != nil {
		topicCriteria := ""
		_, topicCriteria, err = m.checkDMTopic(ctx, criteria.Topic)
		if err != nil {
			ctx.JSON(http.StatusBadRequest, gin.H{"error": "topic " + criteria.Topic + " does not exist"})
			return
		}
		// hack to get new created DM Topic
		err := topic.FindByTopic(criteria.Topic, true)
		if err != nil {
			ctx.JSON(http.StatusBadRequest, gin.H{"error": "topic " + criteria.Topic + " does not exist (2)"})
			return
		}
		criteria.Topic = topicCriteria
	}

	out := &messagesJSON{}

	var user models.User
	var e error
	if utils.GetCtxUsername(ctx) != "" {
		user, e = PreCheckUser(ctx)
		if e != nil {
			return
		}
		isReadAccess := topic.IsUserReadAccess(user)
		if !isReadAccess {
			ctx.JSON(http.StatusForbidden, gin.H{"error": "No Read Access to this topic"})
			return
		}
		out.IsTopicRw = topic.IsUserRW(&user)
	} else if !topic.IsROPublic {
		ctx.JSON(http.StatusForbidden, gin.H{"error": "No Public Read Access Public to this topic"})
		return
	} else if topic.IsROPublic && strings.HasPrefix(topic.Topic, "/Private") {
		ctx.JSON(http.StatusForbidden, gin.H{"error": "No Public Read Access to this topic"})
		return
	}

	// send presence
	if presenceArg != "" && !user.IsSystem {
		go func() {
			var presence = models.Presence{}
			err := presence.Upsert(user, topic, presenceArg)
			if err != nil {
				log.Errorf("Error while InsertPresence %s", err)
			}
			go models.WSPresence(&models.WSPresenceJSON{Action: "create", Presence: presence})
		}()

	}

	messages, err := models.ListMessages(criteria)
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	out.Messages = messages
	ctx.JSON(http.StatusOK, out)
}