public List createStanzas()

in server/extensions/xep0045-muc/src/main/java/org/apache/vysper/xmpp/modules/extension/xep0045_muc/model/DiscussionHistory.java [81:149]


    public List<Stanza> createStanzas(Occupant receiver, boolean includeJid, History history) {
        synchronized (items) {

            int maxstanzas = history != null && history.getMaxStanzas() != null ? history.getMaxStanzas() : -1;
            int maxchars = history != null && history.getMaxChars() != null ? history.getMaxChars() : -1;
            int seconds = history != null && history.getSeconds() != null ? history.getSeconds() : -1;

            List<Stanza> stanzas = new ArrayList<Stanza>();

            if (maxchars == 0 || maxstanzas == 0 || seconds == 0) {
                // quick return for no-stanza requests
                return Collections.emptyList();
            } else {
                int counter = 0;
                int totalChars = 0;

                List<DiscussionMessage> itemsWithSubject = new ArrayList<DiscussionMessage>(items);
                // add subject if one is provided
                if (subjectMessage != null) {
                    itemsWithSubject.add(subjectMessage);
                }

                // the timestamp at which "seconds" start filtering from 
                long secondsLimit = -1;
                if (seconds != -1) {
                    Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
                    cal.add(Calendar.SECOND, -seconds);
                    secondsLimit = cal.getTimeInMillis();
                }

                // now add all messages, as long as the predicated are fulfilled
                // first, do this in reverse order so that older messages are filtered out
                for (int i = itemsWithSubject.size() - 1; i > -1; i--) {
                    DiscussionMessage item = itemsWithSubject.get(i);
                    Stanza stanza = item.createStanza(receiver, includeJid);
                    counter++;

                    if (secondsLimit != -1 && secondsLimit > item.getTimestamp().getTimeInMillis()) {
                        // too old, break
                        break;
                    }

                    if (history != null && history.getSince() != null && history.getSince().after(item.getTimestamp())) {
                        // too old, break
                        break;
                    }

                    // only count chars if needed
                    if (maxchars != -1) {
                        totalChars += new Renderer(stanza).getComplete().length();

                        if (totalChars > maxchars) {
                            break;
                        }
                    }

                    // checks after this line will include the last stanza
                    stanzas.add(stanza);
                    if (maxstanzas != -1 && counter == maxstanzas) {
                        // max number of stanzas reached, return
                        break;
                    }
                }
            }
            // reverse list so that the oldest message is first
            Collections.reverse(stanzas);
            return stanzas;
        }
    }