Skip to content

Commit

Permalink
Merge pull request #19 from juzibot/feat-tag-cache
Browse files Browse the repository at this point in the history
Feat tag cache
  • Loading branch information
hcfw007 authored Jul 29, 2022
2 parents 57a2595 + 9835752 commit 82c10d1
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 31 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@juzi/wechaty",
"version": "1.0.19",
"version": "1.0.20",
"description": "Wechaty is a RPA SDK for Chatbot Makers.",
"type": "module",
"exports": {
Expand Down Expand Up @@ -109,7 +109,7 @@
},
"homepage": "https://github.com/wechaty/",
"dependencies": {
"@juzi/wechaty-puppet": "^1.0.12",
"@juzi/wechaty-puppet": "^1.0.14",
"@juzi/wechaty-puppet-service": "^1.0.17",
"clone-class": "^1.1.1",
"cmd-ts": "^0.10.0",
Expand Down
17 changes: 13 additions & 4 deletions src/user-modules/contact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -687,10 +687,19 @@ class ContactMixin extends MixinBase implements SayableSayer {
* Add a Tag
*/

async tag (tag: TagInterface): Promise<void> {
log.verbose('Contact', 'tag(%s) for %s', tag, this)

await this.wechaty.puppet.tagContactTagAdd(tag.groupId(), tag.id(), this.id)
async tag (tags: TagInterface | TagInterface[]): Promise<void> {
log.verbose('Contact', 'tag(%s) for %s', tags, this)

let tagIds: string[]
let tagGroupIds: (string | undefined)[]
if (Array.isArray(tags)) {
tagIds = tags.map(c => c.id())
tagGroupIds = tags.map(c => c.groupId())
} else {
tagIds = [tags.id()]
tagGroupIds = [tags.groupId()]
}
await this.wechaty.puppet.tagContactTagAdd(tagGroupIds, tagIds, this.id)

}

Expand Down
13 changes: 10 additions & 3 deletions src/user-modules/room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -728,9 +728,16 @@ class RoomMixin extends MixinBase implements SayableSayer {
* }
* }
*/
async remove (contact: ContactInterface): Promise<void> {
log.verbose('Room', 'del(%s)', contact)
await this.wechaty.puppet.roomDel(this.id, contact.id)
async remove (contacts: ContactInterface | ContactInterface[]): Promise<void> {
log.verbose('Room', 'del(%s)', contacts)

let contactIds: string[]
if (Array.isArray(contacts)) {
contactIds = contacts.map(c => c.id)
} else {
contactIds = [contacts.id]
}
await this.wechaty.puppet.roomDel(this.id, contactIds)
// this.delLocal(contact)
}

Expand Down
58 changes: 45 additions & 13 deletions src/user-modules/tag-group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { validationMixin } from '../user-mixins/validation.js'
import {
wechatifyMixinBase,
} from '../user-mixins/wechatify.js'
import type { TagInterface } from './tag.js'

class TagGroupMixin extends wechatifyMixinBase() {

Expand Down Expand Up @@ -58,13 +59,52 @@ class TagGroupMixin extends wechatifyMixinBase() {
return this.payload.name
}

private static pool: TagGroupInterface[] = []

static async list (forceSync = false): Promise<TagGroupInterface[]> {
log.verbose('TagGroup', 'list(%s)', forceSync)

if (this.pool.length > 0 && !forceSync) {
return this.pool
}

try {
const payloads = await this.wechaty.puppet.tagGroupList()
this.pool = payloads.map(payload => new this(payload))
return this.pool
} catch (e) {
this.wechaty.emitError(e)
log.error('TagGroup', 'list() exception: %s', (e as Error).message)
return []
}
}

static async sync (): Promise<void> {
log.verbose('TagGroup', 'sync()')

await this.list(true)
}

static load (tagGroupId: string): TagGroupInterface | undefined {
log.verbose('TagGroup', 'load(%s)', tagGroupId)

for (const item of this.pool) {
if (item.id() === tagGroupId) {
return item
}
}
return undefined
}

static async createTagGroup (name: string): Promise<TagGroupInterface | void> {
log.verbose('TagGroup', 'createTagGroup(%s, %s)', name)

try {
const payload = await this.wechaty.puppet.tagGroupAdd(name)
if (payload) {
return new this(payload)
const newTagGroup = new this(payload)
this.pool.push(newTagGroup)
return newTagGroup
}
} catch (e) {
this.wechaty.emitError(e)
Expand All @@ -77,23 +117,15 @@ class TagGroupMixin extends wechatifyMixinBase() {

try {
await this.wechaty.puppet.tagGroupDelete(tagGroup.id())
this.pool.splice(this.pool.indexOf(tagGroup), 1)
} catch (e) {
this.wechaty.emitError(e)
log.error('Contact', 'createTag() exception: %s', (e as Error).message)
log.error('TagGroup', 'deleteTagGroup() exception: %s', (e as Error).message)
}
}

static async list (): Promise<TagGroupInterface[]> {
log.verbose('TagGroup', 'list()')

try {
const payloads = await this.wechaty.puppet.tagGroupList()
return payloads.map(payload => new this(payload))
} catch (e) {
this.wechaty.emitError(e)
log.error('TagGroup', 'list() exception: %s', (e as Error).message)
return []
}
async tags (): Promise<TagInterface[]> {
return (await this.wechaty.Tag.list()).filter(tag => tag.groupId() === this.id())
}

}
Expand Down
55 changes: 46 additions & 9 deletions src/user-modules/tag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
wechatifyMixinBase,
} from '../user-mixins/wechatify.js'
import type { ContactInterface } from './contact.js'
import type { TagGroupInterface } from './tag-group.js'

class TagMixin extends wechatifyMixinBase() {

Expand Down Expand Up @@ -67,19 +68,47 @@ class TagMixin extends wechatifyMixinBase() {
return this.payload.name
}

static async list (): Promise<TagInterface[]> {
log.verbose('Tag', 'list()')
group (): TagGroupInterface | undefined {
return this.wechaty.TagGroup.load(this.groupId())
}

private static pool: TagInterface[] = []

static async list (forceSync = false): Promise<TagInterface[]> {
log.verbose('Tag', 'list(%s)', forceSync)

if (this.pool.length > 0 && !forceSync) {
return this.pool
}

try {
const payloads = await this.wechaty.puppet.tagTagList()
return payloads.map(payload => new this(payload))
this.pool = payloads.map(payload => new this(payload))
return this.pool
} catch (e) {
this.wechaty.emitError(e)
log.error('Tag', 'list() exception: %s', (e as Error).message)
return []
}
}

static async sync (): Promise<void> {
log.verbose('Tag', 'sync()')

await this.list(true)
}

static load (tagGroupId: string | undefined, tagId: string): TagInterface | undefined {
log.verbose('TagGroup', 'load(%s, %s)', tagGroupId, tagId)

for (const item of this.pool) {
if (item.id() === tagId && (item.groupId() === tagGroupId || (!item.groupId() && !tagGroupId))) {
return item
}
}
return undefined
}

async contactList (): Promise<ContactInterface[]> {
log.verbose('Tag', 'contactList() for tag id: %s', this.id())

Expand All @@ -88,11 +117,16 @@ class TagMixin extends wechatifyMixinBase() {
return Promise.all(contactPromises)
}

async tag (contact: ContactInterface): Promise<void> {
log.verbose('Tag', 'tag(%s) for tag id: %s', contact, this.id())
async tag (contacts: ContactInterface | ContactInterface[]): Promise<void> {
log.verbose('Tag', 'tag(%s) for tag id: %s', contacts, this.id())

const contactId = contact.id
await this.wechaty.puppet.tagContactTagAdd(this.groupId(), this.id(), contactId)
let contactIds: string[]
if (Array.isArray(contacts)) {
contactIds = contacts.map(c => c.id)
} else {
contactIds = [contacts.id]
}
await this.wechaty.puppet.tagContactTagAdd(this.groupId(), this.id(), contactIds)
}

static async createTag (tagGroupId: string | undefined, name: string): Promise<TagInterface | void> {
Expand All @@ -101,7 +135,9 @@ class TagMixin extends wechatifyMixinBase() {
try {
const payload = await this.wechaty.puppet.tagTagAdd(tagGroupId, name)
if (payload) {
return new this(payload)
const newTag = new this(payload)
this.pool.push(newTag)
return newTag
}
} catch (e) {
this.wechaty.emitError(e)
Expand All @@ -114,9 +150,10 @@ class TagMixin extends wechatifyMixinBase() {

try {
await this.wechaty.puppet.tagTagDelete(tag.groupId(), tag.id())
this.pool.splice(this.pool.indexOf(tag), 1)
} catch (e) {
this.wechaty.emitError(e)
log.error('Contact', 'deleteTag() exception: %s', (e as Error).message)
log.error('Tag', 'deleteTag() exception: %s', (e as Error).message)
}
}

Expand Down

0 comments on commit 82c10d1

Please sign in to comment.