Search in sources :

Example 16 with Forwarded

use of org.jivesoftware.smackx.forward.packet.Forwarded in project xabber-android by redsolution.

the class NextMamManager method initializeStartTimestamp.

/**
 * Request most recent message from all history and save it timestamp to startHistoryTimestamp
 *  If message is null save current time to startHistoryTimestamp
 */
private void initializeStartTimestamp(Realm realm, @NonNull AccountItem accountItem) {
    long startHistoryTimestamp = System.currentTimeMillis();
    MamManager.MamQueryResult queryResult = requestLastMessage(accountItem, null);
    if (queryResult != null && !queryResult.forwardedMessages.isEmpty()) {
        Forwarded forwarded = queryResult.forwardedMessages.get(0);
        startHistoryTimestamp = forwarded.getDelayInformation().getStamp().getTime();
        parseAndSaveMessageFromMamResult(realm, accountItem.getAccount(), forwarded);
    }
    accountItem.setStartHistoryTimestamp(startHistoryTimestamp);
}
Also used : MamManager(org.jivesoftware.smackx.mam.MamManager) Forwarded(org.jivesoftware.smackx.forward.packet.Forwarded)

Example 17 with Forwarded

use of org.jivesoftware.smackx.forward.packet.Forwarded in project xabber-android by redsolution.

the class ReferencesProvider method parse.

@Override
public ReferenceElement parse(XmlPullParser parser, int initialDepth) throws Exception {
    String type = null, beginS = null, endS = null, marker = null, uri = null;
    List<Forwarded> forwardedMessages = new ArrayList<>();
    List<RefMedia> mediaElements = new ArrayList<>();
    boolean bold = false, italic = false, underline = false, strike = false;
    RefUser user = null;
    outerloop: while (true) {
        int eventType = parser.getEventType();
        switch(eventType) {
            case XmlPullParser.START_TAG:
                if (ReferenceElement.ELEMENT.equals(parser.getName()) && ReferenceElement.NAMESPACE.equals(parser.getNamespace())) {
                    type = parser.getAttributeValue("", ReferenceElement.ATTRIBUTE_TYPE);
                    beginS = parser.getAttributeValue("", ReferenceElement.ATTRIBUTE_BEGIN);
                    endS = parser.getAttributeValue("", ReferenceElement.ATTRIBUTE_END);
                    parser.next();
                } else if (Forwarded.ELEMENT.equals(parser.getName()) && Forwarded.NAMESPACE.equals(parser.getNamespace())) {
                    Forwarded forwarded = ForwardedProvider.INSTANCE.parse(parser);
                    if (forwarded != null)
                        forwardedMessages.add(forwarded);
                    parser.next();
                } else if (RefMedia.ELEMENT.equals(parser.getName())) {
                    RefMedia media = parseMedia(parser);
                    if (media != null)
                        mediaElements.add(media);
                    parser.next();
                } else if (RefUser.ELEMENT.equals(parser.getName()) && RefUser.NAMESPACE.equals(parser.getNamespace())) {
                    user = parseUser(parser);
                    parser.next();
                } else if (ReferenceElement.ELEMENT_BOLD.equals(parser.getName())) {
                    bold = true;
                    parser.next();
                } else if (ReferenceElement.ELEMENT_ITALIC.equals(parser.getName())) {
                    italic = true;
                    parser.next();
                } else if (ReferenceElement.ELEMENT_UNDERLINE.equals(parser.getName())) {
                    underline = true;
                    parser.next();
                } else if (ReferenceElement.ELEMENT_STRIKE.equals(parser.getName())) {
                    strike = true;
                    parser.next();
                } else if (ReferenceElement.ELEMENT_URI.equals(parser.getName())) {
                    uri = parser.nextText();
                } else if (ReferenceElement.ELEMENT_MARKER.equals(parser.getName())) {
                    marker = parser.nextText();
                } else
                    parser.next();
                break;
            case XmlPullParser.END_TAG:
                if (ReferenceElement.ELEMENT.equals(parser.getName())) {
                    break outerloop;
                } else
                    parser.next();
                break;
            default:
                parser.next();
        }
    }
    int begin = 0, end = 0;
    if (beginS != null && !beginS.isEmpty())
        begin = Integer.valueOf(beginS);
    if (endS != null && !endS.isEmpty())
        end = Integer.valueOf(endS);
    try {
        ReferenceElement.Type refType = ReferenceElement.Type.valueOf(type);
        switch(refType) {
            case forward:
                return new Forward(begin, end, forwardedMessages);
            case media:
                return new Media(begin, end, mediaElements);
            case markup:
                return new Markup(begin, end, bold, italic, underline, strike, uri);
            case quote:
                return new Quote(begin, end, marker);
            case mention:
                return new Mention(begin, end, uri);
            case groupchat:
                return new Groupchat(begin, end, user);
            default:
                return null;
        }
    } catch (Exception e) {
        LogManager.d(ReferencesProvider.class, e.toString());
        return null;
    }
}
Also used : ArrayList(java.util.ArrayList) Forwarded(org.jivesoftware.smackx.forward.packet.Forwarded)

Example 18 with Forwarded

use of org.jivesoftware.smackx.forward.packet.Forwarded in project Smack by igniterealtime.

the class QueryArchiveTest method checkMamQueryResults.

