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

Delay friendship #1891

Merged
merged 8 commits into from Jan 30, 2020
Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
64 changes: 64 additions & 0 deletions src/user/friendship.ts
Expand Up @@ -357,4 +357,68 @@ export class Friendship extends Accessory implements Acceptable {
: FriendshipType.Unknown
}

/**
* get friendShipPayload Json
* @returns {FriendshipPayload}
*
* @example
* const bot = new Wechaty()
* bot.on('friendship', async friendship => {
* try {
* const payload = await friendship.toJson()
* } catch (e) {
* console.error(e)
* }
* }
* .start()
*/
public async toJson (): Promise<string> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

toJSON() behavior

If an object being stringified has a property named toJSON whose value is a function, then the toJSON() method customizes JSON stringification behavior: instead of the object being serialized, the value returned by the toJSON() method when called will be serialized. JSON.stringify() calls toJSON with one parameter:

  • if this object is a property value, the property name
  • if it is in an array, the index in the array, as a string
  • an empty string if JSON.stringify() was directly called on this object

The method name should be named properly as toJSON() with a return type string instead of a Promise.

if (!this.isReady()) {
await this.ready()
}

if (!this.payload) {
throw new Error('no payload')
}

return JSON.stringify(this.payload)
}

/**
* create friendShip by friendshipJson
* @example
* const bot = new Wechaty()
* bot.on('friendship', async friendship => {
* try {
* const friendshipToBeSaved = await friendship.toJson()
* saveFriendship(friendshipToBeSaved)
* } catch (e) {
* console.error(e)
* }
* }
* .start()
*
* const friendshipFromDisk = getFriendshipFromDisk()
* const newFriendship = bot.FriendShip.fromJson(friendshipFromDisk)
* await newFriendship.accept()
*/
public static async fromJson (friendship: string): Promise<Friendship> {
const friendshipPayload: FriendshipPayload = JSON.parse(friendship)
const newFriendship = this.wechaty.Friendship.load(friendshipPayload.id)
await newFriendship.ready()

if (newFriendship.isReady()) {
return newFriendship
}

const payload = await newFriendship.puppet.friendshipSave(friendshipPayload)
if (!payload) {
throw new Error('no payload')
}

newFriendship.payload = payload

return newFriendship
}

}