const Koa = require('koa')
const app = new Koa()
const rq = require('request-promise-native')
const router = require('koa-router')()
const bodyParser = require('koa-bodyparser')
const log4js = require('log4js')
const logger = log4js.getLogger('disqus-proxy')
const cors = require('kcors')
// const config = require('./config')
const config = require('./my-config')


if (config.log === 'file') log4js.configure({
  appenders: [{
    type: 'file',
    filename: 'disqus-proxy.log'
  }]
})

app.use(bodyParser())
app.use(cors())

let req = {}
const socks5 = config.socks5Proxy

if (socks5 && typeof socks5.host === 'string' && typeof socks5.port === 'number') {
  req = {
    agentClass: require('socks5-https-client/lib/Agent'),
    agentOptions: {
      socksHost: socks5.host,
      socksPort: socks5.port
    },
  }
}

router.get('/api/getThreads', async function (ctx) {
  logger.info('Get thread with identifier: ' + ctx.request.query.identifier)
  let result

  try {
    result = await rq(Object.assign(req, {
      method: 'GET',
      url: 'https://disqus.com/api/3.0/threads/list.json?' +
      'api_secret=' + config.api_secret +
      '&forum=' + config.shortname +
      '&thread:ident=' + ctx.request.query.identifier,
      json: true
    }))
  } catch (e) {
    ctx.body = e.error
    logger.error('Error when get thread:' + JSON.stringify(e.error))
    return
  }
  logger.info('Get thread successfully with response code: ' + result.code)
  ctx.body = result
})


router.get('/api/getComments', async function (ctx) {
  logger.info('Get Comments with identifier: ' + ctx.request.query.identifier)
  let result

  try {
    result = await rq(Object.assign(req, {
      method: 'GET',
      url: 'https://disqus.com/api/3.0/threads/listPosts.json?' +
      'api_secret=' + config.api_secret +
      '&forum=' + config.shortname +
      '&thread:ident=' + ctx.request.query.identifier,
      json: true
    }))
  } catch (e) {
    ctx.body = e.error
    logger.error('Error when get comment:' + JSON.stringify(e.error))
    return
  }
  logger.info('Get comments successfully with response code: ' + result.code)
  ctx.body = result
})

router.post('/api/createComment', async function (ctx) {
  logger.info('Create comment: ' + JSON.stringify(ctx.request.body))
  let result
  try {
    result = await rq(Object.assign(req, {
      url: 'https://disqus.com/api/3.0/posts/create.json',
      method: 'POST',
      form: Object.assign(ctx.request.body, {
        api_key: 'E8Uh5l5fHZ6gD8U3KycjAIAk46f68Zw7C6eW8WSjZvCLXebZ7p0r1yrYDrLilk2F',
      }),
      json: true
    }))
  } catch (e) {
    logger.error('Error when create comment:' + JSON.stringify(e.error))
    ctx.body = e.error
    return
  }
  ctx.body = result
  logger.info('Create comment successfully with response code: ' + result.code)
})

app
  .use(router.routes())
  .use(router.allowedMethods())

app.listen(config.port)

console.log('Disqus proxy server start at port: ' + config.port)

if (config.log === 'file') {
  console.log('See disqus-proxy.log in current directory.')
  logger.info('Server start at port: ' + config.port)
}

@@ -286,11 +286,8 @@
 s')%0A
-// 
 cons
@@ -317,16 +317,19 @@
 onfig')%0A
+// 
 const co
@@ -359,17 +359,16 @@
 nfig')%0A%0A
-%0A
 if (conf
@@ -385,16 +385,20 @@
  'file')
+ %7B%0A 
  log4js.
@@ -411,16 +411,18 @@
 ure(%7B%0A  
+ 
+ 
 appender
@@ -427,16 +427,18 @@
 ers: %5B%7B%0A
+  
     type
@@ -451,16 +451,18 @@
 e',%0A    
+  
 filename
@@ -488,13 +488,19 @@
 '%0A  
+ 
+ 
 %7D%5D%0A
+  %7D)%0A
 %7D
-)
 %0A%0Aap
@@ -820,17 +820,16 @@
 rt%0A    %7D
-,
 %0A  %7D%0A%7D%0A%0A
@@ -1181,32 +1181,35 @@
 hortname +%0A     
+ //
  '&thread:ident=
@@ -1234,32 +1234,176 @@
 ery.identifier,%0A
+      // %E6%AD%A4%E5%A4%84%E5%A6%82%E6%9E%9C%E4%B8%8D%E5%AF%B9identifier encode, %E5%A6%82%E6%9E%9Cidentifier%E4%B8%AD%E5%90%AB%E6%9C%89%E4%B8%AD%E6%96%87%EF%BC%8Cdisqus api%E4%BC%9A%E4%B8%8D%E8%AF%86%E5%88%AB%E6%8A%A5%E9%94%99%0A      '&thread:ident=' + encodeURIComponent(ctx.request.query.identifier),%0A
       json: true
@@ -1634,17 +1634,16 @@
 ult%0A%7D)%0A%0A
-%0A
 router.g
@@ -1999,24 +1999,27 @@
 name +%0A     
+ //
  '&thread:id
@@ -2052,24 +2052,168 @@
 identifier,%0A
+      // %E6%AD%A4%E5%A4%84%E5%A6%82%E6%9E%9C%E4%B8%8D%E5%AF%B9identifier encode, %E5%A6%82%E6%9E%9Cidentifier%E4%B8%AD%E5%90%AB%E6%9C%89%E4%B8%AD%E6%96%87%EF%BC%8Cdisqus api%E4%BC%9A%E4%B8%8D%E8%AF%86%E5%88%AB%E6%8A%A5%E9%94%99%0A      '&thread:ident=' + encodeURIComponent(ctx.request.query.identifier),%0A
       json: 

简化配置~