Esempio n. 1
0
        public void LeaveConversation(Conversation conv)
        {
            Connection.Send(CommandBuilder.Leave(conv));

            ConversationEvent evt = new ConversationEvent(Local, Tools.GetUnixTime(), conv.Id, EventType.UserLeft);

            ProcessConversationItem(evt);
        }
Esempio n. 2
0
        public void ChangeConversationName(Conversation conv, string name)
        {
            Connection.Send(CommandBuilder.ChangeName(conv, name));

            ConversationEvent evt = new ConversationEvent(Local, Tools.GetUnixTime(), conv.Id, EventType.NameChanged);

            evt.Args["Name"] = name;
            ProcessConversationItem(evt);
        }
Esempio n. 3
0
        public void AddParticipant(Conversation conv, User user)
        {
            Connection.Send(CommandBuilder.Add(conv, user));

            using (var db = new LiteDatabase(DbPath))
            {
                // If the user is not already in the db, add it
                var users = db.GetCollection <User>("users");
                if (!users.Exists(Query.EQ("_id", user.Id)))
                {
                    users.Insert(user);
                }
            }

            ConversationEvent evt = new ConversationEvent(Local, Tools.GetUnixTime(), conv.Id, EventType.UserAdded);

            evt.Args["User"] = user;
            ProcessConversationItem(evt);
        }
Esempio n. 4
0
        internal void ProcessConversationItem(IConversationItem item)
        {
            using (var db = new LiteDatabase(DbPath))
            {
                Conversation conv = Conversations.Where(x => x.Id == item.ConversationId).First();

                // Update the db
                if (item.IsMessage)
                {
                    Message msg      = item as Message;
                    var     messages = db.GetCollection <Message>("messages");
                    if (messages.Exists(Query.EQ("_id", msg.Id)))
                    {
                        return;
                    }
                    messages.Insert(item as Message);
                }
                else
                {
                    ConversationEvent evt = item as ConversationEvent;

                    var events = db.GetCollection <ConversationEvent>("events");

                    if (events.Exists(Query.EQ("_id", evt.Id)))
                    {
                        return;
                    }
                    events.Insert(evt);

                    var conversations = db.GetCollection <Conversation>("conversations");
                    conversations.EnsureIndex(x => x.Id);
                    Conversation dbconv = conversations.FindOne(Query.EQ("_id", evt.ConversationId));

                    /* In case of an event, some property of the conversation must be updated
                     * (both in-memory and in the db) */
                    switch (evt.Type)
                    {
                    case EventType.NameChanged:
                        RunOnUI(new ThreadStart(() => { conv.Name = evt.Args["Name"] as string; }));

                        dbconv.Name = evt.Args["Name"] as string;
                        break;

                    case EventType.UserAdded:
                        var  users = db.GetCollection <User>("users");
                        User user  = evt.Args["User"] as User;

                        if (conv.Participants.Where(x => x.Id == user.Id).Count() > 0)
                        {
                            return;
                        }

                        RunOnUI(new ThreadStart(() => { conv.Participants.Add(user); }));

                        dbconv.Participants.Add(user);
                        break;

                    case EventType.UserLeft:
                        if (conv.Participants.Where(x => x.Id == evt.Sender.Id).Count() > 0)
                        {
                            RunOnUI(new ThreadStart(() => { conv.Participants.Remove(conv.Participants.Where(x => x.Id == evt.Sender.Id).First()); }));
                        }

                        if (dbconv.Participants.Where(x => x.Id == evt.Sender.Id).Count() > 0)
                        {
                            dbconv.Participants.Remove(dbconv.Participants.Where(x => x.Id == evt.Sender.Id).First());
                        }
                        break;
                    }

                    conversations.Update(dbconv);  // Save the changes
                }

                // Update the in-memory model
                RunOnUI(new ThreadStart(() => {
                    conv.AddItem(item);
                    ConversationItemEvent?.Invoke(this, new ConversationItemArgs(item));
                }));
            }
        }
