Search in sources :

Example 1 with ForwardId

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

the class AbstractChat method parseForwardedMessage.

public RealmList<ForwardId> parseForwardedMessage(boolean ui, Stanza packet, String parentMessageId) {
    List<Forwarded> forwarded = ReferencesManager.getForwardedFromReferences(packet);
    if (forwarded.isEmpty())
        forwarded = ForwardManager.getForwardedFromStanza(packet);
    if (forwarded.isEmpty())
        return null;
    RealmList<ForwardId> forwardedIds = new RealmList<>();
    for (Forwarded forward : forwarded) {
        Stanza stanza = forward.getForwardedStanza();
        DelayInformation delayInformation = forward.getDelayInformation();
        Date timestamp = delayInformation.getStamp();
        if (stanza instanceof Message) {
            forwardedIds.add(new ForwardId(parseInnerMessage(ui, (Message) stanza, timestamp, parentMessageId)));
        }
    }
    return forwardedIds;
}
Also used : RealmList(io.realm.RealmList) DelayInformation(org.jivesoftware.smackx.delay.packet.DelayInformation) Message(org.jivesoftware.smack.packet.Message) Stanza(org.jivesoftware.smack.packet.Stanza) Forwarded(org.jivesoftware.smackx.forward.packet.Forwarded) ForwardId(com.xabber.android.data.database.messagerealm.ForwardId) Date(java.util.Date)

Example 2 with ForwardId

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

the class ForwardManager method forwardMessage.

public static void forwardMessage(List<String> messages, AccountJid account, UserJid user, String text) {
    final AbstractChat chat = MessageManager.getInstance().getOrCreateChat(account, user);
    final MessageItem messageItem = chat.createNewMessageItem(text);
    RealmList<ForwardId> ids = new RealmList<>();
    for (String message : messages) {
        ids.add(new ForwardId(message));
    }
    messageItem.setForwardedIds(ids);
    MessageDatabaseManager.getInstance().getRealmUiThread().executeTransactionAsync(new Realm.Transaction() {

        @Override
        public void execute(Realm realm) {
            realm.copyToRealm(messageItem);
            EventBus.getDefault().post(new NewMessageEvent());
            chat.sendMessages();
        }
    });
}
Also used : MessageItem(com.xabber.android.data.database.messagerealm.MessageItem) RealmList(io.realm.RealmList) ForwardId(com.xabber.android.data.database.messagerealm.ForwardId) Realm(io.realm.Realm)

Example 3 with ForwardId

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

the class RoomChat method parseInnerMessage.

@Override
protected String parseInnerMessage(boolean ui, Message message, Date timestamp, String parentMessageId) {
    if (message.getType() == Message.Type.error)
        return null;
    final org.jxmpp.jid.Jid from = message.getFrom();
    final Resourcepart resource = from.getResourceOrNull();
    String text = message.getBody();
    final String subject = message.getSubject();
    if (text == null)
        return null;
    if (subject != null)
        return null;
    RealmList<Attachment> attachments = HttpFileUploadManager.parseFileMessage(message);
    String uid = UUID.randomUUID().toString();
    RealmList<ForwardId> forwardIds = parseForwardedMessage(ui, message, uid);
    String originalStanza = message.toXML().toString();
    String originalFrom = message.getFrom().toString();
    boolean fromMUC = message.getType().equals(Type.groupchat);
    // forward comment (to support previous forwarded xep)
    String forwardComment = ForwardManager.parseForwardComment(message);
    if (forwardComment != null)
        text = forwardComment;
    // modify body with references
    Pair<String, String> bodies = ReferencesManager.modifyBodyWithReferences(message, text);
    text = bodies.first;
    String markupText = bodies.second;
    // create message with file-attachments
    if (attachments.size() > 0)
        createAndSaveFileMessage(ui, uid, resource, text, markupText, null, timestamp, getDelayStamp(message), true, false, false, false, getStanzaId(message), attachments, originalStanza, parentMessageId, originalFrom, fromMUC, true, null);
    else
        // create message without attachments
        createAndSaveNewMessage(ui, uid, resource, text, markupText, null, timestamp, getDelayStamp(message), true, false, false, false, getStanzaId(message), originalStanza, parentMessageId, originalFrom, forwardIds, fromMUC, true, null);
    return uid;
}
Also used : Attachment(com.xabber.android.data.database.messagerealm.Attachment) ForwardId(com.xabber.android.data.database.messagerealm.ForwardId) Resourcepart(org.jxmpp.jid.parts.Resourcepart)

Example 4 with ForwardId

