Ejemplo n.º 1
0
Archivo: messages.go Proyecto: 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)})
}
Ejemplo n.º 2
0
Archivo: messages.go Proyecto: ovh/tat
// List messages on one topic, with given criteria
func (m *MessagesController) List(ctx *gin.Context) {
	out, user, topic, criteria, httpCode, err := m.innerList(ctx)

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

	if criteria.OnlyCount == tat.True {
		count, e := messageDB.CountMessages(criteria, user.Username, topic)
		if e != nil {
			ctx.JSON(http.StatusInternalServerError, gin.H{"error": e.Error()})
			return
		}
		ctx.JSON(http.StatusOK, &tat.MessagesCountJSON{Count: count})
		return
	}

	// send presence
	presenceArg := ctx.Query("presence")
	if presenceArg != "" && !user.IsSystem {
		go func() {
			var presence = tat.Presence{}
			if e := presenceDB.Upsert(&presence, user, topic, presenceArg); e != nil {
				log.Errorf("Error while InsertPresence %s", e)
			}
		}()
	}

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

}
Ejemplo n.º 3
0
Archivo: messages.go Proyecto: 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)})
}
Ejemplo n.º 4
0
Archivo: messages.go Proyecto: ovh/tat
func (m *MessagesController) preCheckTopic(ctx *gin.Context, messageIn *tat.MessageJSON) (tat.Message, tat.Topic, *tat.User, error) {
	var message = tat.Message{}

	user, e := PreCheckUser(ctx)
	if e != nil {
		return message, tat.Topic{}, nil, e
	}

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

	if messageIn.Topic == "/" && messageIn.IDReference != "" {
		log.Warnf("preCheckTopic fallback to FindByIDDefaultCollection for %s", messageIn.IDReference)
		if efind := messageDB.FindByIDDefaultCollection(&message, messageIn.IDReference); efind != nil {
			e := errors.New("Invalid request, no topic and message " + messageIn.IDReference + " not found in default collection")
			ctx.JSON(http.StatusNotFound, gin.H{"error": e.Error()})
			return message, tat.Topic{}, nil, e
		}
		messageIn.Topic = message.Topic
	}

	topic, efind := topicDB.FindByTopic(messageIn.Topic, true, true, true, &user)
	if efind != nil {
		topica, _, edm := checkDMTopic(ctx, messageIn.Topic)
		if edm != nil {
			e := errors.New("Topic " + messageIn.Topic + " does not exist or you have no read access on it")
			ctx.JSON(http.StatusNotFound, gin.H{"error": e.Error()})
			return message, tat.Topic{}, nil, e
		}
		topic = topica
	}

	if messageIn.IDReference == "" &&
		messageIn.TagReference == "" &&
		messageIn.StartTagReference == "" &&
		messageIn.LabelReference == "" &&
		messageIn.StartLabelReference == "" {
		// nothing here
	} else if messageIn.IDReference != "" ||
		messageIn.StartTagReference != "" || messageIn.TagReference != "" ||
		messageIn.StartLabelReference != "" || messageIn.LabelReference != "" {
		if messageIn.IDReference != "" {
			if efind := messageDB.FindByID(&message, messageIn.IDReference, *topic); efind != nil {
				e := errors.New("Message " + messageIn.IDReference + " does not exist or you have no read access on it")
				ctx.JSON(http.StatusNotFound, gin.H{"error": e.Error()})
				return message, tat.Topic{}, nil, e
			}
		} else { // TagReference, StartTagReference,LabelReference, StartLabelReference
			onlyMsgRoot := tat.True // default value must be true
			if messageIn.OnlyRootReference == tat.False {
				onlyMsgRoot = tat.False
			}
			c := &tat.MessageCriteria{
				AndTag:      messageIn.TagReference,
				StartTag:    messageIn.StartTagReference,
				AndLabel:    messageIn.LabelReference,
				StartLabel:  messageIn.StartLabelReference,
				OnlyMsgRoot: onlyMsgRoot,
				Topic:       topic.Topic,
			}
			mlist, efind := messageDB.ListMessages(c, user.Username, *topic)
			if efind != nil {
				e := errors.New("Searched Message does not exist or you have no read access on it")
				ctx.JSON(http.StatusNotFound, gin.H{"error": e.Error()})
				return message, tat.Topic{}, nil, e
			}
			if len(mlist) != 1 {
				if messageIn.Action != "" {
					e := errors.New(fmt.Sprintf("Searched Message, expected 1 message and %d message(s) matching on tat", len(mlist)))
					ctx.JSON(http.StatusNotFound, gin.H{"error": e.Error()})
					return message, tat.Topic{}, nil, e
				}
				// take last root message
				if len(mlist) > 0 {
					message = mlist[0]
				}
			} else {
				message = mlist[0]
			}
		}

		topicName := ""
		if messageIn.Action == tat.MessageActionUpdate {
			topicName = messageIn.Topic
		} else if messageIn.Action == "" || messageIn.Action == tat.MessageActionReply ||
			messageIn.Action == tat.MessageActionLike || messageIn.Action == tat.MessageActionUnlike ||
			messageIn.Action == tat.MessageActionLabel || messageIn.Action == tat.MessageActionUnlabel ||
			messageIn.Action == tat.MessageActionVoteup || messageIn.Action == tat.MessageActionVotedown ||
			messageIn.Action == tat.MessageActionUnvoteup || messageIn.Action == tat.MessageActionUnvotedown ||
			messageIn.Action == tat.MessageActionRelabel || messageIn.Action == tat.MessageActionConcat {
			topicName = m.inverseIfDMTopic(ctx, message.Topic)
		} else if messageIn.Action == tat.MessageActionMove {
			topicName = topicIn
		} else if messageIn.Action == tat.MessageActionTask || messageIn.Action == tat.MessageActionUntask {
			topicName = m.inverseIfDMTopic(ctx, message.Topic)
		} else {
			e := errors.New("Invalid Call. IDReference not empty with unknown action")
			ctx.JSON(http.StatusBadRequest, gin.H{"error": e.Error()})
			return message, tat.Topic{}, nil, e
		}
		if topicName == "" && messageIn.Action == "" {
			topicName = messageIn.Topic
		}

		topic, err = topicDB.FindByTopic(topicName, true, true, true, &user)
		if err != nil {
			e := errors.New("Topic " + topicName + " does not exist")
			ctx.JSON(http.StatusNotFound, gin.H{"error": e.Error()})
			return message, tat.Topic{}, nil, e
		}
	} else {
		e := errors.New("Topic and Reference (ID, StartTag, Tag, StartLabel, Label) are null. Wrong request")
		ctx.JSON(http.StatusBadRequest, gin.H{"error": e.Error()})
		return message, tat.Topic{}, nil, e
	}
	return message, *topic, &user, nil
}