From 353449c5a2f9a9c0919b4e38a44012fdf98ec8a9 Mon Sep 17 00:00:00 2001 From: Simonas Karuzas Date: Fri, 13 Mar 2020 17:19:48 +0200 Subject: [PATCH] fix: Removing MessageMetaData entity --- .../daf-core/src/__tests__/entities.test.ts | 4 +- .../src/entities/message-meta-data.ts | 20 ---------- packages/daf-core/src/entities/message.ts | 25 +++++-------- .../src/graphql/graphql-base-type-defs.ts | 1 - packages/daf-data-store/src/data-store.ts | 37 ++++++------------- packages/daf-data-store/src/graphql.ts | 2 +- packages/daf-did-jwt/src/message-validator.ts | 2 +- 7 files changed, 24 insertions(+), 67 deletions(-) delete mode 100644 packages/daf-core/src/entities/message-meta-data.ts diff --git a/packages/daf-core/src/__tests__/entities.test.ts b/packages/daf-core/src/__tests__/entities.test.ts index a7d4bda24..f6b8f5713 100644 --- a/packages/daf-core/src/__tests__/entities.test.ts +++ b/packages/daf-core/src/__tests__/entities.test.ts @@ -1,5 +1,5 @@ import { createConnection, Connection, In } from 'typeorm' -import { Identity, Key, Message, Credential, Presentation, Claim, MessageMetaData } from '../index' +import { Identity, Key, Message, Credential, Presentation, Claim } from '../index' import { Entities } from '../index' import { blake2bHex } from 'blakejs' import fs from 'fs' @@ -154,7 +154,7 @@ describe('daf-core', () => { await message.save() - const fromDb = await Message.findOne(customId, { relations: ['metaData'] }) + const fromDb = await Message.findOne(customId) expect(fromDb.id).toEqual(customId) expect(fromDb.type).toEqual('custom') diff --git a/packages/daf-core/src/entities/message-meta-data.ts b/packages/daf-core/src/entities/message-meta-data.ts deleted file mode 100644 index 1a2f46adf..000000000 --- a/packages/daf-core/src/entities/message-meta-data.ts +++ /dev/null @@ -1,20 +0,0 @@ -import { Entity, Column, BaseEntity, ManyToOne, PrimaryGeneratedColumn } from 'typeorm' -import { Message } from './message' - -@Entity() -export class MessageMetaData extends BaseEntity { - @PrimaryGeneratedColumn() - id: string - - @Column() - type: string - - @Column({ nullable: true }) - value?: string - - @ManyToOne( - type => Message, - message => message.metaData, - ) - message: Message -} diff --git a/packages/daf-core/src/entities/message.ts b/packages/daf-core/src/entities/message.ts index 0d73c5ca0..ea8e058dc 100644 --- a/packages/daf-core/src/entities/message.ts +++ b/packages/daf-core/src/entities/message.ts @@ -11,10 +11,14 @@ import { UpdateDateColumn, } from 'typeorm' import { Identity } from './identity' -import { MessageMetaData } from './message-meta-data' import { Presentation } from './presentation' import { Credential } from './credential' +export interface MetaData { + type: string + value?: string +} + @Entity() export class Message extends BaseEntity { constructor(data?: { raw: string; meta?: { type: string; value?: string } }) { @@ -82,14 +86,8 @@ export class Message extends BaseEntity { ) to?: Identity - @OneToMany( - type => MessageMetaData, - messageMetaData => messageMetaData.message, - { - cascade: true, - }, - ) - metaData: MessageMetaData[] + @Column('simple-json', { nullable: true }) + metaData?: MetaData[] @ManyToMany( type => Presentation, @@ -107,12 +105,7 @@ export class Message extends BaseEntity { @JoinTable() credentials: Credential[] - addMetaData(input: { type: string; value?: string }) { - const meta = new MessageMetaData() - meta.type = input.type - if (input.value) { - meta.value = input.value - } + addMetaData(meta: MetaData) { if (this.metaData) { this.metaData.push(meta) } else { @@ -120,7 +113,7 @@ export class Message extends BaseEntity { } } - getLastMetaData(): MessageMetaData | null { + getLastMetaData(): MetaData | null { if (this.metaData?.length > 0) { return this.metaData[this.metaData.length - 1] } else { diff --git a/packages/daf-core/src/graphql/graphql-base-type-defs.ts b/packages/daf-core/src/graphql/graphql-base-type-defs.ts index 94f871a62..50aed57da 100644 --- a/packages/daf-core/src/graphql/graphql-base-type-defs.ts +++ b/packages/daf-core/src/graphql/graphql-base-type-defs.ts @@ -22,7 +22,6 @@ export const baseTypeDefs = ` } type MessageMetaData { - id: ID! type: String! value: String } diff --git a/packages/daf-data-store/src/data-store.ts b/packages/daf-data-store/src/data-store.ts index a1b91b557..c13624b21 100644 --- a/packages/daf-data-store/src/data-store.ts +++ b/packages/daf-data-store/src/data-store.ts @@ -1,4 +1,4 @@ -import { Identity, Message, MessageMetaData, Credential, Presentation, Claim } from 'daf-core' +import { Identity, Message, Credential, Presentation, Claim } from 'daf-core' import { In } from 'typeorm' import Debug from 'debug' @@ -124,6 +124,11 @@ export class DataStore { where.push({ to: receiver }) } + if (sender || receiver) { + // SDR can have empty to field + where.push({ to: null }) + } + const messages = await Message.find({ where, order: { saveDate: 'DESC' }, @@ -161,25 +166,8 @@ export class DataStore { } async latestMessageTimestamps() { - const innerQb = await Message.createQueryBuilder('message') - .leftJoinAndSelect('message.metaData', 'meta') - .orderBy('message.createdAt', 'DESC') - - const res = await Message.createQueryBuilder() - .select('message_toDid as did, meta_type as type, meta_value as value, message_createdAt as createdAt') - .from('(' + innerQb.getQuery() + ')', 'inner') - .setParameters(innerQb.getParameters()) - .groupBy('message_toDid') - .addGroupBy('meta_type') - .addGroupBy('meta_value') - .getRawMany() - - return res.map(item => ({ - did: item.did, - type: item.type, - id: item.value, - timestamp: Math.round(new Date(item.createdAt).getTime() / 1000), - })) + // FIXME + return [] } async shortId(did: string) { @@ -209,12 +197,9 @@ export class DataStore { return credential.messages.map(this.messageToLegacyFormat) } - async messageMetaData(id: string) { - const messages = await Message.findOne(id, { - relations: ['metaData'], - }) - - return messages.metaData + async metaData(id: string) { + const message = await Message.findOne(id) + return message.metaData } async deleteMessage(id: string) { diff --git a/packages/daf-data-store/src/graphql.ts b/packages/daf-data-store/src/graphql.ts index 5ea5c687c..3aa8309d2 100644 --- a/packages/daf-data-store/src/graphql.ts +++ b/packages/daf-data-store/src/graphql.ts @@ -7,7 +7,7 @@ export interface Context { export const resolvers = { Message: { vc: async (message: any, {}, { dataStore }: Context) => dataStore.credentialsForMessageId(message.id), - metaData: async (message: any, {}, { dataStore }: Context) => dataStore.messageMetaData(message.id), + metaData: async (message: any, {}, { dataStore }: Context) => dataStore.metaData(message.id), thread: async (message: any, {}, { dataStore }: Context) => { if (!message.threadId) { return [] diff --git a/packages/daf-did-jwt/src/message-validator.ts b/packages/daf-did-jwt/src/message-validator.ts index 6cf50328b..c9e7f24bc 100644 --- a/packages/daf-did-jwt/src/message-validator.ts +++ b/packages/daf-did-jwt/src/message-validator.ts @@ -1,4 +1,4 @@ -import { Core, AbstractMessageValidator, Message, MessageMetaData } from 'daf-core' +import { Core, AbstractMessageValidator, Message } from 'daf-core' import { verifyJWT, decodeJWT } from 'did-jwt' import Debug from 'debug' const debug = Debug('daf:did-jwt:message-validator')