Search in sources :

Example 11 with PodThreadManager

use of com.fanap.podchat.util.PodThreadManager in project pod-chat-android-sdk by FanapSoft.

the class ChatCore method getThreadParticipantsMain.

private String getThreadParticipantsMain(int count, int offset, long threadId, String typeCode, boolean useCache, ChatHandler handler) {
    String uniqueId = generateUniqueId();
    final int mCount = count != 0 ? count : 50;
    PodThreadManager podThreadManager = new PodThreadManager();
    podThreadManager.addTask(() -> {
        if (cache && useCache) {
            loadParticipantsFromCache(uniqueId, mCount, offset, threadId);
        }
    });
    podThreadManager.addTask(() -> {
        if (chatReady) {
            JsonObject content = new JsonObject();
            content.addProperty("count", mCount);
            content.addProperty("offset", offset);
            AsyncMessage chatMessage = new AsyncMessage();
            chatMessage.setContent(content.toString());
            chatMessage.setType(Constants.THREAD_PARTICIPANTS);
            chatMessage.setTokenIssuer("1");
            chatMessage.setToken(getToken());
            chatMessage.setUniqueId(uniqueId);
            chatMessage.setSubjectId(threadId);
            chatMessage.setTypeCode(Util.isNullOrEmpty(typeCode) ? getTypeCode() : typeCode);
            JsonObject jsonObject = (JsonObject) gson.toJsonTree(chatMessage);
            String asyncContent = jsonObject.toString();
            setCallBacks(null, null, null, true, Constants.THREAD_PARTICIPANTS, (long) offset, uniqueId);
            sendAsyncMessage(asyncContent, AsyncAckType.Constants.WITHOUT_ACK, "SEND_GET_THREAD_PARTICIPANT");
            if (handler != null) {
                handler.onGetThreadParticipant(uniqueId);
            }
        } else {
            captureError(ChatConstant.ERROR_CHAT_READY, ChatConstant.ERROR_CODE_CHAT_READY, uniqueId);
        }
    });
    podThreadManager.runTasksSynced();
    return uniqueId;
}
Also used : PodThreadManager(com.fanap.podchat.util.PodThreadManager) AsyncMessage(com.fanap.podchat.mainmodel.AsyncMessage) JsonObject(com.google.gson.JsonObject)

Example 12 with PodThreadManager

use of com.fanap.podchat.util.PodThreadManager in project pod-chat-android-sdk by FanapSoft.

the class ChatCore method createThreadWithFile.

// 
// 1. Create thread.
// 2.1. Upload thread image and update thread info if RequestUploadImage is set.
// 2.2. Upload file to server with thread userGroupHash and send uploaded file as message by sendFileMessage.
// 
public ArrayList<String> createThreadWithFile(RequestCreateThreadWithFile request, @Nullable ProgressHandler.sendFileMessage progressHandler) {
    ArrayList<String> uniqueIds = new ArrayList<>();
    String requestUniqueId = generateUniqueId();
    uniqueIds.add(requestUniqueId);
    if (needReadStoragePermission(request.getFile().getActivity()))
        return uniqueIds;
    if (chatReady) {
        handlerSend.put(requestUniqueId, new ChatHandler() {

            @Override
            public void onThreadCreated(ResultThread thread) {
                super.onThreadCreated(thread);
                if (request.getUploadThreadImageRequest() != null)
                    new PodThreadManager().doThisAndGo(() -> updateThreadImage(thread, request.getUploadThreadImageRequest()));
                RequestFileMessage.Builder requestFileBuilder = new RequestFileMessage.Builder(request.getMessage() != null ? request.getMessage().getText() : null, request.getFile().getActivity(), thread.getThread().getId(), request.getFile().getFileUri(), request.getMessage() != null ? request.getMessage().getSystemMetadata() : null, request.getMessageType(), thread.getThread().getUserGroupHash());
                if (request.getFile() instanceof RequestUploadImage) {
                    requestFileBuilder.setImageHc(String.valueOf(((RequestUploadImage) request.getFile()).gethC()));
                    requestFileBuilder.setImageWc(String.valueOf(((RequestUploadImage) request.getFile()).getwC()));
                    requestFileBuilder.setImageXc(String.valueOf(((RequestUploadImage) request.getFile()).getxC()));
                    requestFileBuilder.setImageYc(String.valueOf(((RequestUploadImage) request.getFile()).getyC()));
                }
                RequestFileMessage requestFile = requestFileBuilder.build();
                sendFileMessage(requestFile, requestUniqueId, progressHandler);
            }
        });
        RequestCreateThread requestCreateThread = new RequestCreateThread.Builder(request.getType(), request.getInvitees()).title(request.getTitle()).withDescription(request.getDescription()).withImage(request.getImage()).withMetadata(request.getMessage() != null ? request.getMessage().getSystemMetadata() : null).build();
        createThread(requestCreateThread, requestUniqueId);
    // prepareCreateThreadWithFile(request, requestUniqueId, innerMessageUniqueId, forwardUniqueIds, "");
    } else {
        captureError(ChatConstant.ERROR_CHAT_READY, ChatConstant.ERROR_CODE_CHAT_READY, requestUniqueId);
    }
    return uniqueIds;
}
Also used : PodThreadManager(com.fanap.podchat.util.PodThreadManager) RequestUploadImage(com.fanap.podchat.requestobject.RequestUploadImage) GsonBuilder(com.google.gson.GsonBuilder) ArrayList(java.util.ArrayList) RequestCreateThread(com.fanap.podchat.requestobject.RequestCreateThread) ResultThread(com.fanap.podchat.model.ResultThread) RequestFileMessage(com.fanap.podchat.requestobject.RequestFileMessage)

