Skip to content

Commit

Permalink
fix: Removing MessageMetaData entity
Browse files Browse the repository at this point in the history
  • Loading branch information
simonas-notcat committed Mar 13, 2020
1 parent 7b5959e commit 353449c
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 67 deletions.
4 changes: 2 additions & 2 deletions packages/daf-core/src/__tests__/entities.test.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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')
Expand Down
20 changes: 0 additions & 20 deletions packages/daf-core/src/entities/message-meta-data.ts

This file was deleted.

25 changes: 9 additions & 16 deletions packages/daf-core/src/entities/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 } }) {
Expand Down Expand Up @@ -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,
Expand All @@ -107,20 +105,15 @@ 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 {
this.metaData = [meta]
}
}

getLastMetaData(): MessageMetaData | null {
getLastMetaData(): MetaData | null {
if (this.metaData?.length > 0) {
return this.metaData[this.metaData.length - 1]
} else {
Expand Down
1 change: 0 additions & 1 deletion packages/daf-core/src/graphql/graphql-base-type-defs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ export const baseTypeDefs = `
}
type MessageMetaData {
id: ID!
type: String!
value: String
}
Expand Down
37 changes: 11 additions & 26 deletions packages/daf-data-store/src/data-store.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand Down Expand Up @@ -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' },
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion packages/daf-data-store/src/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 []
Expand Down
2 changes: 1 addition & 1 deletion packages/daf-did-jwt/src/message-validator.ts
Original file line number Diff line number Diff line change
@@ -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')
Expand Down

0 comments on commit 353449c

Please sign in to comment.