@@ -475,18 +475,57 @@ public void updateConversation(@NonNull final Conversation conversation,
475475 @ Nullable final SaveCallback <Conversation > callback ) {
476476 final Database publicDB = this .skygear .getPublicDatabase ();
477477 final String conversationId = conversation .getId ();
478- Record record = new Record (conversation .record .getType (), conversationId );
479- for (Map .Entry <String , Object > entry : updates .entrySet ()) {
480- record .set (entry .getKey (), entry .getValue ());
481- }
482- publicDB .save (record , new SaveResponseAdapter <Conversation >(callback ) {
478+ Query query = new Query (Conversation .TYPE_KEY );
479+ query .equalTo ("_id" , conversationId );
480+ publicDB .query (query , new RecordQueryResponseHandler () {
483481 @ Override
484- public Conversation convert (Record record ) {
485- return new Conversation (record );
482+ public void onQuerySuccess (Record [] records ) {
483+ if (records .length == 0 ) {
484+ if (callback != null ) {
485+ callback .onFail (new ConversationNotFoundError (conversationId ));
486+ }
487+ return ;
488+ }
489+
490+ Record conversationRecord = records [0 ];
491+ for (Map .Entry <String , Object > entry : updates .entrySet ()) {
492+ conversationRecord .set (entry .getKey (), entry .getValue ());
493+ }
494+ publicDB .save (conversationRecord , new SaveResponseAdapter <Conversation >(callback ) {
495+ @ Override
496+ public Conversation convert (Record record ) {
497+ // FIXME
498+ // admins_ids and participant_ids are appended attributes from lambda
499+ // so these attributes are missing in the record save handler
500+ // hot fix to copy them from the original conversation record if exists
501+ // ultimately we should have update conversation lambda so that we can
502+ // update conversation in one request like create
503+ if (conversation .record .get (Conversation .ADMIN_IDS_KEY ) != null ) {
504+ record .set (
505+ Conversation .ADMIN_IDS_KEY ,
506+ conversation .record .get (Conversation .ADMIN_IDS_KEY )
507+ );
508+ }
509+ if (conversation .record .get (Conversation .PARTICIPANT_IDS_KEY ) != null ) {
510+ record .set (
511+ Conversation .PARTICIPANT_IDS_KEY ,
512+ conversation .record .get (Conversation .PARTICIPANT_IDS_KEY )
513+ );
514+ }
515+ return new Conversation (record );
516+ }
517+
518+ @ Override
519+ public void onSaveFail (Error error ) {
520+ if (callback != null ) {
521+ callback .onFail (new ConversationOperationError (error ));
522+ }
523+ }
524+ });
486525 }
487526
488527 @ Override
489- public void onSaveFail (Error error ) {
528+ public void onQueryError (Error error ) {
490529 if (callback != null ) {
491530 callback .onFail (new ConversationOperationError (error ));
492531 }
0 commit comments