Search in sources :

Example 6 with RequestClientGetRoom

use of net.iGap.request.RequestClientGetRoom in project iGap-Android by KianIranian-STDG.

the class HelperMessageResponse method handleMessage.

public static void handleMessage(final long roomId, final ProtoGlobal.RoomMessage roomMessage, final ProtoGlobal.Room.Type roomType, final ProtoResponse.Response response, final String identity) {
    DbManager.getInstance().doRealmTransaction(realm -> {
        /**
         * put message to realm
         */
        FileLog.i("HelperMessageResponse roomType -> " + roomType.toString() + " messageId -> " + roomMessage.getMessageId() + " roomId -> " + roomId);
        RealmRoomMessage realmRoomMessage = RealmRoomMessage.putOrUpdate(realm, roomId, roomMessage, new StructMessageOption().setGap());
        final RealmRoom room = realm.where(RealmRoom.class).equalTo("id", roomId).findFirst();
        /**
         * because user may have more than one device, his another device should not
         * be recipient but sender. so I check current userId with room message user id,
         * and if not equals and response is null, so we sure recipient is another user
         */
        if (!roomMessage.getAuthor().getHash().equals(RealmUserInfo.getCurrentUserAuthorHash())) {
            /**
             * i'm recipient
             *
             * if author has user check that client have latest info for this user or no
             * if author don't have use this means that message is from channel so client
             * don't have user id for message sender for get info
             */
            if (roomMessage.getAuthor().hasUser()) {
                RealmRegisteredInfo.needUpdateUser(roomMessage.getAuthor().getUser().getUserId(), roomMessage.getAuthor().getUser().getCacheId());
            }
        // } else if (!response.getId().isEmpty()) {
        // /**
        // * i'm the sender
        // *
        // * delete message that created with fake messageId as identity
        // * because in new version of realm client can't update primary key
        // */
        // RealmRoomMessage.deleteMessage(realm, Long.parseLong(identity));
        }
        if (identity != null && identity.length() > 0) {
            RealmRoomMessage.deleteMessage(realm, Long.parseLong(identity), roomId);
        }
        if (room == null) {
            /**
             * if first message received but the room doesn't exist, send request for create new room
             */
            new RequestClientGetRoom().clientGetRoom(roomId, null);
        } else {
            if (room.getType() == ProtoGlobal.Room.Type.CHAT)
                room.setDeleted(false);
            if (!roomMessage.getAuthor().getHash().equals(RealmUserInfo.getCurrentUserAuthorHash()) && (room.getLastMessage() == null || (room.getLastMessage() != null && room.getLastMessage().getMessageId() < roomMessage.getMessageId()))) {
                room.setUnreadCount(room.getUnreadCount() + 1);
            }
            if (!roomMessage.getAuthor().getHash().equals(RealmUserInfo.getCurrentUserAuthorHash())) {
                if (room.getFirstUnreadMessage() == null) {
                    room.setFirstUnreadMessage(realmRoomMessage);
                }
                if (roomMessage.getStatus() != ProtoGlobal.RoomMessageStatus.SEEN && RealmNotificationRoomMessage.canShowNotif(realm, roomMessage.getMessageId(), roomId)) {
                    RealmNotificationRoomMessage.putToDataBase(realm, roomMessage.getMessageId(), roomId);
                    HelperNotification.getInstance().addMessage(roomId, roomMessage, roomType, room, realm, AccountManager.getInstance().getCurrentUser());
                }
            }
            /**
             * update last message sent/received in room table
             */
            if (room.getLastMessage() != null) {
                if (room.getLastMessage().getMessageId() <= roomMessage.getMessageId()) {
                    room.setLastMessage(realmRoomMessage);
                }
            } else {
                room.setLastMessage(realmRoomMessage);
            }
        }
    });
    if (response.getId().isEmpty()) {
        /**
         * invoke following callback when i'm not the sender, because i already done everything after sending message
         */
        ChatSendMessageUtil.getInstance(AccountManager.selectedAccount).onMessageReceive(roomId, roomMessage.getMessage(), roomMessage.getMessageType(), roomMessage, roomType);
    } else {
        /**
         * invoke following callback when I'm the sender and the message has updated
         */
        ChatSendMessageUtil.getInstance(AccountManager.selectedAccount).onMessageUpdate(roomId, roomMessage.getMessageId(), roomMessage.getStatus(), identity, roomMessage);
    }
    if ((roomMessage.getAuthor().getUser().getUserId() == AccountManager.getInstance().getCurrentUser().getId() || roomMessage.getAuthor().getUser().getUserId() == 0) && roomMessage.getAttachment() != null) {
        G.runOnUiThread(() -> EventManager.getInstance(AccountManager.selectedAccount).postEvent(EventManager.ON_UPLOAD_COMPLETED, roomMessage.getMessageType(), roomMessage.getMessageId(), roomMessage.getAttachment().getCacheId(), roomMessage.getAttachment().getToken()));
    }
}
Also used : StructMessageOption(net.iGap.module.structs.StructMessageOption) RequestClientGetRoom(net.iGap.request.RequestClientGetRoom) RealmRoomMessage(net.iGap.realm.RealmRoomMessage) RealmRoom(net.iGap.realm.RealmRoom)

