Beispiel #1
0
func (m Message) ToJson() (models.JSON, error) {
	var model models.JSON

	if err := model.FromObject(m); err != nil {
		return model, err
	}

	return model, nil
}
Beispiel #2
0
func (b *messageBuilder) BuildFromModel(m models.JSON) Message {
	optionsI := m["options"]
	var options models.Options
	if optionsI != nil {
		options = optionsI.(models.Options)
	}

	pldMap := m["pld"]
	pld := models.JSON{}
	if pldMap != nil {
		pld.FromMap(pldMap.(map[string]interface{}))
	}

	return b.Build(
		m["op"].(string),
		m["origin"].(string),
		pld,
		options,
		m["type"].(string),
		m["app"].(string),
		m["token"].(string),
	)
}
func (p WsMessageReceiver) processDatabaseUpdate(
	val map[string]interface{},
	builder *messaging.MessageBuilder,
	opts models.SubscribeOptions,
) {
	messageBuilder := *builder

	pld := models.JSON{}
	var dbOp string
	newVal := val["new_val"]
	oldVal := val["old_val"]

	if newVal != nil && oldVal == nil {
		dbOp = messaging.OP_CREATE
	} else if newVal == nil && oldVal != nil {
		dbOp = messaging.OP_DELETE
	} else {
		dbOp = messaging.OP_UPDATE
	}

	//only emit messages with the same operation as the subscriber
	if dbOp != opts.Operation {
		return
	}

	var dbVal map[string]interface{}
	if dbOp == messaging.OP_CREATE {
		if newVal == nil {
			log.Error("Invalid message:", val)
			return
		}

		dbVal = newVal.(map[string]interface{})
	} else if dbOp == messaging.OP_DELETE && oldVal != nil {
		if oldVal == nil {
			log.Error("Invalid message:", val)
			return
		}

		dbVal = oldVal.(map[string]interface{})
	} else {
		//update
		if newVal == nil {
			log.Error("Invalid message:", val)
			return
		}

		dbVal = newVal.(map[string]interface{})
	}

	pld = pld.FromMap(dbVal)
	pld = utils.BlacklistFields([]string{db.TYPE_FIELD, db.APP_ID_FIELD}, pld)

	msg := messageBuilder.Build(
		dbOp,
		messaging.ORIGIN_API,
		pld,
		models.Options{},
		opts.Type,
		opts.AppId,
		"", //TODO: token?
	)
	msg.Topic = opts.Topic

	log.Info("Publishing database feed: ", strings.ToUpper(msg.Operation), opts, val, msg)

	publishArgs := []interface{}{msg}
	p.WsClient.Publish(opts.Topic, publishArgs, nil)
}