Skip to content

Commit

Permalink
feat: move items to items field and add metadata to them
Browse files Browse the repository at this point in the history
Signed-off-by: Ariel Gentile <[email protected]>
  • Loading branch information
genaris committed Jan 24, 2023
1 parent 4820c8a commit 61bc9e8
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 52 deletions.
10 changes: 5 additions & 5 deletions src/MediaSharingApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
OutboundMessageContext,
} from '@aries-framework/core'
import { ShareMediaHandler } from './handlers'
import { MediaSharingRecord, SharedMediaItem } from './repository'
import { MediaSharingRecord, SharedMediaItem, SharedMediaItemOptions } from './repository'
import { MediaSharingService } from './services'

export interface MediaSharingCreateOptions {
Expand All @@ -22,7 +22,7 @@ export interface MediaSharingShareOptions {
recordId: string
parentThreadId?: string
description?: string
items?: SharedMediaItem[]
items?: SharedMediaItemOptions[]
}

@injectable()
Expand All @@ -35,12 +35,12 @@ export class MediaSharingApi {
public constructor(
dispatcher: Dispatcher,
messageSender: MessageSender,
authCodeService: MediaSharingService,
mediaSharingService: MediaSharingService,
connectionService: ConnectionService,
agentContext: AgentContext
) {
this.messageSender = messageSender
this.mediaSharingService = authCodeService
this.mediaSharingService = mediaSharingService
this.connectionService = connectionService
this.agentContext = agentContext
this.registerHandlers(dispatcher)
Expand Down Expand Up @@ -73,7 +73,7 @@ export class MediaSharingApi {

const { message: payload } = await this.mediaSharingService.createMediaShare(this.agentContext, {
record: record,
items: options.items,
items: options.items?.map(item => new SharedMediaItem(item)),
description: options.description,
parentThreadId: options.parentThreadId,
})
Expand Down
21 changes: 6 additions & 15 deletions src/messages/ShareMediaMessage.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AgentMessage, Attachment, AttachmentData, IsValidMessageType, parseMessageType } from '@aries-framework/core'
import { uuid } from '@aries-framework/core/build/utils/uuid'
import { AgentMessage, IsValidMessageType, parseMessageType } from '@aries-framework/core'
import { Type } from 'class-transformer'
import { IsOptional, IsString } from 'class-validator'
import { SharedMediaItem } from '../repository'

Expand Down Expand Up @@ -30,26 +30,17 @@ export class ShareMediaMessage extends AgentMessage {
}

this.description = options.description
for (const item of options.items) {
const itemId = item.id ?? uuid()
this.addAppendedAttachment(
new Attachment({
id: itemId,
filename: item.filename,
mimeType: item.mimeType,
byteCount: item.byteCount,
description: item.description,
data: new AttachmentData({ links: [item.uri] }),
})
)
}
this.items = options.items
}
}

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

@Type(() => SharedMediaItem)
public items!: SharedMediaItem[]

@IsValidMessageType(ShareMediaMessage.type)
public readonly type = ShareMediaMessage.type.messageTypeUri
public static readonly type = parseMessageType('https://2060.io/didcomm/media-sharing/0.1/share-media')
Expand Down
41 changes: 32 additions & 9 deletions src/repository/MediaSharingRecord.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,37 @@
import { v4 as uuid } from 'uuid'
import { AriesFrameworkError, BaseRecord } from '@aries-framework/core'
import { AriesFrameworkError, Attachment, AttachmentData, BaseRecord } from '@aries-framework/core'
import { MediaSharingRole, MediaSharingState } from '../model'
import { AttachmentOptions } from '@aries-framework/core/build/decorators/attachment/Attachment'
import { Exclude } from 'class-transformer'

export interface SharedMediaItem {
id?: string
mimeType?: string
filename?: string
byteCount?: number
description?: string
export interface CipheringInfo {
algorithm: string
parameters: Record<string, unknown>
}

export interface SharedMediaItemOptions extends Omit<AttachmentOptions, 'data'> {
uri: string
ciphering?: CipheringInfo
metadata?: Record<string, unknown>
}

export class SharedMediaItem extends Attachment {

@Exclude()
public get uri() {
return this.data.links ? this.data.links[0] : undefined
}

public ciphering?: CipheringInfo
public metadata?: Record<string, unknown>

public constructor(options: SharedMediaItemOptions) {
super({ ...options, data: new AttachmentData({ links: [ options?.uri ] })})
if (options) {
this.ciphering = options.ciphering
this.metadata = options.metadata
}
}
}

export interface MediaSharingStorageProps {
Expand Down Expand Up @@ -47,7 +70,7 @@ export class MediaSharingRecord extends BaseRecord {
this.threadId = props.threadId
this.parentThreadId = props.parentThreadId
this.description = props.description
this.items = props.items?.map((item) => ({ ...item, id: item.id ?? uuid() })) ?? []
this.items = props.items ?? []
if (props.metadata) {
Object.keys(props.metadata).forEach((key) => {
this.metadata.set(key, props.metadata?.[key])
Expand Down Expand Up @@ -82,7 +105,7 @@ export class MediaSharingRecord extends BaseRecord {

if (!expectedStates.includes(this.state)) {
throw new Error(
`Auth code record is in invalid state ${this.state}. Valid states are: ${expectedStates.join(', ')}.`
`Media sharing record is in invalid state ${this.state}. Valid states are: ${expectedStates.join(', ')}.`
)
}
}
Expand Down
24 changes: 4 additions & 20 deletions src/services/MediaSharingService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { AgentContext, AriesFrameworkError, EventEmitter, MessageHandlerInboundM
import { Lifecycle, scoped } from 'tsyringe'

import { MediaSharingEventTypes, MediaSharingStateChangedEvent } from '../MediaSharingEvents'
import { MediaSharingRepository, MediaSharingRecord, SharedMediaItem } from '../repository'
import { MediaSharingRepository, MediaSharingRecord } from '../repository'
import { ShareMediaMessage } from '../messages'
import { ShareMediaHandler } from '../handlers'
import { MediaSharingRole, MediaSharingState } from '../model'
Expand Down Expand Up @@ -103,29 +103,13 @@ export class MediaSharingService {

const record = await this.findByThreadId(messageContext.agentContext, message.threadId)

// Auth record record already exists
// Media sharing record already exists
if (record) {
throw new AriesFrameworkError(`There is already a MediaSharingRecord with thread Id ${message.threadId}`)
} else {
const connection = messageContext.assertReadyConnection()

const items: SharedMediaItem[] = []
if (message.appendedAttachments) {
for (const attachment of message.appendedAttachments) {
if (attachment.data.links && attachment.data.links[0]) {
items.push({
uri: attachment.data.links[0],
id: attachment.id,
mimeType: attachment.mimeType,
filename: attachment.filename,
byteCount: attachment.byteCount,
description: attachment.description,
})
}
}
}

if (items.length === 0) {
if (message.items.length === 0) {
throw new AriesFrameworkError('There are no valid items in MediaSharingRecord')
}

Expand All @@ -136,7 +120,7 @@ export class MediaSharingService {
parentThreadId: messageContext.message.thread?.parentThreadId,
state: MediaSharingState.MediaShared,
role: MediaSharingRole.Receiver,
items,
items: message.items,
description: message.description,
})

Expand Down
11 changes: 8 additions & 3 deletions test/mediasharing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,19 @@ describe('media test', () => {

await aliceAgent.modules.media.share({
recordId: aliceRecord.id,
items: [{ uri: 'http://blabla' }],
items: [{ mimeType: 'image/png', uri: 'http://blabla' }],
})

recordsAddedByType(bobAgent, MediaSharingRecord)
//.pipe(filter((e) => e.state === MediaSharingState.MediaShared))
.subscribe(subjectBob)

const bobThread = await firstValueFrom(subjectBob)
const aliceThread = await firstValueFrom(subjectAlice)
const bobRecord = await firstValueFrom(subjectBob)
await firstValueFrom(subjectAlice)

expect(bobRecord.items?.length).toBe(1)
expect(bobRecord.items![0].mimeType).toBe('image/png')
expect(bobRecord.items![0].uri).toBe('http://blabla')

})
})

0 comments on commit 61bc9e8

Please sign in to comment.