Пример #1
0
        public APIResult<List<Event>> Events(ApplicationCredentialsModel model)
        {
            try
            {
                if (model == null)
                    throw new Exception("Please pass model");

                using (var bo = new ApplicationBusinessObject())
                {
                    if (!bo.Check(model.ApplicationId, model.ApplicationSecretKey))
                        throw new Exception("Invalid application credentials");
                }

                using (var bo = new EventBusinessObject())
                {
                    var result = bo.GetList();

                    return new APIResult<List<Event>>(result);
                }

            }
            catch (Exception ex)
            {
                return new APIResult<List<Event>>(ex.Message, ex.ToString());
            }
        }
Пример #2
0
        public APIResult DeleteEvent(DeleteEventModel model)
        {
            try
            {
                if (model == null)
                    throw new Exception("Please pass model");

                using (var bo = new ApplicationBusinessObject())
                {
                    if (!bo.Check(model.ApplicationId, model.ApplicationSecretKey))
                        throw new Exception("Invalid application credentials");
                }

                using (var bo = new EventBusinessObject())
                {
                    bo.Delete(model.ApplicationId, model.EventKey);
                }
            }
            catch (Exception ex)
            {
                return new APIResult(ex.Message, ex.ToString());
            }

            return new APIResult();
        }
Пример #3
0
        public override async Task OnConnected()
        {
            var applicationIdString = Context.QueryString[QUERY_STRING_APPLICATION_KEY];
            var externalUserToken = Context.QueryString[QUERY_STRING_TOKEN_KEY];
            var externalUserIPAddress = GetIPAddress();
            int applicationId;
            string checkUserIdUrl;

            if (!Int32.TryParse(applicationIdString, out applicationId))
                return;

            using (var bo = new ApplicationBusinessObject())
            {
                var application = bo.GetById(applicationId);
                if (application == null)
                    return;

                checkUserIdUrl = application.CheckUserIdUrl;
            }

            var externalUserId = await GetExternalUserId(checkUserIdUrl, externalUserToken, externalUserIPAddress);

            if (String.IsNullOrWhiteSpace(externalUserId))
                return;

            // Check if user exists in our database
            using (var bo = new UserBusinessObject())
            {
                var user = bo.GetList(x => x.ApplicationId == applicationId && x.ExternalUserId == externalUserId).FirstOrDefault();
                if (user == null) return;
            }

            GroupName = applicationId + externalUserId;


            await Groups.Add(Context.ConnectionId, GroupName);

            Clients.Caller.AuthenticationSuccess(applicationId, externalUserId);
            Clients.Group(GroupName).UserConnected(externalUserId);

            await base.OnConnected();
        }
Пример #4
0
        public APIResult<List<Message>> Messages(GetMessagesModel model)
        {
            try
            {
                if (model == null)
                    throw new Exception("Please pass model");

                using (var bo = new ApplicationBusinessObject())
                {
                    if (!bo.Check(model.ApplicationId, model.ApplicationSecretKey))
                        throw new Exception("Invalid application credentials");
                }

                if (String.IsNullOrEmpty(model.Ids))
                    throw new Exception("Please pass Ids parameter");

                var ids = model.Ids.Split(',')
                    .Select(x =>
                    {
                        int i;
                        return Int32.TryParse(x, out i) ? (int?)i : null;
                    })
                    .Where(x => x.HasValue)
                    .ToList();

                if (ids.Count == 0)
                    return new APIResult<List<Message>>(new List<Message>());

                using (var bo = new MessageBusinessObject())
                {
                    var result = bo.GetList(x => ids.Contains(x.Id));

                    return new APIResult<List<Message>>(result);
                }
            }
            catch (Exception ex)
            {
                return new APIResult<List<Message>>(ex.Message, ex.ToString());
            }
        }
Пример #5
0
        public APIResult UnsubscribeEvent(SubscribeUserModel model)
        {
            try
            {
                if (model == null)
                    throw new Exception("Please pass model");

                using (var bo = new ApplicationBusinessObject())
                {
                    if (!bo.Check(model.ApplicationId, model.ApplicationSecretKey))
                        throw new Exception("Invalid application credentials");
                }

                int eventId;
                using (var bo = new EventBusinessObject())
                {
                    var ev = bo.GetByKey(model.ApplicationId, model.EventKey);
                    if (ev == null)
                        throw new Exception("Event not found");

                    eventId = ev.Id;
                }

                int userId;
                using (var bo = new UserBusinessObject())
                {
                    var user = bo.GetByExternalUserId(model.ApplicationId, model.ExternalUserId);
                    if (user == null)
                        throw new Exception("User not found");

                    userId = user.Id;
                }

                Provider provider;
                using (var bo = new ProviderBusinessObject())
                {
                    provider = bo.GetByKey(model.ProviderKey);
                    if (provider == null)
                        throw new Exception("Provider not found");
                }

                var subscription = new Subscription
                {
                    EventId = eventId,
                    UserId = userId,
                    ProviderId = provider.Id
                };

                using (var bo = new SubscriptionBusinessObject())
                {
                    bo.Unsubscribe(subscription);
                }

            }
            catch (Exception ex)
            {
                return new APIResult(ex.Message, ex.ToString());
            }

            return new APIResult();
        }
