Search in sources :

Example 21 with MessageItem

use of com.xabber.android.data.database.messagerealm.MessageItem in project xabber-android by redsolution.

the class MessageManager method processCarbonsMessage.

public void processCarbonsMessage(AccountJid account, final Message message, CarbonExtension.Direction direction) {
    if (direction == CarbonExtension.Direction.sent) {
        UserJid companion;
        try {
            companion = UserJid.from(message.getTo()).getBareUserJid();
        } catch (UserJid.UserJidCreateException e) {
            return;
        }
        AbstractChat chat = getChat(account, companion);
        if (chat == null) {
            chat = createChat(account, companion);
        }
        final String body = message.getBody();
        if (body == null) {
            return;
        }
        final AbstractChat finalChat = chat;
        // TODO: 12.03.18 ANR - WRITE (переписать без UI)
        final long startTime = System.currentTimeMillis();
        MessageDatabaseManager.getInstance().getRealmUiThread().executeTransaction(new Realm.Transaction() {

            @Override
            public void execute(Realm realm) {
                MessageItem newMessageItem = finalChat.createNewMessageItem(body);
                newMessageItem.setStanzaId(message.getStanzaId());
                newMessageItem.setSent(true);
                newMessageItem.setForwarded(true);
                realm.copyToRealm(newMessageItem);
                LogManager.d("REALM", Thread.currentThread().getName() + " save carbons message: " + (System.currentTimeMillis() - startTime));
            }
        });
        EventBus.getDefault().post(new NewMessageEvent());
        return;
    }
    UserJid companion = null;
    try {
        companion = UserJid.from(message.getFrom()).getBareUserJid();
    } catch (UserJid.UserJidCreateException e) {
        return;
    }
    // check for spam
    if (SettingsManager.spamFilterMode() != SettingsManager.SpamFilterMode.disabled && RosterManager.getInstance().getRosterContact(account, companion) == null) {
        // just ignore carbons from not-authorized user
        return;
    }
    boolean processed = false;
    for (AbstractChat chat : chats.getNested(account.toString()).values()) {
        if (chat.onPacket(companion, message, true)) {
            processed = true;
            break;
        }
    }
    if (getChat(account, companion) != null) {
        return;
    }
    if (processed) {
        return;
    }
    final String body = message.getBody();
    if (body == null) {
        return;
    }
    createChat(account, companion).onPacket(companion, message, true);
}
Also used : MessageItem(com.xabber.android.data.database.messagerealm.MessageItem) UserJid(com.xabber.android.data.entity.UserJid) Realm(io.realm.Realm)

Example 22 with MessageItem

use of com.xabber.android.data.database.messagerealm.MessageItem in project xabber-android by redsolution.

the class MessageManager method exportChat.

/**
 * Export chat to file with specified name.
 *
 * @param account
 * @param user
 * @param fileName
 * @throws NetworkException
 */
public File exportChat(AccountJid account, UserJid user, String fileName) throws NetworkException {
    final File file = new File(Environment.getExternalStorageDirectory(), fileName);
    try {
        BufferedWriter out = new BufferedWriter(new FileWriter(file));
        final String titleName = RosterManager.getInstance().getName(account, user) + " (" + user + ")";
        out.write("<html><head><title>");
        out.write(StringUtils.escapeHtml(titleName));
        out.write("</title></head><body>");
        final AbstractChat abstractChat = getChat(account, user);
        if (abstractChat != null) {
            final boolean isMUC = abstractChat instanceof RoomChat;
            final String accountName = AccountManager.getInstance().getNickName(account);
            final String userName = RosterManager.getInstance().getName(account, user);
            Realm realm = MessageDatabaseManager.getInstance().getNewBackgroundRealm();
            RealmResults<MessageItem> messageItems = MessageDatabaseManager.getChatMessages(realm, account, user);
            for (MessageItem messageItem : messageItems) {
                if (messageItem.getAction() != null) {
                    continue;
                }
                final String name;
                if (isMUC) {
                    name = messageItem.getResource().toString();
                } else {
                    if (messageItem.isIncoming()) {
                        name = userName;
                    } else {
                        name = accountName;
                    }
                }
                out.write("<b>");
                out.write(StringUtils.escapeHtml(name));
                out.write("</b>&nbsp;(");
                out.write(StringUtils.getDateTimeText(new Date(messageItem.getTimestamp())));
                out.write(")<br />\n<p>");
                out.write(StringUtils.escapeHtml(messageItem.getText()));
                out.write("</p><hr />\n");
            }
            realm.close();
        }
        out.write("</body></html>");
        out.close();
    } catch (IOException e) {
        throw new NetworkException(R.string.FILE_NOT_FOUND);
    }
    return file;
}
Also used : MessageItem(com.xabber.android.data.database.messagerealm.MessageItem) FileWriter(java.io.FileWriter) IOException(java.io.IOException) RoomChat(com.xabber.android.data.extension.muc.RoomChat) Date(java.util.Date) BufferedWriter(java.io.BufferedWriter) File(java.io.File) Realm(io.realm.Realm) NetworkException(com.xabber.android.data.NetworkException)

