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

fix media file api

上级 f6b81c5c
......@@ -16,8 +16,8 @@
* limitations under the License.
* @ignore
*/
import * as path from 'path'
import * as cuid from 'cuid'
// import * as path from 'path'
// import * as cuid from 'cuid'
import {
FileBox,
......@@ -37,9 +37,9 @@ import Room from './room'
import {
MessageDirection,
MessageMOOptions,
MessageMOOptionsText,
MessageMOOptionsFile,
// MessageMOOptions,
// MessageMOOptionsText,
// MessageMOOptionsFile,
MessagePayload,
MessageType,
} from './message.type'
......@@ -91,89 +91,95 @@ export class Message extends PuppetAccessory implements Sayable {
* "mobile originated" or "mobile terminated"
* https://www.tatango.com/resources/video-lessons/video-mo-mt-sms-messaging/
*/
public static createMO(
options: MessageMOOptions,
): Message {
log.verbose('Message', 'static createMobileOriginated()')
/**
* Must NOT use `Message` at here
* MUST use `this` at here
*
* because the class will be `cloneClass`-ed
*/
const msg = new this(cuid())
const direction = MessageDirection.MO
const to = options.to
const room = options.room
const text = (options as MessageMOOptionsText).text
const date = new Date()
const file = (options as MessageMOOptionsFile).file
const roomId = room && room.id
const toId = to && to.id
if (text) {
/**
* 1. Text
*/
msg.payload = {
type: MessageType.Text,
direction,
toId,
roomId,
text,
date,
}
} else if (file) {
/**
* 2. File
*/
let type: MessageType
const ext = path.extname(file.name)
switch (ext.toLowerCase()) {
case '.bmp':
case '.jpg':
case '.jpeg':
case '.png':
case '.gif': // type = WebMsgType.EMOTICON
type = MessageType.Image
break
// private static createMO(
// options: MessageMOOptions,
// ): Message {
// log.verbose('Message', 'static createMobileOriginated()')
// /**
// * Must NOT use `Message` at here
// * MUST use `this` at here
// *
// * because the class will be `cloneClass`-ed
// */
// const msg = new this(cuid())
// const direction = MessageDirection.MO
// const to = options.to
// const room = options.room
// const text = (options as MessageMOOptionsText).text
// const date = new Date()
// const file = (options as MessageMOOptionsFile).file
// const roomId = room && room.id
// const toId = to && to.id
// if (text) {
// /**
// * 1. Text
// */
// msg.payload = {
// type: MessageType.Text,
// direction,
// toId,
// roomId,
// text,
// date,
// }
// } else if (file) {
// /**
// * 2. File
// */
// let type: MessageType
// const ext = path.extname(file.name)
// switch (ext.toLowerCase()) {
// case '.bmp':
// case '.jpg':
// case '.jpeg':
// case '.png':
// case '.gif': // type = WebMsgType.EMOTICON
// type = MessageType.Image
// break
case '.mp4':
type = MessageType.Video
break
// case '.mp4':
// type = MessageType.Video
// break
case '.mp3':
type = MessageType.Audio
break
// case '.mp3':
// type = MessageType.Audio
// break
default:
throw new Error('unknown ext:' + ext)
}
// default:
// throw new Error('unknown ext:' + ext)
// }
msg.payload = {
type,
direction,
toId,
roomId,
file,
date,
}
} else {
throw new Error('neither text nor file!?')
}
// msg.payload = {
// type,
// direction,
// toId,
// roomId,
// file,
// date,
// }
// } else {
// throw new Error('neither text nor file!?')
// }
return msg
}
// return msg
// }
/**
* "mobile originated" or "mobile terminated"
* https://www.tatango.com/resources/video-lessons/video-mo-mt-sms-messaging/
*/
public static createMT(
id: string,
id : string,
payload? : MessagePayload,
): Message {
log.verbose('Message', 'static createMobileTerminated(%s)',
log.verbose('Message', 'static createMobileTerminated(%s, %s)',
id,
payload,
)
/**
* Must NOT use `Message` at here
......@@ -184,15 +190,25 @@ export class Message extends PuppetAccessory implements Sayable {
const msg = new this(id)
msg.direction = MessageDirection.MT
if (payload) {
msg.payload = payload
}
return msg
}
/**
* Create a Mobile Originated Message
* @param options MO Options
* @alias createMT
* Create a Mobile Terminated Message
*/
public static create(options: MessageMOOptions): Message {
return this.createMO(options)
public static create(
id : string,
payload? : MessagePayload,
): Message {
return this.createMT(
id,
payload,
)
}
/**
......@@ -246,6 +262,8 @@ export class Message extends PuppetAccessory implements Sayable {
if (this.type() === Message.Type.Text) {
msgStrList.push(`<${this.text()}>`)
} else {
log.verbose('Message', 'toString() this.type()=%s', Message.Type[this.type()])
if (!this.payload) {
throw new Error('no payload')
}
......@@ -424,7 +442,10 @@ export class Message extends PuppetAccessory implements Sayable {
textOrFile : string | FileBox,
mention? : Contact | Contact[],
): Promise<void> {
log.verbose('Message', 'say(%s, %s)', textOrFile, mention)
log.verbose('Message', 'say(%s, %s)',
textOrFile.toString(),
mention,
)
// const user = this.puppet.userSelf()
const from = this.from()
......
......@@ -10,7 +10,7 @@ import {
} from './room'
export enum MessageDirection {
MO,
// MO,
MT,
}
......@@ -18,6 +18,7 @@ export enum MessageType {
Unknown = 0,
Attachment,
Audio,
Emoticon,
Image,
Text,
Video,
......
......@@ -190,7 +190,7 @@ async function onMessage(
this : PuppetPuppeteer,
rawPayload : WebMessageRawPayload,
): Promise<void> {
let msg = this.Message.createMT(rawPayload.MsgId)
const msg = this.Message.createMT(rawPayload.MsgId)
try {
await msg.ready()
......@@ -206,9 +206,9 @@ async function onMessage(
case WebMessageType.SYS:
if (msg.room()) {
const joinResult = await Firer.checkRoomJoin.call(this , msg)
const leaveResult = await Firer.checkRoomLeave.call(this , msg)
const topicRestul = await Firer.checkRoomTopic.call(this , msg)
const joinResult = await Firer.checkRoomJoin.call(this, msg)
const leaveResult = await Firer.checkRoomLeave.call(this, msg)
const topicRestul = await Firer.checkRoomTopic.call(this, msg)
if (!joinResult && !leaveResult && !topicRestul) {
log.warn('PuppetPuppeteerEvent', `checkRoomSystem message: <${msg.text()}> not found`)
......@@ -219,31 +219,31 @@ async function onMessage(
break
}
/**
* Check Type for special Message
* reload if needed
*/
switch (rawPayload.MsgType) {
case WebMessageType.EMOTICON:
case WebMessageType.IMAGE:
case WebMessageType.VIDEO:
case WebMessageType.VOICE:
case WebMessageType.MICROVIDEO:
case WebMessageType.APP:
log.verbose('PuppetPuppeteerEvent', 'onMessage() EMOTICON/IMAGE/VIDEO/VOICE/MICROVIDEO message')
msg = this.Message.createMT(rawPayload.MsgId)
break
// /**
// * Check Type for special Message
// * reload if needed
// */
// switch (rawPayload.MsgType) {
// case WebMessageType.EMOTICON:
// case WebMessageType.IMAGE:
// case WebMessageType.VIDEO:
// case WebMessageType.VOICE:
// case WebMessageType.MICROVIDEO:
// case WebMessageType.APP:
// log.verbose('PuppetPuppeteerEvent', 'onMessage() EMOTICON/IMAGE/VIDEO/VOICE/MICROVIDEO message')
// msg = this.Message.createMT(rawPayload.MsgId)
// break
// case WebMessageType.TEXT:
// if (rawPayload.SubMsgType === WebMessageType.LOCATION) {
// log.verbose('PuppetPuppeteerEvent', 'onMessage() (TEXT&LOCATION) message')
// msg = this.Message.createMT(rawPayload.MsgId)
// }
// break
// }
// await msg.ready()
case WebMessageType.TEXT:
if (rawPayload.SubMsgType === WebMessageType.LOCATION) {
log.verbose('PuppetPuppeteerEvent', 'onMessage() (TEXT&LOCATION) message')
msg = this.Message.createMT(rawPayload.MsgId)
}
break
}
await msg.ready()
this.emit('message', msg)
} catch (e) {
......
......@@ -204,7 +204,7 @@ function parseRoomJoin(
this: PuppetPuppeteer,
content: string,
): [string[], string] {
log.verbose('PuppetPuppeteerFirer', 'checkRoomJoin(%s)', content)
log.verbose('PuppetPuppeteerFirer', 'parseRoomJoin(%s)', content)
const reListInvite = regexConfig.roomJoinInvite
const reListQrcode = regexConfig.roomJoinQrcode
......@@ -214,7 +214,7 @@ function parseRoomJoin(
let foundQrcode: string[]|null = []
reListQrcode.some(re => !!(foundQrcode = content.match(re)))
if ((!foundInvite || !foundInvite.length) && (!foundQrcode || !foundQrcode.length)) {
throw new Error('checkRoomJoin() not found matched re of ' + content)
throw new Error('parseRoomJoin() not found matched re of ' + content)
}
/**
* 管理员 invited 庆次、小桔妹 to the group chat
......@@ -233,7 +233,7 @@ async function checkRoomJoin(
const room = msg.room()
if (!room) {
log.warn('PuppetPuppeteerFirer', 'fireRoomJoin() `room` not found')
log.warn('PuppetPuppeteerFirer', 'checkRoomJoin() `room` not found')
return false
}
......@@ -243,23 +243,23 @@ async function checkRoomJoin(
try {
[inviteeList, inviter] = parseRoomJoin.call(this, text)
} catch (e) {
log.silly('PuppetPuppeteerFirer', 'fireRoomJoin() "%s" is not a join message', text)
log.silly('PuppetPuppeteerFirer', 'checkRoomJoin() "%s" is not a join message', text)
return false // not a room join message
}
log.silly('PuppetPuppeteerFirer', 'fireRoomJoin() inviteeList: %s, inviter: %s',
log.silly('PuppetPuppeteerFirer', 'checkRoomJoin() inviteeList: %s, inviter: %s',
inviteeList.join(','),
inviter,
)
let inviterContact: Contact | null = null
let inviteeContactList: Contact[] = []
let inviterContact: null | Contact = null
let inviteeContactList: Contact[] = []
try {
if (inviter === 'You' || inviter === '' || inviter === 'you') {
inviterContact = this.userSelf()
}
const max = 20
const max = 20
const backoff = 300
const timeout = max * (backoff * max) / 2
// 20 / 300 => 63,000
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册