@Test
public void checkMamQueryResults() throws Exception {
    Message message = StanzaBuilder.buildMessage("iasd207").from("coven@chat.shakespeare.lit").to("hag66@shakespeare.lit/pda").build();
    GregorianCalendar calendar = new GregorianCalendar(2002, 10 - 1, 13, 23, 58, 37);
    calendar.setTimeZone(TimeZone.getTimeZone("UTC"));
    Date date = calendar.getTime();
    DelayInformation delay = new DelayInformation(date);
    Message forwardedMessage = StanzaBuilder.buildMessage("162BEBB1-F6DB-4D9A-9BD8-CFDCC801A0B2").from(JidCreate.from("coven@chat.shakespeare.lit/firstwitch")).ofType(Message.Type.chat).setBody("Thrice the brinded cat hath mew.").build();
    Forwarded<Message> forwarded = new Forwarded<>(forwardedMessage, delay);
    message.addExtension(MamVersion.MAM2.newElementFactory().newResultExtension("g27", "34482-21985-73620", forwarded));
    assertEquals(mamQueryResultExample, message.toXML(StreamOpen.CLIENT_NAMESPACE).toString());
    MamResultExtension mamResultExtension = MamResultExtension.from(message);
    assertEquals(mamResultExtension.getId(), "34482-21985-73620");
    assertEquals(mamResultExtension.getForwarded().getDelayInformation().getStamp(), date);
    Message resultMessage = mamResultExtension.getForwarded().getForwardedStanza();
    assertEquals(resultMessage.getFrom(), JidCreate.from("coven@chat.shakespeare.lit/firstwitch"));
    assertEquals(resultMessage.getStanzaId(), "162BEBB1-F6DB-4D9A-9BD8-CFDCC801A0B2");
    assertEquals(resultMessage.getType(), Message.Type.chat);
    assertEquals(resultMessage.getBody(), "Thrice the brinded cat hath mew.");
}
Also used : Message(org.jivesoftware.smack.packet.Message) DelayInformation(org.jivesoftware.smackx.delay.packet.DelayInformation) GregorianCalendar(java.util.GregorianCalendar) Forwarded(org.jivesoftware.smackx.forward.packet.Forwarded) MamResultExtension(org.jivesoftware.smackx.mam.element.MamElements.MamResultExtension) Date(java.util.Date) Test(org.junit.jupiter.api.Test)

Example 19 with Forwarded

use of org.jivesoftware.smackx.forward.packet.Forwarded in project Smack by igniterealtime.

the class ForwardedProvider method parse.

@Override
public Forwarded<?> parse(XmlPullParser parser, int initialDepth, XmlEnvironment xmlEnvironment) throws XmlPullParserException, IOException, SmackParsingException, ParseException {
    DelayInformation di = null;
    Stanza packet = null;
    outerloop: while (true) {
        XmlPullParser.Event eventType = parser.next();
        switch(eventType) {
            case START_ELEMENT:
                String name = parser.getName();
                String namespace = parser.getNamespace();
                switch(name) {
                    case DelayInformation.ELEMENT:
                        if (DelayInformation.NAMESPACE.equals(namespace)) {
                            di = DelayInformationProvider.INSTANCE.parse(parser, parser.getDepth(), null);
                        } else {
                            LOGGER.warning("Namespace '" + namespace + "' does not match expected namespace '" + DelayInformation.NAMESPACE + "'");
                        }
                        break;
                    case Message.ELEMENT:
                        packet = PacketParserUtils.parseMessage(parser);
                        break;
                    default:
                        LOGGER.warning("Unsupported forwarded packet type: " + name);
                }
                break;
            case END_ELEMENT:
                if (parser.getDepth() == initialDepth) {
                    break outerloop;
                }
                break;
            default:
                // Catch all for incomplete switch (MissingCasesInEnumSwitch) statement.
                break;
        }
    }
    if (packet == null) {
        // TODO: Should be SmackParseException.
        throw new IOException("forwarded extension must contain a packet");
    }
    return new Forwarded<>(packet, di);
}
Also used : DelayInformation(org.jivesoftware.smackx.delay.packet.DelayInformation) Stanza(org.jivesoftware.smack.packet.Stanza) Forwarded(org.jivesoftware.smackx.forward.packet.Forwarded) IOException(java.io.IOException)

Aggregations

Forwarded (org.jivesoftware.smackx.forward.packet.Forwarded)19 ArrayList (java.util.ArrayList)10 DelayInformation (org.jivesoftware.smackx.delay.packet.DelayInformation)8 MessageItem (com.xabber.android.data.database.messagerealm.MessageItem)5 Date (java.util.Date)5 Message (org.jivesoftware.smack.packet.Message)5 Stanza (org.jivesoftware.smack.packet.Stanza)4 MamManager (org.jivesoftware.smackx.mam.MamManager)4 SmackException (org.jivesoftware.smack.SmackException)3 NonNull (androidx.annotation.NonNull)2 IOException (java.io.IOException)2 ExtensionElement (org.jivesoftware.smack.packet.ExtensionElement)2 MamResultExtension (org.jivesoftware.smackx.mam.element.MamElements.MamResultExtension)2 ForwardId (com.xabber.android.data.database.messagerealm.ForwardId)1 AccountJid (com.xabber.android.data.entity.AccountJid)1 UserJid (com.xabber.android.data.entity.UserJid)1 AbstractChat (com.xabber.android.data.message.AbstractChat)1 RealmList (io.realm.RealmList)1 GregorianCalendar (java.util.GregorianCalendar)1 HashMap (java.util.HashMap)1