Example 23 with MessageItem

use of com.xabber.android.data.database.messagerealm.MessageItem in project xabber-android by redsolution.

the class ReceiptManager method markAsError.

private void markAsError(final AccountJid account, final Message message) {
    Realm realm = MessageDatabaseManager.getInstance().getNewBackgroundRealm();
    realm.beginTransaction();
    MessageItem first = realm.where(MessageItem.class).equalTo(MessageItem.Fields.ACCOUNT, account.toString()).equalTo(MessageItem.Fields.STANZA_ID, message.getStanzaId()).findFirst();
    if (first != null) {
        first.setError(true);
        XMPPError error = message.getError();
        if (error != null) {
            String errorStr = error.toString();
            String descr = error.getDescriptiveText(null);
            first.setErrorDescription(errorStr + "\n" + descr);
        }
    }
    realm.commitTransaction();
    realm.close();
    EventBus.getDefault().post(new MessageUpdateEvent(account));
}
Also used : MessageItem(com.xabber.android.data.database.messagerealm.MessageItem) XMPPError(org.jivesoftware.smack.packet.XMPPError) Realm(io.realm.Realm)

Example 24 with MessageItem

use of com.xabber.android.data.database.messagerealm.MessageItem in project xabber-android by redsolution.

the class MessageDatabaseManager method copyDataFromSqliteToRealm.

void copyDataFromSqliteToRealm() {
    Realm realm = getNewBackgroundRealm();
    realm.beginTransaction();
    LogManager.i("DatabaseManager", "copying from sqlite to Reaml");
    long counter = 0;
    Cursor cursor = MessageTable.getInstance().getAllMessages();
    while (cursor.moveToNext()) {
        try {
            MessageItem messageItem = MessageTable.createMessageItem(cursor);
            realm.copyToRealm(messageItem);
        } catch (XmppStringprepException | UserJid.UserJidCreateException e) {
            LogManager.exception(this, e);
        }
        counter++;
    }
    cursor.close();
    LogManager.i("DatabaseManager", counter + " messages copied to Realm");
    LogManager.i("DatabaseManager", "onSuccess. removing messages from sqlite:");
    int removedMessages = MessageTable.getInstance().removeAllMessages();
    LogManager.i("DatabaseManager", removedMessages + " messages removed from sqlite");
    realm.commitTransaction();
    realm.close();
}
Also used : MessageItem(com.xabber.android.data.database.messagerealm.MessageItem) XmppStringprepException(org.jxmpp.stringprep.XmppStringprepException) Cursor(android.database.Cursor) Realm(io.realm.Realm) DynamicRealm(io.realm.DynamicRealm)

Aggregations

MessageItem (com.xabber.android.data.database.messagerealm.MessageItem)24 Realm (io.realm.Realm)14 Date (java.util.Date)6 UserJid (com.xabber.android.data.entity.UserJid)4 AccountItem (com.xabber.android.data.account.AccountItem)3 ArrayList (java.util.ArrayList)3 Message (org.jivesoftware.smack.packet.Message)3 DelayInformation (org.jivesoftware.smackx.delay.packet.DelayInformation)3 NetworkException (com.xabber.android.data.NetworkException)2 SyncInfo (com.xabber.android.data.database.messagerealm.SyncInfo)2 AccountJid (com.xabber.android.data.entity.AccountJid)2 AbstractChat (com.xabber.android.data.message.AbstractChat)2 ChatAction (com.xabber.android.data.message.ChatAction)2 RealmResults (io.realm.RealmResults)2 File (java.io.File)2 IOException (java.io.IOException)2 ActivityNotFoundException (android.content.ActivityNotFoundException)1 Intent (android.content.Intent)1 Cursor (android.database.Cursor)1 Bitmap (android.graphics.Bitmap)1