Пример #1
0
 public MastodonAccount CreateMastodonAccount(Username username, Instance instance, DisplayName displayName,
                                              MastodonAccessToken accessToken, AccountIconUrl iconUrl)
 {
     return(new(username, instance, displayName, accessToken, iconUrl,
                new IsReadingPostsFromThisAccount(true)));
 }
Пример #2
0
        public async Task Authorize(InstanceAndAuthenticationCode instanceAndAuthenticationCode,
                                    CancellationToken cancellationToken)
        {
            var instance = new Instance(instanceAndAuthenticationCode.Instance);

            AccessInfo accessInfo;

            try
            {
                var client = await _mastodonClientRepository.FindMastodonClient(instance, cancellationToken);

                if (client is null)
                {
                    _showMastodonAuthenticationError.ShowMastodonAuthenticationError();
                    return;
                }

                var authorizationInfo = new AuthorizationInfo(instance.Value, client.ClientId.Value,
                                                              client.ClientSecret.Value, instanceAndAuthenticationCode.AuthenticationCode);
                accessInfo =
                    await _authorizeMastodonAccountWithCode.AuthorizeMastodonAccountWithCode(authorizationInfo,
                                                                                             cancellationToken);
            }
            catch
            {
                _showMastodonAuthenticationError.ShowMastodonAuthenticationError();
                return;
            }

            AccountInfo accountInfo;

            try
            {
                accountInfo = await _getAccountInfo.GetAccountInfo(accessInfo, cancellationToken);
            }
            catch
            {
                _showGetMastodonAccountInfoError.ShowGetMastodonAccountInfoError();
                return;
            }

            var username    = new Username(accountInfo.Username);
            var displayName = new DisplayName(accountInfo.DisplayName);
            var accessToken = new MastodonAccessToken(accessInfo.Token);
            var iconUrl     = new AccountIconUrl(accountInfo.IconUrl);

            var account =
                await _mastodonAccountRepository.FindMastodonAccount(new AccountIdentifier(username, instance),
                                                                     cancellationToken);

            if (account is null)
            {
                account = _mastodonAccountRepository.CreateMastodonAccount(username, instance, displayName, accessToken,
                                                                           iconUrl);
            }
            else
            {
                account = account with {
                    AccessToken = accessToken, IconUrl = iconUrl
                };
            }

            await _mastodonAccountRepository.SaveMastodonAccount(account, cancellationToken);

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

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

            _finishAuthorizeMastodonAccount.FinishAuthorizeMastodonAccount();
        }
    }