You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2019/04/02 07:04:32 UTC
[camel] branch camel-2.22.x updated: CAMEL-13387
camel-twitter-direct-message doesn't filter by filterOld parameter
https://issues.apache.org/jira/browse/CAMEL-13387
This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch camel-2.22.x
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/camel-2.22.x by this push:
new 8bbda0b CAMEL-13387 camel-twitter-direct-message doesn't filter by filterOld parameter https://issues.apache.org/jira/browse/CAMEL-13387
8bbda0b is described below
commit 8bbda0b1b8a4ee4be4e536feb5d648b9d69683b5
Author: Claudio Miranda <cl...@claudius.com.br>
AuthorDate: Mon Apr 1 16:41:35 2019 -0300
CAMEL-13387 camel-twitter-direct-message doesn't filter by filterOld parameter
https://issues.apache.org/jira/browse/CAMEL-13387
---
.../DirectMessageConsumerHandler.java | 57 +++++++++++++++++++---
1 file changed, 49 insertions(+), 8 deletions(-)
diff --git a/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/directmessage/DirectMessageConsumerHandler.java b/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/directmessage/DirectMessageConsumerHandler.java
index d550864..59dd766 100644
--- a/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/directmessage/DirectMessageConsumerHandler.java
+++ b/components/camel-twitter/src/main/java/org/apache/camel/component/twitter/directmessage/DirectMessageConsumerHandler.java
@@ -16,6 +16,7 @@
*/
package org.apache.camel.component.twitter.directmessage;
+import java.util.Iterator;
import java.util.List;
import org.apache.camel.Exchange;
@@ -23,6 +24,7 @@ import org.apache.camel.component.twitter.TwitterEndpoint;
import org.apache.camel.component.twitter.consumer.AbstractTwitterConsumerHandler;
import org.apache.camel.component.twitter.consumer.TwitterEventType;
import twitter4j.DirectMessage;
+import twitter4j.DirectMessageList;
import twitter4j.TwitterException;
/**
@@ -36,19 +38,58 @@ public class DirectMessageConsumerHandler extends AbstractTwitterConsumerHandler
@Override
public List<Exchange> pollConsume() throws TwitterException {
- List<DirectMessage> directMessages = getTwitter().getDirectMessages(endpoint.getProperties().getCount());
- for (int i = 0; i < directMessages.size(); i++) {
- setLastIdIfGreater(directMessages.get(i).getId());
+ // the first call doesn't require any cursor as parameters
+ DirectMessageList directMessages = directMessages(null, null);
+ if (directMessages.size() > 0) {
+ // the DM response list is in reverse chronological order, so the last id is the first element.
+ setLastId(directMessages.get(0).getId());
}
-
return TwitterEventType.DIRECT_MESSAGE.createExchangeList(endpoint, directMessages);
}
+ private DirectMessageList directMessages(String previousCursor, String cursor) throws TwitterException {
+ // https://developer.twitter.com/en/docs/direct-messages/sending-and-receiving/api-reference/list-events
+ // if there are more DM to retrieve the the next_cursor parameter is set.
+ // however next_cursor is always set in the response, so we must handle pagination correctly
+ // after the first call, there is always a second call to check if there is new DM and we compare
+ // the previous and current next_cursor, if not equals there is pagination.
+ DirectMessageList directMessages;
+ if (cursor != null) {
+ directMessages = getTwitter().getDirectMessages(endpoint.getProperties().getCount(), cursor);
+ } else {
+ directMessages = getTwitter().getDirectMessages(endpoint.getProperties().getCount());
+ }
+
+ String nextCursor = directMessages.getNextCursor();
+ // the condition will always be false for the first call.
+ if (nextCursor != null && nextCursor.equals(previousCursor) || directMessages.isEmpty()) {
+ directMessages.clear();
+ return directMessages;
+ }
+
+ if (nextCursor != null) {
+ DirectMessageList directMessages2 = directMessages(cursor, nextCursor);
+ directMessages.addAll(directMessages2);
+ }
+
+ // filter out messages retrieved before.
+ if (endpoint.getProperties().isFilterOld()) {
+ Iterator<DirectMessage> iterator = directMessages.iterator();
+ while (iterator.hasNext()) {
+ DirectMessage dm = iterator.next();
+ long id = dm.getId();
+ if (getLastId() > 1 && id <= getLastId()) {
+ iterator.remove();
+ }
+ }
+ }
+
+ return directMessages;
+ }
+
@Override
public List<Exchange> directConsume() throws TwitterException {
- return TwitterEventType.DIRECT_MESSAGE.createExchangeList(
- endpoint,
- getTwitter().getDirectMessages(endpoint.getProperties().getCount())
- );
+ DirectMessageList directMessages = directMessages(null, null);
+ return TwitterEventType.DIRECT_MESSAGE.createExchangeList(endpoint, directMessages);
}
}