File size: 1,834 Bytes
81d8726
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const { sendServerChan, sendEmail, sendTg, sendWebhook } = require('../utils/notify')
const { NotifyConfigDB, NotifyDB } = require('../utils/db-class')
const notifyDB = new NotifyDB().getInstance()
const notifyConfigDB = new NotifyConfigDB().getInstance()

async function getNotifyConfig({ res }) {
  const data = await notifyConfigDB.findOneAsync({})
  return res.success({ data })
}

async function updateNotifyConfig({ res, request }) {
  let { body: { noticeConfig } } = request
  let { type } = noticeConfig
  try {
    // console.log('noticeConfig: ', noticeConfig[type])
    switch (type) {
      case 'sct':
        await sendServerChan(noticeConfig[type]['sendKey'], 'EasyNode通知测试', '这是一条测试通知')
        break
      case 'email':
        await sendEmail(noticeConfig[type], 'EasyNode通知测试', '这是一条测试通知')
        break
      case 'tg':
        await sendTg(noticeConfig[type], 'EasyNode通知测试', '这是一条测试通知')
        break
      case 'webhook':
        await sendWebhook(noticeConfig[type], 'EasyNode通知测试', '这是一条测试通知')
        break
    }
    await notifyConfigDB.update({}, { $set: noticeConfig }, { upsert: true })
    return res.success({ msg: '测试通过 | 保存成功' })
  } catch (error) {
    return res.fail({ msg: error.message })
  }
}

async function getNotifyList({ res }) {
  const data = await notifyDB.findAsync({})
  res.success({ data })
}

async function updateNotifyList({ res, request }) {
  let { body: { type, sw } } = request
  if (!([true, false].includes(sw))) return res.fail({ msg: `Error type for sw:${ sw }, must be Boolean` })
  await notifyDB.updateAsync({ type }, { $set: { sw } })
  res.success()
}

module.exports = {
  getNotifyConfig,
  updateNotifyConfig,
  getNotifyList,
  updateNotifyList
}