-
Notifications
You must be signed in to change notification settings - Fork 1
/
cloud.js
83 lines (75 loc) · 2.89 KB
/
cloud.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
79
80
81
82
83
const process = require('node:process')
const AV = require('leanengine')
const request = require('request')
const mail = require('./utilities/send-mail')
const Comment = AV.Object.extend('Comment')
function formatComment(comment) {
return `评论(${comment.get('objectId')}) by ${comment.get('nick')} - 【${comment.get('comment')}】`
}
async function sendMailByComment(comment) {
const taskList = []
let err = false
const isNotified = comment.get('isNotified')
const notifyStatus = comment.get('notifyStatus')
if (!isNotified || notifyStatus === 'noticed') {
taskList.push(mail.notice(comment).catch((e) => {
err = true
console.error(`通知站长失败: ${formatComment(comment)}`, e)
}).then((msg) => {
if (typeof msg === 'string' && msg.startsWith('notice skipped'))
console.log(`跳过(${msg.slice('notice skipped'.length)}): ${formatComment(comment)}`)
else
console.log(`通知站长成功: ${formatComment(comment)}`)
comment.set('notifyStatus', 'noticed')
}))
}
if (!isNotified || notifyStatus === 'sended') {
taskList.push(mail.send(comment).catch((e) => {
err = true
console.error(`发送被@者失败: ${formatComment(comment)}`, e)
}).then((msg) => {
if (typeof msg === 'string' && msg.startsWith('send skipped'))
console.log(`跳过(${msg.slice('send skipped'.length)}): ${formatComment(comment)}`)
else
console.log(`发送被@者成功: ${formatComment(comment)}`)
comment.set('notifyStatus', 'sended')
}))
}
await Promise.allSettled(taskList)
if (!err) {
comment.set('isNotified', true)
comment.set('notifyStatus', 'finish')
}
comment.save()
if (err)
throw new Error('发送邮件失败')
}
AV.Cloud.afterSave('Comment', async (request) => {
const currentComment = request.object
console.log('hook(after save comment - 收到一条评论): ', formatComment(currentComment))
await sendMailByComment(currentComment)
return 'finish'
})
AV.Cloud.define('resend_mails', async () => {
const query = new AV.Query(Comment)
query.greaterThanOrEqualTo('createdAt', new Date(new Date().getTime() - 24 * 60 * 60 * 1000))
query.notEqualTo('isNotified', true)
// 如果你的评论量很大,可以适当调高数量限制,最高1000
query.limit(200)
const results = await query.find()
await Promise.allSettled(results.map(comment => sendMailByComment(comment)))
console.log(`昨日${results.length}条未成功发送的通知邮件处理完毕!`)
return results.length
})
AV.Cloud.define('verify_mail', async () => {
const res = await mail.verify()
console.log(res)
return res
})
AV.Cloud.define('self_wake', () => {
request(process.env.ADMIN_URL, (error, response) => {
if (error)
console.error('自唤醒任务执行失败', error)
console.log('自唤醒任务执行成功,响应状态码为:', response && response.statusCode)
})
})