001/*
002 * Copyright (c) 2010-2020 Mark Allen, Norbert Bartels.
003 *
004 * Permission is hereby granted, free of charge, to any person obtaining a copy
005 * of this software and associated documentation files (the "Software"), to deal
006 * in the Software without restriction, including without limitation the rights
007 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008 * copies of the Software, and to permit persons to whom the Software is
009 * furnished to do so, subject to the following conditions:
010 *
011 * The above copyright notice and this permission notice shall be included in
012 * all copies or substantial portions of the Software.
013 *
014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
020 * THE SOFTWARE.
021 */
022package com.restfb.webhook;
023
024import com.restfb.types.webhook.*;
025import com.restfb.types.webhook.instagram.InstagramCommentsValue;
026import com.restfb.types.webhook.instagram.InstagramMentionsValue;
027import com.restfb.types.webhook.instagram.InstagramStoryInsightsValue;
028
029public class Webhook {
030
031  private WebhookMessagingListener messagingListener;
032
033  private WebhookChangeListener eventListener;
034
035  /**
036   * registers an listener that handles the messaging Items of an incoming webhook request
037   * 
038   * @param listener
039   */
040  public void registerListener(WebhookMessagingListener listener) {
041    this.messagingListener = listener;
042  }
043
044  /**
045   * registers a listener that handles the change values of an incoming webhook request
046   * 
047   * @param listener
048   */
049  public void registerListener(WebhookChangeListener listener) {
050    this.eventListener = listener;
051  }
052
053  public void process(WebhookObject webhookObject) {
054    if (messagingListener != null) {
055      handleMessaging(webhookObject);
056    }
057    if (eventListener != null) {
058      handleEvents(webhookObject);
059    }
060  }
061
062  private void handleEvents(WebhookObject webhookObject) {
063    webhookObject.getEntryList().stream().flatMap(entry -> entry.getChanges().stream()).forEach(change -> {
064      if (change.getValue() instanceof FeedCommentValue) {
065        eventListener.feedCommentValue(change.getValue().convertChangeValue(FeedCommentValue.class));
066      }
067
068      if (change.getValue() instanceof FeedPhotoAddValue) {
069        eventListener.feedPhotoAddValue(change.getValue().convertChangeValue(FeedPhotoAddValue.class));
070      }
071
072      if (change.getValue() instanceof FeedPhotoRemoveValue) {
073        eventListener.feedPhotoRemoveValue(change.getValue().convertChangeValue(FeedPhotoRemoveValue.class));
074      }
075
076      if (change.getValue() instanceof FeedVideoValue) {
077        eventListener.feedVideoValue(change.getValue().convertChangeValue(FeedVideoValue.class));
078      }
079
080      if (change.getValue() instanceof FeedVideoRemoveValue) {
081        eventListener.feedVideoRemoveValue(change.getValue().convertChangeValue(FeedVideoRemoveValue.class));
082      }
083
084      if (change.getValue() instanceof FeedStatusValue) {
085        eventListener.feedStatusValue(change.getValue().convertChangeValue(FeedStatusValue.class));
086      }
087
088      if (change.getValue() instanceof FeedPostValue) {
089        eventListener.feedPostValue(change.getValue().convertChangeValue(FeedPostValue.class));
090      }
091
092      if (change.getValue() instanceof FeedVideoBlockMute) {
093        eventListener.feedVideoBlockMute(change.getValue().convertChangeValue(FeedVideoBlockMute.class));
094      }
095
096      if (change.getValue() instanceof FeedReactionValue) {
097        eventListener.feedReactionValue(change.getValue().convertChangeValue(FeedReactionValue.class));
098      }
099
100      if (change.getValue() instanceof FeedShareValue) {
101        eventListener.feedShareValue(change.getValue().convertChangeValue(FeedShareValue.class));
102      }
103
104      if (change.getValue() instanceof FeedAlbumEditedValue) {
105        eventListener.feedAlbumEditedValue(change.getValue().convertChangeValue(FeedAlbumEditedValue.class));
106      }
107
108      if (change.getValue() instanceof FeedEventValue) {
109        eventListener.feedEventValue(change.getValue().convertChangeValue(FeedEventValue.class));
110      }
111
112      if (change.getValue() instanceof FeedLikeValue) {
113        eventListener.feedLikeValue(change.getValue().convertChangeValue(FeedLikeValue.class));
114      }
115
116      if (change.getValue() instanceof FeedAlbumAddValue) {
117        eventListener.feedAlbumAddValue(change.getValue().convertChangeValue(FeedAlbumAddValue.class));
118      }
119
120      if (change.getValue() instanceof RatingsRatingValue) {
121        eventListener.ratingsRatingValue(change.getValue().convertChangeValue(RatingsRatingValue.class));
122      }
123      if (change.getValue() instanceof MentionPostAddValue) {
124        eventListener.mentionPostAddValue(change.getValue().convertChangeValue(MentionPostAddValue.class));
125      }
126
127      if (change.getValue() instanceof RatingsCommentValue) {
128        eventListener.ratingsCommentValue(change.getValue().convertChangeValue(RatingsCommentValue.class));
129      }
130
131      if (change.getValue() instanceof RatingsLikeValue) {
132        eventListener.ratingsLikeValue(change.getValue().convertChangeValue(RatingsLikeValue.class));
133      }
134
135      if (change.getValue() instanceof RatingsReactionValue) {
136        eventListener.ratingsReactionValue(change.getValue().convertChangeValue(RatingsReactionValue.class));
137      }
138
139      if (change.getValue() instanceof UserPageValue) {
140        eventListener.userPageValue(change.getValue().convertChangeValue(UserPageValue.class));
141      }
142
143      if (change.getValue() instanceof PermissionChangeValue) {
144        eventListener.permissionChangeValue(change.getValue().convertChangeValue(PermissionChangeValue.class));
145      }
146
147      if (change.getValue() instanceof PageLeadgen) {
148        eventListener.pageLeadgen(change.getValue().convertChangeValue(PageLeadgen.class));
149      }
150
151      if (change.getValue() instanceof InstagramStoryInsightsValue) {
152        eventListener
153          .instagramStoryInsightsValue(change.getValue().convertChangeValue(InstagramStoryInsightsValue.class));
154      }
155
156      if (change.getValue() instanceof InstagramMentionsValue) {
157        eventListener.instagramMentionsValue(change.getValue().convertChangeValue(InstagramMentionsValue.class));
158      }
159
160      if (change.getValue() instanceof InstagramCommentsValue) {
161        eventListener.instagramCommentsValue(change.getValue().convertChangeValue(InstagramCommentsValue.class));
162      }
163    });
164  }
165
166  private void handleMessaging(WebhookObject webhookObject) {
167    webhookObject.getEntryList().stream().flatMap(entry -> entry.getMessaging().stream()).forEach(messagingItem -> {
168      if (messagingItem.isAccountLinking()) {
169        messagingListener.accountLinking(messagingItem.getAccountLinking(), messagingItem.getRecipient(),
170          messagingItem.getSender());
171        messagingListener.accountLinking(messagingItem.getAccountLinking(), messagingItem.getRecipient(),
172          messagingItem.getSender(), messagingItem.getTimestamp());
173      }
174      if (messagingItem.isAppRoles()) {
175        messagingListener.appRoles(messagingItem.getAppRoles(), messagingItem.getRecipient(),
176          messagingItem.getSender());
177        messagingListener.appRoles(messagingItem.getAppRoles(), messagingItem.getRecipient(), messagingItem.getSender(),
178          messagingItem.getTimestamp());
179      }
180      if (messagingItem.isCheckoutUpdate()) {
181        messagingListener.checkoutUpdate(messagingItem.getCheckoutUpdate(), messagingItem.getRecipient(),
182          messagingItem.getSender());
183        messagingListener.checkoutUpdate(messagingItem.getCheckoutUpdate(), messagingItem.getRecipient(),
184          messagingItem.getSender(), messagingItem.getTimestamp());
185      }
186      if (messagingItem.isDelivery()) {
187        messagingListener.delivery(messagingItem.getDelivery(), messagingItem.getRecipient(),
188          messagingItem.getSender());
189        messagingListener.delivery(messagingItem.getDelivery(), messagingItem.getRecipient(), messagingItem.getSender(),
190          messagingItem.getTimestamp());
191      }
192      if (messagingItem.isMessage()) {
193        messagingListener.message(messagingItem.getMessage(), messagingItem.getRecipient(), messagingItem.getSender());
194        messagingListener.message(messagingItem.getMessage(), messagingItem.getRecipient(), messagingItem.getSender(),
195          messagingItem.getTimestamp());
196      }
197      if (messagingItem.isOptin()) {
198        messagingListener.optin(messagingItem.getOptin(), messagingItem.getRecipient(), messagingItem.getSender());
199        messagingListener.optin(messagingItem.getOptin(), messagingItem.getRecipient(), messagingItem.getSender(),
200          messagingItem.getTimestamp());
201      }
202      if (messagingItem.isPassThreadControl()) {
203        messagingListener.passThreadControl(messagingItem.getPassThreadControl(), messagingItem.getRecipient(),
204          messagingItem.getSender());
205        messagingListener.passThreadControl(messagingItem.getPassThreadControl(), messagingItem.getRecipient(),
206          messagingItem.getSender(), messagingItem.getTimestamp());
207      }
208      if (messagingItem.isPayment()) {
209        messagingListener.payment(messagingItem.getPayment(), messagingItem.getRecipient(), messagingItem.getSender());
210        messagingListener.payment(messagingItem.getPayment(), messagingItem.getRecipient(), messagingItem.getSender(),
211          messagingItem.getTimestamp());
212      }
213      if (messagingItem.isPolicyEnforcement()) {
214        messagingListener.policyEnforcement(messagingItem.getPolicyEnforcement(), messagingItem.getRecipient(),
215          messagingItem.getSender());
216        messagingListener.policyEnforcement(messagingItem.getPolicyEnforcement(), messagingItem.getRecipient(),
217          messagingItem.getSender(), messagingItem.getTimestamp());
218      }
219      if (messagingItem.isPostback()) {
220        messagingListener.postback(messagingItem.getPostback(), messagingItem.getRecipient(),
221          messagingItem.getSender());
222        messagingListener.postback(messagingItem.getPostback(), messagingItem.getRecipient(), messagingItem.getSender(),
223          messagingItem.getTimestamp());
224      }
225      if (messagingItem.isReaction()) {
226        messagingListener.reaction(messagingItem.getReaction(), messagingItem.getRecipient(),
227          messagingItem.getSender());
228        messagingListener.reaction(messagingItem.getReaction(), messagingItem.getRecipient(), messagingItem.getSender(),
229          messagingItem.getTimestamp());
230      }
231      if (messagingItem.isRead()) {
232        messagingListener.read(messagingItem.getRead(), messagingItem.getRecipient(), messagingItem.getSender());
233        messagingListener.read(messagingItem.getRead(), messagingItem.getRecipient(), messagingItem.getSender(),
234          messagingItem.getTimestamp());
235      }
236      if (messagingItem.isReferral()) {
237        messagingListener.referral(messagingItem.getReferral(), messagingItem.getRecipient(),
238          messagingItem.getSender());
239        messagingListener.referral(messagingItem.getReferral(), messagingItem.getRecipient(), messagingItem.getSender(),
240          messagingItem.getTimestamp());
241      }
242      if (messagingItem.isRequestThreadControl()) {
243        messagingListener.requestThreadControl(messagingItem.getRequestThreadControl(), messagingItem.getRecipient(),
244          messagingItem.getSender());
245        messagingListener.requestThreadControl(messagingItem.getRequestThreadControl(), messagingItem.getRecipient(),
246          messagingItem.getSender(), messagingItem.getTimestamp());
247      }
248      if (messagingItem.isTakeThreadControl()) {
249        messagingListener.takeThreadControl(messagingItem.getTakeThreadControl(), messagingItem.getRecipient(),
250          messagingItem.getSender());
251        messagingListener.takeThreadControl(messagingItem.getTakeThreadControl(), messagingItem.getRecipient(),
252          messagingItem.getSender(), messagingItem.getTimestamp());
253      }
254    });
255  }
256}