public IUser Store(IUser user)
        {
            if (!WriteUsersEnabled)
            {
                throw new Exception("not enabled");
            }
            lock (this) {
                user.Id = UserSerializer.GetId(user);
                if (user.CreateTime.Year <= 1900)
                {
                    user.CreateTime = DateTime.Now.ToUniversalTime();
                }
                user.UpdateTime = DateTime.Now.ToUniversalTime();
                var json = UserSerializer.GetJson(user, "store");
                var url  = GetBaseUrl() + user.Id + "?refresh=true";

                var result = EsClient.ExecuteCommand(url, json);
                if (null == result)
                {
                    throw new Exception("invalid storage operation", EsClient.LastError);
                }
                var j       = result.jsonify();
                var version = j.num("_version");
                user.Version       = version;
                _cache[user.Login] = user;
                return(user);
            }
        }
 public void MarkSent(string id)
 {
     EsClient.ExecuteCommand(BaseUrl() + "/" + id + "/_update?refresh=true", new {
         doc = new {
             WasSent  = true,
             SentTime = DateTime.Now.ToUniversalTime()
         }
     }.stringify());
 }
        private void Store(PostMessage message)
        {
            var result = EsClient.ExecuteCommand(BaseUrl() + "/" + message.Id + "?refresh=true", message.stringify());

            if (null == result)
            {
                Logg.Error(new {
                    error_in_es = EsClient.LastError.Message,
                    urls        = EsClient.Urls.ToArray()
                }.stringify());
                throw  EsClient.LastError;
            }
        }
 public IUser GetUser(string login)
 {
     if (!WriteUsersEnabled)
     {
         return(null);
     }
     lock (this) {
         CheckCache();
         if (EsClient.InvalidConnection)
         {
             return(null);
         }
         if (_cache.ContainsKey(login))
         {
             return(_cache[login]);
         }
         string json;
         if (login.StartsWith("!certid:"))
         {
             var post = new { query = new { match = new { publickey = login.Replace("!certid:", "") } } }.stringify();
             json = EsClient.ExecuteCommand(GetBaseUrl() + "_search", post);
         }
         else
         {
             var id = UserSerializer.GetId(login);
             json = EsClient.ExecuteCommand(GetBaseUrl() + id);
         }
         if (null == json)
         {
             return(null);
         }
         var j     = json.jsonify();
         var found = j.bul("found") || j.num("*.total") > 0;
         if (found)
         {
             var src     = j.map("*._source");
             var version = j.resolvenum("_version", "__version");
             var user    = UserSerializer.CreateFromJson(src);
             user.Id       = j.resolvestr("_id", "__id");
             user.Version  = version;
             _cache[login] = user;
         }
         else
         {
             _cache[login] = null;
         }
         return(_cache[login]);
     }
 }
        public IEnumerable <IUser> SearchUsers(UserSearchQuery query)
        {
            var q = string.IsNullOrWhiteSpace(query.Query)? "login:*" : query.Query;

            if (!string.IsNullOrWhiteSpace(query.Login))
            {
                q += " AND login:"******" AND name:" + query.Name;
            }
            if (!query.Groups)
            {
                q += " AND NOT isgroup:true";
            }
            if (!query.Users)
            {
                q += " AND isgroup:true";
            }
            if (!string.IsNullOrWhiteSpace(query.Domain))
            {
                q += " AND domain:" + query.Domain;
            }
            var esquery = new {
                query = new { query_string = new { query = q } }
            };
            var json = EsClient.ExecuteCommand(GetBaseUrl() + "_search", esquery.stringify());

            if (null == json)
            {
                yield break;
            }
            var j    = json.jsonify();
            var hits = j.arr("hits.hits");

            foreach (var hit in hits)
            {
                var src     = hit.map("*._source");
                var version = hit.resolvenum("_version", "__version");
                var user    = UserSerializer.CreateFromJson(src);
                user.Id            = hit.resolvestr("_id", "__id");
                user.Version       = version;
                _cache[user.Login] = user;
                yield return(user);
            }
        }
        public PostMessage GetMessage(string id)
        {
            var result = EsClient.ExecuteCommand("/" + Index + "/" + Type + "/" + id);

            if (null == result)
            {
                throw EsClient.LastError;
            }
            var j = result.jsonify();

            if (!j.bul("found"))
            {
                return(null);
            }
            var pm = CreateFromJson(j);

            return(pm);
        }
        private void CheckCache(bool forced = false)
        {
            if (!_initialized)
            {
                Initialize();
                _initialized = true;
            }
            if (!forced && EsClient.InvalidConnection)
            {
                if (EsClient.LastPing.AddMilliseconds(PingRate) > DateTime.Now)
                {
                    return;
                }
            }
            if (!forced && LastCheck.AddMilliseconds(CacheRate) > DateTime.Now)
            {
                return;
            }
            var currentETag    = ETag;
            var currentVersion = Version;
            var url            = GetBaseUrl() + "_search?search_type=count";
            var query          = leasequery;
            var json           = EsClient.ExecuteCommand(url, query);

            if (null == json)
            {
                ETag      = null;
                Version   = DateTime.MinValue;
                LastCheck = DateTime.MinValue;
            }
            else
            {
                var j = json.jsonify();
                ETag    = j.str("aggregations._version.value");
                Version = j.date("aggregations._timestamp.value_as_string");
            }
            if (ETag != currentETag || Version != currentVersion)
            {
                _cache.Clear();
            }
        }
        public IEnumerable <PostMessage> SearchMessages(object query)
        {
            var q = "";

            if (query is string)
            {
                q = (string)query;
                if (!q.StartsWith("{"))
                {
                    q = new {
                        query = new {
                            query_string = new {
                                query = q
                            }
                        }
                    }.stringify();
                }
            }
            else
            {
                q = query.stringify();
            }
            var result = EsClient.ExecuteCommand(BaseUrl() + "/_search", q);

            if (null == result)
            {
                throw EsClient.LastError;
            }
            var j    = result.jsonify();
            var hits = j.arr("hits.hits");

            if (null == hits)
            {
                yield break;
            }
            foreach (var hit in hits)
            {
                yield return(CreateFromJson(hit));
            }
        }