Skip to content

Commit

Permalink
List public keys
Browse files Browse the repository at this point in the history
  • Loading branch information
boreq committed Oct 13, 2023
1 parent 93403de commit 9ed66bb
Show file tree
Hide file tree
Showing 19 changed files with 188 additions and 13 deletions.
1 change: 1 addition & 0 deletions cmd/crossposting-service/di/inject_application.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ var applicationSet = wire.NewSet(
wire.Bind(new(sqlitepubsub.SendTweetHandler), new(*app.SendTweetHandler)),

app.NewGetSessionAccountHandler,
app.NewGetAccountPublicKeysHandler,
app.NewLoginOrRegisterHandler,
app.NewLinkPublicKeyHandler,
)
8 changes: 5 additions & 3 deletions cmd/crossposting-service/di/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export class PublicKey {
npub?: string;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import {PublicKey} from "@/dto/PublicKey";

export class PublicKeys {
publicKeys?: PublicKey[];
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import axios, {AxiosResponse} from 'axios';
import {CurrentUser} from "@/dto/CurrentUser";
import {Mutation} from '@/store';
import {PublicKeys} from "@/dto/PublicKeys";

export class APIService {

Expand All @@ -14,6 +15,11 @@ export class APIService {
return this.axios.get<CurrentUser>(url);
}

publicKeys(): Promise<AxiosResponse<PublicKeys>> {
const url = `/api/public-keys`;
return this.axios.get<PublicKeys>(url);
}

refreshCurrentUser(): Promise<CurrentUser> {
return new Promise((resolve, reject) => {
this.currentUser()
Expand Down
28 changes: 28 additions & 0 deletions frontend/nos-crossposting-service-frontend/src/views/HomeView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,22 @@

<div v-if="!loading && user">
<CurrentUser :user="user"/>

<div v-if="!publicKeys">
Loading public keys...
</div>

<div v-if="publicKeys">
<ul v-if="publicKeys.publicKeys?.length > 0">
<li v-for="publicKey in publicKeys.publicKeys" :key="publicKey.npub">
{{ publicKey.npub }}
</li>
</ul>

<p v-if="publicKeys.publicKeys?.length == 0">
You haven't added any public keys yet.
</p>
</div>
</div>

</div>
Expand All @@ -23,6 +39,8 @@ import Explanation from '@/components/Explanation.vue';
import LogInWithTwitterButton from "@/components/LogInWithTwitterButton.vue";
import {User} from "@/dto/User";
import CurrentUser from "@/components/CurrentUser.vue";
import {APIService} from "@/services/APIService";
import {PublicKeys} from "@/dto/PublicKeys";
@Options({
Expand All @@ -34,14 +52,24 @@ import CurrentUser from "@/components/CurrentUser.vue";
})
export default class HomeView extends Vue {
private readonly apiService = new APIService(useStore());
private readonly store = useStore();
publicKeys: PublicKeys | null = null;
get loading(): boolean {
return this.store.state.user === undefined;
}
get user(): User {
return this.store.state.user;
}
created() {
this.apiService.publicKeys()
.then(response => {
this.publicKeys = response.data;
})
}
}
</script>
15 changes: 15 additions & 0 deletions service/adapters/sqlite/public_key_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,21 @@ WHERE public_key = $1`,
return m.readPublicKeys(rows)
}

func (m *PublicKeyRepository) ListByAccountID(accountID accounts.AccountID) ([]*domain.LinkedPublicKey, error) {
rows, err := m.tx.Query(`
SELECT account_id, public_key, created_at
FROM public_keys
WHERE account_id = $1`,
accountID.String(),
)
if err != nil {
return nil, errors.Wrap(err, "query error")
}
defer rows.Close()

return m.readPublicKeys(rows)
}

func (m *PublicKeyRepository) readPublicKeys(rows *sql.Rows) ([]*domain.LinkedPublicKey, error) {
var results []*domain.LinkedPublicKey
for rows.Next() {
Expand Down
10 changes: 10 additions & 0 deletions service/adapters/sqlite/public_key_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,14 @@ func TestPublicKeyRepository_ListByPublicKeyReturnsOnlyRelevantData(t *testing.T
return nil
})
require.NoError(t, err)

err = adapters.TransactionProvider.Transact(ctx, func(ctx context.Context, adapters sqlite.TestAdapters) error {
results, err := adapters.PublicKeyRepository.ListByAccountID(accountID)
require.NoError(t, err)

require.Len(t, results, 2)

return nil
})
require.NoError(t, err)
}
9 changes: 6 additions & 3 deletions service/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type PublicKeyRepository interface {
Save(linkedPublicKey *domain.LinkedPublicKey) error
List() ([]*domain.LinkedPublicKey, error)
ListByPublicKey(publicKey domain.PublicKey) ([]*domain.LinkedPublicKey, error)
ListByAccountID(accountID accounts.AccountID) ([]*domain.LinkedPublicKey, error)
}

type ProcessedEventRepository interface {
Expand Down Expand Up @@ -84,9 +85,11 @@ type Adapters struct {
}

type Application struct {
GetSessionAccount *GetSessionAccountHandler
LoginOrRegister *LoginOrRegisterHandler
LinkPublicKey *LinkPublicKeyHandler
GetSessionAccount *GetSessionAccountHandler
GetAccountPublicKeys *GetAccountPublicKeysHandler

LoginOrRegister *LoginOrRegisterHandler
LinkPublicKey *LinkPublicKeyHandler
}

type ReceivedEvent struct {
Expand Down
54 changes: 54 additions & 0 deletions service/app/handler_get_account_public_keys.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package app

import (
"context"

"github.com/boreq/errors"
"github.com/planetary-social/nos-crossposting-service/internal/logging"
"github.com/planetary-social/nos-crossposting-service/service/domain"
"github.com/planetary-social/nos-crossposting-service/service/domain/accounts"
)

type GetAccountPublicKeys struct {
accountID accounts.AccountID
}

func NewGetAccountPublicKeys(accountID accounts.AccountID) GetAccountPublicKeys {
return GetAccountPublicKeys{accountID: accountID}
}

type GetAccountPublicKeysHandler struct {
transactionProvider TransactionProvider
logger logging.Logger
metrics Metrics
}

func NewGetAccountPublicKeysHandler(
transactionProvider TransactionProvider,
logger logging.Logger,
metrics Metrics,
) *GetAccountPublicKeysHandler {
return &GetAccountPublicKeysHandler{
transactionProvider: transactionProvider,
logger: logger.New("getAccountPublicKeys"),
metrics: metrics,
}
}

func (h *GetAccountPublicKeysHandler) Handle(ctx context.Context, cmd GetAccountPublicKeys) (result []*domain.LinkedPublicKey, err error) {
defer h.metrics.StartApplicationCall("getAccountPublicKeys").End(&err)

if err := h.transactionProvider.Transact(ctx, func(ctx context.Context, adapters Adapters) error {
publicKeys, err := adapters.PublicKeys.ListByAccountID(cmd.accountID)
if err != nil {
return errors.Wrap(err, "error getting a session")
}

result = publicKeys
return nil
}); err != nil {
return nil, errors.Wrap(err, "transaction error")
}

return result, nil
}
2 changes: 1 addition & 1 deletion service/ports/http/frontend/index.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<!doctype html><html lang=""><head><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><link rel="icon" href="/favicon.ico"><title>nos-crossposting-service-frontend</title><script defer="defer" src="/js/chunk-vendors.6ebd820c.js"></script><script defer="defer" src="/js/app.1a52fb73.js"></script><link href="/css/app.ddbde395.css" rel="stylesheet"></head><body><noscript><strong>We're sorry but nos-crossposting-service-frontend doesn't work properly without JavaScript enabled. Please enable it to continue.</strong></noscript><div id="app"></div></body></html>
<!doctype html><html lang=""><head><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><link rel="icon" href="/favicon.ico"><title>nos-crossposting-service-frontend</title><script defer="defer" src="/js/chunk-vendors.aa55fb88.js"></script><script defer="defer" src="/js/app.a0bb4349.js"></script><link href="/css/app.ddbde395.css" rel="stylesheet"></head><body><noscript><strong>We're sorry but nos-crossposting-service-frontend doesn't work properly without JavaScript enabled. Please enable it to continue.</strong></noscript><div id="app"></div></body></html>
2 changes: 0 additions & 2 deletions service/ports/http/frontend/js/app.1a52fb73.js

This file was deleted.

1 change: 0 additions & 1 deletion service/ports/http/frontend/js/app.1a52fb73.js.map

This file was deleted.

Loading

0 comments on commit 9ed66bb

Please sign in to comment.