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.JsonProperty;
019import com.fasterxml.jackson.core.JsonParser;
020import com.fasterxml.jackson.databind.DeserializationContext;
021import com.fasterxml.jackson.databind.JsonNode;
022import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
023import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
024import com.vonage.client.Jsonable;
025import com.vonage.client.JsonableBaseObject;
026import com.vonage.client.common.ChannelType;
027import com.vonage.client.users.channels.Channel;
028import java.io.IOException;
029import java.util.Objects;
030
031/**
032 * Contains the channel properties for {@link Member#getChannel()}.
033 */
034@JsonDeserialize(using = MemberChannel.Deserializer.class)
035public class MemberChannel extends JsonableBaseObject {
036        ChannelType type;
037        Channel from, to;
038
039        protected MemberChannel() {
040        }
041
042        /**
043         * Main channel type.
044         * 
045         * @return The channel type as an enum, or {@code null} if unspecified.
046         */
047        @JsonProperty("type")
048        public ChannelType getType() {
049                return type;
050        }
051
052        /**
053         * Sender channel.
054         * 
055         * @return The from channel, or {@code null} if unspecified.
056         */
057        @JsonProperty("from")
058        public Channel getFrom() {
059                return from;
060        }
061
062        /**
063         * Receiver channel.
064         * 
065         * @return The to channel, or {@code null} if unspecified.
066         */
067        @JsonProperty("to")
068        public Channel getTo() {
069                return to;
070        }
071
072        static class Deserializer extends StdDeserializer<MemberChannel> {
073                private MemberChannel mc;
074
075                protected Deserializer() {
076                        super(MemberChannel.class);
077                }
078
079                @Override
080                public MemberChannel deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
081                        return deserialize(p, ctxt, new MemberChannel());
082                }
083
084                @Override
085                public MemberChannel deserialize(JsonParser p, DeserializationContext ctxt, MemberChannel intoValue) throws IOException {
086                        mc = Objects.requireNonNull(intoValue);
087                        JsonNode rootNode = p.readValueAsTree(), typeNode = rootNode.get("type");
088                        if (typeNode != null) {
089                                mc.type = ChannelType.fromString(typeNode.asText());
090                        }
091                        mc.from = inferConcreteChannel(rootNode.get("from"));
092                        mc.to = inferConcreteChannel(rootNode.get("to"));
093                        return mc;
094                }
095
096                private Channel inferConcreteChannel(JsonNode node) {
097                        if (node == null || !node.isObject()) return null;
098                        JsonNode typeNode = node.get("type");
099                        ChannelType fromType = typeNode != null ? ChannelType.fromString(typeNode.asText()) : mc.type;
100                        Class<? extends Channel> concreteClass = Channel.getConcreteClass(fromType);
101                        if (concreteClass == null) {
102                                throw new IllegalStateException("Unmapped class for type "+fromType);
103                        }
104                        return Jsonable.fromJson(node.toString(), concreteClass);
105                }
106        }
107}