Example 7 with RequestClientGetRoom

use of net.iGap.request.RequestClientGetRoom in project iGap-Android by KianIranian-STDG.

the class BotInit method checkDrIgap.

public static void checkDrIgap() {
    if (!BotInit.getCheckDrIgap())
        return;
    new RequestClientGetPromote().getPromote();
    G.ipromote = new Ipromote() {

        @Override
        public void onGetPromoteResponse(ProtoClientGetPromote.ClientGetPromoteResponse.Builder builder) {
            setCheckDrIgap(false);
            ArrayList<Long> promoteIds = new ArrayList<>();
            for (int i = 0; i < builder.getPromoteList().size(); i++) promoteIds.add(builder.getPromoteList().get(i).getId());
            DbManager.getInstance().doRealmTask(realm -> {
                realm.executeTransaction(new Realm.Transaction() {

                    @Override
                    public void execute(Realm realm) {
                        RealmResults<RealmRoom> roomList = realm.where(RealmRoom.class).equalTo("isFromPromote", true).findAll();
                        for (RealmRoom room : roomList) {
                            if (!promoteIds.contains(room.getPromoteId())) {
                                room.setFromPromote(false);
                                RoomController.getInstance(AccountManager.selectedAccount).clientPinRoom(room.getId(), false);
                            }
                        }
                    }
                });
                for (int i = builder.getPromoteList().size() - 1; i >= 0; i--) {
                    ProtoClientGetPromote.ClientGetPromoteResponse.Promote.Type TYPE = builder.getPromoteList().get(i).getType();
                    RealmRoom realmRoom;
                    if (TYPE == ProtoClientGetPromote.ClientGetPromoteResponse.Promote.Type.USER) {
                        realmRoom = realm.where(RealmRoom.class).equalTo("chatRoom.peer_id", builder.getPromoteList().get(i).getId()).equalTo("isFromPromote", true).findFirst();
                    } else {
                        realmRoom = realm.where(RealmRoom.class).equalTo("id", builder.getPromoteList().get(i).getId()).equalTo("isFromPromote", true).findFirst();
                    }
                    if (realmRoom == null) {
                        if (TYPE == ProtoClientGetPromote.ClientGetPromoteResponse.Promote.Type.USER) {
                            // RealmRoom.setPromote(builder.getPromoteList().get(i).getId(), TYPE);
                            G.onChatGetRoom = new OnChatGetRoom() {

                                @Override
                                public void onChatGetRoom(final ProtoGlobal.Room room) {
                                    G.onChatGetRoom = null;
                                    DbManager.getInstance().doRealmTransaction(realm1 -> {
                                        RealmRoom realmRoom1 = RealmRoom.putOrUpdate(room, realm1);
                                        realmRoom1.setFromPromote(true);
                                        realmRoom1.setPromoteId(realmRoom1.getChatRoom().getPeerId());
                                    // RealmRoom.setPromote(2297310L, ProtoClientGetPromote.ClientGetPromoteResponse.Promote.Type.USER);
                                    });
                                    RoomController.getInstance(AccountManager.selectedAccount).clientPinRoom(room.getId(), true);
                                    ActivityPopUpNotification.sendMessage("/start", room.getId(), ProtoGlobal.Room.Type.CHAT);
                                }

                                @Override
                                public void onChatGetRoomTimeOut() {
                                }

                                @Override
                                public void onChatGetRoomError(int majorCode, int minorCode) {
                                }
                            };
                            new RequestChatGetRoom().chatGetRoom(builder.getPromoteList().get(i).getId());
                        } else {
                            new RequestClientGetRoom().clientGetRoom(builder.getPromoteList().get(i).getId(), RequestClientGetRoom.CreateRoomMode.getPromote);
                        }
                    } else {
                        RoomController.getInstance(AccountManager.selectedAccount).clientPinRoom(realmRoom.getId(), true);
                    }
                }
            });
        }
    };
}
Also used : LinearLayout(android.widget.LinearLayout) TypeToken(com.google.gson.reflect.TypeToken) NonNull(androidx.annotation.NonNull) RequestClientGetRoom(net.iGap.request.RequestClientGetRoom) GsonBuilder(com.google.gson.GsonBuilder) G(net.iGap.G) RealmUserInfo(net.iGap.realm.RealmUserInfo) JSONException(org.json.JSONException) AdditionalType(net.iGap.module.additionalData.AdditionalType) Gson(com.google.gson.Gson) View(android.view.View) ProtoClientGetPromote(net.iGap.proto.ProtoClientGetPromote) ContextCompat(androidx.core.content.ContextCompat) Realm(io.realm.Realm) RealmRoom(net.iGap.realm.RealmRoom) AppCompatButton(androidx.appcompat.widget.AppCompatButton) ViewGroup(android.view.ViewGroup) OnChatGetRoom(net.iGap.observers.interfaces.OnChatGetRoom) TextView(android.widget.TextView) ViewMaker.i_Dp(net.iGap.adapter.items.chat.ViewMaker.i_Dp) G.isLocationFromBot(net.iGap.G.isLocationFromBot) G.context(net.iGap.G.context) RelativeLayout(android.widget.RelativeLayout) MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) RequestChatGetRoom(net.iGap.request.RequestChatGetRoom) AppCompatTextView(androidx.appcompat.widget.AppCompatTextView) Context(android.content.Context) ResourcesCompat(androidx.core.content.res.ResourcesCompat) ActivityPopUpNotification(net.iGap.activities.ActivityPopUpNotification) HelperUrl(net.iGap.helper.HelperUrl) AccountManager(net.iGap.module.accountManager.AccountManager) HashMap(java.util.HashMap) InputMethodManager(android.view.inputmethod.InputMethodManager) ArrayList(java.util.ArrayList) ButtonActionType(net.iGap.module.additionalData.ButtonActionType) Toast(android.widget.Toast) RealmRoomMessage(net.iGap.realm.RealmRoomMessage) Ipromote(net.iGap.observers.interfaces.Ipromote) RoomController(net.iGap.controllers.RoomController) MessageObject(net.iGap.structs.MessageObject) RealmResults(io.realm.RealmResults) RequestClientGetPromote(net.iGap.request.RequestClientGetPromote) DialogAction(com.afollestad.materialdialogs.DialogAction) Color(android.graphics.Color) Gravity(android.view.Gravity) SharedPreferences(android.content.SharedPreferences) ViewMaker(net.iGap.adapter.items.chat.ViewMaker) ButtonEntity(net.iGap.module.additionalData.ButtonEntity) ProtoGlobal(net.iGap.proto.ProtoGlobal) R(net.iGap.R) Activity(android.app.Activity) DbManager(net.iGap.module.accountManager.DbManager) JSONArray(org.json.JSONArray) RequestChatGetRoom(net.iGap.request.RequestChatGetRoom) ArrayList(java.util.ArrayList) RequestClientGetPromote(net.iGap.request.RequestClientGetPromote) Ipromote(net.iGap.observers.interfaces.Ipromote) AdditionalType(net.iGap.module.additionalData.AdditionalType) ButtonActionType(net.iGap.module.additionalData.ButtonActionType) OnChatGetRoom(net.iGap.observers.interfaces.OnChatGetRoom) RequestClientGetRoom(net.iGap.request.RequestClientGetRoom) RealmRoom(net.iGap.realm.RealmRoom) Realm(io.realm.Realm) RequestClientGetRoom(net.iGap.request.RequestClientGetRoom) RealmRoom(net.iGap.realm.RealmRoom) OnChatGetRoom(net.iGap.observers.interfaces.OnChatGetRoom) RequestChatGetRoom(net.iGap.request.RequestChatGetRoom)

