use of com.fanap.podchat.model.ChatResponse in project pod-chat-android-sdk by FanapSoft.
the class ChatTest method getThreadHistoryBeforeAndAfterLastSeenMessage.
@Test
@LargeTest
public // and greater in second case
void getThreadHistoryBeforeAndAfterLastSeenMessage() {
populateThreadsListFromServerOrCache();
System.out.println("** Get history of " + threads.get(0).getTitle());
Thread thread = threads.get(0);
final long lastSeen = thread.getLastSeenMessageTime() + thread.getLastSeenMessageNanos();
AtomicInteger numOfCacheResp = new AtomicInteger(0);
ChatListener historyListeners = new ChatListener() {
@Override
public void onGetHistory(String content, ChatResponse<ResultHistory> history) {
int invokeTimes = 0;
if (history.isCache()) {
invokeTimes = numOfCacheResp.getAndIncrement();
}
checkTimesIsValid(history, invokeTimes, lastSeen);
if (invokeTimes >= 2) {
resumeProcess();
}
}
};
chat.addListener(historyListeners);
RequestGetHistory requestGetHistoryBeforeLastSeenTime = new RequestGetHistory.Builder(thread.getId()).toTimeNanos(lastSeen).offset(0).count(25).order("desc").build();
presenter.getHistory(requestGetHistoryBeforeLastSeenTime, null);
RequestGetHistory requestGetHistoryAfterLastSeenTime = new RequestGetHistory.Builder(thread.getId()).fromTimeNanos(lastSeen).offset(0).count(25).order("asc").build();
presenter.getHistory(requestGetHistoryAfterLastSeenTime, null);
pauseProcess();
sleep(2000);
Mockito.verify(view, Mockito.atLeast(2)).onGetThreadHistory(Mockito.any());
}
use of com.fanap.podchat.model.ChatResponse in project pod-chat-android-sdk by FanapSoft.
the class ChatCore method reformatThreadParticipantsForRemove.
private ChatResponse<ResultParticipant> reformatThreadParticipantsForRemove(Callback callback, ChatMessage chatMessage) {
ArrayList<Participant> participants = new ArrayList<>();
if (!Util.isNullOrEmpty(chatMessage.getContent())) {
try {
participants = gson.fromJson(chatMessage.getContent(), new TypeToken<ArrayList<Participant>>() {
}.getType());
} catch (Exception e) {
showErrorLog(e.getMessage());
onUnknownException(chatMessage.getUniqueId(), e);
}
}
if (cache) {
List<CacheParticipant> cacheParticipants = new ArrayList<>();
if (!Util.isNullOrEmpty(chatMessage.getContent())) {
try {
cacheParticipants = gson.fromJson(chatMessage.getContent(), new TypeToken<ArrayList<CacheParticipant>>() {
}.getType());
} catch (JsonSyntaxException e) {
showErrorLog(e.getMessage());
onUnknownException(chatMessage.getUniqueId(), e);
}
}
if (!cacheParticipants.isEmpty()) {
messageDatabaseHelper.deleteParticipant(chatMessage.getSubjectId(), cacheParticipants.get(0).getId());
}
}
ChatResponse<ResultParticipant> outPutParticipant = new ChatResponse<>();
outPutParticipant.setErrorCode(0);
outPutParticipant.setErrorMessage("");
outPutParticipant.setHasError(false);
outPutParticipant.setUniqueId(chatMessage.getUniqueId());
outPutParticipant.setSubjectId(chatMessage.getSubjectId());
ResultParticipant resultParticipant = new ResultParticipant();
resultParticipant.setContentCount(chatMessage.getContentCount());
resultParticipant.setThreadId(chatMessage.getSubjectId());
if (callback != null) {
resultParticipant.setHasNext(participants.size() + callback.getOffset() < chatMessage.getContentCount());
resultParticipant.setNextOffset(callback.getOffset() + participants.size());
}
resultParticipant.setParticipants(participants);
outPutParticipant.setResult(resultParticipant);
return outPutParticipant;
}
use of com.fanap.podchat.model.ChatResponse in project pod-chat-android-sdk by FanapSoft.
the class ChatCore method handleRemoveRole.
private void handleRemoveRole(ChatMessage chatMessage) {
ChatResponse<ResultSetAdmin> chatResponse = new ChatResponse<>();
ResultSetAdmin resultSetAdmin = new ResultSetAdmin();
ArrayList<Admin> admins = gson.fromJson(chatMessage.getContent(), new TypeToken<ArrayList<Admin>>() {
}.getType());
resultSetAdmin.setAdmins(admins);
chatResponse.setResult(resultSetAdmin);
chatResponse.setUniqueId(chatMessage.getUniqueId());
chatResponse.setSubjectId(chatMessage.getSubjectId());
String responseJson = gson.toJson(chatResponse);
OutputSetRoleToUser output = new OutputSetRoleToUser();
output.setResultSetAdmin(resultSetAdmin);
long threadId = chatMessage.getSubjectId();
if (cache) {
dataSource.updateParticipantRoles(admins, threadId);
}
if (sentryResponseLog) {
showLog("RECEIVE_REMOVE_ROLE", responseJson);
} else {
showLog("RECEIVE_REMOVE_ROLE");
}
listenerManager.callOnRemoveRoleFromUser(responseJson, chatResponse);
}
use of com.fanap.podchat.model.ChatResponse in project pod-chat-android-sdk by FanapSoft.
the class ChatCore method sendFileMessage.
/**
* This method first check the messageType of the file and then choose the right
* server and send that
* <p>
* description Its the description that you want to send with file in the thread
* fileUri Uri of the file that you want to send to thread
* threadId Id of the thread that you want to send file
* systemMetaData [optional]
* handler it is for send file message with progress
*/
// public String sendFileMessage(RequestFileMessage requestFileMessage, ProgressHandler.sendFileMessage handler) {
//
// long threadId = requestFileMessage.getThreadId();
// Activity activity = requestFileMessage.getActivity();
// Uri fileUri = requestFileMessage.getFileUri();
// String description = requestFileMessage.getDescription();
// int messageType = requestFileMessage.getMessageType();
// String systemMetadata = requestFileMessage.getSystemMetadata();
//
// return sendFileMessage(activity, description, threadId, fileUri, systemMetadata, messageType, handler);
// }
public String sendFileMessage(RequestFileMessage requestFileMessage, ProgressHandler.sendFileMessage handler) {
String uniqueId = generateUniqueId();
if (needReadStoragePermission(requestFileMessage.getActivity())) {
String jsonError = captureError(ChatConstant.ERROR_READ_EXTERNAL_STORAGE_PERMISSION, ChatConstant.ERROR_CODE_READ_EXTERNAL_STORAGE_PERMISSION, uniqueId);
ErrorOutPut error = new ErrorOutPut(true, ChatConstant.ERROR_READ_EXTERNAL_STORAGE_PERMISSION, ChatConstant.ERROR_CODE_READ_EXTERNAL_STORAGE_PERMISSION, uniqueId);
if (handler != null) {
handler.onError(jsonError, error);
}
return uniqueId;
}
if (!chatReady) {
onChatNotReady(uniqueId);
return uniqueId;
}
if (getPodSpaceServer() == null) {
captureError("PodSpace server is null", 0, uniqueId);
return uniqueId;
}
try {
Subscription subscription = PodUploader.uploadToPodSpace(uniqueId, requestFileMessage.getFileUri(), requestFileMessage.getUserGroupHash(), context, getPodSpaceServer(), getToken(), TOKEN_ISSUER, requestFileMessage.getImageXc(), requestFileMessage.getImageYc(), requestFileMessage.getImageHc(), requestFileMessage.getImageWc(), new PodUploader.IPodUploadFileToPodSpace() {
@Override
public void onSuccess(UploadToPodSpaceResult response, File file, String mimeType, long length) {
removeFromUploadQueue(uniqueId);
ChatResponse<ResultFile> chatResponse = PodUploader.generateImageUploadResultForSendMessage(response, uniqueId);
String json = gson.toJson(chatResponse);
showLog("FILE_UPLOADED_TO_SERVER", json);
listenerManager.callOnUploadFile(json, chatResponse);
if (handler != null) {
handler.onFinishFile(json, chatResponse);
}
String jsonMeta = createFileMetadata(file, response.getHashCode(), 0, mimeType, length, response.getParentHash());
sendTextMessageWithFile(requestFileMessage.getDescription(), requestFileMessage.getThreadId(), jsonMeta, requestFileMessage.getSystemMetadata(), uniqueId, typeCode, requestFileMessage.getMessageType());
}
@Override
public void onSuccess(UploadToPodSpaceResult response, File file, String mimeType, long length, int actualWidth, int actualHeight, int width, int height) {
removeFromUploadQueue(uniqueId);
ChatResponse<ResultImageFile> chatResponse = PodUploader.generateImageUploadResultForSendMessage(response, uniqueId, actualWidth, actualHeight, width, height, getPodSpaceImageUrl(response.getHashCode()));
String imageJson = gson.toJson(chatResponse);
listenerManager.callOnUploadImageFile(imageJson, chatResponse);
if (handler != null) {
handler.onFinishImage(imageJson, chatResponse);
}
showLog("RECEIVE_UPLOAD_IMAGE", imageJson);
String jsonMeta = createImageMetadata(file, response.getHashCode(), 0, height, width, mimeType, length, response.getParentHash(), false, null);
sendTextMessageWithFile(requestFileMessage.getDescription(), requestFileMessage.getThreadId(), jsonMeta, requestFileMessage.getSystemMetadata(), uniqueId, typeCode, requestFileMessage.getMessageType());
}
@Override
public void onFailure(String cause, Throwable t) {
String jsonError = captureError(cause, ChatConstant.ERROR_CODE_UPLOAD_FILE, uniqueId, t);
ErrorOutPut error = new ErrorOutPut(true, ChatConstant.ERROR_INVALID_FILE_URI, ChatConstant.ERROR_CODE_INVALID_FILE_URI, uniqueId);
if (handler != null) {
handler.onError(jsonError, error);
}
}
@Override
public void onUploadStarted(String mimeType, File file, long length) {
addToUploadQueue(requestFileMessage.getDescription(), requestFileMessage.getFileUri(), requestFileMessage.getMessageType(), requestFileMessage.getThreadId(), requestFileMessage.getUserGroupHash(), uniqueId, requestFileMessage.getSystemMetadata(), mimeType, file, length);
showLog("UPLOAD_FILE_TO_SERVER_STARTED");
showLog(requestFileMessage.toString());
}
@Override
public void onProgressUpdate(int progress, int totalBytesSent, int totalBytesToSend) {
if (handler != null)
handler.onProgressUpdate(uniqueId, progress, totalBytesSent, totalBytesToSend);
}
});
initCancelUpload(uniqueId, subscription);
} catch (Exception e) {
String jsonError = captureError(ChatConstant.ERROR_INVALID_FILE_URI, ChatConstant.ERROR_CODE_INVALID_FILE_URI, uniqueId, e);
ErrorOutPut error = new ErrorOutPut(true, ChatConstant.ERROR_INVALID_FILE_URI, ChatConstant.ERROR_CODE_INVALID_FILE_URI, uniqueId);
if (handler != null) {
handler.onError(jsonError, error);
}
}
return uniqueId;
}
use of com.fanap.podchat.model.ChatResponse in project pod-chat-android-sdk by FanapSoft.
the class ChatCore method reformatGetThreadsResponse.
/**
* Reformat the get thread response
*/
private ChatResponse<ResultThreads> reformatGetThreadsResponse(ChatMessage chatMessage, Callback callback) {
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);
}
}
ResultThreads resultThreads = new ResultThreads();
resultThreads.setThreads(threads);
resultThreads.setContentCount(chatMessage.getContentCount());
outPutThreads.setErrorCode(0);
outPutThreads.setErrorMessage("");
outPutThreads.setHasError(false);
outPutThreads.setUniqueId(chatMessage.getUniqueId());
if (callback != null) {
resultThreads.setHasNext(threads.size() + callback.getOffset() < chatMessage.getContentCount());
resultThreads.setNextOffset(callback.getOffset() + threads.size());
}
outPutThreads.setResult(resultThreads);
return outPutThreads;
}
Aggregations