Пример #6
0
        public APIResult<List<SubscriptionResult>> Subscriptions(GetSubscriptionsModel model)
        {
            try
            {
                if (model == null)
                    throw new Exception("Please pass model");

                using (var bo = new ApplicationBusinessObject())
                {
                    if (!bo.Check(model.ApplicationId, model.ApplicationSecretKey))
                        throw new Exception("Invalid application credentials");
                }

                var eventKeys = new List<string>();
                var providerKeys = new List<string>();
                var userKeys = new List<string>();

                if (!String.IsNullOrEmpty(model.EventKeys))
                    eventKeys = model.EventKeys.Split(',').ToList();

                if (!String.IsNullOrEmpty(model.ProviderKeys))
                    providerKeys = model.ProviderKeys.Split(',').ToList();

                if (!String.IsNullOrEmpty(model.ExternalUserIds))
                    userKeys = model.ExternalUserIds.Split(',').ToList();

                using (var bo = new SubscriptionBusinessObject())
                {
                    var items = bo.GetList(model.ApplicationId, eventKeys, providerKeys, userKeys);

                    var result = items.Select(x => new SubscriptionResult
                    {
                        EventKey = x.Event.Key,
                        ProviderKey = x.Provider.Key,
                        ExternalUserId = x.User.ExternalUserId
                    }).ToList();

                    return new APIResult<List<SubscriptionResult>>(result);
                }

            }
            catch (Exception ex)
            {
                return new APIResult<List<SubscriptionResult>>(ex.Message, ex.ToString());
            }
        }
Пример #7
0
        public APIResult<List<int>> Send(SendModel model)
        {
            var messageIds = new List<int>();

            try
            {
                if (model == null)
                    throw new Exception("Please pass model");

                using (var bo = new ApplicationBusinessObject())
                {
                    if (!bo.Check(model.ApplicationId, model.ApplicationSecretKey))
                        throw new Exception("Invalid application credentials");
                }

                if (String.IsNullOrEmpty(model.Content))
                    throw new Exception("Content can't be empty");

                Provider provider;
                using (var bo = new ProviderBusinessObject())
                {
                    provider = bo.GetByKey(model.ProviderKey);
                    if (provider == null)
                        throw new Exception("Provider not found");
                }

                if (model.ExternalUserIds == null)
                    model.ExternalUserIds = new List<string>();

                if (!String.IsNullOrEmpty(model.ExternalUserId))
                    model.ExternalUserIds.Add(model.ExternalUserId);

                var userInfos = GetUserInfos(model.ApplicationId, model.ExternalUserIds, provider.Id);

                if (!String.IsNullOrEmpty(model.To))
                    userInfos.Add(new Tuple<int?, string>(null, model.To));

                if (userInfos.Count == 0)
                    throw new Exception("Please set: ExternalUserId, ExternalUserIds, or To");

                var groupId = Guid.NewGuid();

                foreach (var info in userInfos)
                {
                    var message = new Message
                    {
                        UserId = info.Item1,
                        To = info.Item2,
                        ProviderId = provider.Id,
                        ApplicationId = model.ApplicationId,
                        Subject = model.Subject,
                        Content = model.Content,
                        ProcessDate = model.ProcessDate,
                        GroupId = groupId,
                        Priority = model.Priority ?? MessagePriority.Normal
                    };

                    using (var bo = new MessageBusinessObject())
                    {
                        var id = bo.Create(message);
                        messageIds.Add(id);
                    }
                }
            }
            catch (Exception ex)
            {
                return new APIResult<List<int>>(ex.Message, ex.ToString());
            }

            return new APIResult<List<int>>(messageIds);
        }
Пример #8
0
        public APIResult SaveUsers(SaveUsersModel model)
        {
            try
            {
                if (model == null)
                    throw new Exception("Please pass model");

                using (var bo = new ApplicationBusinessObject())
                {
                    if (!bo.Check(model.ApplicationId, model.ApplicationSecretKey))
                        throw new Exception("Invalid application credentials");
                }

                foreach (var item in model.Users)
                {
                    var user = new User
                    {
                        ApplicationId = model.ApplicationId,
                        ExternalUserId = item.ExternalUserId,
                        FirstName = item.FirstName,
                        FullName = item.FullName,
                        Email = item.Email,
                        MobileNumber = item.MobileNumber
                    };

                    using (var bo = new UserBusinessObject())
                    {
                        bo.Save(user);
                    }
                }

            }
            catch (Exception ex)
            {
                return new APIResult(ex.Message, ex.ToString());
            }

            return new APIResult();
        }
Пример #9
0
        public APIResult SaveEvent(SaveEventViewModel model)
        {
            try
            {
                if (model == null)
                    throw new Exception("Please pass model");

                using (var bo = new ApplicationBusinessObject())
                {
                    if (!bo.Check(model.ApplicationId, model.ApplicationSecretKey))
                        throw new Exception("Invalid application credentials");
                }

                using (var bo = new EventBusinessObject())
                {
                    var item = new Event
                    {
                        ApplicationId = model.ApplicationId,
                        Key = model.Key,
                        Description = model.Description
                    };

                    bo.Save(item);
                }
            }
            catch (Exception ex)
            {
                return new APIResult(ex.Message, ex.ToString());
            }

            return new APIResult();
        }