Exemple #1
0
        public async Task SwitchAccountConnection(string username, string instance, bool connect,
                                                  CancellationToken cancellationToken)
        {
            var id = new AccountIdentifier(new Username(username), new Instance(instance));

            var mastodonAccount = await _mastodonAccountRepository.FindMastodonAccount(id, cancellationToken);

            if (mastodonAccount is not null)
            {
                if (connect)
                {
                    var connection = _makeMastodonConnection.MakeConnection(mastodonAccount.Username.Value, mastodonAccount.Instance.Value,
                                                                            mastodonAccount.AccessToken.Token);
                    _connectionRepository.AddConnection(new Connection(id, connection));
                }
                else
                {
                    _connectionRepository.StopConnection(id);
                }

                mastodonAccount.IsReadingPostsFromThisAccount = new IsReadingPostsFromThisAccount(connect);
                await _mastodonAccountRepository.SaveMastodonAccount(mastodonAccount, cancellationToken);

                return;
            }

            var misskeyAccount = await _misskeyAccountRepository.FindMisskeyAccount(id, cancellationToken);

            if (misskeyAccount is not null)
            {
                if (connect)
                {
                    var connection = _makeMisskeyConnection.MakeConnection(misskeyAccount.Username.Value,
                                                                           misskeyAccount.Instance.Value, misskeyAccount.AccessToken.Token);
                    _connectionRepository.AddConnection(new Connection(id, connection));
                }
                else
                {
                    _connectionRepository.StopConnection(id);
                }

                misskeyAccount.IsReadingPostsFromThisAccount = new IsReadingPostsFromThisAccount(connect);
                await _misskeyAccountRepository.SaveMisskeyAccount(misskeyAccount, cancellationToken);

                return;
            }
        }
        public async Task Login(string host, CancellationToken cancellationToken)
        {
            string secret;

            try
            {
                secret = await _registerClient.RegisterClient(host, cancellationToken);
            }
            catch
            {
                _showRegisterClientError.ShowRegisterClientError();
                throw;
            }

            var(sessionToken, authorizeUrl) =
                await _getAuthorizeUrl.GetAuthorizeUri(host, secret, cancellationToken);

            _showAuthUrl.ShowAuthUrl(authorizeUrl);

            while (true)
            {
                try
                {
                    await _waitAuthorize.WaitAuthorize(cancellationToken);

                    break;
                }
                catch
                {
                    _showAuthorizeError.ShowAuthorizeError();
                }
            }

            var(accessTokenData, user) =
                await _getAccessToken.GetAccessToken(host, secret, sessionToken, cancellationToken);

            var username    = new Username(user.Username);
            var instance    = new Instance(host);
            var displayName = new DisplayName(user.DisplayName);
            var identifier  = new AccountIdentifier(username, instance);
            var accessToken = new MisskeyAccessToken(accessTokenData);
            var iconUrl     = new AccountIconUrl(user.IconUrl);

            var account = await _accountRepository.FindMisskeyAccount(identifier, cancellationToken);

            if (account is not null)
            {
                account = account with {
                    AccessToken = accessToken, IconUrl = iconUrl
                };
            }
            else
            {
                account = new MisskeyAccount(username, instance, displayName, accessToken, iconUrl,
                                             new IsReadingPostsFromThisAccount(true));
            }

            await _accountRepository.SaveMisskeyAccount(account, cancellationToken);

            var connection = _makeMisskeyConnection.MakeConnection(account.Username.Value, account.Instance.Value,
                                                                   account.AccessToken.Token);

            _connectionRepository.AddConnection(new Connection(account.AccountIdentifier, connection));
        }