88import com .pubnub .api .builder .PubNubErrorBuilder ;
99import com .pubnub .api .enums .PNOperationType ;
1010import com .pubnub .api .managers .MapperManager ;
11- import com .pubnub .api .models .server .HistoryForChannelsEnvelope ;
11+ import com .pubnub .api .models .consumer .history .PNFetchMessagesResult ;
12+ import com .pubnub .api .models .consumer .pubsub .PNMessageResult ;
13+ import com .pubnub .api .models .server .FetchMessagesEnvelope ;
1214import com .pubnub .api .models .server .HistoryForChannelsItem ;
1315import com .pubnub .api .vendor .Crypto ;
1416import lombok .Setter ;
2123import retrofit2 .http .QueryMap ;
2224
2325import java .util .ArrayList ;
26+ import java .util .HashMap ;
2427import java .util .List ;
2528import java .util .Map ;
2629
2730@ Accessors (chain = true , fluent = true )
28- public class FetchMessages extends Endpoint <HistoryForChannelsEnvelope , HistoryForChannelsEnvelope > {
31+ public class FetchMessages extends Endpoint <FetchMessagesEnvelope , PNFetchMessagesResult > {
2932 private static final int MAX_MESSAGES = 25 ;
3033 @ Setter
3134 private List <String > channels ;
@@ -44,9 +47,9 @@ public FetchMessages(PubNub pubnub, Retrofit retrofit) {
4447
4548 private interface HistoryForChannelsService {
4649 @ GET ("v3/history/sub-key/{subKey}/channel/{channels}" )
47- Call <HistoryForChannelsEnvelope > fetchHistoryForChannels (@ Path ("subKey" ) String subKey ,
48- @ Path ("channels" ) String channels ,
49- @ QueryMap Map <String , String > options );
50+ Call <FetchMessagesEnvelope > fetchMessages (@ Path ("subKey" ) String subKey ,
51+ @ Path ("channels" ) String channels ,
52+ @ QueryMap Map <String , String > options );
5053 }
5154
5255 @ Override
@@ -62,7 +65,7 @@ protected void validateParams() throws PubNubException {
6265 }
6366
6467 @ Override
65- protected Call <HistoryForChannelsEnvelope > doWork (Map <String , String > params ) {
68+ protected Call <FetchMessagesEnvelope > doWork (Map <String , String > params ) {
6669
6770 HistoryForChannelsService service = this .getRetrofit ().create (HistoryForChannelsService .class );
6871
@@ -75,27 +78,39 @@ protected Call<HistoryForChannelsEnvelope> doWork(Map<String, String> params) {
7578 params .put ("end" , Long .toString (end ).toLowerCase ());
7679 }
7780
78- return service .fetchHistoryForChannels (this .getPubnub ().getConfiguration ().getSubscribeKey (), PubNubUtil .joinString (channels , "," ), params );
81+ return service .fetchMessages (this .getPubnub ().getConfiguration ().getSubscribeKey (), PubNubUtil .joinString (channels , "," ), params );
7982 }
8083
8184 @ Override
82- protected HistoryForChannelsEnvelope createResponse (Response <HistoryForChannelsEnvelope > input ) throws PubNubException {
85+ protected PNFetchMessagesResult createResponse (Response <FetchMessagesEnvelope > input ) throws PubNubException {
8386 if (input .body () == null ) {
8487 throw PubNubException .builder ().pubnubError (PubNubErrorBuilder .PNERROBJ_PARSING_ERROR ).build ();
8588 }
8689
87- HistoryForChannelsEnvelope envelope = input .body ();
88- Map <String , List <HistoryForChannelsItem >> channelsList = envelope .getChannels ();
90+ PNFetchMessagesResult .PNFetchMessagesResultBuilder result = PNFetchMessagesResult .builder ();
91+ Map <String , List <PNMessageResult >> listMap = new HashMap <>();
92+
93+ FetchMessagesEnvelope envelope = input .body ();
94+
95+ for (Map .Entry <String , List <HistoryForChannelsItem >> entry : envelope .getChannels ().entrySet ()) {
96+
97+ List <PNMessageResult > messages = new ArrayList <>();
8998
90- for (Map .Entry <String , List <HistoryForChannelsItem >> entry : channelsList .entrySet ()) {
9199 for (HistoryForChannelsItem item : entry .getValue ()) {
100+ PNMessageResult .PNMessageResultBuilder pnMessageResultBuilder = PNMessageResult .builder ();
101+ pnMessageResultBuilder .channel (entry .getKey ());
92102 JsonNode message = processMessage (item .getMessage ());
93- item .setMessage (message );
103+ pnMessageResultBuilder .message (message );
104+ pnMessageResultBuilder .timetoken (item .getTimeToken ());
105+ messages .add (pnMessageResultBuilder .build ());
94106 }
95107
108+ listMap .put (entry .getKey (), messages );
96109 }
97110
98- return envelope ;
111+ result .channels (listMap );
112+
113+ return result .build ();
99114 }
100115
101116 @ Override
@@ -120,19 +135,19 @@ private JsonNode processMessage(JsonNode message) throws PubNubException {
120135 String outputText ;
121136 JsonNode outputObject ;
122137
123- if (message .isObject () && message .has ("message " )) {
124- inputText = message .get ("message " ).asText ();
138+ if (message .isObject () && message .has ("pn_other " )) {
139+ inputText = message .get ("pn_other " ).asText ();
125140 } else {
126141 inputText = message .asText ();
127142 }
128143
129144 outputText = crypto .decrypt (inputText );
130145 outputObject = mapper .fromJson (outputText , JsonNode .class );
131146
132- // inject the decoded response into the payload
133- if (message .isObject () && message .has ("message " )) {
147+ // inject the decoded resposne into the payload
148+ if (message .isObject () && message .has ("pn_other " )) {
134149 ObjectNode objectNode = (ObjectNode ) message ;
135- objectNode .set ("message " , outputObject );
150+ objectNode .set ("pn_other " , outputObject );
136151 outputObject = objectNode ;
137152 }
138153
0 commit comments