Example #1
0
        /// <summary>
        /// Formats the given CsQuery DOM Elements into a list Message objects with all
        /// the relevant information
        /// </summary>
        ///
        /// <param name="messages">
        /// The list of all DOM elements containing the chat messages
        /// </param>
        ///
        /// <param name="isOutMessage">
        /// Indicates whether these are messages sent by the user (outcoming)
        /// or received by the user (sent by other users - incoming)
        /// </param>
        private List <ChatMessage> FormatMessages(CsQuery.CQ messages, bool isOutMessage)
        {
            var res = messages
                      .Select(x => x.Cq())
                      .Select(x =>
            {
                // get the message author. in case this are out-messages, then the author is me
                var author = (isOutMessage) ? "Me" : x.Find(".message-author .text-clickable").Text().Trim();

                // try fetch the content. in case we can't fetch anything - then
                // lets assume that the current message contains a photo and try
                // fetch its url
                var content = x.Find(".selectable-text").Text().Trim();

                // in case the message contains an image instead of text
                // TODO: understand how to use the string to extract the image
                // TODO: add support for videos as well
                if (string.IsNullOrEmpty(content))
                {
                    content = x.Find(".image-thumb > img").Attr("src");
                }

                // TODO: handle this properly, right now we skip any messages
                // that we couldn't find any text or url for
                // we skip them by returning null here and then filtering
                // any null messages, see few lines below \/ \/ \/ \/ \/
                if (string.IsNullOrEmpty(content))
                {
                    return(null);
                }

                return(new ChatMessage
                {
                    Author = author,
                    Content = content
                });
            })
                      .Where(x => x != null)
                      .ToList();

            // resolve author names for incoming messages
            if (!isOutMessage)
            {
                ResolveAuthors(res);
            }
            return(res);
        }