1414import org .qortal .api .ApiErrors ;
1515import org .qortal .api .ApiExceptionFactory ;
1616import org .qortal .api .Security ;
17+ import org .qortal .controller .ChatTransactionDelegate ;
1718import org .qortal .crypto .Crypto ;
1819import org .qortal .data .chat .ActiveChats ;
1920import org .qortal .data .chat .ChatMessage ;
2021import org .qortal .data .transaction .ChatTransactionData ;
2122import org .qortal .data .transaction .TransactionData ;
2223import org .qortal .repository .DataException ;
23- import org .qortal .repository .Repository ;
24- import org .qortal .repository .RepositoryManager ;
25- import org .qortal .transaction .ChatTransaction ;
26- import org .qortal .transaction .Transaction ;
2724import org .qortal .transaction .Transaction .TransactionType ;
2825import org .qortal .transaction .Transaction .ValidationResult ;
2926import org .qortal .transform .TransformationException ;
@@ -99,8 +96,8 @@ public List<ChatMessage> searchChat(@QueryParam("before") Long before, @QueryPar
9996 if (chatReference != null )
10097 chatReferenceBytes = Base58 .decode (chatReference );
10198
102- try ( final Repository repository = RepositoryManager . getRepository ()) {
103- return repository . getChatRepository ().getMessagesMatchingCriteria (
99+ try {
100+ return ChatTransactionDelegate . getInstance ().getMessagesMatchingCriteria (
104101 before ,
105102 after ,
106103 txGroupId ,
@@ -168,8 +165,8 @@ public int countChatMessages(@QueryParam("before") Long before, @QueryParam("aft
168165 if (chatReference != null )
169166 chatReferenceBytes = Base58 .decode (chatReference );
170167
171- try ( final Repository repository = RepositoryManager . getRepository ()) {
172- return repository . getChatRepository ().getMessagesMatchingCriteria (
168+ try {
169+ return ChatTransactionDelegate . getInstance ().getMessagesMatchingCriteria (
173170 before ,
174171 after ,
175172 txGroupId ,
@@ -202,16 +199,14 @@ public int countChatMessages(@QueryParam("before") Long before, @QueryParam("aft
202199 )
203200 @ ApiErrors ({ApiError .INVALID_CRITERIA , ApiError .INVALID_ADDRESS , ApiError .REPOSITORY_ISSUE })
204201 public ChatMessage getMessageBySignature (@ PathParam ("signature" ) String signature58 , @ QueryParam ("encoding" ) Encoding encoding ) {
205- byte [] signature = Base58 . decode ( signature58 );
202+ try {
206203
207- try (final Repository repository = RepositoryManager .getRepository ()) {
208-
209- ChatTransactionData chatTransactionData = (ChatTransactionData ) repository .getTransactionRepository ().fromSignature (signature );
204+ ChatTransactionData chatTransactionData = ChatTransactionDelegate .getInstance ().fromSignature (signature58 );
210205 if (chatTransactionData == null ) {
211206 throw ApiExceptionFactory .INSTANCE .createCustomException (request , ApiError .INVALID_CRITERIA , "Message not found" );
212207 }
213208
214- return repository . getChatRepository ().toChatMessage (chatTransactionData , encoding );
209+ return ChatTransactionDelegate . getInstance ().toChatMessage (chatTransactionData , encoding );
215210 } catch (DataException e ) {
216211 throw ApiExceptionFactory .INSTANCE .createException (request , ApiError .REPOSITORY_ISSUE , e );
217212 }
@@ -241,12 +236,8 @@ public ActiveChats getActiveChats(
241236 ) {
242237 if (address == null || !Crypto .isValidAddress (address ))
243238 throw ApiExceptionFactory .INSTANCE .createException (request , ApiError .INVALID_ADDRESS );
244-
245- try (final Repository repository = RepositoryManager .getRepository ()) {
246- return repository .getChatRepository ().getActiveChats (address , encoding , hasChatReference );
247- } catch (DataException e ) {
248- throw ApiExceptionFactory .INSTANCE .createException (request , ApiError .REPOSITORY_ISSUE , e );
249- }
239+
240+ return ChatTransactionDelegate .getInstance ().getActiveChats (address , encoding , hasChatReference );
250241 }
251242
252243 @ POST
@@ -279,19 +270,15 @@ public ActiveChats getActiveChats(
279270 public String buildChat (@ HeaderParam (Security .API_KEY_HEADER ) String apiKey , ChatTransactionData transactionData ) {
280271 Security .checkApiCallAllowed (request );
281272
282- try (final Repository repository = RepositoryManager .getRepository ()) {
283- ChatTransaction chatTransaction = (ChatTransaction ) Transaction .fromData (repository , transactionData );
284-
285- ValidationResult result = chatTransaction .isValidUnconfirmed ();
273+ try {
274+ ValidationResult result = ChatTransactionDelegate .getInstance ().isValid (transactionData , false );
286275 if (result != ValidationResult .OK )
287276 throw TransactionsResource .createTransactionInvalidException (request , result );
288277
289278 byte [] bytes = ChatTransactionTransformer .toBytes (transactionData );
290279 return Base58 .encode (bytes );
291280 } catch (TransformationException e ) {
292281 throw ApiExceptionFactory .INSTANCE .createException (request , ApiError .TRANSFORMATION_ERROR , e );
293- } catch (DataException e ) {
294- throw ApiExceptionFactory .INSTANCE .createException (request , ApiError .REPOSITORY_ISSUE , e );
295282 }
296283 }
297284
@@ -327,7 +314,7 @@ public String buildChat(@HeaderParam(Security.API_KEY_HEADER) String apiKey, Cha
327314 public String buildChat (@ HeaderParam (Security .API_KEY_HEADER ) String apiKey , String rawBytes58 ) {
328315 Security .checkApiCallAllowed (request );
329316
330- try ( final Repository repository = RepositoryManager . getRepository ()) {
317+ try {
331318 byte [] rawBytes = Base58 .decode (rawBytes58 );
332319 // We're expecting unsigned transaction, so append empty signature prior to decoding
333320 rawBytes = Bytes .concat (rawBytes , new byte [TransactionTransformer .SIGNATURE_LENGTH ]);
@@ -339,19 +326,17 @@ public String buildChat(@HeaderParam(Security.API_KEY_HEADER) String apiKey, Str
339326 if (transactionData .getType () != TransactionType .CHAT )
340327 throw ApiExceptionFactory .INSTANCE .createException (request , ApiError .INVALID_DATA );
341328
342- ChatTransaction chatTransaction = (ChatTransaction ) Transaction .fromData (repository , transactionData );
329+ if ( !(transactionData instanceof ChatTransactionData ) )
330+ throw ApiExceptionFactory .INSTANCE .createException (request , ApiError .INVALID_DATA );
331+
332+ ChatTransactionData chatTransactionData = (ChatTransactionData ) transactionData ;
343333
344334 // Quicker validity check first before we compute nonce
345- ValidationResult result = chatTransaction . isValid ();
335+ ValidationResult result = ChatTransactionDelegate . getInstance (). isValid (chatTransactionData , false );
346336 if (result != ValidationResult .OK )
347337 throw TransactionsResource .createTransactionInvalidException (request , result );
348338
349- chatTransaction .computeNonce ();
350-
351- // Re-check, but ignores signature
352- result = chatTransaction .isValidUnconfirmed ();
353- if (result != ValidationResult .OK )
354- throw TransactionsResource .createTransactionInvalidException (request , result );
339+ ChatTransactionDelegate .getInstance ().computeNonce (chatTransactionData );
355340
356341 // Strip zeroed signature
357342 transactionData .setSignature (null );
@@ -360,8 +345,6 @@ public String buildChat(@HeaderParam(Security.API_KEY_HEADER) String apiKey, Str
360345 return Base58 .encode (bytes );
361346 } catch (TransformationException e ) {
362347 throw ApiExceptionFactory .INSTANCE .createException (request , ApiError .TRANSFORMATION_ERROR , e );
363- } catch (DataException e ) {
364- throw ApiExceptionFactory .INSTANCE .createException (request , ApiError .REPOSITORY_ISSUE , e );
365348 }
366349 }
367350
0 commit comments