Exemple #1
0
        /// <inheritdoc/>
        public override IWolfResponse Deserialize(Type responseType, SerializedMessageData responseData)
        {
            GroupStatisticsResponse result = (GroupStatisticsResponse)base.Deserialize(responseType, responseData);

            GetResponseJson(responseData).PopulateObject(result.GroupStatistics, "body.details");
            return(result);
        }
        /// <inheritdoc/>
        public override SerializedMessageData Serialize(IWolfMessage message)
        {
            SerializedMessageData result = base.Serialize(message);
            JObject body = result.Payload["body"] as JObject;

            if (body == null)
            {
                return(result);
            }
            // metadata props
            JObject metadata = new JObject();

            SerializationHelper.MovePropertyIfExists(ref body, ref metadata, "isDeleted");
            SerializationHelper.MovePropertyIfExists(ref body, ref metadata, "isTipped");
            if (metadata.HasValues)
            {
                body.Add(new JProperty("metadata", metadata));
            }

            // palringo inconsistency strikes again!!!
            // when receiving, recipient is in object as "recipient". But when sending, it's "recipientId". So here, for sending we rename it
            // new protocol feature, same damn bad design
            if (message is ChatUpdateMessage updateMessage && updateMessage.SenderID == null)
            {
                JToken recipient = body["recipient"];
                body.Remove("recipient");
                body.Add("recipientId", recipient);
            }
            return(result);
        }
Exemple #3
0
        /// <inheritdoc/>
        public override IWolfResponse Deserialize(Type responseType, SerializedMessageData responseData)
        {
            // if body contains an object that contains yet another body, means it's nested, so treat it as normal
            // otherwise, it's just one group, and we need to nest it deeper so this serializer works
            // yes. This protocol is damn stupid. "What is consistency? We don't know, unless it's consistently bad!"
            SerializedMessageData data = responseData;
            IEnumerable <JToken>  nestedGroupBodies = GetResponseJson(responseData).SelectTokens("body.*.body");

            if (nestedGroupBodies?.Any() != true)
            {
                JToken  newJson = responseData.Payload.DeepClone();
                JObject newBody = GetResponseJson(newJson).SelectToken("body") as JObject;
                JEnumerable <JToken> children = newBody.Children();
                JObject groupBody             = new JObject();
                foreach (JToken obj in children)
                {
                    groupBody.Add(obj);
                }
                newBody.RemoveAll();
                newBody.Add(new JProperty("0", new JObject(new JProperty("body", groupBody))));
                data = new SerializedMessageData(newJson, responseData.BinaryMessages);
            }

            GroupProfileResponse result = (GroupProfileResponse)base.Deserialize(responseType, data);

            return(result);
        }
Exemple #4
0
        /// <inheritdoc/>
        public virtual IWolfMessage Deserialize(string command, SerializedMessageData messageData)
        {
            IWolfMessage result = messageData.Payload.ToObject <T>(SerializationHelper.DefaultSerializer);

            messageData.Payload.FlattenCommonProperties(result, SerializationHelper.DefaultSerializer);
            return(result);
        }
        /// <inheritdoc/>
        public override IWolfResponse Deserialize(Type responseType, SerializedMessageData responseData)
        {
            // first deserialize the json message
            ChatHistoryResponse result = (ChatHistoryResponse)base.Deserialize(responseType, responseData);

            // then assign binary data to each of the messages
            JArray responseBody = GetResponseJson(responseData)["body"] as JArray;

            if (responseBody == null)
            {
                throw new ArgumentException("Chat history response requires to have a body property that is a JSON array", nameof(responseData));
            }
            foreach (JToken responseChatMessage in responseBody)
            {
                WolfTimestamp msgTimestamp = responseChatMessage["timestamp"].ToObject <WolfTimestamp>(SerializationHelper.DefaultSerializer);
                IChatMessage  msg          = result.Messages.First(m => m.Timestamp == msgTimestamp);
                JToken        numProp      = responseChatMessage["data"]?["num"];
                if (numProp != null)
                {
                    int binaryIndex = numProp.ToObject <int>(SerializationHelper.DefaultSerializer);
                    SerializationHelper.PopulateMessageRawData(ref msg, responseData.BinaryMessages.ElementAt(binaryIndex));
                }
            }

            return(result);
        }
