001/*
002 *   Copyright 2024 Vonage
003 *
004 *   Licensed under the Apache License, Version 2.0 (the "License");
005 *   you may not use this file except in compliance with the License.
006 *   You may obtain a copy of the License at
007 *
008 *        http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *   Unless required by applicable law or agreed to in writing, software
011 *   distributed under the License is distributed on an "AS IS" BASIS,
012 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *   See the License for the specific language governing permissions and
014 *   limitations under the License.
015 */
016package com.vonage.client.conversations;
017
018import com.fasterxml.jackson.annotation.JsonIgnore;
019import com.vonage.client.common.MessageType;
020import java.net.URI;
021
022/**
023 * Represents a {@link EventType#MESSAGE} event. All possible fields are presented and accessible,
024 * but only those applicable to the message type will be populated. Use {@linkplain #getMessageType()}
025 * to determine the type of message, and query the other fields accordingly.
026 */
027public final class MessageEvent extends EventWithBody<MessageEventBody> {
028
029    MessageEvent() {}
030
031    MessageEvent(Builder builder) {
032        super(builder);
033        body = new MessageEventBody(builder);
034    }
035
036    /**
037     * Describes the media type for this event.
038     *
039     * @return The message type as an enum.
040     */
041    @JsonIgnore
042    public MessageType getMessageType() {
043        return body.messageType;
044    }
045
046    /**
047     * If {@linkplain #getMessageType()} is {@linkplain MessageType#TEXT}, returns the text.
048     *
049     * @return The message text, or {@code null} if not applicable.
050     */
051    @JsonIgnore
052    public String getText() {
053        return body.text;
054    }
055
056    /**
057     * If {@linkplain #getMessageType()} is multimedia, returns the URL of the media.
058     *
059     * @return The absolute media URL, or {@code null} if not applicable.
060     */
061    @JsonIgnore
062    public URI getUrl() {
063        switch (getMessageType()) {
064            default: return null;
065            case FILE: return body.file.url;
066            case IMAGE: return body.image.url;
067            case AUDIO: return body.audio.url;
068            case VIDEO: return body.video.url;
069            case VCARD: return body.vcard.url;
070        }
071    }
072
073    /**
074     * If {@linkplain #getMessageType()} is {@linkplain MessageType#LOCATION}, returns the location.
075     *
076     * @return The location details, or {@code null} if not applicable.
077     */
078    @JsonIgnore
079    public Location getLocation() {
080        return body.location;
081    }
082
083    /**
084     * Entry point for constructing an instance of this class.
085     *
086     * @param messageType The type of message for this event.
087     *
088     * @return A new Builder.
089     */
090    public static Builder builder(MessageType messageType) {
091        return new Builder(messageType);
092    }
093
094    /**
095     * Builder for configuring parameters of the event request.
096     */
097    public static final class Builder extends EventWithBody.Builder<MessageEvent, Builder> {
098        final MessageType messageType;
099        String text;
100        URI url;
101        Location location;
102
103        Builder(MessageType messageType) {
104            super(EventType.MESSAGE);
105            this.messageType = messageType;
106        }
107
108        /**
109         * Sets the message text, if the type is {@linkplain MessageType#TEXT}.
110         *
111         * @param text The message text.
112         *
113         * @return This builder.
114         */
115        public Builder text(String text) {
116            this.text = text;
117            return this;
118        }
119
120        /**
121         * Sets the URL, if appropriate for the type.
122         *
123         * @param url The absolute media URL as a string.
124         *
125         * @return This builder.
126         */
127        public Builder url(String url) {
128            this.url = URI.create(url);
129            return this;
130        }
131
132        /**
133         * Sets the message location, if the type is {@linkplain MessageType#LOCATION}.
134         *
135         * @param location The location details.
136         *
137         * @return This builder.
138         */
139        public Builder location(Location location) {
140            this.location = location;
141            return this;
142        }
143
144        @Override
145        public MessageEvent build() {
146            return new MessageEvent(this);
147        }
148    }
149}