示例#1
0
文件: add.go 项目: ovh/tatcli
// Create creates a message in specified topic
func Create(topic, message string) (*tat.MessageJSONOut, error) {
	m := tat.MessageJSON{Text: message, Topic: topic}
	if dateCreation > 0 {
		m.DateCreation = float64(dateCreation)
	}
	for _, label := range cmdLabel {
		s := strings.Split(label, ";")
		if len(s) == 2 {
			m.Labels = append(m.Labels, tat.Label{Text: s[1], Color: s[0]})
		} else {
			return nil, fmt.Errorf("Invalid argument label %s to add a message: tatcli msg add --help\n", label)
		}
	}

	return internal.Client().MessageAdd(m)
}
示例#2
0
文件: messages.go 项目: 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
}