Skip to content

Commit

Permalink
Add management of blocks and connections
Browse files Browse the repository at this point in the history
  • Loading branch information
sondreb committed Aug 24, 2024
1 parent d483d5d commit 10556bd
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 16 deletions.
32 changes: 29 additions & 3 deletions app/src/app/connection.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -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,
};

Expand All @@ -131,7 +157,7 @@ export class ConnectionService {
message: {
filter: {
protocol: connectionDefinition.protocol,
protocolPath: 'blocks',
protocolPath: 'block',
schema: connectionDefinition.types.block.schema,
},
dateSort: DwnDateSort.CreatedAscending,
Expand Down
14 changes: 3 additions & 11 deletions app/src/app/notifications/notifications.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -58,22 +58,14 @@ <h1>Notifications</h1>
<!--
TODO: Use the record.author, not from data. From data is for temporary prototyping.
<app-profile-header [did]="notification.record.author"> -->
<app-profile-header [did]="notification.data.author">
<button mat-flat-button>ACCEPT</button>
<button mat-button>DELETE</button>
<button mat-button>BLOCK</button>
</app-profile-header>
<app-profile-header [did]="notification.data.author"></app-profile-header>
} @else if(notification.data.app === 'Connect') {
<div class="notification-description">{{ notification.data.description }}</div>
<app-profile-header [did]="notification.record.author">
<button mat-flat-button>ACCEPT</button>
<button mat-button>DELETE</button>
<button mat-button>BLOCK</button>
</app-profile-header>
<app-profile-header [did]="notification.record.author"></app-profile-header>
}
</mat-card-content>
<mat-card-actions>
<button mat-flat-button>ACCEPT</button>
<button mat-flat-button (click)="accept(notification)">ACCEPT</button>
<button mat-button (click)="deleteNotification(notification)">DELETE</button>
<button mat-button (click)="block(notification)">BLOCK</button>
</mat-card-actions>
Expand Down
46 changes: 44 additions & 2 deletions app/src/app/notifications/notifications.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -44,16 +44,48 @@ export class NotificationsComponent {

notifications = signal<NotificationEvent[]>([]);

blocks = signal<ConnectionEntry[]>([]);

connections = signal<ConnectionEntry[]>([]);

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();
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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');
Expand Down

0 comments on commit 10556bd

Please sign in to comment.