Example 8 with RequestClientGetRoom

use of net.iGap.request.RequestClientGetRoom in project iGap-Android by KianIranian-STDG.

the class RealmRoom method needUpdateRoomInfo.

/**
 * if room isn't exist get info from server
 */
public static boolean needUpdateRoomInfo(long roomId) {
    return DbManager.getInstance().doRealmTask(realm -> {
        RealmRoom realmRoom = realm.where(RealmRoom.class).equalTo("id", roomId).findFirst();
        if (realmRoom != null) {
            return false;
        }
        new RequestClientGetRoom().clientGetRoom(roomId, RequestClientGetRoom.CreateRoomMode.justInfo);
        return true;
    });
}
Also used : RequestClientGetRoom(net.iGap.request.RequestClientGetRoom)

Example 9 with RequestClientGetRoom

use of net.iGap.request.RequestClientGetRoom in project iGap-Android by KianIranian-STDG.

the class HelperGetOwnerInfo method checkRoomExist.

private static void checkRoomExist(long id, final Listener listener) {
    DbManager.getInstance().doRealmTask(realm -> {
        RealmRoom realmRoom = realm.where(RealmRoom.class).equalTo("id", id).findFirst();
        if (realmRoom == null) {
            G.onClientGetRoomResponse = new OnClientGetRoomResponse() {

                @Override
                public void onClientGetRoomResponse(ProtoGlobal.Room room, ProtoClientGetRoom.ClientGetRoomResponse.Builder builder, RequestClientGetRoom.IdentityClientGetRoom identity) {
                    if (identity.createRoomMode == RequestClientGetRoom.CreateRoomMode.requestFromOwner) {
                        if (listener != null) {
                            listener.OnResponse();
                        }
                    }
                }

                @Override
                public void onError(int majorCode, int minorCode) {
                }

                @Override
                public void onTimeOut() {
                }
            };
            new RequestClientGetRoom().clientGetRoom(id, RequestClientGetRoom.CreateRoomMode.requestFromOwner);
        } else {
            if (listener != null) {
                listener.OnResponse();
            }
        }
    });
}
Also used : RequestClientGetRoom(net.iGap.request.RequestClientGetRoom) OnClientGetRoomResponse(net.iGap.observers.interfaces.OnClientGetRoomResponse) ProtoGlobal(net.iGap.proto.ProtoGlobal) RealmRoom(net.iGap.realm.RealmRoom) OnClientGetRoomResponse(net.iGap.observers.interfaces.OnClientGetRoomResponse)

