From bc6299cda6bae795ee7006fb833482ceca32384f Mon Sep 17 00:00:00 2001 From: Huan LI Date: Sat, 11 Aug 2018 13:04:34 +0800 Subject: [PATCH] add Message.find() (#1534) --- src/user/message.ts | 100 +++++++++++++++++++++++++++++++------------- 1 file changed, 72 insertions(+), 28 deletions(-) diff --git a/src/user/message.ts b/src/user/message.ts index e48979202..d495fd477 100644 --- a/src/user/message.ts +++ b/src/user/message.ts @@ -16,41 +16,40 @@ * limitations under the License. * @ignore */ -// import path from 'path' -// import cuid from 'cuid' - import { instanceToClass, -} from 'clone-class' +} from 'clone-class' import { FileBox, -} from 'file-box' +} from 'file-box' + +import { + MessagePayload, + MessageQueryFilter, + MessageType, +} from 'wechaty-puppet' import { Accessory, -} from '../accessory' +} from '../accessory' import { FOUR_PER_EM_SPACE, log, -} from '../config' + Raven, +} from '../config' import { Sayable, -} from '../types' +} from '../types' import { Contact, -} from './contact' +} from './contact' import { Room, -} from './room' +} from './room' import { UrlLink, -} from './url-link' - -import { - MessagePayload, - MessageType, -} from 'wechaty-puppet' +} from './url-link' /** * All wechat messages will be encapsulated as a Message. @@ -77,9 +76,24 @@ export class Message extends Accessory implements Sayable { */ public static async find ( this: T, - query: any, + query : string | MessageQueryFilter, ): Promise { - return (await this.findAll(query))[0] + log.verbose('Message', 'find(%s)', JSON.stringify(query)) + + if (typeof query === 'string') { + query = { text: query } + } + + const messageList = await this.findAll(query) + if (messageList.length < 1) { + return null + } + + if (messageList.length > 1) { + log.warn('Message', 'findAll() got more than one(%d) result', messageList.length) + } + + return messageList[0] } /** @@ -87,14 +101,34 @@ export class Message extends Accessory implements Sayable { * @todo add function */ public static async findAll ( - this: T, - query: any, + this : T, + query?: MessageQueryFilter, ): Promise> { - log.verbose('Message', 'findAll(%s)', query) - return [ - new (this as any)({ MsgId: 'id1' }), - new (this as any)({ MsdId: 'id2' }), - ] + log.verbose('Message', 'findAll(%s)', JSON.stringify(query)) + + try { + const MessageIdList = await this.puppet.messageSearch(query) + const messageList = MessageIdList.map(id => this.load(id)) + await Promise.all( + messageList.map( + message => { + try { + return message.ready() + } catch (e) { + return {} as any + } + }, + ), + ) + + return messageList.filter(message => !!message.id) + + } catch (e) { + log.verbose('Message', 'findAll() rejected: %s', e.message) + console.error(e) + Raven.captureException(e) + return [] // fail safe + } } /** @@ -103,9 +137,8 @@ export class Message extends Accessory implements Sayable { * "mobile originated" or "mobile terminated" * https://www.tatango.com/resources/video-lessons/video-mo-mt-sms-messaging/ */ - // TODO: rename create to load ??? Huan 201806 - public static create (id: string): Message { - log.verbose('Message', 'static create(%s)', id) + public static load (id: string): Message { + log.verbose('Message', 'static load(%s)', id) /** * Must NOT use `Message` at here @@ -118,6 +151,17 @@ export class Message extends Accessory implements Sayable { return msg } + /** + * TODO: rename create to load ??? Huan 201806 + * @deprecated: use load() instead + * @private + */ + + public static create (id: string): Message { + log.warn('Message', 'static create(%s) DEPRECATED. Use load() instead', id) + return this.load(id) + } + /** * * Instance Properties