Exemplo n.º 1
0
// Delete a message, works only on /Private/username/... topics
func (m *MessagesController) Delete(ctx *gin.Context) {
	idMessageIn, err := GetParam(ctx, "idMessage")
	if err != nil {
		return
	}

	message := models.Message{}
	err = message.FindByID(idMessageIn)
	if err != nil {
		ctx.JSON(http.StatusNotFound, gin.H{"error": fmt.Sprintf("Message %s does not exist", idMessageIn)})
		return
	}

	user, e := PreCheckUser(ctx)
	if e != nil {
		return
	}

	topic := models.Topic{}
	err = topic.FindByTopic(message.Topics[0], true)
	if err != nil {
		ctx.JSON(http.StatusNotFound, gin.H{"error": fmt.Sprintf("Topic %s does not exist", message.Topics[0])})
		return
	}
	isRw := topic.IsUserRW(&user)
	if !isRw {
		ctx.JSON(http.StatusForbidden, gin.H{"error": fmt.Sprintf("No RW Access to topic %s", message.Topics[0])})
		return
	}

	if !strings.HasPrefix(message.Topics[0], "/Private/"+user.Username) && !topic.CanDeleteMsg && !topic.CanDeleteAllMsg {
		if !topic.CanDeleteMsg && !topic.CanDeleteAllMsg {
			ctx.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("You can't delete a message on this topic %s", topic.Topic)})
			return
		}
		ctx.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("Could not delete a message in a non private topic %s", message.Topics[0])})
		return
	}

	if !topic.CanDeleteAllMsg && message.Author.Username != user.Username {
		ctx.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("Could not delete a message from another user %s than you %s", message.Author.Username, user.Username)})
		return
	}

	for _, topicName := range message.Topics {
		// if msg is only in tasks topic, ok to delete it
		if strings.HasPrefix(topicName, "/Private/") && strings.HasSuffix(topicName, "/Tasks") && len(message.Topics) > 1 {
			ctx.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("Could not delete a message in a tasks topic")})
			return
		}
	}

	err = message.Delete()
	if err != nil {
		log.Errorf("Error while delete a message %s", err)
		ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	go models.WSMessage(&models.WSMessageJSON{Action: "delete", Username: user.Username, Message: message})
	ctx.JSON(http.StatusOK, gin.H{"info": fmt.Sprintf("Message deleted from %s", topic.Topic)})
}
Exemplo n.º 2
0
func (m *MessagesController) preCheckTopic(ctx *gin.Context) (messageJSON, models.Message, models.Topic, error) {
	var topic = models.Topic{}
	var message = models.Message{}
	var messageIn messageJSON
	ctx.Bind(&messageIn)

	topicIn, err := GetParam(ctx, "topic")
	if err != nil {
		return messageIn, message, topic, err
	}
	messageIn.Topic = topicIn

	if messageIn.IDReference == "" || messageIn.Action == "" {
		err := topic.FindByTopic(messageIn.Topic, true)
		if err != nil {
			topic, _, err = m.checkDMTopic(ctx, messageIn.Topic)
			if err != nil {
				e := errors.New("Topic " + messageIn.Topic + " does not exist")
				ctx.JSON(http.StatusNotFound, gin.H{"error": e.Error()})
				return messageIn, message, topic, e
			}
		}
	} else if messageIn.IDReference != "" {
		err := message.FindByID(messageIn.IDReference)
		if err != nil {
			e := errors.New("Message " + messageIn.IDReference + " does not exist")
			ctx.JSON(http.StatusNotFound, gin.H{"error": e.Error()})
			return messageIn, message, topic, e
		}

		topicName := ""
		if messageIn.Action == "update" {
			topicName = messageIn.Topic
		} else if messageIn.Action == "reply" || messageIn.Action == "unbookmark" ||
			messageIn.Action == "like" || messageIn.Action == "unlike" ||
			messageIn.Action == "label" || messageIn.Action == "unlabel" ||
			messageIn.Action == "tag" || messageIn.Action == "untag" {
			topicName = m.inverseIfDMTopic(ctx, message.Topics[0])
		} else if messageIn.Action == "task" || messageIn.Action == "untask" {
			topicName, err = m.getTopicNonPrivateTasks(ctx, message.Topics)
			if err != nil {
				ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
				return messageIn, message, topic, err
			}
		} else if messageIn.Action == "bookmark" {
			topicAction := m.getTopicNameFromAction(utils.GetCtxUsername(ctx), messageIn.Action)
			if !strings.HasPrefix(messageIn.Topic, topicAction) {
				e := fmt.Errorf("Invalid Topic name for action %s mTopic %s topicAction:%s ", messageIn.Action, messageIn.Topic, topicAction)
				ctx.JSON(http.StatusBadRequest, gin.H{"error": e.Error()})
				return messageIn, message, topic, e
			}
			topicName = messageIn.Topic
		} else {
			e := errors.New("Invalid Call. IDReference not empty with unknown action")
			ctx.JSON(http.StatusBadRequest, gin.H{"error": e.Error()})
			return messageIn, message, topic, e
		}
		err = topic.FindByTopic(topicName, true)
		if err != nil {
			e := errors.New("Topic " + topicName + " does not exist")
			ctx.JSON(http.StatusNotFound, gin.H{"error": e.Error()})
			return messageIn, message, topic, e
		}
	} else {
		e := errors.New("Topic and IDReference are null. Wrong request")
		ctx.JSON(http.StatusBadRequest, gin.H{"error": e.Error()})
		return messageIn, message, topic, e
	}
	return messageIn, message, topic, nil
}