Пример #1
0
        internal ResultBase SendMessage(string userKey, string message, string communityId)
        {
            IMongoCollection <DBEntry.Community> communitiesCollection = Program.Database.GetCollection <DBEntry.Community>("communities");
            IMongoCollection <DBEntry.Message>   messageCollection     = Program.Database.GetCollection <DBEntry.Message>("messages");

            (UserGatewayController.CheckUserResult userResult, DBEntry.User user) = UserGatewayController.CheckUser(userKey);
            if (!userResult.success)
            {
                return(userResult);
            }
            List <DBEntry.Community> communities = communitiesCollection.Find(x => x.UniqueName == communityId).ToList();

            if (communities.Count != 1)
            {
                return(new Api.Result()
                {
                    type = "no community", authorized = true, success = false, message = "community does not exists"
                });
            }
            DBEntry.Community community = communities[0];
            if (!community.Users.Contains(user.Username))
            {
                return(new Api.Result()
                {
                    type = "unauthorized for community", authorized = true, success = false, message = "you are not part of that community"
                });
            }
            MongoDB.Bson.ObjectId objectId = MongoDB.Bson.ObjectId.GenerateNewId();
            messageCollection.InsertOneAsync(new DBEntry.Message()
            {
                Author = user.Username, CommunityId = communityId, Content = message, Ip = Request.HttpContext.Connection.RemoteIpAddress.ToString(), TimeOfSending = DateTime.Now, _id = objectId
            });
            if (RESTMessageDictionary.ContainsKey(community.UniqueName))
            {
                GetMessageResult.Message RESTMessage = new GetMessageResult.Message()
                {
                    author        = user.Username,
                    content       = message,
                    timeOfSending = ((DateTimeOffset)DateTime.Now).ToUnixTimeSeconds(),
                    objectId      = objectId.ToString()
                };
                while (RESTMessageDictionary[community.UniqueName].Count != 0)
                {
                    if (!RESTMessageDictionary[community.UniqueName][0].Task.Task.IsCompleted)
                    {
                        RESTMessageDictionary[community.UniqueName][0].Task.SetResult(RESTMessage);
                    }
                    try {
                        RESTMessageDictionary[community.UniqueName].RemoveAt(0);
                    }
                    catch (Exception) { };
                }
            }
            return(new Api.Result()
            {
                type = "successful", authorized = true, success = true, message = "all good"
            });
        }
Пример #2
0
        internal async Task <RESTMessageResult> RESTMessage(string userKey, string communityId)
        {
            IMongoCollection <DBEntry.Community> communitiesCollection = Program.Database.GetCollection <DBEntry.Community>("communities");
            IMongoCollection <DBEntry.Message>   messageCollection     = Program.Database.GetCollection <DBEntry.Message>("messages");

            (UserGatewayController.CheckUserResult userResult, DBEntry.User user) = UserGatewayController.CheckUser(userKey);
            if (!userResult.success)
            {
                return(new RESTMessageResult()
                {
                    authorized = userResult.authorized,
                    message = userResult.message,
                    restMessage = null,
                    success = userResult.success,
                    type = userResult.type
                });
            }
            List <DBEntry.Community> communities = communitiesCollection.Find(x => x.UniqueName == communityId).ToList();

            if (communities.Count != 1)
            {
                return(new RESTMessageResult()
                {
                    type = "no community", authorized = true, success = false, message = "community does not exists"
                });
            }
            DBEntry.Community community = communities[0];
            if (!community.Users.Contains(user.Username))
            {
                return(new RESTMessageResult()
                {
                    type = "unauthorized for community", authorized = true, success = false, message = "you are not part of that community"
                });
            }
            TaskCompletionSource <GetMessageResult.Message> tcs = new TaskCompletionSource <GetMessageResult.Message>();

            if (RESTMessageDictionary.ContainsKey(communityId))
            {
                RESTMessageDictionary[communityId].Add(new Resting()
                {
                    Ip   = Request.HttpContext.Connection.RemoteIpAddress.ToString(),
                    Task = tcs,
                    User = user.Username
                });
            }
            else
            {
                RESTMessageDictionary.Add(communityId, new List <Resting>()
                {
                    new Resting()
                    {
                        Ip   = Request.HttpContext.Connection.RemoteIpAddress.ToString(),
                        Task = tcs
                    }
                });
            }
            GetMessageResult.Message awaitedMessage = await tcs.Task;
            return(new RESTMessageResult()
            {
                type = "successful",
                authorized = true,
                success = true,
                message = "all good",
                restMessage = awaitedMessage
            });
        }