Example 10 with RequestClientGetRoom

use of net.iGap.request.RequestClientGetRoom in project iGap-Android by KianIranian-STDG.

the class HelperLogMessage method getAuthorName.

private static String getAuthorName(StructMyLog log, Realm realm) throws InvalidProtocolBufferException {
    String result = "";
    if (ProtoGlobal.RoomMessage.Author.parseFrom(log.author).hasUser()) {
        result = MessageDataStorage.getInstance(AccountManager.selectedAccount).getDisplayNameWithUserId(ProtoGlobal.RoomMessage.Author.parseFrom(log.author).getUser().getUserId());
        if (result != null) {
            result = " " + result + " ";
        } else {
            logMessageUpdateList.put(ProtoGlobal.RoomMessage.Author.parseFrom(log.author).getUser().getUserId(), log);
            new RequestUserInfo().userInfoAvoidDuplicate(ProtoGlobal.RoomMessage.Author.parseFrom(log.author).getUser().getUserId());
        }
    } else if (ProtoGlobal.RoomMessage.Author.parseFrom(log.author).hasRoom()) {
        RealmRoom realmRoom = MessageDataStorage.getInstance(AccountManager.selectedAccount).getRoom(ProtoGlobal.RoomMessage.Author.parseFrom(log.author).getRoom().getRoomId());
        ;
        if (realmRoom != null) {
            result = " " + realmRoom.getTitle() + " ";
        } else {
            logMessageUpdateList.put(ProtoGlobal.RoomMessage.Author.parseFrom(log.author).getRoom().getRoomId(), log);
            RealmRoom.needUpdateRoomInfo(ProtoGlobal.RoomMessage.Author.parseFrom(log.author).getRoom().getRoomId());
            new RequestClientGetRoom().clientGetRoom(ProtoGlobal.RoomMessage.Author.parseFrom(log.author).getRoom().getRoomId(), RequestClientGetRoom.CreateRoomMode.justInfo);
        }
    }
    return result;
}
Also used : RequestClientGetRoom(net.iGap.request.RequestClientGetRoom) RequestUserInfo(net.iGap.request.RequestUserInfo) RealmRoom(net.iGap.realm.RealmRoom)

Aggregations

RequestClientGetRoom (net.iGap.request.RequestClientGetRoom)13 RealmRoom (net.iGap.realm.RealmRoom)7 OnClientGetRoomResponse (net.iGap.observers.interfaces.OnClientGetRoomResponse)4 ProtoGlobal (net.iGap.proto.ProtoGlobal)4 ArrayList (java.util.ArrayList)3 Realm (io.realm.Realm)2 RealmResults (io.realm.RealmResults)2 DbManager (net.iGap.module.accountManager.DbManager)2 OnChatGetRoom (net.iGap.observers.interfaces.OnChatGetRoom)2 RealmRoomMessage (net.iGap.realm.RealmRoomMessage)2 RequestChatGetRoom (net.iGap.request.RequestChatGetRoom)2 Activity (android.app.Activity)1 Context (android.content.Context)1 SharedPreferences (android.content.SharedPreferences)1 Color (android.graphics.Color)1 SpannableStringBuilder (android.text.SpannableStringBuilder)1 TextPaint (android.text.TextPaint)1 Gravity (android.view.Gravity)1 View (android.view.View)1 ViewGroup (android.view.ViewGroup)1