use of com.fanap.podchat.model.ChatResponse in project pod-chat-android-sdk by FanapSoft.
the class ChatCore method handleUnBlock.
private void handleUnBlock(ChatMessage chatMessage, String messageUniqueId) {
BlockedContact contact = gson.fromJson(chatMessage.getContent(), BlockedContact.class);
ChatResponse<ResultBlock> chatResponse = new ChatResponse<>();
ResultBlock resultBlock = new ResultBlock();
resultBlock.setContact(contact);
chatResponse.setResult(resultBlock);
chatResponse.setErrorCode(0);
chatResponse.setHasError(false);
chatResponse.setUniqueId(chatMessage.getUniqueId());
String jsonUnBlock = gson.toJson(chatResponse);
if (sentryResponseLog) {
showLog("RECEIVE_UN_BLOCK", jsonUnBlock);
} else {
showLog("RECEIVE_UN_BLOCK");
}
messageCallbacks.remove(messageUniqueId);
if (cache) {
dataSource.deleteBlockedContactById(contact.getBlockId());
}
listenerManager.callOnUnBlock(jsonUnBlock, chatResponse);
}
use of com.fanap.podchat.model.ChatResponse in project pod-chat-android-sdk by FanapSoft.
the class ChatCore method reformatGetThreadsResponseForMutual.
/**
* Reformat the get thread response
*/
private ChatResponse<ResultThreads> reformatGetThreadsResponseForMutual(ChatMessage chatMessage, long userId) {
ChatResponse<ResultThreads> outPutThreads = new ChatResponse<>();
ArrayList<Thread> threads = gson.fromJson(chatMessage.getContent(), new TypeToken<ArrayList<Thread>>() {
}.getType());
if (cache) {
// get threads summary shouldn't update cache
if (!handlerSend.containsKey(chatMessage.getUniqueId())) {
// messageDatabaseHelper.saveThreads(threads);
dataSource.saveThreadResultFromServer(threads);
dataSource.saveMutualThreadResultFromServer(threads, userId);
}
}
ResultThreads resultThreads = new ResultThreads();
resultThreads.setThreads(threads);
resultThreads.setContentCount(chatMessage.getContentCount());
outPutThreads.setErrorCode(0);
outPutThreads.setErrorMessage("");
outPutThreads.setHasError(false);
outPutThreads.setUniqueId(chatMessage.getUniqueId());
outPutThreads.setResult(resultThreads);
return outPutThreads;
}
use of com.fanap.podchat.model.ChatResponse in project pod-chat-android-sdk by FanapSoft.
the class ChatCore method addGroupContacts.
private PublishSubject<List<PhoneContact>> addGroupContacts(List<PhoneContact> phoneContacts, String uniqueId) {
PublishSubject<List<PhoneContact>> subject = PublishSubject.create();
ArrayList<String> firstNames = new ArrayList<>();
ArrayList<String> cellphoneNumbers = new ArrayList<>();
ArrayList<String> lastNames = new ArrayList<>();
ArrayList<String> typeCodes = new ArrayList<>();
ArrayList<String> uniqueIds = new ArrayList<>();
ArrayList<String> emails = new ArrayList<>();
for (PhoneContact contact : phoneContacts) {
firstNames.add(contact.getName());
lastNames.add(contact.getLastName());
uniqueIds.add(generateUniqueId());
String phoneNum = String.valueOf(contact.getPhoneNumber());
if (phoneNum.startsWith("9")) {
phoneNum = phoneNum.replaceFirst("9", "09");
}
cellphoneNumbers.add(phoneNum);
emails.add("");
typeCodes.add(getTypeCode());
}
Observable<Response<Contacts>> addContactsObservable;
if (getPlatformHost() != null) {
if (!Util.isNullOrEmpty(getTypeCode())) {
addContactsObservable = contactApi.addContacts(getToken(), TOKEN_ISSUER, firstNames, lastNames, emails, uniqueIds, cellphoneNumbers, typeCodes);
} else {
addContactsObservable = contactApi.addContacts(getToken(), TOKEN_ISSUER, firstNames, lastNames, emails, uniqueIds, cellphoneNumbers);
}
showLog("Call add contacts " + new Date());
addContactsObservable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(contactsResponse -> {
showLog(">>> Server Respond " + new Date());
boolean error = false;
if (contactsResponse.body() != null) {
error = contactsResponse.body().getHasError();
showLog(">>>Response: " + contactsResponse.code());
showLog(">>>ReferenceNumber: " + contactsResponse.body().getReferenceNumber());
showLog(">>>Ott: " + contactsResponse.body().getOtt());
}
if (contactsResponse.isSuccessful()) {
if (error) {
captureError(contactsResponse.body().getMessage(), contactsResponse.body().getErrorCode(), uniqueId);
showLog("Error add Contacts: " + contactsResponse.body().getMessage());
subject.onError(new Throwable(contactsResponse.body().getMessage()));
} else {
// successful response
Contacts contacts = contactsResponse.body();
ChatResponse<Contacts> chatResponse = new ChatResponse<>();
chatResponse.setResult(contacts);
chatResponse.setUniqueId(uniqueId);
Runnable updatePhoneContactsDBTask = () -> {
try {
boolean result = phoneContactDbHelper.addPhoneContacts(phoneContacts);
if (!result) {
disableCache(() -> phoneContactDbHelper.addPhoneContacts(phoneContacts));
}
} catch (Exception e) {
showErrorLog("Updating Contacts cache failed: " + e.getMessage());
onUnknownException(uniqueId, e);
}
};
Runnable updateCachedContactsTask = () -> {
if (cache) {
try {
// messageDatabaseHelper.saveContacts(chatResponse.getResult().getResult(), getExpireAmount());
dataSource.saveContactsResultFromServer(chatResponse.getResult().getResult());
} catch (Exception e) {
showErrorLog("Saving Contacts Failed: " + e.getMessage());
onUnknownException(uniqueId, e);
}
}
};
new PodThreadManager().addNewTask(updatePhoneContactsDBTask).addNewTask(updateCachedContactsTask).addNewTask(() -> subject.onNext(phoneContacts)).runTasksSynced();
}
} else {
captureError(contactsResponse.message(), contactsResponse.code(), uniqueId);
showLog("Error add Contacts: " + contactsResponse.raw());
subject.onError(new Throwable(contactsResponse.message()));
}
}, throwable -> {
captureError(throwable.getMessage(), ChatConstant.ERROR_CODE_UNKNOWN_EXCEPTION, uniqueId);
subject.onError(throwable);
});
}
return subject;
}
use of com.fanap.podchat.model.ChatResponse in project pod-chat-android-sdk by FanapSoft.
the class ChatCore method loadParticipantsFromCache.
@SuppressWarnings("unchecked")
private void loadParticipantsFromCache(String uniqueId, int count, int offset, long threadId) {
try {
messageDatabaseHelper.getThreadParticipant(offset, count, threadId, (obj, listData) -> {
if (listData != null) {
List<Participant> participantsList = (List<Participant>) listData;
long participantCount = (long) obj;
ChatResponse<ResultParticipant> chatResponse = new ChatResponse<>();
ResultParticipant resultParticipant = new ResultParticipant();
resultParticipant.setContentCount(participantsList.size());
resultParticipant.setHasNext(participantsList.size() + offset < participantCount);
resultParticipant.setParticipants(participantsList);
chatResponse.setResult(resultParticipant);
chatResponse.setCache(true);
chatResponse.setUniqueId(uniqueId);
resultParticipant.setNextOffset(offset + participantsList.size());
String jsonParticipant = gson.toJson(chatResponse);
listenerManager.callOnGetThreadParticipant(jsonParticipant, chatResponse);
showLog("PARTICIPANT FROM CACHE", jsonParticipant);
}
});
} catch (RoomIntegrityException e) {
disableCache();
}
}
use of com.fanap.podchat.model.ChatResponse in project pod-chat-android-sdk by FanapSoft.
the class ChatCore method getUserInfo.
/**
* It Gets the information of the current user
*/
public String getUserInfo(ChatHandler handler) {
String uniqueId = generateUniqueId();
Runnable cacheLoading = () -> {
if (permit && cache && handler != null) {
try {
UserInfo userInfo = messageDatabaseHelper.getUserInfo();
if (userInfo != null) {
ChatResponse<ResultUserInfo> chatResponse = new ChatResponse<>();
ResultUserInfo result = new ResultUserInfo();
setUserId(userInfo.getId());
result.setUser(userInfo);
chatResponse.setResult(result);
chatResponse.setCache(true);
chatResponse.setUniqueId(uniqueId);
String userInfoJson = gson.toJson(chatResponse);
listenerManager.callOnUserInfo(userInfoJson, chatResponse);
showLog("CACHE_USER_INFO", userInfoJson);
}
} catch (Exception e) {
showErrorLog(e.getMessage());
onUnknownException(uniqueId, e);
}
}
};
Runnable requestServer = () -> {
if (asyncReady) {
ChatMessage chatMessage = new ChatMessage();
chatMessage.setType(Constants.USER_INFO);
chatMessage.setUniqueId(uniqueId);
chatMessage.setToken(getToken());
chatMessage.setTokenIssuer("1");
setCallBacks(null, null, null, true, Constants.USER_INFO, null, uniqueId);
JsonObject jsonObject = (JsonObject) gson.toJsonTree(chatMessage);
if (Util.isNullOrEmpty(getTypeCode())) {
jsonObject.remove("typeCode");
} else {
jsonObject.remove("typeCode");
jsonObject.addProperty("typeCode", getTypeCode());
}
String asyncContent = jsonObject.toString();
showLog("SEND_USER_INFO", asyncContent);
async.sendMessage(asyncContent, AsyncAckType.Constants.WITHOUT_ACK);
if (handler != null) {
handler.onGetUserInfo(uniqueId);
}
}
};
new PodThreadManager().addNewTask(cacheLoading).addNewTask(requestServer).runTasksSynced();
return uniqueId;
}
Aggregations