Beispiel #1
0
func (m *MessagesController) updateMessage(ctx *gin.Context, messageIn *messageJSON, message models.Message, user models.User, topic models.Topic) {
	info := ""
	if messageIn.Action == "update" {

		if !topic.CanUpdateMsg && !topic.CanUpdateAllMsg {
			ctx.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("You can't update a message on topic %s", topic.Topic)})
			return
		}

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

		message.Text = messageIn.Text
		err := message.Update(user, topic)
		if err != nil {
			log.Errorf("Error while update a message %s", err)
			ctx.AbortWithError(http.StatusInternalServerError, err)
			return
		}
		info = fmt.Sprintf("Message updated in %s", topic.Topic)
	} else {
		ctx.AbortWithError(http.StatusBadRequest, errors.New("Invalid action : "+messageIn.Action))
		return
	}
	go models.WSMessage(&models.WSMessageJSON{Action: messageIn.Action, Username: user.Username, Message: message})
	out := &messageJSONOut{Message: message, Info: info}
	ctx.JSON(http.StatusOK, out)
}