闭社主体 forked from https://github.com/tootsuite/mastodon
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

149 lines
4.3 KiB

  1. import dotenv from 'dotenv'
  2. import express from 'express'
  3. import redis from 'redis'
  4. import pg from 'pg'
  5. import log from 'npmlog'
  6. const env = process.env.NODE_ENV || 'development'
  7. dotenv.config({
  8. path: env === 'production' ? '.env.production' : '.env'
  9. })
  10. const pgConfigs = {
  11. development: {
  12. database: 'mastodon_development',
  13. host: '/var/run/postgresql',
  14. max: 10
  15. },
  16. production: {
  17. user: process.env.DB_USER || 'mastodon',
  18. password: process.env.DB_PASS || '',
  19. database: process.env.DB_NAME || 'mastodon_production',
  20. host: process.env.DB_HOST || 'localhost',
  21. port: process.env.DB_PORT || 5432,
  22. max: 10
  23. }
  24. }
  25. const app = express()
  26. const pgPool = new pg.Pool(pgConfigs[env])
  27. const authenticationMiddleware = (req, res, next) => {
  28. const authorization = req.get('Authorization')
  29. if (!authorization) {
  30. err = new Error('Missing access token')
  31. err.statusCode = 401
  32. return next(err)
  33. }
  34. const token = authorization.replace(/^Bearer /, '')
  35. pgPool.connect((err, client, done) => {
  36. if (err) {
  37. return next(err)
  38. }
  39. client.query('SELECT oauth_access_tokens.resource_owner_id, users.account_id FROM oauth_access_tokens INNER JOIN users ON oauth_access_tokens.resource_owner_id = users.id WHERE oauth_access_tokens.token = $1 LIMIT 1', [token], (err, result) => {
  40. done()
  41. if (err) {
  42. return next(err)
  43. }
  44. if (result.rows.length === 0) {
  45. err = new Error('Invalid access token')
  46. err.statusCode = 401
  47. return next(err)
  48. }
  49. req.accountId = result.rows[0].account_id
  50. next()
  51. })
  52. })
  53. }
  54. const errorMiddleware = (err, req, res, next) => {
  55. log.error(err)
  56. res.writeHead(err.statusCode || 500, { 'Content-Type': 'application/json' })
  57. res.end(JSON.stringify({ error: err.statusCode ? `${err}` : 'An unexpected error occured' }))
  58. }
  59. const placeholders = (arr, shift = 0) => arr.map((_, i) => `$${i + 1 + shift}`).join(', ');
  60. const streamFrom = (id, req, res, needsFiltering = false) => {
  61. log.verbose(`Starting stream from ${id} for ${req.accountId}`)
  62. res.setHeader('Content-Type', 'text/event-stream')
  63. res.setHeader('Transfer-Encoding', 'chunked')
  64. const redisClient = redis.createClient({
  65. host: process.env.REDIS_HOST || '127.0.0.1',
  66. port: process.env.REDIS_PORT || 6379,
  67. password: process.env.REDIS_PASSWORD
  68. })
  69. redisClient.on('message', (channel, message) => {
  70. const { event, payload } = JSON.parse(message)
  71. // Only messages that may require filtering are statuses, since notifications
  72. // are already personalized and deletes do not matter
  73. if (needsFiltering && event === 'update') {
  74. pgPool.connect((err, client, done) => {
  75. if (err) {
  76. log.error(err)
  77. return
  78. }
  79. const unpackedPayload = JSON.parse(payload)
  80. const targetAccountIds = [unpackedPayload.account.id].concat(unpackedPayload.mentions.map(item => item.id)).concat(unpackedPayload.reblog ? [unpackedPayload.reblog.account.id] : [])
  81. client.query(`SELECT target_account_id FROM blocks WHERE account_id = $1 AND target_account_id IN (${placeholders(targetAccountIds, 1)})`, [req.accountId].concat(targetAccountIds), (err, result) => {
  82. done()
  83. if (err) {
  84. log.error(err)
  85. return
  86. }
  87. if (result.rows.length > 0) {
  88. return
  89. }
  90. res.write(`event: ${event}\n`)
  91. res.write(`data: ${payload}\n\n`)
  92. })
  93. })
  94. } else {
  95. res.write(`event: ${event}\n`)
  96. res.write(`data: ${payload}\n\n`)
  97. }
  98. })
  99. const heartbeat = setInterval(() => res.write(':thump\n'), 15000)
  100. req.on('close', () => {
  101. log.verbose(`Ending stream from ${id} for ${req.accountId}`)
  102. clearInterval(heartbeat)
  103. redisClient.quit()
  104. })
  105. redisClient.subscribe(id)
  106. }
  107. app.use(authenticationMiddleware)
  108. app.use(errorMiddleware)
  109. app.get('/api/v1/streaming/user', (req, res) => streamFrom(`timeline:${req.accountId}`, req, res))
  110. app.get('/api/v1/streaming/public', (req, res) => streamFrom('timeline:public', req, res, true))
  111. app.get('/api/v1/streaming/hashtag', (req, res) => streamFrom(`timeline:hashtag:${req.params.tag}`, req, res, true))
  112. log.level = 'verbose'
  113. log.info(`Starting HTTP server on port ${process.env.PORT || 4000}`)
  114. app.listen(process.env.PORT || 4000)