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);
}
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> (");
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;
}
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));
}
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();
}
Aggregations