Exemplo n.º 1
0
        public async Task StoreAsync <T>(string key, T value)
        {
            key.NullInspect(nameof(key));

            var dbWorker = new GmailDbContextWorker();

            var json = JsonConvert.SerializeObject(value);

            int userId;

            if (!int.TryParse(key, out userId))
            {
                throw new ArgumentException("Wrong key, it must be an User Id number.", key);
            }

            var userModel = await dbWorker.FindUserAsync(userId);

            if (userModel == null)
            {
                throw new DbDataStoreException(
                          $"Can't store refreshed data in database. {nameof(UserModel)} record with id {userId} is absent in the database.");
            }

            JsonConvert.PopulateObject(json, userModel);
            await dbWorker.UpdateUserRecordAsync(userModel);

            LogMaker.Log(Logger, $"{nameof(UserModel)} record with userId={userId} updated with new access token.", false);
        }
Exemplo n.º 2
0
        public async Task SendAuthorizeLink(ISender message, AuthorizeLinks links)
        {
            try
            {
                var gmailDbContextWorker = new GmailDbContextWorker();

                LogMaker.Log(Logger, $"Start authorizing user with UserId={message.From.Id}.", false);
                var userModel = await gmailDbContextWorker.FindUserAsync(message.From.Id) ??
                                await gmailDbContextWorker.AddNewUserAsync(message.From);

                LogMaker.Log(Logger, $"The user with id:{userModel.UserId} has requested authorization", false);

                var fullAccessState   = Base64.Encode($"{userModel.UserId},{UserAccess.FULL}");
                var notifyAccessState = Base64.Encode($"{userModel.UserId},{UserAccess.NOTIFY}");

                var pendingUserModel = await gmailDbContextWorker.FindPendingUserAsync(userModel.UserId);

                if (pendingUserModel != null)
                {
                    await gmailDbContextWorker.UpdateRecordJoinTimeAsync(pendingUserModel.Id, DateTime.UtcNow);
                }
                else
                {
                    await gmailDbContextWorker.QueueAsync(userModel.UserId);
                }

                Uri notifyAccessUri = null;
                Uri fullAccessUri   = null;
                switch (links)
                {
                case AuthorizeLinks.Both:
                    notifyAccessUri = GetAuthenticationUri(notifyAccessState, UserAccessAttribute.GetScopesValue(UserAccess.NOTIFY));
                    fullAccessUri   = GetAuthenticationUri(fullAccessState, UserAccessAttribute.GetScopesValue(UserAccess.FULL));
                    break;

                case AuthorizeLinks.Full:
                    fullAccessUri = GetAuthenticationUri(fullAccessState, UserAccessAttribute.GetScopesValue(UserAccess.FULL));
                    break;

                case AuthorizeLinks.Notify:
                    notifyAccessUri = GetAuthenticationUri(notifyAccessState, UserAccessAttribute.GetScopesValue(UserAccess.NOTIFY));
                    break;

                default:
                    throw new ArgumentOutOfRangeException(nameof(links), links, null);
                }

                await _botActions.AuthorizeMessage(message.From.Id.ToString(), notifyAccessUri, fullAccessUri);
            }
            catch (Exception ex)
            {
                throw new AuthorizeException("An error occurred while trying to send the authentication link to the user", ex);
            }
        }
Exemplo n.º 3
0
        public async Task DeleteAsync <T>(string key)
        {
            key.NullInspect(nameof(key));

            var dbWorker = new GmailDbContextWorker();
            int userId;

            if (!int.TryParse(key, out userId))
            {
                throw new ArgumentException("Wrong key, it must be an User Id number.", key);
            }

            var service = ServiceFactory.Instance?.ServiceCollection.Find(s => s.From == userId);

            if (service == null)
            {
                return;
            }

            var messageHandler = BotInitializer.Instance?.MessageHandler;

            if (messageHandler != null)
            {
                try
                {
                    await messageHandler.HandleStopWatchCommandAsync(service);

                    LogMaker.Log(Logger, $"Push notifications receiving has stopped for user with userId={userId}.", false);
                }
                catch (Exception ex)
                {
                    LogMaker.Log(Logger, ex, $"exception throwed in attempt to stop push notifications. userId={userId}.");
                }
            }

            var userModel = await dbWorker.FindUserAsync(userId);

            if (userModel == null)
            {
                LogMaker.Log(Logger,
                             $"Error while removing {nameof(UserModel)} record from database. {nameof(UserModel)} record with id {userId} is absent in the database.",
                             false);
            }
            else
            {
                await dbWorker.RemoveUserRecordAsync(userModel);

                LogMaker.Log(Logger, $"{nameof(UserModel)} record with userId={userId} deleted.", false);
            }

            var userSettingsModel = await dbWorker.FindUserSettingsAsync(userId);

            if (userSettingsModel == null)
            {
                LogMaker.Log(Logger,
                             $"Error while removing {nameof(UserSettingsModel)} record from database. {nameof(UserSettingsModel)} record with id {userId} is absent in the database.",
                             false);
            }
            else
            {
                await dbWorker.RemoveUserSettingsRecordAsync(userSettingsModel);

                LogMaker.Log(Logger, $"{nameof(UserSettingsModel)} record with userId={userId} deleted.", false);
            }

            var nmStoreModel = await dbWorker.FindNmStoreAsync(userId);

            if (nmStoreModel != null)
            {
                await dbWorker.RemoveNmStoreAsync(nmStoreModel);

                LogMaker.Log(Logger, $"{nameof(NmStoreModel)} record with userId={userId} deleted.", false);
            }
            var tempData = await dbWorker.FindTempDataAsync(userId);

            if (tempData != null)
            {
                await dbWorker.RemoveTempDataAsync(tempData);

                LogMaker.Log(Logger, $"{nameof(TempDataModel)} record with userId={userId} deleted.", false);
            }
            ServiceFactory.Instance?.ServiceCollection.Remove(service);
            LogMaker.Log(Logger, $"Removed service from {nameof(ServiceFactory.ServiceCollection)} with userId={userId}.", false);
        }