Пример #1
0
// Update updates a message from database
func (message *Message) Update(user User, topic Topic) error {

	err := message.CheckAndFixText(topic)
	if err != nil {
		return err
	}

	err = Store().clMessages.Update(
		bson.M{"_id": message.ID},
		bson.M{"$set": bson.M{
			"text":         message.Text,
			"dateUpdate":   time.Now().Unix(),
			"tags":         hashtag.ExtractHashtags(message.Text),
			"userMentions": hashtag.ExtractMentions(message.Text),
			"urls":         xurls.Strict.FindAllString(message.Text, -1),
		}})
	if err != nil {
		log.Errorf("Error while update a message %s", err)
	}

	return nil
}
Пример #2
0
// Insert a new message on one topic
func (message *Message) Insert(user User, topic Topic, text, inReplyOfID string, dateCreation int64) error {
	message.Text = text
	err := message.CheckAndFixText(topic)
	if err != nil {
		return err
	}
	message.ID = bson.NewObjectId().Hex()
	message.InReplyOfID = inReplyOfID
	dateToStore := time.Now().Unix()

	if dateCreation > 0 {
		if !topic.CanForceDate {
			return fmt.Errorf("You can't force date on topic %s", topic.Topic)
		}

		if !user.IsSystem {
			return fmt.Errorf("You can't force date on topic %s, you're not a system user", topic.Topic)
		}

		if !topic.CanForceDate {
			return fmt.Errorf("Error while converting dateCreation %d - error:%s", dateCreation, err.Error())
		}
		dateToStore = dateCreation
	}

	if inReplyOfID != "" {
		var messageReference = &Message{}
		err = messageReference.FindByID(inReplyOfID)
		if err != nil {
			return err
		}
		if messageReference.InReplyOfID != "" {
			message.InReplyOfIDRoot = messageReference.InReplyOfIDRoot
		} else {
			message.InReplyOfIDRoot = messageReference.ID
		}
		message.Topics = messageReference.Topics

		// if msgRef.dateCreation >= dateToStore -> dateToStore must be after
		if dateToStore <= messageReference.DateCreation {
			dateToStore = messageReference.DateCreation + 1
		}
	} else {
		message.Topics = append(message.Topics, topic.Topic)

		topicDM := "/Private/" + user.Username + "/DM/"
		if strings.HasPrefix(topic.Topic, topicDM) {
			part := strings.Split(topic.Topic, "/")
			if len(part) != 5 {
				log.Errorf("wrong topic name for DM")
				return fmt.Errorf("Wrong topic name for DM:%s", topic.Topic)
			}
			topicInverse := "/Private/" + part[4] + "/DM/" + user.Username
			message.Topics = append(message.Topics, topicInverse)
		}
	}

	message.NbLikes = 0
	var author = Author{}
	author.Username = user.Username
	author.Fullname = user.Fullname
	message.Author = author

	message.DateCreation = dateToStore
	message.DateUpdate = time.Now().Unix()
	message.Tags = hashtag.ExtractHashtags(message.Text)
	message.UserMentions = hashtag.ExtractMentions(message.Text)
	message.Urls = xurls.Strict.FindAllString(message.Text, -1)

	err = Store().clMessages.Insert(message)
	if err != nil {
		log.Errorf("Error while inserting new message %s", err)
	}
	return err
}