Skip to content

Commit

Permalink
Merge branch 'main' into fix-logout-in-stop
Browse files Browse the repository at this point in the history
  • Loading branch information
huan authored Jun 14, 2023
2 parents 7749ffb + c3c9037 commit 16af2a1
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 39 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
[![Linux/Mac Build Status](https://travis-ci.com/wechaty/wechaty-puppet.svg?branch=master)](https://travis-ci.com/wechaty/wechaty-puppet)
[![ES Modules](https://img.shields.io/badge/ES-Modules-brightgreen)](https://github.com/Chatie/tsconfig/issues/16)

![chatie puppet](https://wechaty.github.io/wechaty-puppet/images/puppet-logo.jpg)
![chatie puppet](./docs/images/puppet-logo.jpg)

> Picture Credit: [https://www.shareicon.net](https://www.shareicon.net/puppet-marionette-puppeteer-puppet-variant-marionette-variant-665400)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
"@chatie/git-scripts": "^0.6.2",
"@chatie/semver": "^0.4.7",
"@chatie/tsconfig": "^4.6.2",
"@swc/core": "^1.2.113",
"@swc/core": "1.3.44",
"@swc/helpers": "^0.2.14",
"@types/uuid": "^8.3.3",
"nop": "^1.0.0",
Expand Down
2 changes: 1 addition & 1 deletion src/agents/cache-agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class CacheAgent {
*/
const lruOptions = (maxSize = 100): QuickLruOptions<any, any> => ({
maxAge: 15 * 60 * 1000 * 1000, // 15 minutes
maxSize: maxSize,
maxSize,
})

this.contact = new QuickLru<string, ContactPayload>(lruOptions(
Expand Down
2 changes: 1 addition & 1 deletion src/mixins/contact-mixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ const contactMixin = <MixinBase extends CacheMixin & typeof PuppetSkeleton>(mixi
try {
// make sure the contact id has valid payload
await this.contactPayload(query.id)
return [query.id]
return [ query.id ]
} catch (e) {
log.verbose('PuppetContactMixin', 'contactSearch() payload not found for id "%s"', query.id)
await this.contactPayloadDirty(query.id)
Expand Down
4 changes: 2 additions & 2 deletions src/mixins/message-mixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ const messageMixin = <MinxinBase extends typeof PuppetSkeleton & CacheMixin>(bas

messageList (): string[] {
log.verbose('PuppetMessageMixin', 'messageList()')
return [...this.cache.message.keys()]
return [ ...this.cache.message.keys() ]
}

async messageSearch (
Expand All @@ -159,7 +159,7 @@ const messageMixin = <MinxinBase extends typeof PuppetSkeleton & CacheMixin>(bas
try {
// make sure the room id has valid payload
await this.messagePayload(query.id)
return [query.id]
return [ query.id ]
} catch (e) {
log.verbose('PuppetMessageMixin', 'messageSearch() payload not found for id "%s"', query.id)
return []
Expand Down
2 changes: 1 addition & 1 deletion src/mixins/post-mixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ const postMixin = <MinxinBase extends typeof PuppetSkeleton & CacheMixin>(baseMi
*/
postList (): string[] {
log.verbose('PuppetPostMixin', 'postList()')
return [...this.cache.post.keys()]
return [ ...this.cache.post.keys() ]
}

async postPayloadDirty (
Expand Down
34 changes: 21 additions & 13 deletions src/mixins/room-member-mixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const roomMemberMixin = <MixinBase extends typeof PuppetSkeleton & ContactMixin>
async roomMemberSearch (
roomId : string,
query : (symbol | string) | RoomMemberQueryFilter,
memberIdList?: string[],
): Promise<string[]> {
log.verbose('PuppetRoomMemberMixin', 'roomMemberSearch(%s, %s)', roomId, JSON.stringify(query))

Expand All @@ -51,7 +52,7 @@ const roomMemberMixin = <MixinBase extends typeof PuppetSkeleton & ContactMixin>
*/
if (typeof query === 'symbol') {
if (query === YOU) {
return [this.currentUserId]
return [ this.currentUserId ]
}
/**
* Huan(202111): We use `symbol` instead of `uniq symbol` in the method argument
Expand All @@ -64,16 +65,25 @@ const roomMemberMixin = <MixinBase extends typeof PuppetSkeleton & ContactMixin>
throw new Error('unknown symbol found: ' + String(query))
}

/**
* Prevent repeated requests when query is string
* https://github.com/wechaty/puppet/issues/203
*/
if (!memberIdList) {
memberIdList = await this.roomMemberList(roomId)
}

/**
* 1. for Text Query
*/
if (typeof query === 'string') {
let contactIdList: string[] = []
contactIdList = contactIdList.concat(
await this.roomMemberSearch(roomId, { roomAlias: query }),
await this.roomMemberSearch(roomId, { name: query }),
await this.roomMemberSearch(roomId, { contactAlias: query }),
)
const contactIds = await Promise.all([
this.roomMemberSearch(roomId, { roomAlias: query }, memberIdList),
this.roomMemberSearch(roomId, { name: query }, memberIdList),
this.roomMemberSearch(roomId, { contactAlias: query }, memberIdList),
])
contactIdList = contactIdList.concat(...contactIds)
// Keep the unique id only
// https://stackoverflow.com/a/14438954/1123955
// return [...new Set(contactIdList)]
Expand All @@ -85,7 +95,6 @@ const roomMemberMixin = <MixinBase extends typeof PuppetSkeleton & ContactMixin>
/**
* 2. for RoomMemberQueryFilter
*/
const memberIdList = await this.roomMemberList(roomId)

let idList: string[] = []

Expand All @@ -107,13 +116,12 @@ const roomMemberMixin = <MixinBase extends typeof PuppetSkeleton & ContactMixin>
)
}

const memberPayloadList = await Promise.all(
memberIdList.map(
contactId => this.roomMemberPayload(roomId, contactId),
),
)

if (query.roomAlias) {
const memberPayloadList = await Promise.all(
memberIdList.map(
contactId => this.roomMemberPayload(roomId, contactId),
),
)
idList = idList.concat(
memberPayloadList.filter(
payload => payload.roomAlias === query.roomAlias,
Expand Down
20 changes: 10 additions & 10 deletions src/mixins/room-mixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ const roomMixin = <MixinBase extends typeof PuppetSkeleton & ContactMixin & Room
* Room
*
*/
abstract roomAdd (roomId: string, contactId: string, inviteOnly?: boolean) : Promise<void>
abstract roomAvatar (roomId: string) : Promise<FileBoxInterface>
abstract roomCreate (contactIdList: string[], topic?: string) : Promise<string>
abstract roomDel (roomId: string, contactId: string) : Promise<void>
abstract roomList () : Promise<string[]>
abstract roomQRCode (roomId: string) : Promise<string>
abstract roomQuit (roomId: string) : Promise<void>
abstract roomTopic (roomId: string) : Promise<string>
abstract roomTopic (roomId: string, topic: string) : Promise<void>
abstract roomAdd (roomId: string, contactId: string | string[], inviteOnly?: boolean): Promise<void>
abstract roomAvatar (roomId: string) : Promise<FileBoxInterface>
abstract roomCreate (contactIdList: string[], topic?: string) : Promise<string>
abstract roomDel (roomId: string, contactId: string | string[]) : Promise<void>
abstract roomList () : Promise<string[]>
abstract roomQRCode (roomId: string) : Promise<string>
abstract roomQuit (roomId: string) : Promise<void>
abstract roomTopic (roomId: string) : Promise<string>
abstract roomTopic (roomId: string, topic: string) : Promise<void>

/**
* Issue #155 - https://github.com/wechaty/puppet/issues/155
Expand Down Expand Up @@ -74,7 +74,7 @@ const roomMixin = <MixinBase extends typeof PuppetSkeleton & ContactMixin & Room
try {
// make sure the room id has valid payload
await this.roomPayload(query.id)
return [query.id]
return [ query.id ]
} catch (e) {
log.verbose('PuppetRoomMixin', 'roomSearch() payload not found for id "%s"', query.id)
await this.roomPayloadDirty(query.id)
Expand Down
16 changes: 8 additions & 8 deletions src/puppet/puppet-abstract.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ test('contactQueryFilterFunction()', async t => {

void t.test('filter name by regex', async t => {
const QUERY = { name: REGEX_VALUE }
const ID_LIST = ['id1', 'id3']
const ID_LIST = [ 'id1', 'id3' ]

const func = puppet.contactQueryFilterFactory(QUERY)
const idList = PAYLOAD_LIST.filter(func).map(payload => payload.id)
Expand All @@ -92,7 +92,7 @@ test('contactQueryFilterFunction()', async t => {

void t.test('filter name by text', async t => {
const QUERY = { name: TEXT_VALUE }
const ID_LIST = ['id2', 'id4']
const ID_LIST = [ 'id2', 'id4' ]

const func = puppet.contactQueryFilterFactory(QUERY)
const idList = PAYLOAD_LIST.filter(func).map(payload => payload.id)
Expand All @@ -101,7 +101,7 @@ test('contactQueryFilterFunction()', async t => {

void t.test('filter alias by regex', async t => {
const QUERY = { alias: REGEX_VALUE }
const ID_LIST = ['id2', 'id4']
const ID_LIST = [ 'id2', 'id4' ]

const func = puppet.contactQueryFilterFactory(QUERY)
const idList = PAYLOAD_LIST.filter(func).map(payload => payload.id)
Expand All @@ -110,7 +110,7 @@ test('contactQueryFilterFunction()', async t => {

void t.test('filter alias by text', async t => {
const QUERY = { alias: TEXT_VALUE }
const ID_LIST = ['id1', 'id3']
const ID_LIST = [ 'id1', 'id3' ]

const func = puppet.contactQueryFilterFactory(QUERY)
const idList = PAYLOAD_LIST.filter(func).map(payload => payload.id)
Expand All @@ -119,7 +119,7 @@ test('contactQueryFilterFunction()', async t => {

void t.test('filter contact existing id', async t => {
const QUERY = { id: 'id1' }
const ID_LIST = ['id1']
const ID_LIST = [ 'id1' ]

const func = puppet.contactQueryFilterFactory(QUERY)
const idList = PAYLOAD_LIST.filter(func).map(payload => payload.id)
Expand Down Expand Up @@ -186,7 +186,7 @@ test('roomQueryFilterFunction()', async t => {

void t.test('filter name by regex', async t => {
const QUERY = { topic: REGEX_VALUE }
const ID_LIST = ['id2', 'id4']
const ID_LIST = [ 'id2', 'id4' ]

const func = puppet.roomQueryFilterFactory(QUERY)
const idList = PAYLOAD_LIST.filter(func).map(payload => payload.id)
Expand All @@ -195,7 +195,7 @@ test('roomQueryFilterFunction()', async t => {

void t.test('filter name by text', async t => {
const QUERY = { topic: TEXT_VALUE }
const ID_LIST = ['id1', 'id3']
const ID_LIST = [ 'id1', 'id3' ]

const func = puppet.roomQueryFilterFactory(QUERY)
const idList = PAYLOAD_LIST.filter(func).map(payload => payload.id)
Expand All @@ -204,7 +204,7 @@ test('roomQueryFilterFunction()', async t => {

void t.test('filter name by existing id', async t => {
const QUERY = { id: 'id4' }
const ID_LIST = ['id4']
const ID_LIST = [ 'id4' ]

const func = puppet.roomQueryFilterFactory(QUERY)
const idList = PAYLOAD_LIST.filter(func).map(payload => payload.id)
Expand Down
2 changes: 1 addition & 1 deletion src/puppet/puppet-skeleton.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ test('emit(error, ...) with GError', async t => {
'',
'foo',
[],
[1],
[ 1 ],
{},
{ foo: 'bar' },
new Error(),
Expand Down

0 comments on commit 16af2a1

Please sign in to comment.