Repository: Yoctol/bottender
Pull request #805 (2020-06-17 06:01:17)

type(bottender-facebook): add type FeedPageLike
Add type for this kind of event:

```
{
  value: {
    item: 'like',
    verb: 'add',
  },
  field: 'feed',
}
```

codecov-commenter: # [Codecov](https://codecov.io/gh/Yoctol/bottender/pull/805?src=pr&el=h1) Report
> Merging [#805](https://codecov.io/gh/Yoctol/bottender/pull/805?src=pr&el=desc) into [release/1.5](https://codecov.io/gh/Yoctol/bottender/commit/3589b085aa939b7bf66c3b5b8107e86e683f0475&el=desc) will **increase** coverage by `0.02%`.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/Yoctol/bottender/pull/805/graphs/tree.svg?width=650&height=150&src=pr&token=JX3EGtxYH3)](https://codecov.io/gh/Yoctol/bottender/pull/805?src=pr&el=tree)

```diff
@@               Coverage Diff               @@
##           release/1.5     #805      +/-   ##
===============================================
+ Coverage        83.88%   83.90%   +0.02%     
===============================================
  Files              120      120              
  Lines             6154     6159       +5     
  Branches          1520     1523       +3     
===============================================
+ Hits              5162     5168       +6     
+ Misses             988      987       -1     
  Partials             4        4              
```


| [Impacted Files](https://codecov.io/gh/Yoctol/bottender/pull/805?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [packages/bottender-facebook/src/FacebookContext.ts](https://codecov.io/gh/Yoctol/bottender/pull/805/diff?src=pr&el=tree#diff-cGFja2FnZXMvYm90dGVuZGVyLWZhY2Vib29rL3NyYy9GYWNlYm9va0NvbnRleHQudHM=) | `74.69% <100.00%> (+0.62%)` | :arrow_up: |
| [packages/bottender-facebook/src/FacebookEvent.ts](https://codecov.io/gh/Yoctol/bottender/pull/805/diff?src=pr&el=tree#diff-cGFja2FnZXMvYm90dGVuZGVyLWZhY2Vib29rL3NyYy9GYWNlYm9va0V2ZW50LnRz) | `93.89% <100.00%> (+0.92%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/Yoctol/bottender/pull/805?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/Yoctol/bottender/pull/805?src=pr&el=footer). Last update [3589b08...15b1dcf](https://codecov.io/gh/Yoctol/bottender/pull/805?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/packages/bottender-facebook/src/FacebookContext.ts b/packages/bottender-facebook/src/FacebookContext.ts
index ac2f16c7..44d6a6f4 100644
--- a/packages/bottender-facebook/src/FacebookContext.ts
+++ b/packages/bottender-facebook/src/FacebookContext.ts
@@ -75,19 +75,23 @@ export default class FacebookContext extends Context<
       return;
     }
 
-    if (this._event.rawEvent.value.verb === 'remove') {
+    const value = this._event.rawEvent.value as
+      | Types.FeedPost
+      | Types.FeedComment;
+
+    if (value.verb === 'remove') {
       warning(false, "sendText: can't  work with remove verb");
       return;
     }
 
     let recipient;
-    if (this._event.rawEvent.value.item === 'comment') {
+    if (value.item === 'comment') {
       recipient = {
-        commentId: this._event.rawEvent.value.commentId,
+        commentId: value.commentId,
       };
     } else {
       recipient = {
-        postId: this._event.rawEvent.value.postId,
+        postId: value.postId,
       };
     }
 
@@ -119,18 +123,22 @@ export default class FacebookContext extends Context<
       return;
     }
 
-    if (this._event.rawEvent.value.verb === 'remove') {
+    const value = this._event.rawEvent.value as
+      | Types.FeedPost
+      | Types.FeedComment;
+
+    if (value.verb === 'remove') {
       warning(false, "sendMessage: can't  work with remove verb");
     }
 
     let recipient;
-    if (this._event.rawEvent.value.item === 'comment') {
+    if (value.item === 'comment') {
       recipient = {
-        commentId: this._event.rawEvent.value.commentId,
+        commentId: value.commentId,
       };
     } else {
       recipient = {
-        postId: this._event.rawEvent.value.postId,
+        postId: value.postId,
       };
     }
 
diff --git a/packages/bottender-facebook/src/FacebookEvent.ts b/packages/bottender-facebook/src/FacebookEvent.ts
index c4502386..0c6defc0 100644
--- a/packages/bottender-facebook/src/FacebookEvent.ts
+++ b/packages/bottender-facebook/src/FacebookEvent.ts
@@ -219,7 +219,7 @@ export default class FacebookEvent {
 
     if (
       'value' in this.rawEvent &&
-      this.rawEvent.value.from &&
+      'from' in this.rawEvent.value &&
       this.rawEvent.value.from.id === this.pageId
     ) {
       return true;
@@ -235,7 +235,11 @@ export default class FacebookEvent {
       return false;
     }
 
-    if (!('value' in this.rawEvent && this.rawEvent.value.from)) {
+    if (
+      'value' in this.rawEvent &&
+      this.rawEvent.value.item === 'like' &&
+      !('from' in this.rawEvent.value)
+    ) {
       return true;
     }
 
diff --git a/packages/bottender-facebook/src/FacebookTypes.ts b/packages/bottender-facebook/src/FacebookTypes.ts
index a3a9977e..cfa6e913 100644
--- a/packages/bottender-facebook/src/FacebookTypes.ts
+++ b/packages/bottender-facebook/src/FacebookTypes.ts
@@ -23,7 +23,7 @@ export type FeedPost = {
   };
   item: 'post';
   postId: string;
-  verb: 'remove';
+  verb: 'add' | 'edited' | 'remove';
   createdTime: number;
 };
 
@@ -75,6 +75,11 @@ export type FeedShare = {
   [key: string]: any; // FIXME
 };
 
+export type FeedPageLike = {
+  item: 'like';
+  verb: 'add';
+};
+
 export type FacebookRawEvent = {
   field: 'feed';
   value:
@@ -85,7 +90,8 @@ export type FacebookRawEvent = {
     | FeedEvent
     | FeedPhoto
     | FeedVideo
-    | FeedShare;
+    | FeedShare
+    | FeedPageLike;
 };
 
 export type ChangesEntry = {


Test patch:
diff --git a/packages/bottender-facebook/src/__tests__/FacebookEvent.spec.ts b/packages/bottender-facebook/src/__tests__/FacebookEvent.spec.ts
index 0f1dc1bf..259ef91f 100644
--- a/packages/bottender-facebook/src/__tests__/FacebookEvent.spec.ts
+++ b/packages/bottender-facebook/src/__tests__/FacebookEvent.spec.ts
@@ -95,6 +95,14 @@ const commentRemove: FacebookRawEvent = {
   },
 };
 
+const pageLikeAdd: FacebookRawEvent = {
+  value: {
+    item: 'like',
+    verb: 'add',
+  },
+  field: 'feed',
+};
+
 const reactionAdd: FacebookRawEvent = {
   field: 'feed',
   value: {
@@ -670,4 +678,5 @@ it('#isPageLike', () => {
     false
   );
   expect(new FacebookEvent(sentByPage, { pageId }).isPageLike).toEqual(false);
+  expect(new FacebookEvent(pageLikeAdd, { pageId }).isPageLike).toEqual(true);
 });