use of com.xabber.android.data.database.messagerealm.ForwardId 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, true);
            }
            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;
        }
        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, true);
        } else {
            boolean notify = true;
            Date delay = getDelayStamp(message);
            if (delay != null) {
                notify = false;
            }
            // forward comment (to support previous forwarded xep)
            String forwardComment = ForwardManager.parseForwardComment(stanza);
            if (forwardComment != null)
                text = forwardComment;
            // modify body with references
            Pair<String, String> bodies = ReferencesManager.modifyBodyWithReferences(message, text);
            text = bodies.first;
            String markupText = bodies.second;
            String originalFrom = stanza.getFrom().toString();
            String messageUId = getMessageIdIfInHistory(getStanzaId(message), text);
            if (messageUId != null) {
                if (isSelf(resource)) {
                    markMessageAsDelivered(messageUId, originalFrom);
                }
                return true;
            }
            if (isSelf(resource)) {
                notify = false;
            }
            updateThreadId(message.getThread());
            RealmList<Attachment> attachments = HttpFileUploadManager.parseFileMessage(stanza);
            String uid = UUID.randomUUID().toString();
            RealmList<ForwardId> forwardIds = parseForwardedMessage(true, stanza, uid);
            String originalStanza = stanza.toXML().toString();
            // create message with file-attachments
            if (attachments.size() > 0)
                createAndSaveFileMessage(true, uid, resource, text, markupText, null, null, delay, true, notify, false, false, getStanzaId(message), attachments, originalStanza, null, originalFrom, true, false, null);
            else
                // create message without attachments
                createAndSaveNewMessage(true, uid, resource, text, markupText, null, null, delay, true, notify, false, false, getStanzaId(message), originalStanza, null, originalFrom, forwardIds, true, false, null);
            EventBus.getDefault().post(new NewIncomingMessageEvent(account, user));
        }
    } 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;
}
Also used : MUCUser(org.jivesoftware.smackx.muc.packet.MUCUser) NewIncomingMessageEvent(com.xabber.android.data.message.NewIncomingMessageEvent) Message(org.jivesoftware.smack.packet.Message) UserJid(com.xabber.android.data.entity.UserJid) Attachment(com.xabber.android.data.database.messagerealm.Attachment) Date(java.util.Date) Resourcepart(org.jxmpp.jid.parts.Resourcepart) Presence(org.jivesoftware.smack.packet.Presence) ForwardId(com.xabber.android.data.database.messagerealm.ForwardId)

Example 5 with ForwardId

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

the class NextMamManager method determineSaveOrUpdate.

private MessageItem determineSaveOrUpdate(Realm realm, final MessageItem message, boolean ui) {
    Message originalMessage = null;
    try {
        originalMessage = (Message) PacketParserUtils.parseStanza(message.getOriginalStanza());
    } catch (Exception e) {
        e.printStackTrace();
    }
    AbstractChat chat = MessageManager.getInstance().getOrCreateChat(message.getAccount(), message.getUser());
    if (chat == null)
        return null;
    MessageItem localMessage = findSameLocalMessage(realm, chat, message);
    if (localMessage == null) {
        // forwarded
        if (originalMessage != null) {
            RealmList<ForwardId> forwardIds = chat.parseForwardedMessage(ui, originalMessage, message.getUniqueId());
            if (forwardIds != null && !forwardIds.isEmpty())
                message.setForwardedIds(forwardIds);
        }
        // notify about new message
        chat.enableNotificationsIfNeed();
        boolean notify = !message.isRead() && (message.getText() != null && !message.getText().trim().isEmpty()) && message.isIncoming() && chat.notifyAboutMessage();
        boolean visible = MessageManager.getInstance().isVisibleChat(chat);
        if (notify && !visible)
            NotificationManager.getInstance().onMessageNotification(message);
        return message;
    } else {
        realm.beginTransaction();
        localMessage.setArchivedId(message.getArchivedId());
        realm.commitTransaction();
        return localMessage;
    }
}
Also used : MessageItem(com.xabber.android.data.database.messagerealm.MessageItem) PlainTextMessage(net.java.otr4j.io.messages.PlainTextMessage) Message(org.jivesoftware.smack.packet.Message) AbstractChat(com.xabber.android.data.message.AbstractChat) ForwardId(com.xabber.android.data.database.messagerealm.ForwardId) SmackException(org.jivesoftware.smack.SmackException) XMPPException(org.jivesoftware.smack.XMPPException) IOException(java.io.IOException)

Aggregations

ForwardId (com.xabber.android.data.database.messagerealm.ForwardId)7 Attachment (com.xabber.android.data.database.messagerealm.Attachment)4 MessageItem (com.xabber.android.data.database.messagerealm.MessageItem)3 UserJid (com.xabber.android.data.entity.UserJid)3 Message (org.jivesoftware.smack.packet.Message)3 Resourcepart (org.jxmpp.jid.parts.Resourcepart)3 RefUser (com.xabber.android.data.extension.references.RefUser)2 RealmList (io.realm.RealmList)2 Date (java.util.Date)2 MUCUser (org.jivesoftware.smackx.muc.packet.MUCUser)2 AccountJid (com.xabber.android.data.entity.AccountJid)1 AbstractChat (com.xabber.android.data.message.AbstractChat)1 NewIncomingMessageEvent (com.xabber.android.data.message.NewIncomingMessageEvent)1 Realm (io.realm.Realm)1 IOException (java.io.IOException)1 PlainTextMessage (net.java.otr4j.io.messages.PlainTextMessage)1 SmackException (org.jivesoftware.smack.SmackException)1 XMPPException (org.jivesoftware.smack.XMPPException)1 Presence (org.jivesoftware.smack.packet.Presence)1 Stanza (org.jivesoftware.smack.packet.Stanza)1