提交 bd8c61fc 编写于 作者: Huan (李卓桓)'s avatar Huan (李卓桓)

refactoring MessagePayload to use id instead of the instance

上级 b277ecbb
......@@ -110,6 +110,9 @@ export class Message extends PuppetAccessory implements Sayable {
const date = new Date()
const file = (options as MessageMOOptionsFile).file
const roomId = room && room.id
const toId = to && to.id
if (text) {
/**
* 1. Text
......@@ -117,8 +120,8 @@ export class Message extends PuppetAccessory implements Sayable {
msg.payload = {
type: MessageType.Text,
direction,
to,
room,
toId,
roomId,
text,
date,
}
......@@ -154,8 +157,8 @@ export class Message extends PuppetAccessory implements Sayable {
msg.payload = {
type,
direction,
to,
room,
toId,
roomId,
file,
date,
}
......@@ -279,11 +282,12 @@ export class Message extends PuppetAccessory implements Sayable {
// return
// }
const from = this.payload.from
if (!from) {
const fromId = this.payload.fromId
if (!fromId) {
throw new Error('no from')
}
const from = this.puppet.Contact.load(fromId)
return from
}
......@@ -311,7 +315,13 @@ export class Message extends PuppetAccessory implements Sayable {
// return
// }
return this.payload.to || null
const toId = this.payload.toId
if (!toId) {
return null
}
const to = this.puppet.Contact.load(toId)
return to
}
// /**
......@@ -338,8 +348,13 @@ export class Message extends PuppetAccessory implements Sayable {
// this.payload.room = room
// return
// }
const roomId = this.payload.roomId
if (!roomId) {
return null
}
return this.payload.room || null
const room = this.puppet.Room.load(roomId)
return room
}
// /**
......
......@@ -65,8 +65,8 @@ export interface MessagePayload {
text? : string,
file? : FileBox,
direction : MessageDirection,
from? : Contact,
fromId? : string,
date : Date,
to? : null | Contact, // if to is not set, then room must be set
room? : null | Room,
toId? : null | string, // if to is not set, then room must be set
roomId? : null | string,
}
......@@ -198,9 +198,9 @@ export class PuppetMock extends Puppet {
const payload: MessagePayload = {
date : new Date(),
direction : this.Message.Direction.MT,
from : this.Contact.load('xxx'),
fromId : 'xxx',
text : 'mock message text',
to : this.userSelf(),
toId : this.userSelf().id,
type : this.Message.Type.Text,
}
return payload
......
......@@ -348,18 +348,19 @@ export class PuppetPuppeteer extends Puppet {
const type: MessageType = this.messageTypeFromWeb(rawPayload.MsgType)
const fromId = from && from.id
const roomId = room && room.id
const toId = to && to.id
const payload: MessagePayload = {
direction: MessageDirection.MT,
type,
from,
to,
room,
fromId,
toId,
roomId,
text,
// status: rawPayload.Status,
// digest: rawPayload.MMDigest,
date,
file,
// url: rawPayload.Url || rawObj.MMAppMsgDownloadUrl || rawObj.MMLocationUrl,
}
// TODO: parse the url to FileBox
......
......@@ -253,8 +253,8 @@ test('self()', async t => {
function mockMessagePayload() {
const payload: MessagePayload = {
from : MOCK_CONTACT,
to : {} as any,
fromId : MOCK_CONTACT.id,
toId : 'to_id',
type : {} as any,
direction : {} as any,
date : {} as any,
......
......@@ -303,19 +303,25 @@ export abstract class Puppet extends EventEmitter implements Sayable {
const rawPayload = await this.messageRawPayload(id)
const payload = await this.messageRawPayloadParser(rawPayload)
console.log('this.messageRawPayloadParser().payload.from.puppet = ', payload.from!.puppet + '')
/**
* Make sure all the contacts & room have already been ready
*/
if (payload.from && !payload.from.isReady()) {
await payload.from.ready()
const fromId = payload.fromId
const roomId = payload.roomId
const toId = payload.toId
const from = fromId && this.Contact.load(fromId)
const room = roomId && this.Room.load(roomId)
const to = toId && this.Contact.load(toId)
if (from && !from.isReady()) {
await from.ready()
}
if (payload.to && !payload.to.isReady()) {
await payload.to.ready()
if (to && !to.isReady()) {
await to.ready()
}
if (payload.room && !payload.room.isReady()) {
await payload.room.ready()
if (room && !room.isReady()) {
await room.ready()
}
return payload
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册