Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/channel message wechaty #212

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wechaty-puppet",
"version": "1.21.1",
"version": "1.21.2",
"description": "Abstract Puppet for Wechaty",
"type": "module",
"exports": {
Expand Down 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 ]
hcfw007 marked this conversation as resolved.
Show resolved Hide resolved
} catch (e) {
log.verbose('PuppetContactMixin', 'contactSearch() payload not found for id "%s"', query.id)
await this.contactPayloadDirty(query.id)
Expand Down
9 changes: 7 additions & 2 deletions src/mixins/message-mixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import {
type SayablePayload,
sayableTypes,
} from '../schemas/sayable.js'
import type { ChannelPayload } from '../schemas/channel.js'

const filebox = (filebox: string | FileBoxInterface) => typeof filebox === 'string' ? FileBox.fromJSON(filebox) : filebox

Expand Down Expand Up @@ -67,6 +68,7 @@ const messageMixin = <MinxinBase extends typeof PuppetSkeleton & CacheMixin>(bas
abstract messageMiniProgram (messageId: string) : Promise<MiniProgramPayload>
abstract messageUrl (messageId: string) : Promise<UrlLinkPayload>
abstract messageLocation (messageId: string) : Promise<LocationPayload>
abstract messageChannel (messageId: string) : Promise<ChannelPayload>

abstract messageForward (conversationId: string, messageId: string,) : Promise<void | string>
abstract messageSendContact (conversationId: string, contactId: string) : Promise<void | string>
Expand All @@ -76,6 +78,7 @@ const messageMixin = <MinxinBase extends typeof PuppetSkeleton & CacheMixin>(bas
abstract messageSendPost (conversationId: string, postPayload: PostPayload) : Promise<void | string>
abstract messageSendText (conversationId: string, text: string, mentionIdList?: string[]) : Promise<void | string>
abstract messageSendUrl (conversationId: string, urlLinkPayload: UrlLinkPayload) : Promise<void | string>
abstract messageSendChannel (conversationId: string, channelPayload: ChannelPayload) : Promise<void | string>

abstract messageRecall (messageId: string) : Promise<boolean>

Expand Down Expand Up @@ -144,7 +147,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 +162,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 Expand Up @@ -280,6 +283,8 @@ const messageMixin = <MinxinBase extends typeof PuppetSkeleton & CacheMixin>(bas
return this.messageSendText(conversationId, sayable.payload.text, sayable.payload.mentions)
case sayableTypes.Post:
return this.messageSendPost(conversationId, sayable.payload)
case sayableTypes.Channel:
return this.messageSendChannel(conversationId, sayable.payload)
default:
throw new Error('unsupported sayable payload: ' + JSON.stringify(sayable))
}
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
2 changes: 1 addition & 1 deletion src/mixins/room-member-mixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,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 Down
2 changes: 1 addition & 1 deletion src/mixins/room-mixin.ts
Original file line number Diff line number Diff line change
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
4 changes: 4 additions & 0 deletions src/mixins/sayable-mixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ const sayableMixin = <MixinBase extends typeof PuppetSkeleton & MessageMixin & P
const postPayload = await this.postPayload(sayableId)
return sayablePayloads.post(postPayload)
}
case MessageType.Channel: {
const channelPayload = await this.messageChannel(sayableId)
return sayablePayloads.channel(channelPayload)
}

default:
log.warn('PuppetSayableMixin',
Expand Down
2 changes: 2 additions & 0 deletions src/mods/payloads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import type {
TapPayload,
UrlLinkPayload,
SayablePayload,
ChannelPayload,
} from '../schemas/mod.js'
import {
sayablePayloads,
Expand Down Expand Up @@ -79,6 +80,7 @@ export type {
SayablePayload as Sayable,
TapPayload as Tap,
UrlLinkPayload as UrlLink,
ChannelPayload as Channel,
}
export {
sayablePayloads as sayable, // Sayable payload creators
Expand Down
2 changes: 2 additions & 0 deletions src/mods/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
PostType,
TapType,
sayableTypes,
ChannelType,

CHAT_EVENT_DICT,
PUPPET_EVENT_DICT,
Expand All @@ -32,6 +33,7 @@ export {
PostType as Post,
TapType as Tap,
sayableTypes as Sayable,
ChannelType as Channel,
/**
* Huan(202201): `DirtyType as Payload` will be removed after Dec 31, 2023
* @deprecated: use Dirty instead of Payload
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
19 changes: 19 additions & 0 deletions src/schemas/channel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export interface ChannelPayload {
avatar: string,
coverUrl: string,
desc: string,
extras: string,
feedType: number,
nickname: string,
thumbUrl: string,
url: string,
objectId?: string,
objectNonceId?: string,
}

export enum ChannelType {
Unknown = 0,
Photo = 2,
Video = 4,
Live = 9,
hcfw007 marked this conversation as resolved.
Show resolved Hide resolved
}
1 change: 1 addition & 0 deletions src/schemas/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export enum MessageType {
Url = 14, // Url(5)
Video = 15, // Video(4), Video(43)
Post = 16, // Moment, Channel, Tweet, etc
Channel = 17, // Channel
}

/**
Expand Down
7 changes: 7 additions & 0 deletions src/schemas/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ import type {
ChatEventName,
} from './puppet.js'

import {
type ChannelPayload,
ChannelType,
} from './channel.js'

import {
sayablePayloads,
sayableTypes,
Expand Down Expand Up @@ -121,6 +126,7 @@ export {
sayableTypes,
ScanStatus,
TapType,
ChannelType,
type ChatEventName,
type ContactPayload,
type ContactQueryFilter,
Expand Down Expand Up @@ -168,5 +174,6 @@ export {
type TapPayload,
type TapQueryFilter,
type UrlLinkPayload,
type ChannelPayload,
YOU,
}
4 changes: 4 additions & 0 deletions src/schemas/sayable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { MessageType } from './message.js'
import type { LocationPayload } from './location.js'
import type { UrlLinkPayload } from './url-link.js'
import type { MiniProgramPayload } from './mini-program.js'
import type { ChannelPayload } from './channel.js'
import type {
PostPayload,
SayablePayloadPost,
Expand All @@ -21,6 +22,7 @@ const payloadLocation = (locationPayload: LocationPayload) => ({ ...loc
const payloadMiniProgram = (miniProgramPayload: MiniProgramPayload) => ({ ...miniProgramPayload })
const payloadUrlLink = (urlLinkPayload: UrlLinkPayload) => ({ ...urlLinkPayload })
const payloadPost = (postPayload: PostPayload) => ({ ...postPayload })
const payloadChannel = (channelPayload: ChannelPayload) => ({ ...channelPayload })

/**
* using `types` as a static typed string name list for `createAction`
Expand Down Expand Up @@ -61,6 +63,7 @@ const location = createAction(sayableTypes.Location, payloadLocation)()
const miniProgram = createAction(sayableTypes.MiniProgram, payloadMiniProgram)()
const url = createAction(sayableTypes.Url, payloadUrlLink)()
const post = createAction(sayableTypes.Post, payloadPost)()
const channel = createAction(sayableTypes.Channel, payloadChannel)()

/**
* Huan(202201): Recursive type references
Expand All @@ -77,6 +80,7 @@ const sayablePayloadsNoPost = {
text,
url,
video,
channel,
} as const

/**
Expand Down