コード例 #1
0
        public async Task ReceiveReblogNotification(MastodonReblogNotificationData data, CancellationToken cancellationToken)
        {
            var id = new AccountIdentifier(new Username(data.Username), new Instance(data.Instance));

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

            if (account is null)
            {
                return;
            }
            var displayName = account.DisplayName.Value;

            var item = new MastodonReblogNotification(id, displayName, data.ReblogUserDisplayName,
                                                      data.ReblogUserUsername, data.Content, data.MediaDescriptions);

            var container = _readingTextContainerRepository.GetContainer();

            container.Add(item.ConvertToReadingText());
        }
コード例 #2
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;
            }
        }
コード例 #3
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();
        }
    }