use of com.xabber.android.data.database.messagerealm.MessageItem in project xabber-android by redsolution.
the class RoomChat method onPacket.
@Override
protected boolean onPacket(UserJid bareAddress, Stanza stanza, boolean isCarbons) {
if (!super.onPacket(bareAddress, stanza, isCarbons)) {
return false;
}
// MUCUser mucUserExtension = MUCUser.from(stanza);
// if (mucUserExtension != null && mucUserExtension.getInvite() != null) {
// return false;
// }
final org.jxmpp.jid.Jid from = stanza.getFrom();
final Resourcepart resource = from.getResourceOrNull();
if (stanza instanceof Message) {
final Message message = (Message) stanza;
if (message.getType() == Message.Type.error) {
UserJid invite = invites.remove(message.getStanzaId());
if (invite != null) {
newAction(nickname, invite.toString(), ChatAction.invite_error);
}
return true;
}
MUCUser mucUser = MUCUser.from(stanza);
if (mucUser != null && mucUser.getDecline() != null) {
onInvitationDeclined(mucUser.getDecline().getFrom(), mucUser.getDecline().getReason());
return true;
}
if (mucUser != null && mucUser.getStatus() != null && mucUser.getStatus().contains(MUCUser.Status.create("100")) && ChatManager.getInstance().isSuppress100(account, user)) {
// 'This room is not anonymous'
return true;
}
final String text = message.getBody();
final String subject = message.getSubject();
if (text == null && subject == null) {
return true;
}
if (subject != null) {
if (this.subject.equals(subject)) {
return true;
}
this.subject = subject;
RosterManager.onContactChanged(account, bareAddress);
newAction(resource, subject, ChatAction.subject);
} else {
boolean notify = true;
String stanzaId = message.getStanzaId();
// disabling because new messages without stanza will be repeated
// if (stanzaId == null) stanzaId = UUID.randomUUID().toString();
DelayInformation delayInformation = DelayInformation.from(message);
Date delay = null;
if (delayInformation != null) {
delay = delayInformation.getStamp();
}
if (delay != null) {
notify = false;
}
final long startTime = System.currentTimeMillis();
Realm realm = MessageDatabaseManager.getInstance().getRealmUiThread();
final MessageItem sameMessage = realm.where(MessageItem.class).equalTo(MessageItem.Fields.STANZA_ID, stanzaId).findFirst();
// Server send our own message back
if (sameMessage != null) {
// realm.beginTransaction();
// sameMessage.setDelivered(true);
// realm.commitTransaction();
LogManager.d("REALM", Thread.currentThread().getName() + " save message delivered: " + (System.currentTimeMillis() - startTime));
return true;
}
if (isSelf(resource)) {
// Own message from other client
notify = false;
}
updateThreadId(message.getThread());
createAndSaveNewMessage(resource, text, null, delay, true, notify, false, false, stanzaId);
}
} else if (stanza instanceof Presence) {
Presence presence = (Presence) stanza;
if (presence.getType() == Presence.Type.available) {
Occupant oldOccupant = occupants.get(resource);
Occupant newOccupant = createOccupant(resource, presence);
newOccupant.setJid(from);
occupants.put(resource, newOccupant);
if (oldOccupant == null) {
onAvailable(resource);
RosterManager.onContactChanged(account, user);
} else {
boolean changed = false;
if (oldOccupant.getAffiliation() != newOccupant.getAffiliation()) {
changed = true;
onAffiliationChanged(resource, newOccupant.getAffiliation());
}
if (oldOccupant.getRole() != newOccupant.getRole()) {
changed = true;
onRoleChanged(resource, newOccupant.getRole());
}
if (oldOccupant.getStatusMode() != newOccupant.getStatusMode() || !oldOccupant.getStatusText().equals(newOccupant.getStatusText())) {
changed = true;
onStatusChanged(resource, newOccupant.getStatusMode(), newOccupant.getStatusText());
}
if (changed) {
RosterManager.onContactChanged(account, user);
}
}
} else if (presence.getType() == Presence.Type.unavailable && state == RoomState.available) {
occupants.remove(resource);
MUCUser mucUser = MUCUser.from(presence);
if (mucUser != null && mucUser.getStatus() != null) {
if (mucUser.getStatus().contains(MUCUser.Status.KICKED_307)) {
onKick(resource, mucUser.getItem().getActor());
} else if (mucUser.getStatus().contains(MUCUser.Status.BANNED_301)) {
onBan(resource, mucUser.getItem().getActor());
} else if (mucUser.getStatus().contains(MUCUser.Status.NEW_NICKNAME_303)) {
Resourcepart newNick = mucUser.getItem().getNick();
if (newNick == null) {
return true;
}
onRename(resource, newNick);
Occupant occupant = createOccupant(newNick, presence);
occupants.put(newNick, occupant);
} else if (mucUser.getStatus().contains(MUCUser.Status.REMOVED_AFFIL_CHANGE_321)) {
onRevoke(resource, mucUser.getItem().getActor());
}
} else {
onLeave(resource);
}
RosterManager.onContactChanged(account, user);
}
}
return true;
}
use of com.xabber.android.data.database.messagerealm.MessageItem in project xabber-android by redsolution.
the class AbstractChat method createAndSaveNewMessage.
/**
* Creates new message.
* <p/>
* Any parameter can be <code>null</code> (except boolean values).
*
* @param resource Contact's resource or nick in conference.
* @param text message.
* @param action Informational message.
* @param delayTimestamp Time when incoming message was sent or outgoing was created.
* @param incoming Incoming message.
* @param notify Notify user about this message when appropriated.
* @param encrypted Whether encrypted message in OTR chat was received.
* @param offline Whether message was received from server side offline storage.
* @return
*/
protected void createAndSaveNewMessage(Resourcepart resource, String text, final ChatAction action, final Date delayTimestamp, final boolean incoming, boolean notify, final boolean encrypted, final boolean offline, final String stanzaId) {
final MessageItem messageItem = createMessageItem(resource, text, action, delayTimestamp, incoming, notify, encrypted, offline, stanzaId);
saveMessageItem(messageItem);
EventBus.getDefault().post(new NewMessageEvent());
}
use of com.xabber.android.data.database.messagerealm.MessageItem in project xabber-android by redsolution.
the class AbstractChat method sendMessage.
@SuppressWarnings("WeakerAccess")
boolean sendMessage(MessageItem messageItem) {
String text = prepareText(messageItem.getText());
messageItem.setEncrypted(OTRManager.getInstance().isEncrypted(text));
Long timestamp = messageItem.getTimestamp();
Date currentTime = new Date(System.currentTimeMillis());
Date delayTimestamp = null;
if (timestamp != null) {
if (currentTime.getTime() - timestamp > 60000) {
delayTimestamp = currentTime;
}
}
Message message = null;
if (text != null) {
message = createMessagePacket(text);
}
if (message != null) {
ChatStateManager.getInstance().updateOutgoingMessage(AbstractChat.this, message);
CarbonManager.getInstance().updateOutgoingMessage(AbstractChat.this, message);
if (delayTimestamp != null) {
message.addExtension(new DelayInformation(delayTimestamp));
}
final String messageId = messageItem.getUniqueId();
try {
StanzaSender.sendStanza(account, message, new StanzaListener() {
@Override
public void processStanza(Stanza packet) throws SmackException.NotConnectedException {
Realm realm = MessageDatabaseManager.getInstance().getNewBackgroundRealm();
realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
MessageItem acknowledgedMessage = realm.where(MessageItem.class).equalTo(MessageItem.Fields.UNIQUE_ID, messageId).findFirst();
if (acknowledgedMessage != null) {
acknowledgedMessage.setAcknowledged(true);
}
}
});
realm.close();
}
});
} catch (NetworkException e) {
return false;
}
}
if (message == null) {
messageItem.setError(true);
messageItem.setErrorDescription("Internal error: message is null");
} else {
messageItem.setStanzaId(message.getStanzaId());
}
if (delayTimestamp != null) {
messageItem.setDelayTimestamp(delayTimestamp.getTime());
}
if (messageItem.getTimestamp() == null) {
messageItem.setTimestamp(currentTime.getTime());
}
messageItem.setSent(true);
return true;
}
use of com.xabber.android.data.database.messagerealm.MessageItem in project xabber-android by redsolution.
the class AbstractChat method newFileMessage.
String newFileMessage(final File file) {
Realm realm = MessageDatabaseManager.getInstance().getNewBackgroundRealm();
final String messageId = UUID.randomUUID().toString();
realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
MessageItem messageItem = new MessageItem(messageId);
messageItem.setAccount(account);
messageItem.setUser(user);
messageItem.setText(file.getName());
messageItem.setFilePath(file.getPath());
messageItem.setIsImage(FileManager.fileIsImage(file));
messageItem.setTimestamp(System.currentTimeMillis());
messageItem.setRead(true);
messageItem.setSent(true);
messageItem.setError(false);
messageItem.setIncoming(false);
messageItem.setInProgress(true);
realm.copyToRealm(messageItem);
}
});
realm.close();
return messageId;
}
use of com.xabber.android.data.database.messagerealm.MessageItem in project xabber-android by redsolution.
the class MessageManager method setVisibleChat.
/**
* Sets currently visible chat.
*/
public void setVisibleChat(BaseEntity visibleChat) {
AbstractChat chat = getChat(visibleChat.getAccount(), visibleChat.getUser());
if (chat == null) {
chat = createChat(visibleChat.getAccount(), visibleChat.getUser());
} else {
final AccountJid account = chat.getAccount();
final UserJid user = chat.getUser();
MessageDatabaseManager.getInstance().getRealmUiThread().executeTransactionAsync(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
RealmResults<MessageItem> unreadMessages = realm.where(MessageItem.class).equalTo(MessageItem.Fields.ACCOUNT, account.toString()).equalTo(MessageItem.Fields.USER, user.toString()).equalTo(MessageItem.Fields.READ, false).findAll();
List<MessageItem> unreadMessagesList = new ArrayList<>(unreadMessages);
for (MessageItem messageItem : unreadMessagesList) {
messageItem.setRead(true);
}
}
});
}
this.visibleChat = chat;
}
Aggregations