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

Use flash store replace memory store #57

Merged
merged 6 commits into from
Jan 18, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wechaty-puppet-whatsapp",
"version": "1.11.27",
"version": "1.11.28",
"description": "Wechaty Puppet for WhatsApp",
"type": "module",
"exports": {
Expand Down
110 changes: 101 additions & 9 deletions src/data-manager/cache-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as fs from 'fs-extra'
import * as os from 'os'

import { FlashStore } from 'flash-store'
import type { Message } from '../schema/index.js'
import type { Contact, InviteV4Data, Message } from '../schema/index.js'

import { log } from '../config.js'
import WAError from '../pure-function-helpers/error-type.js'
Expand Down Expand Up @@ -49,7 +49,9 @@ export class CacheManager {
* ************************************************************************
*/
// Static cache, won't change over time
private cacheMessageRawPayload? : FlashStore<string, Message>
private cacheMessageRawPayload?: FlashStore<string, Message>
private cacheContactOrRoomRawPayload?: FlashStore<string, Contact>
private cacheRoomInvitationRawPayload?: FlashStore<string, Partial<InviteV4Data>>

/**
* -------------------------------
Expand Down Expand Up @@ -78,6 +80,90 @@ export class CacheManager {
return this.cacheMessageRawPayload
}

/**
* -------------------------------
* Contact Cache Section
* --------------------------------
*/
public async getContactOrRoomRawPayload (id: string) {
const cache = this.getContactOrRoomCache()
return cache.get(id)
}

public async setContactOrRoomRawPayload (id: string, payload: Contact): Promise<void> {
const cache = this.getContactOrRoomCache()
await cache.set(id, payload)
}

public deleteContactOrRoom (id: string) {
const cache = this.getContactOrRoomCache()
return cache.delete(id)
}

private getContactOrRoomCache () {
if (!this.cacheContactOrRoomRawPayload) {
throw new WAError(WA_ERROR_TYPE.ERR_NO_CACHE, 'getContactOrRoomCache() has no cache')
}
return this.cacheContactOrRoomRawPayload
}

public async getContactIdList () {
const cache = this.getContactOrRoomCache()
const list = []
for await (const key of cache.keys()) {
const value = await cache.get(key)
if (!value) {
continue
}
if (!value.isGroup) {
list.push(value.id._serialized)
}
}
return list
}

public async getRoomIdList () {
const cache = this.getContactOrRoomCache()
const list = []
for await (const key of cache.keys()) {
const value = await cache.get(key)
if (!value) {
continue
}
if (value.isGroup) {
list.push(value.id._serialized)
}
}
return list
}

/**
* -------------------------------
* Room Invitation Cache Section
* --------------------------------
*/
public async getRoomInvitationRawPayload (id: string) {
const cache = this.getRoomInvitationCache()
return cache.get(id)
}

public async setRoomInvitationRawPayload (id: string, payload: Partial<InviteV4Data>): Promise<void> {
const cache = this.getRoomInvitationCache()
await cache.set(id, payload)
}

public deleteRoomInvitation (id: string) {
const cache = this.getRoomInvitationCache()
return cache.delete(id)
}

private getRoomInvitationCache () {
if (!this.cacheRoomInvitationRawPayload) {
throw new WAError(WA_ERROR_TYPE.ERR_NO_CACHE, 'getRoomInvitationCache() has no cache')
}
return this.cacheRoomInvitationRawPayload
}

/**
* -------------------------------
* Private Method Section
Expand All @@ -94,12 +180,9 @@ export class CacheManager {

const baseDir = path.join(
os.homedir(),
path.sep,
'.wechaty',
'puppet-whatsapp',
path.sep,
'flash-store-v0.12',
path.sep,
userId,
)

Expand All @@ -109,24 +192,33 @@ export class CacheManager {
await fs.mkdirp(baseDir)
}

this.cacheMessageRawPayload = new FlashStore(path.join(baseDir, 'message'))
this.cacheMessageRawPayload = new FlashStore(path.join(baseDir, 'message'))
this.cacheContactOrRoomRawPayload = new FlashStore(path.join(baseDir, 'contact-or-room'))
this.cacheRoomInvitationRawPayload = new FlashStore(path.join(baseDir, 'room-invitation'))

const messageTotal = await this.cacheMessageRawPayload.size
const messageTotal = await this.cacheMessageRawPayload.size

log.info(PRE, `initCache() inited Messages: ${messageTotal} cacheDir="${baseDir}"`)
}

private async releaseCache () {
log.verbose(PRE, 'releaseCache()')

if (this.cacheMessageRawPayload) {
if (this.cacheMessageRawPayload
&& this.cacheContactOrRoomRawPayload
&& this.cacheRoomInvitationRawPayload
) {
log.silly(PRE, 'releaseCache() closing caches ...')

await Promise.all([
this.cacheMessageRawPayload.close(),
this.cacheContactOrRoomRawPayload.close(),
this.cacheRoomInvitationRawPayload.close(),
])

this.cacheMessageRawPayload = undefined
this.cacheMessageRawPayload = undefined
this.cacheContactOrRoomRawPayload = undefined
this.cacheRoomInvitationRawPayload = undefined

log.silly(PRE, 'releaseCache() cache closed.')
} else {
Expand Down
61 changes: 61 additions & 0 deletions src/manager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { RequestManager } from './request/requestManager.js'
import type { Client as WhatsApp } from 'whatsapp-web.js'
import { CacheManager } from './data-manager/cache-manager.js'
import { log } from './config.js'
import WAError from './pure-function-helpers/error-type.js'
import { WA_ERROR_TYPE } from './schema/error-type.js'

const PRE = 'WhatsAppManager'

export class Manager {

whatsapp?: WhatsApp
requestManager: RequestManager
cacheManager?: CacheManager

constructor (whatsapp: WhatsApp) {

this.whatsapp = whatsapp
this.requestManager = new RequestManager(this.whatsapp)
}

public async start () {
log.info('start()')
}

public async stop () {
await this.releaseCache()
this.whatsapp = undefined
}

public async getCacheManager () {
if (!this.cacheManager) {
throw new WAError(WA_ERROR_TYPE.ERR_INIT, 'no cache manager')
}
return this.cacheManager
}

public async initCache (userId: string) {
log.info(PRE, `initCache(${userId})`)
if (this.cacheManager) {
log.warn(PRE, 'initCache() already initialized, skip the init...')
return
}
await CacheManager.init(userId)
this.cacheManager = CacheManager.Instance
}

public async releaseCache () {
log.verbose(PRE, 'releaseCache()')
if (this.cacheManager) {
log.warn(PRE, 'releaseCache() already initialized, skip the init...')
return
}
await CacheManager.release()
}

setNickname (nickname: string) {
return this.requestManager.setNickname(nickname)
}

}
Loading