Skip to content

Commit

Permalink
feat: add request media
Browse files Browse the repository at this point in the history
Signed-off-by: Ariel Gentile <[email protected]>
  • Loading branch information
genaris committed Jun 22, 2023
1 parent ae1d242 commit d232731
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 5 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Aries JavaScript Media Sharing plug-in
# Aries JavaScript Media Sharing extension module

This module is used to provide an Aries Agent built with Aries Framework JavaScript means to manage [Media Sharing protocol](https://github.com/2060-io/aries-rfcs/tree/feature/media-sharing/features/xxxx-media-sharing).
This module is used to provide an Aries Agent built with Aries Framework JavaScript means to manage [Media Sharing protocol](https://github.com/genaris/didcomm.org/tree/feat/media-sharing/site/content/protocols/media-sharing/1.0).

It's conceived as a plug-in for Aries Framework JavaScript which can be injected to an existing agent instance:
It's conceived as an extension module for Aries Framework JavaScript which can be injected to an existing agent instance:

```ts
import { MediaSharingModule } from 'aries-javascript-media-sharing'
Expand Down
28 changes: 28 additions & 0 deletions src/MediaSharingApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ export interface MediaSharingShareOptions {
items?: SharedMediaItemOptions[]
}

export interface MediaSharingRequestOptions {
connectionId: string
parentThreadId?: string
description?: string
itemIds: string[]
}

@injectable()
export class MediaSharingApi {
private messageSender: MessageSender
Expand Down Expand Up @@ -88,6 +95,27 @@ export class MediaSharingApi {
return record
}

/**
* Receiver role: request media
*/
public async request(options: MediaSharingRequestOptions) {
const connection = await this.connectionService.getById(this.agentContext, options.connectionId)

const { message: payload } = await this.mediaSharingService.createMediaRequest(this.agentContext, {
connectionId: options.connectionId,
itemIds: options.itemIds,
description: options.description,
parentThreadId: options.parentThreadId,
})

await this.messageSender.sendMessage(
new OutboundMessageContext(payload, {
agentContext: this.agentContext,
connection,
})
)
}

public async setMetadata(recordId: string, key: string, value: unknown) {
const record = await this.mediaSharingService.getById(this.agentContext, recordId)
record.metadata.set(key, value)
Expand Down
84 changes: 84 additions & 0 deletions src/messages/RequestMediaMessage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { AgentMessage, IsValidMessageType, parseMessageType } from '@aries-framework/core'
import { DateParser } from '@aries-framework/core/build/utils/transformers'
import { Expose, Transform } from 'class-transformer'
import { IsDate, IsOptional, IsString } from 'class-validator'
import { CipheringInfo } from '../repository'

interface SharedMediaItemDescriptorOptions {
id: string
attachmentId: string
description?: string
ciphering?: CipheringInfo
metadata?: Record<string, unknown>
}

class SharedMediaItemDescriptor {
@Expose({ name: '@id' })
public id!: string

@Expose({ name: 'attachment_id' })
public attachmentId!: string

public ciphering?: CipheringInfo

public metadata?: Record<string, unknown>

public constructor(options: SharedMediaItemDescriptorOptions) {
if (options) {
this.id = options.id
this.attachmentId = options.attachmentId
this.ciphering = options.ciphering
this.metadata = options.metadata
}
}
}

export interface RequestMediaMessageOptions {
id?: string
threadId?: string
parentThreadId?: string
sentTime?: Date
description?: string
itemIds: string[]
}

export class RequestMediaMessage extends AgentMessage {
public constructor(options?: RequestMediaMessageOptions) {
super()

if (options) {
this.id = options.id ?? this.generateId()

if (options.threadId) {
this.setThread({ threadId: options.threadId })
}

if (options.parentThreadId) {
this.setThread({
...this.thread,
parentThreadId: options.parentThreadId,
})
}

this.sentTime = options.sentTime || new Date()
this.description = options.description

this.itemIds = options.itemIds
}
}

@IsOptional()
@IsString()
public description?: string

@Expose({ name: 'sent_time' })
@Transform(({ value }) => DateParser(value))
@IsDate()
public sentTime!: Date

public itemIds!: string[]

@IsValidMessageType(RequestMediaMessage.type)
public readonly type = RequestMediaMessage.type.messageTypeUri
public static readonly type = parseMessageType('https://didcomm.org/media-sharing/1.0/request-media')
}
1 change: 1 addition & 0 deletions src/messages/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './RequestMediaMessage'
export * from './ShareMediaMessage'
26 changes: 24 additions & 2 deletions src/services/MediaSharingService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ import { Lifecycle, scoped } from 'tsyringe'

import { MediaSharingEventTypes, MediaSharingStateChangedEvent } from '../MediaSharingEvents'
import { MediaSharingRepository, MediaSharingRecord, SharedMediaItem } from '../repository'
import { ShareMediaMessage } from '../messages'
import { ShareMediaMessage, RequestMediaMessage } from '../messages'
import { ShareMediaHandler } from '../handlers'
import { MediaSharingRole, MediaSharingState } from '../model'
import { CreateMediaSharingRecordOptions, ShareMediaSharingRecordOptions } from './MediaSharingServiceOptions'
import {
CreateMediaSharingRecordOptions,
RequestMediaSharingRecordOptions,
ShareMediaSharingRecordOptions,
} from './MediaSharingServiceOptions'

@scoped(Lifecycle.ContainerScoped)
export class MediaSharingService {
Expand Down Expand Up @@ -98,6 +102,24 @@ export class MediaSharingService {
return { record, message }
}

/**
* Creates a media request
* @param options
* @returns
*/
public async createMediaRequest(agentContext: AgentContext, options: RequestMediaSharingRecordOptions) {
const conenctionId = options.connectionId

// Create message
const message = new RequestMediaMessage({
parentThreadId: options.parentThreadId,
description: options.description,
itemIds: options.itemIds,
})

return { message }
}

public async processShareMedia(messageContext: MessageHandlerInboundMessage<ShareMediaHandler>) {
const { message } = messageContext

Expand Down
7 changes: 7 additions & 0 deletions src/services/MediaSharingServiceOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,10 @@ export interface ShareMediaSharingRecordOptions {
description?: string
items?: SharedMediaItem[]
}

export interface RequestMediaSharingRecordOptions {
connectionId: string
parentThreadId?: string
description?: string
itemIds: string[]
}

0 comments on commit d232731

Please sign in to comment.