From f5b741b2cb72e03f94cef30c2d4291a7172ee966 Mon Sep 17 00:00:00 2001 From: Artem Bilan Date: Wed, 6 Mar 2024 18:00:40 -0500 Subject: [PATCH] Fix Twitter Supplier for its `DirectMessage` Turns out the `DirectMessage` type is not compatible with JSON (anymore) * Provide an internal `DirectMessageAdapter` and mark unsupported options with `@JsonIgnore` --- .../TwitterMessageSupplierConfiguration.java | 113 +++++++++++++++++- 1 file changed, 111 insertions(+), 2 deletions(-) diff --git a/supplier/spring-twitter-supplier/src/main/java/org/springframework/cloud/fn/supplier/twitter/message/TwitterMessageSupplierConfiguration.java b/supplier/spring-twitter-supplier/src/main/java/org/springframework/cloud/fn/supplier/twitter/message/TwitterMessageSupplierConfiguration.java index 9e0b7bc8..66035ad1 100644 --- a/supplier/spring-twitter-supplier/src/main/java/org/springframework/cloud/fn/supplier/twitter/message/TwitterMessageSupplierConfiguration.java +++ b/supplier/spring-twitter-supplier/src/main/java/org/springframework/cloud/fn/supplier/twitter/message/TwitterMessageSupplierConfiguration.java @@ -17,16 +17,25 @@ package org.springframework.cloud.fn.supplier.twitter.message; import java.util.ArrayList; +import java.util.Date; import java.util.List; import java.util.function.Function; import java.util.function.Supplier; +import com.fasterxml.jackson.annotation.JsonIgnore; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import twitter4j.DirectMessage; import twitter4j.DirectMessageList; +import twitter4j.HashtagEntity; +import twitter4j.MediaEntity; +import twitter4j.RateLimitStatus; +import twitter4j.SymbolEntity; import twitter4j.Twitter; import twitter4j.TwitterException; +import twitter4j.URLEntity; +import twitter4j.User; +import twitter4j.UserMentionEntity; import org.springframework.boot.autoconfigure.AutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; @@ -64,7 +73,8 @@ public MessageCursor cursor() { } @Bean - public Supplier> directMessagesSupplier(TwitterMessageSupplierProperties properties, + @SuppressWarnings({ "unchecked", "rawtypes" }) + public Supplier> twitterMessagesSupplier(TwitterMessageSupplierProperties properties, Twitter twitter, MessageCursor cursorState) { return () -> { @@ -75,7 +85,7 @@ public Supplier> directMessagesSupplier(TwitterMessageSuppli if (messages != null) { cursorState.updateCursor(messages.getNextCursor()); - return messages; + return (List) (List) messages.stream().map(DirectMessageAdapter::new).toList(); } logger.error(String.format("NULL messages response for properties: %s and cursor: %s!", properties, @@ -132,4 +142,103 @@ public String toString() { } + @SuppressWarnings("serial") + private static class DirectMessageAdapter implements DirectMessage { + + private final DirectMessage delegate; + + private DirectMessageAdapter(DirectMessage delegate) { + this.delegate = delegate; + } + + @Override + public long getId() { + return this.delegate.getId(); + } + + @Override + public String getText() { + return this.delegate.getText(); + } + + @Override + public long getSenderId() { + return this.delegate.getSenderId(); + } + + @Override + public long getRecipientId() { + return this.delegate.getRecipientId(); + } + + @Override + public Date getCreatedAt() { + return this.delegate.getCreatedAt(); + } + + @Override + public RateLimitStatus getRateLimitStatus() { + return this.delegate.getRateLimitStatus(); + } + + @Override + public int getAccessLevel() { + return this.delegate.getAccessLevel(); + } + + @Override + public UserMentionEntity[] getUserMentionEntities() { + return this.delegate.getUserMentionEntities(); + } + + @Override + public URLEntity[] getURLEntities() { + return this.delegate.getURLEntities(); + } + + @Override + public HashtagEntity[] getHashtagEntities() { + return this.delegate.getHashtagEntities(); + } + + @Override + public MediaEntity[] getMediaEntities() { + return this.delegate.getMediaEntities(); + } + + @Override + public SymbolEntity[] getSymbolEntities() { + return this.delegate.getSymbolEntities(); + } + + @Deprecated + @JsonIgnore + @Override + public String getSenderScreenName() { + return this.delegate.getSenderScreenName(); + } + + @Deprecated + @JsonIgnore + @Override + public String getRecipientScreenName() { + return this.delegate.getRecipientScreenName(); + } + + @Deprecated + @JsonIgnore + @Override + public User getSender() { + return this.delegate.getSender(); + } + + @Deprecated + @JsonIgnore + @Override + public User getRecipient() { + return this.delegate.getRecipient(); + } + + } + }