MessageMapping: Change json creation to a clean method and event identifier on emitte

This commit is contained in:
italo 2017-08-15 20:28:17 +00:00
parent 57b16e0521
commit c18fd48a22
2 changed files with 89 additions and 62 deletions

View File

@ -87,6 +87,6 @@ module.exports = class CallbackEmitter extends EventEmitter
simplifiedEvent = (event) ->
try
eventJs = JSON.parse(event)
"event: { name: #{eventJs.header?.name}, timestamp: #{eventJs.header?.timestamp} }"
"event: { name: #{eventJs.data?.id}, timestamp: #{eventJs.data.event?.ts} }"
catch e
"event: #{event}"

View File

@ -12,88 +12,115 @@ module.exports = class MessageMapping
# Map internal message based on it's type
mapMessage: (messageObj) ->
if messageObj.header.name in @meetingEvents
if messageObj.header?.name in @meetingEvents
@meetingTemplate(messageObj)
else if messageObj.header.name in @userEvents
else if messageObj.header?.name in @userEvents
@userTemplate(messageObj)
else if messageObj.header.name in @chatEvents
else if messageObj.header?.name in @chatEvents
@chatTemplate(messageObj)
# Map internal to external message for meeting information
meetingTemplate: (messageObj) ->
@mappedObject.data = {}
@mappedObject.data.type = "event"
@mappedObject.data.id = @mapInternalMessage(messageObj.header.name)
@mappedObject.data.attributes = {}
@mappedObject.data.attributes.meeting = {}
@mappedObject.data = {
"type": "event",
"id": @mapInternalMessage(messageObj.header.name),
"attributes":{
"meeting":{
"internal-meeting-id": messageObj.payload.meeting_id,
"external-meeting-id": messageObj.payload.external_meeting_id
}
},
"event":{
"ts": messageObj.header.current_time
}
}
@mappedObject.data.attributes.meeting["external-meeting-id"] = messageObj.payload.external_meeting_id
@mappedObject.data.attributes.meeting["internal-meeting-id"] = messageObj.payload.meeting_id
@mappedObject.data.event = {}
@mappedObject.data.event.ts = messageObj.header.current_time
if messageObj.header.name is "meeting_created_message"
@mappedObject.data.attributes.name = messageObj.payload.name
@mappedObject.data.attributes["is-breakout"] = messageObj.payload.is_breakout
@mappedObject.data.attributes.duration = messageObj.payload.duration
@mappedObject.data.attributes["create-time"] = messageObj.payload.create_time
@mappedObject.data.attributes["create-date"] = messageObj.payload.create_date
@mappedObject.data.attributes["moderator-pass"] = messageObj.payload.moderator_pass
@mappedObject.data.attributes["viewer-pass"] = messageObj.payload.viewer_pass
@mappedObject.data.attributes.recorded = messageObj.payload.recorded
#@mappedObject.data.attributes.record = ?
@mappedObject.data.attributes["voice-conf"] = messageObj.payload.voice_conf
#@mappedObject.data.attributes.dial-number = ?
@mappedObject.data.attributes["max-users"] = messageObj.payload.max_users
@mappedObject.data.attributes.metadata = {}
#@mappedObject.data.attributes.metadata. ?
@mappedObject.data.attributes = {
"meeting":{
"internal-meeting-id": messageObj.payload.meeting_id,
"external-meeting-id": messageObj.payload.external_meeting_id
},
"name": messageObj.payload.name,
"is-breakout": messageObj.payload.is_breakout,
"duration": messageObj.payload.duration,
"create-time": messageObj.payload.create_time,
"create-date": messageObj.payload.create_date,
"moderator-pass": messageObj.payload.moderator_pass,
"viewer-pass": messageObj.payload.viewer_pass,
"recorded": messageObj.payload.recorded,
#"record": ?,
"voice-conf": messageObj.payload.voice_conf,
#"dial-number": ?,
"max-users": messageObj.payload.max_users,
"metadata": {}
}
@mappedMessage = JSON.stringify(@mappedObject)
Logger.info "MessageMapping: Mapped message:", @mappedMessage
# Map internal to external message for user information
userTemplate: (messageObj) ->
@mappedObject.data = {}
@mappedObject.data.type = "event"
@mappedObject.data.id = @mapInternalMessage(messageObj.header.name)
@mappedObject.data.attributes = {}
@mappedObject.data.attributes.meeting = {}
#@mappedObject.data.attributes.meeting["external-meeting-id"] = messageObj.payload. ?
@mappedObject.data.attributes.meeting["internal-meeting-id"] = messageObj.payload.meeting_id
@mappedObject.data.event = {}
@mappedObject.data.event.ts = messageObj.header.current_time
@mappedObject.data.attributes.user = {}
@mappedObject.data.attributes.user["internal-user-id"] = messageObj.payload.user.userid
@mappedObject.data.attributes.user["external-user-id"] = messageObj.payload.user.extern_userid
@mappedObject.data = {
"type": "event",
"id": @mapInternalMessage(messageObj.header.name),
"attributes":{
"meeting":{
"internal-meeting-id": messageObj.payload.meeting_id
#"external-meeting-id": messageObj.payload.?
},
"user":{
"internal-user-id": messageObj.payload.user.userid,
"external-user-id": messageObj.payload.user.extern_userid
}
},
"event":{
"ts": messageObj.header.current_time
}
}
if messageObj.header.name is "user_joined_message"
@mappedObject.data.attributes.user.name = messageObj.payload.user.name
@mappedObject.data.attributes.user.role = messageObj.payload.user.role
@mappedObject.data.attributes.user.presenter = messageObj.payload.user.presenter
#@mappedObject.data.attributes.user["sharing-mic"] = messageObj.payload.user. ?
@mappedObject.data.attributes.user["sharing-video"] = messageObj.payload.user.has_stream
@mappedObject.data.attributes.user["listening-only"] = messageObj.payload.user.listenOnly
@mappedObject.data.attributes.user = {
"internal-user-id": messageObj.payload.user.userid,
"external-user-id": messageObj.payload.user.extern_userid,
"name": messageObj.payload.user.name,
"role": messageObj.payload.user.role,
"presenter": messageObj.payload.user.presenter,
#"sharing-mic"]: messageObj.payload.user. ?
"sharing-video": messageObj.payload.user.has_stream,
"listening-only": messageObj.payload.user.listenOnly
}
@mappedMessage = JSON.stringify(@mappedObject)
Logger.info "MessageMapping: Mapped message:", @mappedMessage
# Map internal to external message for chat information
chatTemplate: (messageObj) ->
@mappedObject.data = {}
@mappedObject.data.type = "event"
@mappedObject.data.id = @mapInternalMessage(messageObj.header.name)
@mappedObject.data.attributes = {}
@mappedObject.data.attributes.meeting = {}
#@mappedObject.data.attributes.meeting["external-meeting-id"] = messageObj.payload. ?
@mappedObject.data.attributes.meeting["internal-meeting-id"] = messageObj.payload.meeting_id
@mappedObject.data.event = {}
@mappedObject.data.event.ts = messageObj.header.current_time
@mappedObject.data.attributes["chat-message"] = {}
@mappedObject.data.attributes["chat-message"].message = messageObj.payload.message.message
@mappedObject.data.attributes["chat-message"].sender = {}
@mappedObject.data.attributes["chat-message"].sender["internal-user-id"] = messageObj.payload.message.fromUserID
@mappedObject.data.attributes["chat-message"].sender["external-user-id"] = messageObj.payload.message.fromUsername
@mappedObject.data.attributes["chat-message"].sender["timezone-offset"] = messageObj.payload.message.fromTimeZoneOffset
@mappedObject.data.attributes["chat-message"].sender.time = messageObj.payload.message.fromTime
@mappedObject.data = {
"type": "event",
"id": @mapInternalMessage(messageObj.header.name),
"attributes":{
"meeting":{
"internal-meeting-id": messageObj.payload.meeting_id
#{}"external-meeting-id": messageObj.payload.?
},
"chat-message":{
"message": messageObj.payload.message.message,
"sender":{
"internal-user-id": messageObj.payload.message.fromUserID,
"external-user-id": messageObj.payload.message.fromUsername,
"timezone-offset": messageObj.payload.message.fromTimeZoneOffset,
"time": messageObj.payload.message.fromTime
}
}
},
"event":{
"ts": messageObj.header.current_time
}
}
if messageObj.header.name.indexOf("private") != -1
@mappedObject.data.attributes["chat-message"].receiver = {}
@mappedObject.data.attributes["chat-message"].receiver["internal-user-id"] = messageObj.payload.message.toUserID
@mappedObject.data.attributes["chat-message"].sender["external-user-id"] = messageObj.payload.message.toUsername
@mappedObject.data.attributes["chat-message"].receiver = {
"internal-user-id": messageObj.payload.message.toUserID,
"external-user-id": messageObj.payload.message.toUsername
}
@mappedMessage = JSON.stringify(@mappedObject)
Logger.info "MessageMapping: Mapped message:", @mappedMessage