This repository was archived by the owner on Mar 15, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy patheditComment.js
78 lines (67 loc) · 2.55 KB
/
editComment.js
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
67
68
69
70
71
72
73
74
75
76
77
78
const ensureSignedIn = require('../../../lib/ensureSignedIn')
const logger = require('../../../lib/logger')
const slack = require('../../../lib/slack')
const uuid = require('uuid/v4')
const renderUrl = require('../../../lib/renderUrl')
const uploadExoscale = require('../../../lib/uploadExoscale')
const FOLDER = 'comments'
const { ASSETS_BASE_URL, FRONTEND_BASE_URL, S3BUCKET } = process.env
module.exports = async (_, args, {pgdb, user, req, t}) => {
ensureSignedIn(req, t)
const { commentId, content } = args
const transaction = await pgdb.transactionBegin()
try {
// ensure comment exists and belongs to user
const comment = await transaction.public.comments.findOne({id: commentId})
if (!comment) {
logger.error('comment not found', { req: req._log(), commentId })
throw new Error(t('api/comment/commentNotFound'))
}
if (comment.userId !== user.id) {
logger.error('comment does not belong to user', { req: req._log(), args })
throw new Error(t('api/comment/notYours'))
}
const feed = await transaction.public.feeds.findOne({id: comment.feedId})
// ensure comment length is within limit
if (content.length > feed.commentMaxLength) {
logger.error('content too long', { req: req._log(), args })
throw new Error(t('api/comment/tooLong'), {commentMaxLength: feed.commentMaxLength})
}
const newComment = await transaction.public.comments.updateAndGetOne({
id: comment.id
}, {
content,
updatedAt: new Date()
})
await transaction.transactionCommit()
// generate sm picture
try {
const smImagePath = `/${FOLDER}/sm/${uuid()}_sm.png`
await renderUrl(`${FRONTEND_BASE_URL}/vote?share=${newComment.id}`, 1200, 628)
.then(async (data) => {
return uploadExoscale({
stream: data,
path: smImagePath,
mimeType: 'image/png',
bucket: S3BUCKET
}).then(async () => {
return pgdb.public.comments.updateOne({id: newComment.id}, {
smImage: ASSETS_BASE_URL + smImagePath
})
})
})
} catch (e) {
logger.error('sm image render failed', { req: req._log(), args, e })
}
try {
await slack.publishCommentUpdate(user, newComment, comment)
} catch (e) {
logger.error('publish commentUpdate on slack failed', { req: req._log(), args, e })
}
return newComment
} catch (e) {
await transaction.transactionRollback()
logger.error('error in transaction', { req: req._log(), args, error: e })
throw e
}
}