Esempio n. 5
0
        public static void Process(LCPClient client, XDocument xml)
        {
            XElement command = xml.Root;
            string   type    = command.Name.ToString();

            switch (type)
            {
            case "Response":
                if ((command.Attribute("Command").Value.Equals("LogIn") || command.Attribute("Command").Value.Equals("SignUp")) &&
                    Boolean.Parse(command.Attribute("Success").Value))
                {
                    client.ProcessLogIn(int.Parse(command.Attribute("Id").Value));
                }
                break;

            case "Error":
                if (command.Attribute("Command").Value.Equals("LogIn"))
                {
                    var reader1 = command.CreateReader();
                    reader1.MoveToContent();
                    string error = reader1.ReadInnerXml();
                    client.ProcessLogInFailed(error);
                }
                else
                {
                    var reader3 = command.CreateReader();
                    reader3.MoveToContent();
                    string error = reader3.ReadInnerXml();
                    Console.WriteLine("Error ! " + error);
                }
                break;

            case "Message":
                if (int.Parse(command.Attribute("Sender").Value).Equals(client.Local.Id))
                {
                    break;
                }

                var reader2 = command.CreateReader();
                reader2.MoveToContent();
                string message = reader2.ReadInnerXml();

                using (var db = new LiteDatabase(client.DbPath))
                {
                    var         users  = db.GetCollection <User>("users");
                    User        sender = users.FindOne(Query.EQ("_id", int.Parse(command.Attribute("Sender").Value)));
                    TextMessage msg    = new TextMessage(message, sender, double.Parse(command.Attribute("Time").Value, CultureInfo.InvariantCulture), command.Attribute("Conversation").Value, sender.Id == client.Local.Id);
                    client.ProcessConversationItem(msg);
                }
                break;

            case "NewConversation":
                using (var db = new LiteDatabase(client.DbPath))
                {
                    var conversations = db.GetCollection <Conversation>("conversations");
                    if (conversations.Exists(Query.EQ("_id", command.Attribute("Id").Value)))
                    {
                        break;
                    }

                    var users = db.GetCollection <User>("users");

                    List <User> participants = new List <User>();
                    byte[]      image        = null;
                    foreach (XElement child in command.Elements())
                    {
                        if (child.Name.LocalName.Equals("Image"))
                        {
                            var reader3 = child.CreateReader();
                            reader3.MoveToContent();
                            image = Convert.FromBase64String(reader3.ReadInnerXml());
                        }
                        else if (child.Name.LocalName.Equals("Participant"))
                        {
                            User user;
                            int  id = int.Parse(child.Attribute("Id").Value);
                            if (users.Exists(Query.EQ("_id", id)))
                            {
                                user = users.FindOne(Query.EQ("_id", id));
                            }
                            else
                            {
                                user = new User(id, child.Attribute("Username").Value);
                                users.Insert(user);
                            }
                            participants.Add(user);
                        }
                    }

                    User creator;
                    int  creator_id = int.Parse(command.Attribute("Creator").Value);
                    if (users.Exists(Query.EQ("_id", creator_id)))
                    {
                        creator = users.FindOne(Query.EQ("_id", creator_id));
                    }
                    else
                    {
                        creator = new User(creator_id, command.Attribute("C_Username").Value);
                        users.Insert(creator);
                    }

                    Conversation conv = new Conversation(command.Attribute("Id").Value,
                                                         command.Attribute("Name").Value, participants, image);
                    client.ProcessConversation(conv, creator, double.Parse(command.Attribute("Time").Value, CultureInfo.InvariantCulture));
                }
                break;

            case "Search":
                List <User> results = new List <User>();
                foreach (XElement child in command.Elements())
                {
                    results.Add(new User(int.Parse(child.Attribute("Id").Value), child.Attribute("Username").Value));
                }
                client.ProcessSearchResults(results);
                break;

            case "UserLeft":

                using (var db = new LiteDatabase(client.DbPath))
                {
                    var  users = db.GetCollection <User>("users");
                    User user  = users.FindOne(Query.EQ("_id", int.Parse(command.Attribute("User").Value)));

                    ConversationEvent evt = new ConversationEvent(user, double.Parse(command.Attribute("Time").Value, CultureInfo.InvariantCulture), command.Attribute("Conversation").Value, EventType.UserLeft);
                    client.ProcessConversationItem(evt);
                }
                break;


            case "ChangeName":
                using (var db = new LiteDatabase(client.DbPath))
                {
                    var  users = db.GetCollection <User>("users");
                    User user  = users.FindOne(Query.EQ("_id", int.Parse(command.Attribute("User").Value)));

                    var reader3 = command.CreateReader();
                    reader3.MoveToContent();
                    string name = reader3.ReadInnerXml();

                    ConversationEvent evt = new ConversationEvent(user, double.Parse(command.Attribute("Time").Value, CultureInfo.InvariantCulture), command.Attribute("Conversation").Value, EventType.NameChanged);
                    evt.Args.Add("Name", name);
                    client.ProcessConversationItem(evt);
                }
                break;

            case "Add":
                using (var db = new LiteDatabase(client.DbPath))
                {
                    var users = db.GetCollection <User>("users");

                    User user;
                    int  id = int.Parse(command.Attribute("Id").Value);
                    if (users.Exists(Query.EQ("_id", id)))
                    {
                        user = users.FindOne(Query.EQ("_id", id));
                    }
                    else
                    {
                        user = new User(id, command.Attribute("Username").Value);
                        users.Insert(user);
                    }

                    User addedby = users.FindOne(Query.EQ("_id", int.Parse(command.Attribute("AddedBy").Value)));

                    ConversationEvent evt = new ConversationEvent(addedby, double.Parse(command.Attribute("Time").Value, CultureInfo.InvariantCulture), command.Attribute("Conversation").Value, EventType.UserAdded);
                    evt.Args["User"] = user;
                    client.ProcessConversationItem(evt);
                }
                break;

            default:
                break;
            }
        }