Ejemplo n.º 1
0
Archivo: messages.go Proyecto: ovh/tat
func (m *MessagesController) updateMessage(ctx *gin.Context, messageIn *tat.MessageJSON, message tat.Message, user tat.User, topic tat.Topic, isAdminOnTopic bool) {
	info := ""

	if isAdminOnTopic && topic.CanUpdateAllMsg {
		// ok, user is admin on topic, and admin can update all msg
	} else {
		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
		}
	}

	err := messageDB.Update(&message, user, topic, messageIn.Text, messageIn.Action)
	if err != nil {
		log.Errorf("Error while update a message %s", err)
		ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}
	info = fmt.Sprintf("Message updated in %s", topic.Topic)
	out := &tat.MessageJSONOut{Info: info, Message: message}
	hook.SendHook(&tat.HookJSON{HookMessage: &tat.HookMessageJSON{MessageJSONOut: out, Action: messageIn.Action}}, topic)
	ctx.JSON(http.StatusOK, out)
}
Ejemplo n.º 2
0
Archivo: messages.go Proyecto: ovh/tat
func (m *MessagesController) addOrRemoveTask(ctx *gin.Context, messageIn *tat.MessageJSON, message tat.Message, user tat.User, topic tat.Topic) {
	info := ""
	if messageIn.Action == tat.MessageActionTask {
		if message.InReplyOfIDRoot != "" {
			log.Warnf("This message is a reply, you can't task it (%s)", message.ID)
			ctx.JSON(http.StatusInternalServerError, gin.H{"error": "This message is a reply, you can't task it"})
			return
		}
		if err := messageDB.AddToTasks(&message, user, topic); err != nil {
			log.Errorf("Error while adding a message to tasks %s", err)
			ctx.JSON(http.StatusInternalServerError, gin.H{"error": "Error while adding a message to tasks"})
			return
		}
		info = fmt.Sprintf("New Task created")
	} else if messageIn.Action == tat.MessageActionUntask {
		if err := messageDB.RemoveFromTasks(&message, user, topic); err != nil {
			log.Errorf("Error while removing a message from tasks %s", err)
			ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
			return
		}
		info = fmt.Sprintf("Task removed")
	} else {
		ctx.JSON(http.StatusBadRequest, gin.H{"error": "Invalid action: " + messageIn.Action})
		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)
}
Ejemplo n.º 3
0
Archivo: messages.go Proyecto: ovh/tat
func (m *MessagesController) likeOrUnlike(ctx *gin.Context, action string, message tat.Message, topic tat.Topic, user tat.User) {

	info := ""
	if action == tat.MessageActionLike {
		if err := messageDB.Like(&message, user, topic); err != nil {
			log.Errorf("Error while like a message %s", err)
			ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
			return
		}
		info = "like added"
	} else if action == tat.MessageActionUnlike {
		if err := messageDB.Unlike(&message, user, topic); err != nil {
			log.Errorf("Error while unlike a message %s", err)
			ctx.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
			return
		}
		info = "like removed"
	} else {
		ctx.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("Invalid action: " + action)})
		return
	}
	out := &tat.MessageJSONOut{Info: info, Message: message}
	hook.SendHook(&tat.HookJSON{HookMessage: &tat.HookMessageJSON{MessageJSONOut: out, Action: action}}, topic)
	ctx.JSON(http.StatusCreated, out)
}
Ejemplo n.º 4
0
Archivo: messages.go Proyecto: ovh/tat
func (m *MessagesController) createSingle(ctx *gin.Context, messageIn *tat.MessageJSON) (*tat.MessageJSONOut, int, error) {

	msg, topic, user, e := m.preCheckTopic(ctx, messageIn)
	if e != nil {
		return nil, http.StatusInternalServerError, fmt.Errorf("No RW Access to topic %s", messageIn.Topic)
	}

	if isRw, _ := topicDB.GetUserRights(&topic, user); !isRw {
		return nil, http.StatusForbidden, fmt.Errorf("No RW Access to topic %s", messageIn.Topic)
	}

	var message = tat.Message{}

	idRef := ""
	if msg.ID != "" {
		idRef = msg.ID
	}

	text := messageIn.Text
	if idRef != "" && messageIn.Text != "" && (len(messageIn.Replies) > 0 || len(messageIn.Messages) > 0) {
		text = ""
	}

	// New root message or reply
	err := messageDB.Insert(&message, *user, topic, text, idRef, messageIn.DateCreation, messageIn.Labels, messageIn.Replies, messageIn.Messages, false, nil)
	if err != nil {
		log.Errorf("%s", err.Error())
		return nil, http.StatusInternalServerError, err
	}
	info := fmt.Sprintf("Message created in %s", topic.Topic)
	out := &tat.MessageJSONOut{Message: message, Info: info}
	hook.SendHook(&tat.HookJSON{HookMessage: &tat.HookMessageJSON{MessageJSONOut: out, Action: tat.MessageActionCreate}}, topic)
	return out, http.StatusCreated, nil
}
Ejemplo n.º 5
0
Archivo: messages.go Proyecto: ovh/tat
func (m *MessagesController) addOrRemoveLabel(ctx *gin.Context, messageIn *tat.MessageJSON, message tat.Message, user tat.User, topic tat.Topic) {
	if messageIn.Text == "" && messageIn.Action != tat.MessageActionRelabel {
		ctx.AbortWithError(http.StatusBadRequest, errors.New("Invalid Text for label"))
		return
	}
	out := &tat.MessageJSONOut{}
	if messageIn.Action == tat.MessageActionLabel {
		addedLabel, err := messageDB.AddLabel(&message, topic, messageIn.Text, messageIn.Option)
		if err != nil {
			errInfo := fmt.Sprintf("Error while adding a label to a message %s", err.Error())
			log.Errorf(errInfo)
			ctx.JSON(http.StatusInternalServerError, gin.H{"error": errInfo})
			return
		}
		out = &tat.MessageJSONOut{Info: fmt.Sprintf("label %s added to message", addedLabel.Text), Message: message}
	} else if messageIn.Action == tat.MessageActionUnlabel {
		if err := messageDB.RemoveLabel(&message, messageIn.Text, topic); err != nil {
			errInfo := fmt.Sprintf("Error while removing a label from a message %s", err.Error())
			log.Errorf(errInfo)
			ctx.JSON(http.StatusInternalServerError, gin.H{"error": errInfo})
			return
		}
		out = &tat.MessageJSONOut{Info: fmt.Sprintf("label %s removed from message", messageIn.Text), Message: message}
	} else if messageIn.Action == tat.MessageActionRelabel && len(messageIn.Options) == 0 {
		if err := messageDB.RemoveAllAndAddNewLabel(&message, messageIn.Labels, topic); err != nil {
			errInfo := fmt.Sprintf("Error while removing all labels and add new ones for a message %s", err.Error())
			log.Errorf(errInfo)
			ctx.JSON(http.StatusInternalServerError, gin.H{"error": errInfo})
			return
		}
		out = &tat.MessageJSONOut{Info: fmt.Sprintf("all labels removed and new labels %s added to message", messageIn.Text), Message: message}
	} else if messageIn.Action == tat.MessageActionRelabel && len(messageIn.Options) > 0 {
		if err := messageDB.RemoveSomeAndAddNewLabel(&message, messageIn.Labels, messageIn.Options, topic); err != nil {
			errInfo := fmt.Sprintf("Error while removing some labels and add new ones for a message %s", err.Error())
			log.Errorf(errInfo)
			ctx.JSON(http.StatusInternalServerError, gin.H{"error": errInfo})
			return
		}
		out = &tat.MessageJSONOut{Info: fmt.Sprintf("Some labels removed and new labels %s added to message", messageIn.Text), Message: message}

	} else {
		ctx.AbortWithError(http.StatusBadRequest, errors.New("Invalid action: "+messageIn.Action))
		return
	}
	hook.SendHook(&tat.HookJSON{HookMessage: &tat.HookMessageJSON{MessageJSONOut: out, Action: messageIn.Action}}, topic)
	ctx.JSON(http.StatusCreated, out)
}
Ejemplo n.º 6
0
Archivo: messages.go Proyecto: ovh/tat
func (m *MessagesController) moveMessage(ctx *gin.Context, messageIn *tat.MessageJSON, message tat.Message, user tat.User, fromTopic tat.Topic) {

	// Check if user can delete msg on from topic
	if err := m.checkBeforeDelete(ctx, message, user, true, fromTopic); err != nil {
		// ctx writes in checkBeforeDelete
		return
	}

	toTopic, err := topicDB.FindByTopic(messageIn.Option, true, false, false, &user)
	if err != nil {
		e := fmt.Sprintf("Topic destination %s does not exist", message.Topic)
		ctx.JSON(http.StatusNotFound, gin.H{"error": e})
		return
	}

	// Check if user can write msg from dest topic
	if isRW, _ := topicDB.GetUserRights(toTopic, &user); !isRW {
		ctx.JSON(http.StatusForbidden, gin.H{"error": fmt.Sprintf("No RW Access to topic %s", toTopic.Topic)})
		return
	}

	// check if message is a reply -> not possible
	if message.InReplyOfIDRoot != "" {
		ctx.JSON(http.StatusForbidden, gin.H{"error": fmt.Sprintf("You can't move a reply message")})
		return
	}

	info := ""
	if messageIn.Action == tat.MessageActionMove {
		err := messageDB.Move(&message, user, fromTopic, *toTopic)
		if err != nil {
			log.Errorf("Error while move a message to topic: %s err: %s", toTopic.Topic, err)
			ctx.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Error while move a message to topic %s", toTopic.Topic)})
			return
		}
		info = fmt.Sprintf("Message move to %s", toTopic.Topic)
	} else {
		ctx.JSON(http.StatusBadRequest, gin.H{"error": "Invalid action: " + messageIn.Action})
		return
	}
	out := &tat.MessageJSONOut{Info: info, Message: message}
	hook.SendHook(&tat.HookJSON{HookMessage: &tat.HookMessageJSON{MessageJSONOut: out, Action: messageIn.Action}}, *toTopic)
	ctx.JSON(http.StatusCreated, out)
}
Ejemplo n.º 7
0
Archivo: messages.go Proyecto: 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)
}