Ejemplo n.º 1
0
        public MessageInfo InsertMessage(string appName, string sessionId, ObjectId from, ObjectId to, string header, string message)
        {
            var dataBase = _serverWrapper.ServerConnection.GetDatabase(appName);
            var collection = dataBase.GetCollection<MessageSessionInfo>(MESSAGE_SESSIONS_COLLECTION_NAME);
            MessageSessionInfo session = EnsureSession(appName, sessionId, from, to, collection);
            bool isUser1From = session.User1 == from;
            MessageInfo messageInfo = new MessageInfo()
            {
                Id = Guid.NewGuid().ToString("N"),
                Body = message,
                From = from,
                Header = header,
                DateCreated = DateTime.Now
            };
            bool isDeletedSession = session.IsDeletedUser1 || session.IsDeletedUser2;
            UpdateBuilder updater = new UpdateBuilder();
            updater = updater.PushWrapped("messages", messageInfo)
                .Set("last_updated", DateTime.Now)
                .Inc(isUser1From ? "user2_count" : "user1_count", 1);

            //Make sure that if the session is deleted by this user, it'll automatically be active again.
            if (isDeletedSession)
            {
                updater.Set("user1_is_deleted", false).Set("user2_is_deleted", false);
            }
            collection.Update(Query.EQ("session_id", sessionId), updater);
            return messageInfo;
        }