Repository: Yoctol/bottender
Pull request #210 (2018-03-17 02:16:58)

implement ViberContext getUserDetails and getOnlineStatus
```js
const user = await context.getUserDetails();
const status = await context.getOnlineStatus();
```


Patch:
diff --git a/src/context/ViberContext.js b/src/context/ViberContext.js
index 5fdfecdb..12c4f271 100644
--- a/src/context/ViberContext.js
+++ b/src/context/ViberContext.js
@@ -45,7 +45,7 @@ class ViberContext extends Context implements PlatformContext {
   }
 
   /**
-   * Send text to the owner of then session.
+   * Send text to the owner of the session.
    *
    */
   async sendText(text: string, options?: Object): Promise<any> {
@@ -61,6 +61,39 @@ class ViberContext extends Context implements PlatformContext {
 
     return this._client.sendText(this._session.user.id, text, options);
   }
+
+  /**
+   * Get user details from the owner of the session.
+   *
+   */
+  async getUserDetails(): Promise<?Object> {
+    if (!this._session) {
+      warning(
+        false,
+        'getUserDetails: should not be called in context without session'
+      );
+      return null;
+    }
+
+    return this._client.getUserDetails(this._session.user.id);
+  }
+
+  /**
+   * Get user online status from the owner of the session.
+   *
+   */
+  async getOnlineStatus(): Promise<?Object> {
+    if (!this._session) {
+      warning(
+        false,
+        'getOnlineStatus: should not be called in context without session'
+      );
+      return null;
+    }
+
+    const status = await this._client.getOnlineStatus([this._session.user.id]);
+    return status[0];
+  }
 }
 
 const sendMethods = [


Test patch:
diff --git a/src/context/__tests__/ViberContext.spec.js b/src/context/__tests__/ViberContext.spec.js
index 48d886b8..6fcf1c31 100644
--- a/src/context/__tests__/ViberContext.spec.js
+++ b/src/context/__tests__/ViberContext.spec.js
@@ -17,6 +17,7 @@ beforeEach(() => {
 });
 
 const createMockViberClient = () => ({
+  getUserDetails: jest.fn(),
   sendMessage: jest.fn(),
   sendText: jest.fn(),
   sendPicture: jest.fn(),
@@ -27,6 +28,7 @@ const createMockViberClient = () => ({
   sendURL: jest.fn(),
   sendSticker: jest.fn(),
   sendCarouselContent: jest.fn(),
+  getOnlineStatus: jest.fn(),
 });
 
 const rawEvent = {
@@ -492,7 +494,69 @@ describe('#sendCarouselContent', () => {
 
     await context.sendCarouselContent(richMedia);
 
-    expect(client.sendSticker).not.toBeCalled();
+    expect(client.sendCarouselContent).not.toBeCalled();
+  });
+});
+
+describe('#getUserDetails', () => {
+  it('should call client.getUserDetails', async () => {
+    const { context, client, session } = setup();
+
+    const user = {
+      id: '01234567890A=',
+      name: 'John McClane',
+      avatar: 'http://avatar.example.com',
+      country: 'UK',
+      language: 'en',
+      primary_device_os: 'android 7.1',
+      api_version: 1,
+      viber_version: '6.5.0',
+      mcc: 1,
+      mnc: 1,
+      device_type: 'iPhone9,4',
+    };
+
+    client.getUserDetails.mockReturnValue(Promise.resolve(user));
+
+    const result = await context.getUserDetails();
+
+    expect(client.getUserDetails).toBeCalledWith(session.user.id);
+    expect(result).toEqual(user);
+  });
+
+  it('should not call send method if dont have session', async () => {
+    const { context, client } = setup({ session: undefined });
+
+    await context.getUserDetails();
+
+    expect(client.getUserDetails).not.toBeCalled();
+  });
+});
+
+describe('#getOnlineStatus', () => {
+  it('should call client.getOnlineStatus', async () => {
+    const { context, client, session } = setup();
+
+    const user = {
+      id: '01234567890=',
+      online_status: 0,
+      online_status_message: 'online',
+    };
+
+    client.getOnlineStatus.mockReturnValue(Promise.resolve([user]));
+
+    const result = await context.getOnlineStatus();
+
+    expect(client.getOnlineStatus).toBeCalledWith([session.user.id]);
+    expect(result).toEqual(user);
+  });
+
+  it('should not call send method if dont have session', async () => {
+    const { context, client } = setup({ session: undefined });
+
+    await context.getOnlineStatus();
+
+    expect(client.getOnlineStatus).not.toBeCalled();
   });
 });