Jibbr.ViewModels.AccountViewModel.OnMessage C# (CSharp) Method

OnMessage() private method

private OnMessage ( object sender, agsXMPP msg ) : void
sender object
msg agsXMPP
return void
        private void OnMessage(object sender, agsXMPP.protocol.client.Message msg)
        {
            lock (chatSessionsMutext)
            {
                //Do we have a chat session with the sender of this message?
                ChatSessionViewModel chatSession = chatSessions.SingleOrDefault(x => x.Target.Bare == msg.From.Bare);
                if (chatSession == null)
                {
                    JIDViewModel friend = friends.SingleOrDefault(x => x.Bare == msg.From.Bare);
                    if (friend == null)
                    {
                        friend = new JIDViewModel(msg.From, this);
                        friends.Add(friend);
                        //No way to get groups here. :\
                    }
                    //Nope.  Create a new one.
                    chatSession = new ChatSessionViewModel(this, friend);
                    chatSessions.Add(chatSession);
                    NotifyChatSessionStarted(chatSession);
                }

                if (!String.IsNullOrEmpty(msg.Body))
                {
                    ChatMessage newMsg = new ChatMessage()
                        {
                            To = msg.To,
                            From = msg.From,
                            Date = GetTimestamp(msg),
                            Message = msg.Body,
                            MessageType = msg.Type
                        };
                    //Add the message.
                    chatSession.OnMessage(newMsg);

                    NotifyChatChatMessage(chatSession, newMsg);
                }
                else
                    chatSession.OnChatState(msg.Chatstate);
            }
        }