Exemplo n.º 1
0
Arquivo: messages.go Projeto: ovh/tat
func (m *MessagesController) messagesDeleteBulk(ctx *gin.Context, cascade, force bool) {
	out, user, topic, criteria, httpCode, err := m.innerList(ctx)
	if criteria.TreeView == "" {
		criteria.TreeView = tat.TreeViewOneTree
	}

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

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

	// check all before
	for _, msg := range out.Messages {
		errCheck := m.checkBeforeDelete(ctx, msg, user, force, topic)
		if errCheck != nil {
			// ctx writes in checkBeforeDelete
			return
		}
		if cascade {
			for _, r := range msg.Replies {
				errCheck := m.checkBeforeDelete(ctx, r, user, force, topic)
				if errCheck != nil {
					// ctx writes in checkBeforeDelete
					return
				}
			}
		} else if len(msg.Replies) > 0 {
			ctx.JSON(http.StatusBadRequest, gin.H{"error": "Could not delete a message, message have replies"})
			return
		}
	}

	nbDelete := 0
	for _, msg := range out.Messages {
		if err = messageDB.Delete(&msg, cascade, topic); err != nil {
			log.Errorf("Error while delete a message %s", err)
			ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
			return
		}
		nbDelete++
	}

	ctx.JSON(http.StatusOK, gin.H{"info": fmt.Sprintf("%d messages (cascade:%t) deleted from %s, limit criteria to %d messages root",
		nbDelete, cascade, topic.Topic, criteria.Limit)})
}
Exemplo n.º 2
0
Arquivo: messages.go Projeto: ovh/tat
func (m *MessagesController) messageDelete(ctx *gin.Context, cascade, force bool) {
	idMessageIn, err := GetParam(ctx, "idMessage")
	if err != nil {
		return
	}

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

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

	topic, errf := topicDB.FindByTopic(topicIn, true, false, false, &user)
	if errf != nil {
		log.Errorf("messageDelete> err:%s", errf)
		e := fmt.Sprintf("Topic %s does not exist", topicIn)
		ctx.JSON(http.StatusNotFound, gin.H{"error": e})
		return
	}

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

	err = m.checkBeforeDelete(ctx, message, user, force, *topic)
	if err != nil {
		// ctx writes in checkBeforeDelete
		return
	}

	c := &tat.MessageCriteria{
		InReplyOfID: message.ID,
		TreeView:    tat.TreeViewOneTree,
		Topic:       topic.Topic,
	}

	msgs, err := messageDB.ListMessages(c, "", *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"})
		return
	}

	if cascade {
		for _, r := range msgs {
			errCheck := m.checkBeforeDelete(ctx, r, user, force, *topic)
			if errCheck != nil {
				// ctx writes in checkBeforeDelete
				return
			}
		}
	} else if len(msgs) > 0 {
		ctx.JSON(http.StatusBadRequest, gin.H{"error": "Could not delete this message, this message have replies"})
		return
	}

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

	ctx.JSON(http.StatusOK, gin.H{"info": fmt.Sprintf("Message deleted from %s", topic.Topic)})
}