forked from readmeio/hoot
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhoot.js
More file actions
146 lines (136 loc) · 3.8 KB
/
Copy pathhoot.js
File metadata and controls
146 lines (136 loc) · 3.8 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
const Filter = require('bad-words');
const router = require('express').Router();
const mongoose = require('mongoose');
const Hoot = mongoose.model('Hoot');
const filter = new Filter({
placeHolder: 'X',
});
/*
* @oas [post] /hoot
* summary: Create a hoot!
* description: Post a new hoot to the site
* tags: ['Hoots']
* requestBody:
* description: The hoot you want to post
* required: true
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/NewHoot'
* responses:
* '200':
* description: successfully created hoot
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/BigHoot'
* '400':
* description: invalid hoot data received
* content:
* 'text/html':
* examples:
* MissingBodyExample:
* summary: Missing Body Example
* description: Longer Description of Missing Body Example
* value: 'MissingBody: You need to include a body'
* '500':
* description: server error
* security:
* - basicAuth: []
*/
router.post('/', (req, res) => {
if (!req.body.post) return res.status(400).send('MissingBody: You need to include a body');
const tweet = new Hoot({
post: filter.clean(req.body.post),
replyto: req.body.replyto || undefined,
username: req.user,
});
return tweet.save((err, _tweet) => {
if (err) {
const errors = [];
Object.keys(err.errors).forEach(key => {
errors.push(err.errors[key].message);
});
return res.status(500).send(errors[0]);
}
return _tweet.populate('replyto', (_err, __tweet) => {
return res.status(201).json(__tweet);
});
});
});
/*
* @oas [get] /hoot/{id}
* summary: Get a hoot
* tags: ['Hoots']
* description: Get a specific hoot
* parameters:
* - $ref: '#/components/parameters/id'
* responses:
* '200':
* description: successfully retrieved hoot
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/BigHoot'
* '404':
* description: hoot not found
* security:
* - basicAuth: []
*/
router.get('/:id', async (req, res) => {
let hoot;
try {
hoot = await Hoot.findOne({ _id: req.params.id }).populate('replyto');
} catch (e) {} // eslint-disable-line no-empty
if (!hoot) return res.status(404).send('Unable to locate that specified hoot id!');
return res.json(hoot);
});
/*
* @oas [post] /hoot/{id}/like
* summary: Like a hoot
* tags: ['Hoots']
* description: Add a like to a hoot
* requestBody:
* content:
* application/json:
* schema:
* type: object
* properties:
* liked:
* type: boolean
* description: Boolean based on if you're adding/removing a like (defaults to toggle)
*
* parameters:
* - $ref: '#/components/parameters/id'
* responses:
* '200':
* description: successfully liked (or unliked) hoot
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/SmolHoot'
* '404':
* description: hoot not found
* '500':
* description: server error
* security:
* - basicAuth: []
*/
router.post('/:id/like', async (req, res) => {
let hoot;
try {
hoot = await Hoot.findOne({ _id: req.params.id });
} catch (e) {} // eslint-disable-line no-empty
if (!hoot) return res.status(404).send('Unable to locate that specified hoot id!');
const previouslyLiked = hoot.likes.includes(req.user);
hoot.likes = hoot.likes.filter(like => like !== req.user);
const { liked } = req.body;
if (liked || (liked === undefined && !previouslyLiked)) {
hoot.likes.push(req.user);
}
return hoot.save((_err, _hoot) => {
if (_err) return res.status(500).send(_err.message);
return res.json(_hoot);
});
});
module.exports = router;