Example 13 with PodThreadManager

use of com.fanap.podchat.util.PodThreadManager in project pod-chat-android-sdk by FanapSoft.

the class ChatCore method getUserInfo.

/**
 * It Gets the information of the current user
 * <p>
 * set useCache = false to disable cache for only this function
 */
public String getUserInfo(boolean useCache, ChatHandler handler) {
    String uniqueId = generateUniqueId();
    Runnable cacheLoading = () -> {
        if (cache && useCache) {
            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 (chatReady) {
            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);
            }
        } else
            onChatNotReady(uniqueId);
    };
    new PodThreadManager().addNewTask(cacheLoading).addNewTask(requestServer).runTasksSynced();
    return uniqueId;
}
Also used : PodThreadManager(com.fanap.podchat.util.PodThreadManager) ChatMessage(com.fanap.podchat.mainmodel.ChatMessage) ResultUserInfo(com.fanap.podchat.model.ResultUserInfo) ChatResponse(com.fanap.podchat.model.ChatResponse) JsonObject(com.google.gson.JsonObject) ResultUserInfo(com.fanap.podchat.model.ResultUserInfo) UserInfo(com.fanap.podchat.mainmodel.UserInfo) JSONException(org.json.JSONException) SentryException(io.sentry.core.protocol.SentryException) IOException(java.io.IOException) JsonSyntaxException(com.google.gson.JsonSyntaxException) PodChatException(com.fanap.podchat.util.PodChatException) RoomIntegrityException(com.fanap.podchat.persistance.RoomIntegrityException)

Aggregations

PodThreadManager (com.fanap.podchat.util.PodThreadManager)13 ChatResponse (com.fanap.podchat.model.ChatResponse)5 RoomIntegrityException (com.fanap.podchat.persistance.RoomIntegrityException)5 PodChatException (com.fanap.podchat.util.PodChatException)5 JsonObject (com.google.gson.JsonObject)5 JsonSyntaxException (com.google.gson.JsonSyntaxException)5 SentryException (io.sentry.core.protocol.SentryException)5 IOException (java.io.IOException)5 ArrayList (java.util.ArrayList)5 JSONException (org.json.JSONException)5 AsyncMessage (com.fanap.podchat.mainmodel.AsyncMessage)3 ResultBlockList (com.fanap.podchat.model.ResultBlockList)3 PhoneContact (com.fanap.podchat.cachemodel.PhoneContact)2 RequestGetHashTagList (com.fanap.podchat.chat.hashtag.model.RequestGetHashTagList)2 RequestGetMentionList (com.fanap.podchat.chat.mention.model.RequestGetMentionList)2 ChatMessage (com.fanap.podchat.mainmodel.ChatMessage)2 UserInfo (com.fanap.podchat.mainmodel.UserInfo)2 Contacts (com.fanap.podchat.model.Contacts)2 ResultUserInfo (com.fanap.podchat.model.ResultUserInfo)2 RequestBlockList (com.fanap.podchat.requestobject.RequestBlockList)2