-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
129 lines (110 loc) · 3.91 KB
/
index.js
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
const port = process.env.PORT || 3000;
const apiDocs = require("./swagger.json");
const successPercentage = parseFloat(process.env.SUCCESS_PERCENTAGE) || 0.75;
const http = require("http");
const express = require("express");
const swaggerUi = require("swagger-ui-express");
const bodyParser = require("body-parser");
const cors = require("cors");
const uuid = require("uuid/v4");
const { all, isNil, isEmpty, pick, prop, propEq, takeLast, times } = require("ramda");
const { Maybe } = require("ramda-fantasy");
const faker = require("faker");
const titleize = require("titleize");
const random = require("random-js");
const isPresent = val => !isNil(val) && !isEmpty(val);
const isBlank = val => !isPresent(val);
const toId = id => String(id);
const app = express();
const buildUser = ({ id, name, avatar } = {}) => ({
id: isPresent(id) ? toId(id) : uuid(),
name: name || titleize(faker.fake("{{hacker.adjective}} {{name.firstName}}")),
avatar: avatar || faker.image.avatar(),
});
const buildMessage = ({ id, content, author, created_at } = {}) => ({
id: isPresent(id) ? toId(id) : uuid(),
content: content || faker.hacker.phrase(),
author: buildUser(author),
created_at: created_at || faker.date.past(),
});
const users = [
"laverne_jacobi11",
"noelia_christiansen",
"kevin_heaney",
"ulises.rath",
"mona_mueller",
"elinor.klein17",
"gunnar_gerhold",
"ramona_davis74",
"gerald47",
"kieran56",
]
.map(id => buildUser({ id }));
const channels = [
["general", "Geral"],
["random", "Random"],
["javascripty", "Javascript"],
["ruby", "Ruby"],
["ajuda", "Ajuda"],
["entrevista", "Entrevista"],
]
.map(([id, name]) => ({
id,
name,
messages: times(() => buildMessage({ author: faker.random.arrayElement(users) }), 5)
.sort((a, b) => b.created_at - a.created_at),
}));
app.use(cors());
app.use(bodyParser.json());
app.get("/me", (req, res) => res.json(faker.random.arrayElement(users)));
app.get("/users", (req, res) => res.json(users));
app.get("/channels", (req, res) =>
res.json(channels.map(pick(["id", "name"]))));
app.get("/channels/:id/messages", (req, res) => {
const id = toId(req.params.id);
const [status, body] = Maybe(channels.find(propEq("id", id)))
.map(prop("messages"))
.map(takeLast(50))
.map(messages => [200, messages])
.getOrElse([404, {
type: "channel_not_found",
error: "Channel not found",
channel: id,
}]);
res.status(status).json(body);
});
app.post("/channels/:id/messages", (req, res) => {
const channelId = toId(req.params.id);
const userId = toId(req.body.author_id);
const channel = channels.find(propEq("id", channelId));
const user = users.find(propEq("id", userId));
const shouldSucceed = /true/i.test(req.query.stable) || random.bool(successPercentage)(random.engines.nativeMath)
if (all(isPresent, [channel, user, req.body.message])) {
if (shouldSucceed) {
const message = buildMessage({ content: req.body.message, author: user, created_at: new Date() });
channel.messages.push(message);
res.json(message);
} else {
res.status(500).json({ type: "internal_error", error: "Unexpected error" });
}
} else if (isBlank(req.body.message)) {
res.status(400).json({
type: "missing_property",
error: 'Property "message" is required',
properties: ["message"],
});
} else if (isBlank(req.body.author_id)) {
res.status(400).json({
type: "missing_property",
error: 'Property "author_id" is required',
properties: ["author_id"],
});
} else if (isBlank(user)) {
res.status(404).json({ type: "user_not_found", error: "User not found", user: userId });
} else {
res.status(404).json({ type: "channel_not_found", error: "Channel not found", channel: channelId });
}
});
app.use("/docs", swaggerUi.serve, swaggerUi.setup(apiDocs));
const server = http.createServer(app);
server.listen(port, () => console.log("App listening on port", port));