001/*
002 * Copyright (c) 2010-2024 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(), messagingItem.getTimestamp());
171      }
172      if (messagingItem.isAppRoles()) {
173        messagingListener.appRoles(messagingItem.getAppRoles(), messagingItem.getRecipient(), messagingItem.getSender(),
174          messagingItem.getTimestamp());
175      }
176      if (messagingItem.isCheckoutUpdate()) {
177        messagingListener.checkoutUpdate(messagingItem.getCheckoutUpdate(), messagingItem.getRecipient(),
178          messagingItem.getSender(), messagingItem.getTimestamp());
179      }
180      if (messagingItem.isDelivery()) {
181        messagingListener.delivery(messagingItem.getDelivery(), messagingItem.getRecipient(), messagingItem.getSender(),
182          messagingItem.getTimestamp());
183      }
184      if (messagingItem.isMessage()) {
185        messagingListener.message(messagingItem.getMessage(), messagingItem.getRecipient(), messagingItem.getSender(),
186          messagingItem.getTimestamp());
187      }
188      if (messagingItem.isOptin()) {
189        messagingListener.optin(messagingItem.getOptin(), messagingItem.getRecipient(), messagingItem.getSender(),
190          messagingItem.getTimestamp());
191      }
192      if (messagingItem.isPassThreadControl()) {
193        messagingListener.passThreadControl(messagingItem.getPassThreadControl(), messagingItem.getRecipient(),
194          messagingItem.getSender(), messagingItem.getTimestamp());
195      }
196      if (messagingItem.isPayment()) {
197        messagingListener.payment(messagingItem.getPayment(), messagingItem.getRecipient(), messagingItem.getSender(),
198          messagingItem.getTimestamp());
199      }
200      if (messagingItem.isPolicyEnforcement()) {
201        messagingListener.policyEnforcement(messagingItem.getPolicyEnforcement(), messagingItem.getRecipient(),
202          messagingItem.getSender(), messagingItem.getTimestamp());
203      }
204      if (messagingItem.isPostback()) {
205        messagingListener.postback(messagingItem.getPostback(), messagingItem.getRecipient(), messagingItem.getSender(),
206          messagingItem.getTimestamp());
207      }
208      if (messagingItem.isReaction()) {
209        messagingListener.reaction(messagingItem.getReaction(), messagingItem.getRecipient(), messagingItem.getSender(),
210          messagingItem.getTimestamp());
211      }
212      if (messagingItem.isRead()) {
213        messagingListener.read(messagingItem.getRead(), messagingItem.getRecipient(), messagingItem.getSender(),
214          messagingItem.getTimestamp());
215      }
216      if (messagingItem.isReferral()) {
217        messagingListener.referral(messagingItem.getReferral(), messagingItem.getRecipient(), messagingItem.getSender(),
218          messagingItem.getTimestamp());
219      }
220      if (messagingItem.isRequestThreadControl()) {
221        messagingListener.requestThreadControl(messagingItem.getRequestThreadControl(), messagingItem.getRecipient(),
222          messagingItem.getSender(), messagingItem.getTimestamp());
223      }
224      if (messagingItem.isTakeThreadControl()) {
225        messagingListener.takeThreadControl(messagingItem.getTakeThreadControl(), messagingItem.getRecipient(),
226          messagingItem.getSender(), messagingItem.getTimestamp());
227      }
228    });
229  }
230}