/// <summary>
        /// Parse the specified JSON obejct into an AttachedUrl
        /// </summary>
        /// <returns>The parse.</returns>
        /// <param name="m">The JSON object</param>
        public static AttachedUrl Parse(JObject m)
        {
            AttachedUrl url = new AttachedUrl();

            if (m == null)
            {
                return(url);
            }

            if (m["url"] != null && m["url"] is JValue)
            {
                url.Url = m["url"].Value <string>();
            }

            url.Meta = new Dictionary <string, string>();
            if (m["meta"] != null && m["meta"] is JObject)
            {
                foreach (var key in (m["meta"] as JObject))
                {
                    url.Meta.Add(key.Key, key.Value.Value <string>());
                }
            }

            url.Headers = new Dictionary <string, string>();
            if (m["headers"] != null && m["headers"] is JObject)
            {
                foreach (var key in (m["headers"] as JObject))
                {
                    url.Headers.Add(key.Key, key.Value.Value <string>());
                }
            }

            url.ParsedUrl = new Dictionary <string, string>();
            if (m["parsedUrl"] != null && m["parsedUrl"] is JObject)
            {
                FlattenJObject(url.ParsedUrl, "", m["parsedUrl"] as JObject);
            }

            return(url);
        }
Exemple #2
0
        public static Message Parse(IMeteor meteor, JObject m)
        {
            Message message = new Message(meteor);

            if (m["_id"] != null)
            {
                message.Id = (m["_id"] as JValue).Value <string>();
            }

            if (m["rid"] != null)
            {
                message.RoomId = (m["rid"] as JValue).Value <string>();
            }

            if (m["t"] != null && m["t"] is JValue)
            {
                switch ((m["t"] as JValue).Value <string>())
                {
                case "room_changed_topic":
                    message.Type = MessageType.ROOM_CHANGED_TOPIC;
                    break;

                case "uj":
                    message.Type = MessageType.USER_JOINED;
                    break;

                case "ul":
                    message.Type = MessageType.USER_LEFT;
                    break;

                case "au":
                    message.Type = MessageType.ADDED_USER;
                    break;

                case "message_pinned":
                    message.Type = MessageType.MESSAGE_PINNED;
                    break;

                default:
                    message.Type = MessageType.MESSAGE;
                    break;
                }
            }
            else
            {
                message.Type = MessageType.MESSAGE;
            }

            if (m["ts"] != null)
            {
                message.Created = TypeUtils.ParseDateTime(m["ts"] as JObject);
            }

            if (m["msg"] != null)
            {
                message.Text = (m["msg"] as JValue).Value <string>();
            }

            if (m["_updatedAt"] != null)
            {
                message.UpdatedAt = TypeUtils.ParseDateTime(m["_updatedAt"] as JObject);
            }

            if (m["editedAt"] != null)
            {
                message.EditedAt = TypeUtils.ParseDateTime(m["editedAt"] as JObject);
            }

            if (m["editedBy"] != null)
            {
                message.EditedBy = User.Parse(m["editedBy"] as JObject);
            }

            message.Urls = new List <AttachedUrl>();

            if (m["urls"] != null)
            {
                foreach (var url in m["urls"] as JArray)
                {
                    message.Urls.Add(AttachedUrl.Parse(url as JObject));
                }
            }

            message.Attachments = new List <Attachment>();
            if (m["attachments"] != null)
            {
                foreach (var attachment in m["attachments"])
                {
                    message.Attachments.Add(Attachment.Parse(attachment as JObject));
                }
            }

            if (m["alias"] != null)
            {
                message.Alias = m["alias"].Value <string>();
            }

            if (m["avatar"] != null)
            {
                message.Avatar = m["avatar"].Value <string>();
            }

            if (m["groupable"] != null)
            {
                message.Groupable = m["groupable"].Value <bool>();
            }

            if (m["parseUrls"] != null)
            {
                message.ParseUrls = m["parseUrls"].Value <bool>();
            }

            if (m["u"] != null)
            {
                message.User = User.Parse(m["u"] as JObject);
            }

            if (m["bot"] != null)
            {
                message.Bot = Bot.Parse(m["bot"] as JObject);
            }

            message.Reactions = new Dictionary <string, List <string> >();
            if (m["reactions"] != null && m["reactions"] is JObject)
            {
                foreach (var reaction in (m["reactions"] as JObject))
                {
                    message.Reactions.Add(reaction.Key, new List <string>());

                    if (reaction.Value["usernames"] != null)
                    {
                        foreach (var user in reaction.Value["usernames"])
                        {
                            message.Reactions[reaction.Key].Add(user.Value <string>());
                        }
                    }
                }
            }

            return(message);
        }