Skip to content

Commit

Permalink
Deleting
Browse files Browse the repository at this point in the history
  • Loading branch information
boreq committed Oct 24, 2023
1 parent 6227221 commit 49b22d1
Show file tree
Hide file tree
Showing 20 changed files with 388 additions and 28 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 @@ -22,4 +22,5 @@ var applicationSet = wire.NewSet(
app.NewLinkPublicKeyHandler,
app.NewGetTwitterAccountDetailsHandler,
app.NewLogoutHandler,
app.NewUnlinkPublicKeyHandler,
)
2 changes: 2 additions & 0 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.

34 changes: 34 additions & 0 deletions frontend/nos-crossposting-service-frontend/src/assets/delete.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ export default class Button extends Vue {
disabled!: boolean;
onClick(event: Event): void {
this.$emit('click', event)
if (!this.disabled) {
this.$emit('buttonClick', event)
}
}
}
</script>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {Mutation, State} from '@/store';
import {PublicKeys} from "@/dto/PublicKeys";
import {AddPublicKeyRequest} from "@/dto/AddPublicKeyRequest";
import {Store} from "vuex";
import {PublicKey} from "@/dto/PublicKey";

export class APIService {

Expand All @@ -23,15 +24,20 @@ export class APIService {
}

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

addPublicKey(req: AddPublicKeyRequest): Promise<AxiosResponse<void>> {
const url = `/api/public-keys`;
const url = `/api/current-user/public-keys`;
return this.axios.post<void>(url, req);
}

deletePublicKey(publicKey: PublicKey): Promise<AxiosResponse<void>> {
const url = `/api/current-user/public-keys/${publicKey.npub}`;
return this.axios.delete<void>(url);
}

logoutCurrentUser(): Promise<void> {
return new Promise((resolve, reject) => {
this.logout()
Expand Down
112 changes: 105 additions & 7 deletions frontend/nos-crossposting-service-frontend/src/views/HomeView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,40 @@

<div v-if="!loadingUser && !user">
<div class="step">
1. Link your X account:
<div class="text">
1. Link your X account:
</div>
</div>

<LogInWithTwitterButton/>
</div>

<div v-if="!loadingUser && user">
<div class="step">
1. Logged in as
<div class="text">
1. Logged in as
</div>
</div>

<CurrentUser :user="user"/>
</div>

<div class="step">
2. Your nostr identities:
<div class="text">
2. Your nostr identities:
</div>
<ul class="actions">
<li v-if="publicKeys && publicKeys.publicKeys?.length > 0 && !editingPublicKeys">
<a @click="startEditingPublicKeys">
Edit
</a>
</li>
<li v-if="editingPublicKeys">
<a @click="endEditingPublicKeys">
Done
</a>
</li>
</ul>
</div>

<div class="public-keys-wrapper" v-if="!loadingUser && user">
Expand All @@ -30,16 +48,20 @@
<ul class="public-keys"
v-if="publicKeys && publicKeys.publicKeys?.length > 0">
<li v-for="publicKey in publicKeys.publicKeys" :key="publicKey.npub">
<a @click="scheduleDelete(publicKey)" v-if="editingPublicKeys"
class="delete-public-key-button">
<img src="../assets/delete.svg"/>
</a>
<div class="npub">{{ publicKey.npub }}</div>
<Checkmark></Checkmark>
<Checkmark v-if="!editingPublicKeys"></Checkmark>
</li>
</ul>
</div>

<div class="link-npub-form">
<Input placeholder="Paste your npub address" v-model="npub"
:disabled="formDisabled"></Input>
<Button text="Add" @click="addPublicKey"
<Button text="Add" @buttonClick="addPublicKey"
:disabled="formDisabled"></Button>
</div>
</div>
Expand All @@ -60,6 +82,7 @@ import Input from "@/components/Input.vue";
import Button from "@/components/Button.vue";
import Checkmark from "@/components/Checkmark.vue";
import {Mutation} from "@/store";
import {PublicKey} from "@/dto/PublicKey";
@Options({
Expand All @@ -79,6 +102,8 @@ export default class HomeView extends Vue {
publicKeys: PublicKeys | null = null;
npub = "";
editingPublicKeys = false;
publicKeysToRemove: PublicKey[] = [];
get loadingUser(): boolean {
return this.store.state.user === undefined;
Expand All @@ -93,25 +118,62 @@ export default class HomeView extends Vue {
}
@Watch('user')
watchUser(oldUser: CurrentUser, newUser: CurrentUser): void {
watchUser(newUser: CurrentUser): void {
if (newUser) {
this.reloadPublicKeys();
} else {
this.publicKeys = {publicKeys: []};
}
}
startEditingPublicKeys(): void {
this.publicKeysToRemove = [];
this.editingPublicKeys = true;
}
endEditingPublicKeys(): void {
for (const publicKey of this.publicKeysToRemove) {
this.apiService.deletePublicKey(publicKey)
.catch(() =>
this.store.commit(Mutation.PushNotificationError, "Error removing a public key.")
);
}
this.publicKeysToRemove = [];
this.editingPublicKeys = false;
this.reloadPublicKeys();
}
scheduleDelete(publicKey: PublicKey): void {
const index = this.publicKeys?.publicKeys?.indexOf(publicKey);
if (index !== undefined && index >= 0) {
this.publicKeys?.publicKeys?.splice(index, 1);
// force vue update
this.publicKeys = {
publicKeys: [...this.publicKeys?.publicKeys || []],
}
}
this.publicKeysToRemove.push(publicKey);
}
addPublicKey(): void {
this.apiService.addPublicKey(new AddPublicKeyRequest(this.npub))
.then(() => {
this.npub = "";
this.cancelEditingPublicKeysWithoutReloading();
this.reloadPublicKeys();
})
.catch(() => {
this.store.commit(Mutation.PushNotificationError, "Error adding the public key.");
});
}
private cancelEditingPublicKeysWithoutReloading(): void {
this.publicKeysToRemove = [];
this.editingPublicKeys = false;
this.reloadPublicKeys();
}
private reloadPublicKeys(): void {
this.publicKeys = null;
this.apiService.publicKeys()
Expand All @@ -132,7 +194,33 @@ export default class HomeView extends Vue {
.step {
font-size: 28px;
margin-top: 2em;
font-weight: 700;
display: flex;
.text {
font-weight: 700;
}
.actions {
padding: 0;
margin: 0 0 0 1em;
display: flex;
color: #F08508;
list-style-type: none;
li {
padding: 0;
margin: 0;
a {
text-decoration: none;
&:hover {
cursor: pointer;
color: #fff;
}
}
}
}
}
.link-npub-form, .public-keys-wrapper {
Expand Down Expand Up @@ -178,6 +266,16 @@ button {
text-overflow: ellipsis;
}
}
.delete-public-key-button {
display: block;
cursor: pointer;
margin-right: .5em;
img {
display: block;
}
}
}
.link-npub-form {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/dghubble/oauth1 v0.7.2
github.com/g8rswimmer/go-twitter/v2 v2.1.5
github.com/google/wire v0.5.0
github.com/gorilla/mux v1.8.0
github.com/gorilla/websocket v1.5.0
github.com/hashicorp/go-multierror v1.1.1
github.com/klaidas/go-oauth1 v0.0.0-20190306224042-169193bf805e
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/wire v0.5.0 h1:I7ELFeVBr3yfPIcc8+MWvrjk+3VjbcSzoXm3JVa+jD8=
github.com/google/wire v0.5.0/go.mod h1:ngWDr9Qvq3yZA10YrxfyGELY/AFWGVpy9c1LTRi1EoU=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
Expand Down
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 @@ -34,6 +34,21 @@ func (m *PublicKeyRepository) Save(linkedPublicKey *domain.LinkedPublicKey) erro
return nil
}

func (m *PublicKeyRepository) Delete(accountID accounts.AccountID, publicKey domain.PublicKey) error {
_, err := m.tx.Exec(`
DELETE FROM public_keys
WHERE account_id = $1 AND public_key = $2
`,
accountID.String(),
publicKey.Hex(),
)
if err != nil {
return errors.Wrap(err, "error executing the delete query")
}

return nil
}

func (m *PublicKeyRepository) List() ([]*domain.LinkedPublicKey, error) {
rows, err := m.tx.Query(`
SELECT account_id, public_key, created_at
Expand Down
Loading

0 comments on commit 49b22d1

Please sign in to comment.