-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
40041bb
commit 698aca4
Showing
5 changed files
with
153 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,89 +1,95 @@ | ||
import { Core } from '../core' | ||
import { LastMessageTimestampForInstance } from '../service/service-manager' | ||
|
||
import { Message } from '../entities/message' | ||
import { In, getRepository } from 'typeorm' | ||
import { Presentation } from '../entities/presentation' | ||
import { Credential } from '../entities/credential' | ||
import { Claim } from '../entities/claim' | ||
|
||
export interface Context { | ||
core: Core | ||
} | ||
|
||
const newMessage = async ( | ||
const saveNewMessage = async ( | ||
_: any, | ||
args: { raw: string; sourceType: string; sourceId?: string }, | ||
args: { raw: string; metaDataType?: string; metaDataValue?: string }, | ||
ctx: Context, | ||
) => { | ||
const message = await ctx.core.validateMessage( | ||
new Message({ | ||
raw: args.raw, | ||
meta: { | ||
type: args.sourceType, | ||
value: args.sourceId, | ||
}, | ||
}), | ||
) | ||
|
||
const res = { | ||
...message, | ||
sender: message.from, | ||
receiver: message.to, | ||
data: JSON.stringify(message.data), | ||
id: message.id, | ||
raw: message.raw, | ||
__typename: 'Message', | ||
} | ||
return res | ||
return await ctx.core.saveNewMessage(args) | ||
} | ||
|
||
const serviceMessagesSince = async ( | ||
const findMessages = async ( | ||
_: any, | ||
args: { ts: LastMessageTimestampForInstance[] }, | ||
args: { | ||
options?:{ take?: number, skip?: number }, | ||
from?: string[], | ||
to?: string[], | ||
type?: string, | ||
threadId?: string, | ||
}, | ||
ctx: Context, | ||
) => { | ||
const res = await ctx.core.getMessagesSince(args.ts) | ||
return [] // FIXME | ||
// return res.map(msg => ({ | ||
// ...msg, | ||
// id: msg.id, | ||
// data: JSON.stringify(msg.data), | ||
// raw: msg.raw, | ||
// metaData: msg.allMeta, | ||
// })) | ||
const options = { where: {} } | ||
|
||
if (args.from) options.where['from'] = In(args.from) | ||
if (args.to) options.where['to'] = In(args.to) | ||
if (args.type) options.where['type'] = args.type | ||
if (args.threadId) options.where['threadId'] = args.threadId | ||
if (args.options?.skip) options['skip'] = args.options.skip | ||
if (args.options?.take) options['take'] = args.options.take | ||
|
||
return await Message.find(options) | ||
} | ||
|
||
|
||
export const resolvers = { | ||
Credential: { | ||
claims: async (credential: Credential, args, ctx) => { | ||
return getRepository(Claim).find({ where: { credential: credential.hash}, relations: ['issuer', 'subject'] }) | ||
} | ||
}, | ||
Presentation: { | ||
credentials: async (presentation: Presentation, args, ctx) => { | ||
return getRepository(Credential).find({ where: { presentations: presentation.hash}, relations: ['issuer', 'subject'] }) | ||
} | ||
}, | ||
Message: { | ||
presentations: async (message: Message, args, ctx) => { | ||
return getRepository(Presentation).find({ where: { messages: message.id}, relations: ['issuer', 'audience'] }) | ||
}, | ||
credentials: async (message: Message, args, ctx) => { | ||
return getRepository(Credential).find({ where: { messages: message.id}, relations: ['issuer', 'subject'] }) | ||
} | ||
}, | ||
Query: { | ||
serviceMessagesSince, | ||
findMessages, | ||
}, | ||
Mutation: { | ||
newMessage, | ||
saveNewMessage, | ||
}, | ||
} | ||
|
||
export const typeDefs = ` | ||
input LastMessageTimestampForInstance { | ||
timestamp: Int! | ||
did: String! | ||
type: String! | ||
id: String! | ||
input FindOptions { | ||
take: Int | ||
skip: Int | ||
orderField: String | ||
orderDirection: String | ||
} | ||
type ServiceMessage { | ||
id: String! | ||
threadId: String, | ||
timestamp: Int, | ||
sender: String, | ||
receiver: String, | ||
type: String, | ||
raw: String, | ||
data: String, | ||
metaData: [MessageMetaData] | ||
input FindMessagesInput { | ||
from: [String] | ||
to: [String] | ||
type: String | ||
threadId: String | ||
options: FindOptions | ||
} | ||
extend type Query { | ||
serviceMessagesSince(ts: [LastMessageTimestampForInstance]!): [ServiceMessage] | ||
findMessages(input: FindMessagesInput): [Message] | ||
} | ||
extend type Mutation { | ||
newMessage(raw: String!, sourceType: String!, sourceId: String): Message | ||
saveNewMessage(raw: String!, metaDataType: String, metaDataValue: String): Message | ||
} | ||
` |