From 10556bd5421225414f31a44f9b2576eea829b997 Mon Sep 17 00:00:00 2001 From: SondreB Date: Sat, 24 Aug 2024 18:15:01 +0200 Subject: [PATCH] Add management of blocks and connections --- app/src/app/connection.service.ts | 32 +++++++++++-- .../notifications.component.html | 14 ++---- .../notifications/notifications.component.ts | 46 ++++++++++++++++++- 3 files changed, 76 insertions(+), 16 deletions(-) diff --git a/app/src/app/connection.service.ts b/app/src/app/connection.service.ts index 15338182..9b73cb44 100644 --- a/app/src/app/connection.service.ts +++ b/app/src/app/connection.service.ts @@ -25,11 +25,37 @@ export class ConnectionService { constructor() {} + /** Creates a connection entry that opens up a trust line between identities. */ async create(data: any) { // Create a new connection that is sent to external DWN. // We save a local copy to see our outgoing connection requests. const eventData = data; + const { record, status } = await this.identity.web5.dwn.records.create({ + data: eventData, + message: { + recipient: eventData.did, + protocol: connectionDefinition.protocol, + protocolPath: 'connection', + schema: connectionDefinition.types.connection.schema, + dataFormat: connectionDefinition.types.connection.dataFormats[0], + }, + }); + + console.log('Connection created:', status, record); + + return { + record, + data: eventData, + id: record!.id, + } as ConnectionEntry; + } + + async request(data: any) { + // Create a new connection that is sent to external DWN. + // We save a local copy to see our outgoing connection requests. + const eventData = data; + const { record, status } = await this.identity.web5.dwn.records.create({ data: eventData, message: { @@ -98,13 +124,13 @@ export class ConnectionService { return list; } - async loadConnections(did: string) { + async loadConnections(did?: string) { const list: ConnectionEntry[] = []; const filter = { author: did ? did : undefined, protocol: connectionDefinition.protocol, - protocolPath: 'connections', + protocolPath: 'connection', schema: connectionDefinition.types.connection.schema, }; @@ -131,7 +157,7 @@ export class ConnectionService { message: { filter: { protocol: connectionDefinition.protocol, - protocolPath: 'blocks', + protocolPath: 'block', schema: connectionDefinition.types.block.schema, }, dateSort: DwnDateSort.CreatedAscending, diff --git a/app/src/app/notifications/notifications.component.html b/app/src/app/notifications/notifications.component.html index f214c9ed..e2208167 100644 --- a/app/src/app/notifications/notifications.component.html +++ b/app/src/app/notifications/notifications.component.html @@ -58,22 +58,14 @@

Notifications

- - - - - + } @else if(notification.data.app === 'Connect') {
{{ notification.data.description }}
- - - - - + } - + diff --git a/app/src/app/notifications/notifications.component.ts b/app/src/app/notifications/notifications.component.ts index c8bea074..a919aff3 100644 --- a/app/src/app/notifications/notifications.component.ts +++ b/app/src/app/notifications/notifications.component.ts @@ -10,7 +10,7 @@ import { MatMenuModule } from '@angular/material/menu'; import { RouterModule } from '@angular/router'; import { ProfileCardComponent } from '../shared/components/profile-card/profile-card.component'; import { ProfileHeaderComponent } from '../shared/components/profile-header/profile-header.component'; -import { ConnectionService } from '../connection.service'; +import { ConnectionEntry, ConnectionService } from '../connection.service'; import { IdentityService } from '../identity.service'; @Component({ @@ -44,16 +44,48 @@ export class NotificationsComponent { notifications = signal([]); + blocks = signal([]); + + connections = signal([]); + constructor() { this.layout.resetActions(); effect(async () => { if (this.app.initialized()) { await this.loadNotifications(); + await this.loadConnections(); + await this.loadBlocks(); + + console.log('Blocks:', this.blocks()); + console.log('Connections:', this.connections()); } }); } + async accept(entry: NotificationEvent) { + console.log('Accepting connection request'); + + if (entry.data.app === 'Friends') { + // If friends request, we will reply with a Verifiable Credential in addition to the connection request. + + this.connection.create({ + did: entry.data.author, // TODO: Change to record.author + }); + + this.deleteNotification(entry); + + // Accept as friend. + } else if (entry.data.app === 'Connect') { + // If connect request, we will only reply with a connection request. + this.connection.create({ + did: entry.data.author, // TODO: Change to record.author + }); + + this.deleteNotification(entry); + } + } + async deleteNotifications() { for (const notification of this.notifications()) { await notification.record.delete(); @@ -111,7 +143,7 @@ export class NotificationsComponent { async generateNotification() { // First simulate an incoming connection request. - await this.connection.create({}); + await this.connection.request({}); const event = await this.notification.create({ author: 'did:dht:bi3bzoke6rq6fbkojpo5ebtg45eqx1owqrb4esex8t9nz14ugnao', @@ -141,6 +173,16 @@ export class NotificationsComponent { this.notifications.set(notifications); } + async loadConnections() { + const records = await this.connection.loadConnections(); + this.connections.set(records); + } + + async loadBlocks() { + const records = await this.connection.loadBlocks(); + this.blocks.set(records); + } + async enableNotifications() { console.log('Notifications enabled'); this.sendNotification('Hello World');