001/**
002 *
003 * Copyright 2020 Florian Schmaus
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.jivesoftware.smackx.pubsub.form;
018
019import java.text.ParseException;
020import java.util.ArrayList;
021import java.util.Date;
022import java.util.List;
023
024import org.jivesoftware.smackx.pubsub.PresenceState;
025import org.jivesoftware.smackx.pubsub.SubscribeOptionFields;
026import org.jivesoftware.smackx.pubsub.packet.PubSub;
027import org.jivesoftware.smackx.xdata.form.FormReader;
028
029public interface SubscribeFormReader extends FormReader {
030
031    String FORM_TYPE = PubSub.NAMESPACE + "#subscribe_options";
032
033    /**
034     * Determines if an entity wants to receive notifications.
035     *
036     * @return true if want to receive, false otherwise
037     */
038    default boolean isDeliverOn() {
039        return readBoolean(SubscribeOptionFields.deliver.getFieldName());
040    }
041
042    /**
043     * Determines if notifications should be delivered as aggregations or not.
044     *
045     * @return true to aggregate, false otherwise
046     */
047    default Boolean isDigestOn() {
048        return readBoolean(SubscribeOptionFields.digest.getFieldName());
049    }
050
051    /**
052     * Gets the minimum number of milliseconds between sending notification digests.
053     *
054     * @return The frequency in milliseconds
055     */
056    default Integer getDigestFrequency() {
057        return readInteger(SubscribeOptionFields.digest_frequency.getFieldName());
058    }
059
060    /**
061     * Get the time at which the leased subscription will expire, or has expired.
062     *
063     * @return The expiry date
064     * @throws ParseException in case the date could not be parsed.
065     */
066    default Date getExpiry() throws ParseException {
067        return readDate(SubscribeOptionFields.expire.getFieldName());
068    }
069
070    /**
071     * Determines whether the entity wants to receive an XMPP message body in
072     * addition to the payload format.
073     *
074     * @return true to receive the message body, false otherwise
075     */
076    default Boolean isIncludeBody() {
077        return readBoolean(SubscribeOptionFields.include_body.getFieldName());
078    }
079
080    /**
081     * Gets the {@link PresenceState} for which an entity wants to receive
082     * notifications.
083     *
084     * @return the list of states
085     */
086    default List<PresenceState> getShowValues() {
087        List<String> values = readStringValues(SubscribeOptionFields.show_values.getFieldName());
088        List<PresenceState> result = new ArrayList<>(values.size());
089
090        for (String state : values) {
091            result.add(PresenceState.valueOf(state));
092        }
093        return result;
094    }
095}