Example #1
0
File: messages.go Project: ovh/tat
func (m *MessagesController) voteMessage(ctx *gin.Context, messageIn *tat.MessageJSON, message tat.Message, user tat.User, topic tat.Topic) {
	info := ""
	errInfo := ""
	if messageIn.Action == tat.MessageActionVoteup {
		if err := messageDB.VoteUP(&message, user, topic); err != nil {
			errInfo = fmt.Sprintf("Error while vote up a message %s", err.Error())
		}
		info = "Vote UP added to message"
	} else if messageIn.Action == tat.MessageActionVotedown {
		if err := messageDB.VoteDown(&message, user, topic); err != nil {
			errInfo = fmt.Sprintf("Error while vote down a message %s", err.Error())
		}
		info = "Vote Down added to message"
	} else if messageIn.Action == tat.MessageActionUnvoteup {
		if err := messageDB.UnVoteUP(&message, user, topic); err != nil {
			errInfo = fmt.Sprintf("Error while remove vote up from message %s", err.Error())
		}
		info = "Vote UP removed from message"
	} else if messageIn.Action == tat.MessageActionUnvotedown {
		if err := messageDB.UnVoteDown(&message, user, topic); err != nil {
			errInfo = fmt.Sprintf("Error while remove vote down from message %s", err.Error())
		}
		info = "Vote Down removed from message"
	} else {
		ctx.AbortWithError(http.StatusBadRequest, errors.New("Invalid action: "+messageIn.Action))
		return
	}
	if errInfo != "" {
		log.Errorf(errInfo)
		ctx.JSON(http.StatusInternalServerError, gin.H{"error": errInfo})
		return
	}
	if err := messageDB.FindByID(&message, messageIn.IDReference, topic); err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{"error": "Error while fetching message after voting"})
		return
	}

	out := &tat.MessageJSONOut{Info: info, Message: message}
	hook.SendHook(&tat.HookJSON{HookMessage: &tat.HookMessageJSON{MessageJSONOut: out, Action: messageIn.Action}}, topic)
	ctx.JSON(http.StatusCreated, out)
}
Example #2
0
File: messages.go Project: 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)})
}
Example #3
0
File: messages.go Project: 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
}