-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
66 lines (52 loc) · 2.25 KB
/
index.ts
File metadata and controls
66 lines (52 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import { Handler } from './handler-type'
import * as website from './website'
import { fromBody } from './fromBody'
import { parseUrl } from '../parse-url'
import { extractFiles } from './extractFiles'
import { getCurrentUser } from './getCurrentUser'
import { getAllMentions } from './leads'
import { getSpies } from './spy'
import * as mailchimp from '../external-apis/mailchimp'
import * as twitter from '../external-apis/twitter'
import { saveFeedback, extractImageData } from '../models/feedback'
module Handlers {
export const leads = getAllMentions
export const spy = getSpies
export const getWebsite = website.getWebsite
export const getMe: Handler<{ photoUrl: null | string }> = async ctx => {
const me = getCurrentUser(ctx)
return { status: 200, body: { photoUrl: me.photo || null } }
}
export const postFeedback: Handler<{ url: string }> = async ctx => {
const status = fromBody(ctx, 'status', 'string')
const matchingUrl = ctx.request.body.domain || ctx.request.body.host
if (!matchingUrl || typeof matchingUrl !== 'string') {
throw { status: 400, message: `Body must include domain, a string` }
}
// Normalize the incoming url
const parsedUrl = parseUrl(matchingUrl)
// Support images under the field name 'images' or 'screenshots'
const images = extractFiles(ctx, 'images')
const user = getCurrentUser(ctx)
const imagesData = await extractImageData(images)
const { url } = await twitter.tweetStatus({
status,
feedback_images: imagesData.map(imageData => imageData.file),
access_token: user.token,
access_token_secret: user.tokenSecret,
})
// tslint:disable-next-line: no-expression-statement
await saveFeedback({ user, status, parsedUrl, imagesData, tweetUrl: url })
return { status: 201, body: { url } }
}
export const logout: Handler<{ loggedOut: true }> = async ctx => {
ctx.session = null // tslint:disable-line: no-expression-statement
return { status: 200, body: { loggedOut: true } }
}
export const subscribe: Handler<undefined> = async ctx => {
const email: string = fromBody(ctx, 'email', 'string')
await mailchimp.subscribe(email) // tslint:disable-line: no-expression-statement
return { status: 201 }
}
}
export = Handlers