Exemple #6
0
        /// <inheritdoc/>
        public override SerializedMessageData Serialize(IWolfMessage message)
        {
            SerializedMessageData result = base.Serialize(message);
            JObject body = result.Payload["body"] as JObject;

            if (body == null)
            {
                return(result);
            }
            // extended props
            JObject extended = new JObject();

            SerializationHelper.MovePropertyIfExists(ref body, ref extended, "name");
            SerializationHelper.MovePropertyIfExists(ref body, ref extended, "about");
            SerializationHelper.MovePropertyIfExists(ref body, ref extended, "lookingFor");
            SerializationHelper.MovePropertyIfExists(ref body, ref extended, "gender");
            SerializationHelper.MovePropertyIfExists(ref body, ref extended, "relationship");
            SerializationHelper.MovePropertyIfExists(ref body, ref extended, "language");
            SerializationHelper.MovePropertyIfExists(ref body, ref extended, "urls");
            if (extended.HasValues)
            {
                body.Add(new JProperty("extended", extended));
            }
            return(result);
        }
Exemple #7
0
        /// <inheritdoc/>
        public override IWolfResponse Deserialize(Type responseType, SerializedMessageData responseData)
        {
            UserUpdateResponse result = (UserUpdateResponse)base.Deserialize(responseType, responseData);

            // flatten props into user profile in addition to what the base class does
            GetResponseJson(responseData).FlattenCommonProperties(result.UserProfile);
            return(result);
        }
        /// <inheritdoc/>
        public override IWolfMessage Deserialize(string command, SerializedMessageData messageData)
        {
            // deserialize message
            TipAddMessage result = (TipAddMessage)base.Deserialize(command, messageData);

            messageData.Payload.PopulateObject(result, "context", SerializationHelper.DefaultSerializer);
            messageData.Payload.PopulateObject(result, "body.context", SerializationHelper.DefaultSerializer);

            return(result);
        }
Exemple #9
0
        /// <inheritdoc/>
        public virtual IWolfResponse Deserialize(Type responseType, SerializedMessageData responseData)
        {
            ThrowIfInvalidType(responseType);

            JToken responseJson = GetResponseJson(responseData);
            object result       = responseJson.ToObject(responseType, SerializationHelper.DefaultSerializer);

            // if response has body or headers, further use it to populate the response entity
            responseJson.FlattenCommonProperties(result);
            return((IWolfResponse)result);
        }
        /// <inheritdoc/>
        public override IWolfMessage Deserialize(string command, SerializedMessageData messageData)
        {
            // deserialize message
            ChatUpdateMessage result = (ChatUpdateMessage)base.Deserialize(command, messageData);

            // parse and populate binary data
            if (messageData.BinaryMessages?.Any() == true)
            {
                SerializationHelper.PopulateMessageRawData(ref result, messageData.BinaryMessages.First());
            }

            return(result);
        }
Exemple #11
0
 /// <inheritdoc/>
 public override IWolfMessage Deserialize(string command, SerializedMessageData messageData)
 {
     if (messageData.Payload["body"]?["id"] == null)
     {
         return(base.Deserialize(command, messageData));
     }
     else
     {
         JToken  payload = messageData.Payload.DeepClone();
         JObject body    = (JObject)payload["body"];
         JToken  value   = body["id"];
         body.Remove("id");
         body.Add("groupId", value);
         return(base.Deserialize(command, new SerializedMessageData(payload, messageData.BinaryMessages)));
     }
 }
        /// <inheritdoc/>
        public IWolfMessage Deserialize(string command, SerializedMessageData messageData)
        {
            // deserialize message
            Type         msgType = GetMessageType(messageData.Payload["body"]);
            IChatMessage result  = (IChatMessage)messageData.Payload.ToObject(msgType, SerializationHelper.DefaultSerializer);

            messageData.Payload.FlattenCommonProperties(result, SerializationHelper.DefaultSerializer);

            // parse and populate binary data
            if (messageData.BinaryMessages?.Any() == true)
            {
                SerializationHelper.PopulateMessageRawData(ref result, messageData.BinaryMessages.First());
            }

            return(result);
        }
