Last active
January 15, 2017 21:36
-
-
Save marinoluck/3eec773a7b7fe17ae8a0db20dfd95602 to your computer and use it in GitHub Desktop.
GetOrCreateCOnversationWith
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.etermax.crackme.core.actions.conversation; | |
import com.etermax.crackme.core.domain.conversation.Conversation; | |
import com.etermax.crackme.core.domain.conversation.ConversationBuilder; | |
import com.etermax.crackme.core.domain.conversation.ConversationRepository; | |
import com.etermax.crackme.core.domain.conversation.ConversationXmppService; | |
import java.util.List; | |
import rx.Observable; | |
import rx.Subscription; | |
public class GetConversationOrCreateWith { | |
private final ConversationRepository conversationRepository; | |
private final Observable<List<Conversation>> conversationObservable; | |
private final ConversationXmppService conversationService; | |
public GetConversationOrCreateWith(ConversationRepository conversationRepository, | |
Observable<List<Conversation>> conversationObservable, ConversationXmppService conversationService) { | |
this.conversationRepository = conversationRepository; | |
this.conversationObservable = conversationObservable; | |
this.conversationService = conversationService; | |
} | |
public Observable<Conversation> execute(String userId) { | |
return Observable.create(mainSubscription -> { | |
if (conversationRepository.existsWith(userId)) { | |
mainSubscription.onNext(conversationRepository.findByUserId(userId)); | |
mainSubscription.onCompleted(); | |
} else { | |
Subscription subscription = conversationObservable | |
.filter(list -> containsConversationBetween(list, getMeUserId(), userId)) | |
.first(list -> containsConversationBetween(list, getMeUserId(), userId)) | |
.flatMap(Observable::from) | |
.subscribe( | |
mainSubscription::onNext, | |
mainSubscription::onError, | |
() -> { | |
mainSubscription.onCompleted(); | |
if (subscription != null && !subscription.isUnsubscribed()) { | |
subscription.unsubscribe(); | |
} | |
}); | |
conversationService.createConversation(makeConversation(userId)); | |
} | |
}); | |
} | |
private boolean containsConversationBetween(List<Conversation> conversations, String meUserId, String userId) { | |
return true; | |
} | |
private Conversation makeConversation(String userId) { | |
return new ConversationBuilder() | |
.conversationId("conversation1") | |
.participantsIds(new String[]{getMeUserId(), userId}) | |
.single() | |
.build(); | |
} | |
private String getMeUserId() { | |
return "fixMe"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment