Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/tradle/aws
Browse files Browse the repository at this point in the history
  • Loading branch information
pgmemk committed Jun 2, 2018
2 parents 8f34491 + 87e7f97 commit 8a8f577
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 45 deletions.
11 changes: 10 additions & 1 deletion src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,15 @@ const bumpSuffix = (id) => {

const getTopicName = (str: string) => parseTopic(str).original

const registry = new Set<String>()

export class EventTopic {
constructor(private name: string) {}
constructor(private name: string) {
registry.add(this.toString())
// registry.add(this.sync.toString())
// registry.add(this.async.toString())
// registry.add(this.async.batch.toString())
}
get sync():EventTopic { return new EventTopic(toSyncEvent(this.name)) }
get async():EventTopic { return new EventTopic(toAsyncEvent(this.name)) }
get batch():EventTopic { return new EventTopic(toBatchEvent(this.name)) }
Expand Down Expand Up @@ -251,6 +258,8 @@ export const topics = {
}
}

export const TOPICS = Array.from(registry)

const BATCH_SUFFIX = ':batch'
const BATCH_REGEX = new RegExp(`${BATCH_SUFFIX}$`)
const ASYNC_PREFIX = 'async:'
Expand Down
7 changes: 1 addition & 6 deletions src/in-house-bot/applications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,6 @@ import {

import { Resource } from '../resource'

interface ICreateCheckOpts {
props: any
req?: IPBReq
}

interface IPBJudgeAppOpts {
req?: IPBReq
application: string|IPBApp|ResourceStub
Expand Down Expand Up @@ -65,7 +60,7 @@ export class Applications {
this.logger = bot.logger.sub('applications')
}

public createCheck = async ({ props, req }: ICreateCheckOpts) => {
public createCheck = async (props) => {
const { bot, productsAPI } = this
const { models } = bot
const type = props[TYPE]
Expand Down
34 changes: 19 additions & 15 deletions src/in-house-bot/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ export default function createProductsBot({
// queueSends: bot.env.TESTING ? true : queueSends
})

// if (event === LambdaEvents.RESOURCE_ASYNC) {
// productsAPI.removeDefaultHandlers()
// }

productsAPI.removeDefaultHandler('shouldSealReceived')
productsAPI.plugins.use({
shouldSealReceived: ({ object }) => {
Expand All @@ -148,21 +152,6 @@ export default function createProductsBot({
}
})

if (bot.isTesting && (event === LambdaEvents.RESOURCE_ASYNC || event === LambdaEvents.MESSAGE)) {
productsAPI.plugins.use({
['onmessage:tradle.IdentityPublishRequest']: async (req: IPBReq) => {
const { user, payload } = req
const { identity } = payload
if (!identity._seal) {
await bot.seals.create({
counterparty: user.id,
object: identity
})
}
}
})
}

const send = (opts) => productsAPI.send(opts)
const employeeManager = createEmployeeManager({
logger: logger.sub('employees'),
Expand Down Expand Up @@ -217,6 +206,10 @@ export default function createProductsBot({
bot.onmessage(productsAPI.onmessage)
}

// if (event === LambdaEvents.RESOURCE_ASYNC) {
// productsAPI.removeDefaultHandlers()
// }

const myIdentityPromise = bot.getMyIdentity()
const components: IBotComponents = {
bot,
Expand Down Expand Up @@ -284,6 +277,17 @@ export default function createProductsBot({
// if (req.application && req.application.draft) {
// req.skipChecks = true
// }

const { user, payload } = req
if (payload[TYPE] === 'tradle.IdentityPublishRequeest') {
const { identity } = payload
if (!identity._seal) {
await bot.seals.create({
counterparty: user.id,
object: identity
})
}
}
}
})

Expand Down
17 changes: 7 additions & 10 deletions src/in-house-bot/plugins/email-based-verification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,16 +175,13 @@ export const createPlugin:CreatePlugin<EmailBasedVerifier> = ({

logger.debug(`created ${EMAIL_CHECK}`, { emailAddress: value })
const createCheck = applications.createCheck({
req,
props: {
[TYPE]: EMAIL_CHECK,
application,
emailAddress: value,
// this org
provider: conf.org.name,
user: user.identity,
dateExpires: Date.now() + TTL.ms
}
[TYPE]: EMAIL_CHECK,
application,
emailAddress: value,
// this org
provider: conf.org.name,
user: user.identity,
dateExpires: Date.now() + TTL.ms
})

const alertUser = await bot.sendSimpleMessage({
Expand Down
28 changes: 15 additions & 13 deletions src/in-house-bot/webhooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { TYPE } from '@tradle/constants'
import { Bot, Logger, IStreamEvent, IBackoffOptions, TopicOrString } from './types'
import Errors from '../errors'
import { runWithTimeout, runWithBackoffWhile, batchProcess, allSettled } from '../utils'
import { topics as EventTopics, EventTopic } from '../events'
import { topics as EventTopics, EventTopic, TOPICS } from '../events'

export interface IWebhookSubscription {
id: string
Expand Down Expand Up @@ -70,10 +70,8 @@ const DEFAULT_BACKOFF_OPTS = {

// const flatten = (all, some) => all.concat(some)

const TOPIC_LIST = Object.keys(EventTopics)
.map(parent => _.values(EventTopics[parent]) as EventTopic[])
.reduce((all, some) => all.concat(some), [])
.map((topic: EventTopic) => new RegExp(`^a?sync:${topic.sync}$`, 'i'))
const TOPIC_LIST = TOPICS
.map((topic: string) => new RegExp(`^(?:async:)?${topic}$`, 'i'))
// derived
.concat([
/^msg:i:.*/,
Expand Down Expand Up @@ -145,6 +143,7 @@ export class Webhooks {
}

public getSubscriptionsForEvent = (eventTopic: TopicOrString):IWebhookSubscription[] => {
eventTopic = eventTopic.toString()
return this.conf.subscriptions.filter(({ topic }) => topic === eventTopic)
}

Expand All @@ -169,15 +168,17 @@ export class Webhooks {
public static validateSubscriptions = (subs: IWebhookSubscription[]) => {
const keys = new Set()
for (const sub of subs) {
const { topic } = sub
if (!isValidTopic(topic)) {
throw new Errors.InvalidInput(`no such topic: "${topic}"`)
}

const key = getWebhookKey(sub)
if (keys.has(key)) {
throw new Errors.InvalidInput('webhook subscriptions must have unique ids, or be unique by topic+endpoint')
}

const { topic } = sub
if (!isValidTopic(topic)) {
throw new Errors.InvalidInput(`no such topic: "${topic}"`)
}
keys.add(key)
}
}

Expand Down Expand Up @@ -288,11 +289,12 @@ export class Webhooks {

let req: SuperAgentRequest
try {
req = request
.post(endpoint)
.set('x-webhook-auth', hash)
.send(data)
req = request.post(endpoint)
if (hash) {
req.set('x-webhook-auth', hash)
}

req = req.send(data)
await runWithTimeout(() => req, { millis: TIMEOUT_MILLIS })
} catch (err) {
this.logger.debug('failed to invoke webhook', {
Expand Down

0 comments on commit 8a8f577

Please sign in to comment.