Exemple #13
0
        /// <inheritdoc/>
        public override SerializedMessageData Serialize(IWolfMessage message)
        {
            ThrowIfInvalidMessageType(message.GetType());

            SerializedMessageData result = base.Serialize(message);

            // when sending, UserID will be null, so can use that to determine
            if ((message is GroupJoinMessage joinMessage && joinMessage.UserID == null) ||
                (message is GroupLeaveMessage leaveMessage && leaveMessage.UserID == null))
            {
                JObject body  = (JObject)result.Payload["body"];
                JToken  value = body["groupId"];
                body.Remove("groupId");
                body.Add("id", value);
            }
            return(result);
        }
        /// <inheritdoc/>
        public override IWolfResponse Deserialize(Type responseType, SerializedMessageData responseData)
        {
            // so, in this abomination of a protocol, tip charmID is serialized as "id" in most cases...
            // ... except when retrieving detailed info on message tips, where it's serialized as "charmId"
            // even though summaries for the same thing call it "id"
            // yes... fixing inconsistencies YET AGAIN. Who designed this protocol?!
            JToken payload   = GetResponseJson(responseData.Payload).DeepClone();
            JArray charmList = payload.SelectToken("body.list") as JArray;

            foreach (JObject charmDetails in charmList.Children().Cast <JObject>())
            {
                JToken value = charmDetails["charmId"];
                if (value != null)
                {
                    charmDetails.Remove("charmId");
                    charmDetails.Add("id", value);
                }
            }
            return((TipDetailsResponse)base.Deserialize(responseType, new SerializedMessageData(payload, responseData.BinaryMessages)));
        }
        /// <inheritdoc/>
        public override SerializedMessageData Serialize(IWolfMessage message)
        {
            SerializedMessageData result = base.Serialize(message);
            JObject body = result.Payload["body"] as JObject;

            if (body == null)
            {
                return(result);
            }
            // metadata props
            JObject context = new JObject();

            SerializationHelper.MovePropertyIfExists(ref body, ref context, "id");
            SerializationHelper.MovePropertyIfExists(ref body, ref context, "type");
            if (context.HasValues)
            {
                body.Add(new JProperty("context", context));
            }

            return(result);
        }
Exemple #16
0
        /// <inheritdoc/>
        public override IWolfResponse Deserialize(Type responseType, SerializedMessageData responseData)
        {
            // first deserialize the json message
            ChatUpdateResponse result = (ChatUpdateResponse)base.Deserialize(responseType, responseData);

            // assign binary data if any
            if (responseData.BinaryMessages?.Any() == true)
            {
                JObject responseBody = GetResponseJson(responseData)["body"] as JObject;
                if (responseBody == null)
                {
                    throw new ArgumentException("Chat update response requires to have a body property that is a JSON object", nameof(responseData));
                }
                if (responseBody["data"] != null)
                {
                    SerializationHelper.PopulateMessageRawData(ref result, responseData.BinaryMessages.First());
                }
            }

            return(result);
        }
        public override SerializedMessageData Serialize(IWolfMessage message)
        {
            SerializedMessageData result = base.Serialize(message);
            JObject body = result.Payload["body"] as JObject;

            if (body == null)
            {
                return(result);
            }
            // extended props
            JObject extended = new JObject();

            SerializationHelper.MovePropertyIfExists(ref body, ref extended, "advancedAdmin");
            SerializationHelper.MovePropertyIfExists(ref body, ref extended, "discoverable");
            SerializationHelper.MovePropertyIfExists(ref body, ref extended, "entryLevel");
            SerializationHelper.MovePropertyIfExists(ref body, ref extended, "language");
            SerializationHelper.MovePropertyIfExists(ref body, ref extended, "longDescription");
            if (extended.HasValues)
            {
                body.Add(new JProperty("extended", extended));
            }
            return(result);
        }
Exemple #18
0
 /// <summary>Gets response json, stripping off wrapping array.</summary>
 /// <param name="responseData">Serialized response data.</param>
 /// <returns>Core response payload.</returns>
 protected static JToken GetResponseJson(SerializedMessageData responseData)
 => GetResponseJson(responseData.Payload);