|
| 1 | +import axios from 'axios' |
| 2 | +import Error400 from '../../../errors/Error400' |
| 3 | +import Permissions from '../../../security/permissions' |
| 4 | +import PermissionChecker from '../../../services/user/permissionChecker' |
| 5 | +import track from '../../../segment/track' |
| 6 | + |
| 7 | +export default async (req, res) => { |
| 8 | + new PermissionChecker(req).validateHasAny([ |
| 9 | + Permissions.values.integrationCreate, |
| 10 | + Permissions.values.integrationEdit, |
| 11 | + ]) |
| 12 | + |
| 13 | + if (req.query.subreddit) { |
| 14 | + try { |
| 15 | + const result = await axios.get( |
| 16 | + `https://www.reddit.com/r/${req.query.subreddit}/new.json?limit=1`, |
| 17 | + ) |
| 18 | + if ( |
| 19 | + result.status === 200 && |
| 20 | + result.data.data.children && |
| 21 | + result.data.data.children.length > 0 |
| 22 | + ) { |
| 23 | + console.log('here') |
| 24 | + track( |
| 25 | + 'Reddit: subreddit input', |
| 26 | + { |
| 27 | + subreddit: req.query.subreddit, |
| 28 | + valid: true, |
| 29 | + }, |
| 30 | + { ...req }, |
| 31 | + ) |
| 32 | + return req.responseHandler.success(req, res, result.data.data.children) |
| 33 | + } |
| 34 | + } catch (e) { |
| 35 | + track( |
| 36 | + 'Reddit: subreddit input', |
| 37 | + { |
| 38 | + subreddit: req.query.subreddit, |
| 39 | + valid: false, |
| 40 | + }, |
| 41 | + { ...req }, |
| 42 | + ) |
| 43 | + return req.responseHandler.error(req, res, new Error400(req.language)) |
| 44 | + } |
| 45 | + } |
| 46 | + track( |
| 47 | + 'Reddit: subreddit input', |
| 48 | + { |
| 49 | + subreddit: req.query.subreddit, |
| 50 | + valid: false, |
| 51 | + }, |
| 52 | + { ...req }, |
| 53 | + ) |
| 54 | + return req.responseHandler.error(req, res, new Error400(req.language)) |
| 55 | +} |
0 commit comments