Ejemplo n.º 1
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));
                }));
            }
        }
Ejemplo n.º 2
0
 public ConversationItemArgs(IConversationItem item)
 {
     Item = item;
 }