Repository: Yoctol/bottender
Pull request #18 (2017-11-11 07:02:32)

implement label api methods for targeting broadcast messages
- `context.associateLabel(labelId)`
- `context.dissociateLabel(labelId)`
- `context.getAssociatedLabels()`


Patch:
diff --git a/src/context/MessengerContext.js b/src/context/MessengerContext.js
index 949ee232..5ccdd5fd 100644
--- a/src/context/MessengerContext.js
+++ b/src/context/MessengerContext.js
@@ -69,6 +69,15 @@ class MessengerContext extends Context implements PlatformContext {
     });
   }
 
+  /**
+   * Sender Actions
+   *
+   * https://github.com/Yoctol/messaging-apis/tree/master/packages/messaging-api-messenger#sender-actions
+   */
+
+  /**
+   * https://github.com/Yoctol/messaging-apis/tree/master/packages/messaging-api-messenger#typingonuserid
+   */
   async typingOn(): Promise<any> {
     if (!this._session) {
       warning(
@@ -83,6 +92,9 @@ class MessengerContext extends Context implements PlatformContext {
     return this._client.typingOn(this._session.user.id);
   }
 
+  /**
+   * https://github.com/Yoctol/messaging-apis/tree/master/packages/messaging-api-messenger#typingoffuserid
+   */
   async typingOff(): Promise<any> {
     if (!this._session) {
       warning(
@@ -97,6 +109,9 @@ class MessengerContext extends Context implements PlatformContext {
     return this._client.typingOff(this._session.user.id);
   }
 
+  /**
+   * https://github.com/Yoctol/messaging-apis/tree/master/packages/messaging-api-messenger#markseenuserid
+   */
   async markSeen(): Promise<any> {
     if (!this._session) {
       warning(
@@ -111,6 +126,15 @@ class MessengerContext extends Context implements PlatformContext {
     return this._client.markSeen(this._session.user.id);
   }
 
+  /**
+   * Handover Protocol
+   *
+   * https://github.com/Yoctol/messaging-apis/tree/master/packages/messaging-api-messenger#handover-protocol-api
+   */
+
+  /**
+   * https://github.com/Yoctol/messaging-apis/tree/master/packages/messaging-api-messenger#passthreadcontroluserid-targetappid-metadata---official-docs
+   */
   async passThreadControl(
     targetAppId: number,
     metadata?: string
@@ -132,6 +156,9 @@ class MessengerContext extends Context implements PlatformContext {
     );
   }
 
+  /**
+   * https://github.com/Yoctol/messaging-apis/tree/master/packages/messaging-api-messenger#passthreadcontroltopageinboxuserid-metadata---official-docs
+   */
   async passThreadControlToPageInbox(): Promise<any> {
     if (!this._session) {
       warning(
@@ -146,6 +173,9 @@ class MessengerContext extends Context implements PlatformContext {
     return this._client.passThreadControlToPageInbox(this._session.user.id);
   }
 
+  /**
+   * https://github.com/Yoctol/messaging-apis/tree/master/packages/messaging-api-messenger#takethreadcontroluserid-metadata---official-docs
+   */
   async takeThreadControl(): Promise<any> {
     if (!this._session) {
       warning(
@@ -159,6 +189,57 @@ class MessengerContext extends Context implements PlatformContext {
 
     return this._client.takeThreadControl(this._session.user.id);
   }
+
+  /**
+   * Targeting Broadcast Messages
+   *
+   * https://github.com/Yoctol/messaging-apis/tree/master/packages/messaging-api-messenger#targeting-broadcast-messages---official-docs
+   */
+
+  /**
+   * https://github.com/Yoctol/messaging-apis/tree/master/packages/messaging-api-messenger#associatelabeluserid-labelid
+   */
+  async associateLabel(labelId: number): Promise<any> {
+    if (!this._session) {
+      warning(
+        false,
+        'associateLabel: should not be called in context without session'
+      );
+      return;
+    }
+
+    return this._client.associateLabel(this._session.user.id, labelId);
+  }
+
+  /**
+   * https://github.com/Yoctol/messaging-apis/tree/master/packages/messaging-api-messenger#dissociatelabeluserid-labelid
+   */
+  async dissociateLabel(labelId: number): Promise<any> {
+    if (!this._session) {
+      warning(
+        false,
+        'dissociateLabel: should not be called in context without session'
+      );
+      return;
+    }
+
+    return this._client.dissociateLabel(this._session.user.id, labelId);
+  }
+
+  /**
+   * https://github.com/Yoctol/messaging-apis/tree/master/packages/messaging-api-messenger#getassociatedlabelsuserid
+   */
+  async getAssociatedLabels(): Promise<any> {
+    if (!this._session) {
+      warning(
+        false,
+        'dissociateLabel: should not be called in context without session'
+      );
+      return;
+    }
+
+    return this._client.getAssociatedLabels(this._session.user.id);
+  }
 }
 
 const sendMethods = [


Test patch:
diff --git a/src/context/__tests__/MessengerContext.spec.js b/src/context/__tests__/MessengerContext.spec.js
index 292a7449..f92d0ec1 100644
--- a/src/context/__tests__/MessengerContext.spec.js
+++ b/src/context/__tests__/MessengerContext.spec.js
@@ -43,6 +43,9 @@ const createMockGraphAPIClient = () => ({
   passThreadControl: jest.fn(),
   passThreadControlToPageInbox: jest.fn(),
   takeThreadControl: jest.fn(),
+  associateLabel: jest.fn(),
+  dissociateLabel: jest.fn(),
+  getAssociatedLabels: jest.fn(),
 });
 
 const rawEvent = {
@@ -733,3 +736,87 @@ describe('#takeThreadControl', () => {
     expect(client.takeThreadControl).not.toBeCalled();
   });
 });
+
+describe('#associateLabel', () => {
+  it('should call api to associate label to the user', async () => {
+    const { context, client, session } = setup();
+
+    await context.associateLabel(1712444532121303);
+
+    expect(client.associateLabel).toBeCalledWith(
+      session.user.id,
+      1712444532121303
+    );
+  });
+
+  it('should call warning if dont have session', async () => {
+    const { context, client } = setup({ session: false });
+
+    await context.associateLabel(1712444532121303);
+
+    expect(warning).toBeCalled();
+    expect(client.associateLabel).not.toBeCalled();
+  });
+});
+
+describe('#dissociateLabel', () => {
+  it('should call api to dissociate label from the user', async () => {
+    const { context, client, session } = setup();
+
+    await context.dissociateLabel(1712444532121303);
+
+    expect(client.dissociateLabel).toBeCalledWith(
+      session.user.id,
+      1712444532121303
+    );
+  });
+
+  it('should call warning if dont have session', async () => {
+    const { context, client } = setup({ session: false });
+
+    await context.dissociateLabel(1712444532121303);
+
+    expect(warning).toBeCalled();
+    expect(client.dissociateLabel).not.toBeCalled();
+  });
+});
+
+describe('#getAssociatedLabels', () => {
+  it('should call api to dissociate label from the user', async () => {
+    const { context, client, session } = setup();
+
+    client.getAssociatedLabels.mockReturnValue({
+      data: [
+        {
+          name: 'myLabel',
+          id: '1001200005003',
+        },
+        {
+          name: 'myOtherLabel',
+          id: '1001200005002',
+        },
+      ],
+      paging: {
+        cursors: {
+          before:
+            'QVFIUmx1WTBpMGpJWXprYzVYaVhabW55dVpycko4U2xURGE5ODNtNFZAPal94a1hTUnNVMUtoMVVoTzlzSDktUkMtQkUzWEFLSXlMS3ZALYUw3TURLelZAPOGVR',
+          after:
+            'QVFIUmItNkpTbjVzakxFWGRydzdaVUFNNnNPaUl0SmwzVHN5ZAWZAEQ3lZANDAzTXFIM0NHbHdYSkQ5OG1GaEozdjkzRmxpUFhxTDl4ZAlBibnE4LWt1eGlTa3Bn',
+        },
+      },
+    });
+
+    await context.getAssociatedLabels();
+
+    expect(client.getAssociatedLabels).toBeCalledWith(session.user.id);
+  });
+
+  it('should call warning if dont have session', async () => {
+    const { context, client } = setup({ session: false });
+
+    await context.getAssociatedLabels();
+
+    expect(warning).toBeCalled();
+    expect(client.getAssociatedLabels).not.toBeCalled();
+  });
+});