Skip to content

Commit

Permalink
Merge pull request #40 from decentralized-identity/fix/name-change-sync
Browse files Browse the repository at this point in the history
fix: move profile updates to agent
  • Loading branch information
dbluhm authored Oct 4, 2023
2 parents 1441815 + f52ef1d commit ceefb35
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 34 deletions.
31 changes: 31 additions & 0 deletions src/lib/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ export class Agent {
this.worker = new Worker(new URL("./worker.ts", import.meta.url))
this.worker.onmessage = this.handleWorkerMessage.bind(this)
this.onAnyMessage(this.handleCoreProtocolMessage.bind(this))
this.onMessage(
"https://didcomm.org/user-profile/1.0/profile",
this.onProfileUpdate.bind(this)
)
this.onMessage(
"https://didcomm.org/user-profile/1.0/request-profile",
this.onProfileRequest.bind(this)
)
}

setupProfile(profile: Profile) {
Expand Down Expand Up @@ -218,6 +226,29 @@ export class Agent {
await this.sendMessage(contact, message as IMessage)
}

async onProfileUpdate(message: AgentMessage) {
let contact = ContactService.getContact(message.message.from)
if (!contact) {
return
}

let label = message.message.body?.profile?.displayName
if (!label) {
return
}

contact.label = label
ContactService.addContact(contact)
}

async onProfileRequest(message: AgentMessage) {
let contact = ContactService.getContact(message.message.from)
if (!contact) {
return
}
await this.sendProfile(contact)
}

public async sendFeatureDiscovery(contact: Contact) {
const message = {
type: "https://didcomm.org/discover-features/2.0/queries",
Expand Down
35 changes: 1 addition & 34 deletions src/pages/profile/messaging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,46 +22,13 @@ class ContactListComponent
oninit() {
this.contacts = ContactService.getContacts()
this.eventbus = eventbus.scoped()
this.eventbus.collect(
agent.onAnyMessage(this.onMessageReceived.bind(this)),
agent.onMessage(
"https://didcomm.org/user-profile/1.0/profile",
this.onProfileUpdate.bind(this)
),
agent.onMessage(
"https://didcomm.org/user-profile/1.0/request-profile",
this.onProfileRequest.bind(this)
)
)
this.eventbus.collect(agent.onAnyMessage(this.onMessageReceived.bind(this)))
}

onremove() {
this.eventbus.close()
}

async onProfileUpdate(message: AgentMessage) {
let contact = ContactService.getContact(message.message.from)
if (!contact) {
return
}

let label = message.message.body?.profile?.displayName
if (!label) {
return
}

contact.label = label
ContactService.addContact(contact)
}

async onProfileRequest(message: AgentMessage) {
let contact = ContactService.getContact(message.message.from)
if (!contact) {
return
}
await agent.sendProfile(contact)
}

async onMessageReceived(message: AgentMessage) {
if (message.message.to[0] != agent.profile.did) return
if (!ContactService.getContact(message.message.from)) {
Expand Down

0 comments on commit ceefb35

Please sign in to comment.