Search in sources :

Example 1 with LocalFileType

use of net.iGap.module.enums.LocalFileType in project iGap-Android by KianIranian-STDG.

the class MessageDataStorage method createForwardMessage.

public void createForwardMessage(final long destinationRoomId, final long newMessageId, MessageObject sourceMessage, boolean isMessage, DatabaseDelegate databaseDelegate) {
    storageQueue.postRunnable(() -> {
        FileLog.i(TAG, "createForwardMessage: " + destinationRoomId + " " + newMessageId);
        try {
            final RealmRoomMessage[] forwardedMessage = new RealmRoomMessage[1];
            MessageObject messageObject = null;
            RealmRoomMessage copyMessage = null;
            database.beginTransaction();
            if (isMessage && sourceMessage.forwardedMessage == null) {
                forwardedMessage[0] = database.createObject(RealmRoomMessage.class, newMessageId);
                forwardedMessage[0].setCreateTime(TimeUtils.currentLocalTime());
                forwardedMessage[0].setRoomId(destinationRoomId);
                forwardedMessage[0].setStatus(ProtoGlobal.RoomMessageStatus.SENDING.toString());
                forwardedMessage[0].setMessageType(ProtoGlobal.RoomMessageType.forNumber(sourceMessage.messageType));
                forwardedMessage[0].setMessage(sourceMessage.message);
                if (sourceMessage.attachment != null) {
                    AttachmentObject attObject = sourceMessage.attachment;
                    LocalFileType type = attObject.filePath == null ? LocalFileType.THUMBNAIL : LocalFileType.FILE;
                    String filePath = attObject.filePath != null ? attObject.filePath : attObject.thumbnailPath;
                    forwardedMessage[0].setAttachment(newMessageId, filePath, attObject.width, attObject.height, attObject.size, attObject.name, attObject.duration, attObject.token, type);
                }
                if (sourceMessage.contact != null) {
                    RoomContactObject contactObject = sourceMessage.contact;
                    ProtoGlobal.RoomMessageContact.Builder builder = ProtoGlobal.RoomMessageContact.newBuilder();
                    builder.setPhone(0, contactObject.phones.get(0));
                    builder.setFirstName(contactObject.firstName);
                    builder.setLastName(contactObject.lastName);
                    builder.setEmail(0, contactObject.emails.get(0));
                    RealmRoomMessageContact roomMessageContact = RealmRoomMessageContact.put(database, builder.build());
                    forwardedMessage[0].setRoomMessageContact(roomMessageContact);
                }
                forwardedMessage[0].setUserId(AccountManager.getInstance().getCurrentUser().getId());
                database.copyToRealmOrUpdate(forwardedMessage[0]);
                copyMessage = database.copyFromRealm(forwardedMessage[0]);
            } else {
                RealmRoomMessage roomMessage = database.where(RealmRoomMessage.class).equalTo("messageId", sourceMessage.id).findFirst();
                if (roomMessage != null) {
                    forwardedMessage[0] = database.createObject(RealmRoomMessage.class, newMessageId);
                    if (roomMessage.getForwardMessage() != null) {
                        forwardedMessage[0].setForwardMessage(roomMessage.getForwardMessage());
                        forwardedMessage[0].setHasMessageLink(roomMessage.getForwardMessage().getHasMessageLink());
                    } else {
                        forwardedMessage[0].setForwardMessage(roomMessage);
                        forwardedMessage[0].setHasMessageLink(roomMessage.getHasMessageLink());
                    }
                    forwardedMessage[0].setCreateTime(TimeUtils.currentLocalTime());
                    forwardedMessage[0].setMessageType(ProtoGlobal.RoomMessageType.TEXT);
                    forwardedMessage[0].setRoomId(destinationRoomId);
                    forwardedMessage[0].setStatus(ProtoGlobal.RoomMessageStatus.SENDING.toString());
                    forwardedMessage[0].setUserId(AccountManager.getInstance().getCurrentUser().getId());
                    copyMessage = database.copyFromRealm(forwardedMessage[0]);
                }
            }
            messageObject = MessageObject.create(copyMessage);
            RealmRoomMessage realmSourceMessage = database.where(RealmRoomMessage.class).equalTo("messageId", sourceMessage.id).findFirst();
            assert realmSourceMessage != null;
            RealmRoomMessage copyOfSource = database.copyFromRealm(realmSourceMessage);
            database.commitTransaction();
            MessageObject finalMessageObject = messageObject;
            RealmRoomMessage finalCopyMessage = copyMessage;
            G.runOnUiThread(() -> {
                databaseDelegate.run(finalMessageObject, finalCopyMessage, copyOfSource.getRoomId(), copyOfSource.getMessageId());
            });
        } catch (Exception e) {
            FileLog.e(e);
        }
    });
}
Also used : LocalFileType(net.iGap.module.enums.LocalFileType) RealmRoomMessageContact(net.iGap.realm.RealmRoomMessageContact) AttachmentObject(net.iGap.structs.AttachmentObject) RoomContactObject(net.iGap.structs.RoomContactObject) RealmRoomMessage(net.iGap.realm.RealmRoomMessage) MessageObject(net.iGap.structs.MessageObject) RealmRoomMessageContact(net.iGap.realm.RealmRoomMessageContact)

Aggregations

LocalFileType (net.iGap.module.enums.LocalFileType)1 RealmRoomMessage (net.iGap.realm.RealmRoomMessage)1 RealmRoomMessageContact (net.iGap.realm.RealmRoomMessageContact)1 AttachmentObject (net.iGap.structs.AttachmentObject)1 MessageObject (net.iGap.structs.MessageObject)1 RoomContactObject (net.iGap.structs.RoomContactObject)1