-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
193 lines (168 loc) · 5.22 KB
/
index.js
File metadata and controls
193 lines (168 loc) · 5.22 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/**
nexmo context:
you can find this as the second parameter of rtcEvent funciton or as part or the request in req.nexmo in every request received by the handler
you specify in the route function.
it contains the following:
const {
generateBEToken,
generateUserToken,
logger,
csClient,
storageClient
} = nexmo;
- generateBEToken, generateUserToken,// those methods can generate a valid token for application
- csClient: this is just a wrapper on https://github.com/axios/axios who is already authenticated as a nexmo application and
is gonna already log any request/response you do on conversation api.
Here is the api spec: https://jurgob.github.io/conversation-service-docs/#/openapiuiv3
- logger: this is an integrated logger, basically a bunyan instance
- storageClient: this is a simple key/value inmemory-storage client based on redis
*/
/**
*
* This function is meant to handle all the asyncronus event you are gonna receive from conversation api
*
* it has 2 parameters, event and nexmo context
* @param {object} event - this is a conversation api event. Find the list of the event here: https://jurgob.github.io/conversation-service-docs/#/customv3
* @param {object} nexmo - see the context section above
* */
const path = require("path");
const CS_URL = `https://api.nexmo.com`;
const WS_URL = `https://ws.nexmo.com`;
const rtcEvent = async (event, { logger, csClient }) => {
try {
} catch (err) {
logger.error({ err }, "Error on rtcEvent function");
}
};
const messageEvent = async (event, { logger, csClient }) => {
try {
} catch (err) {
logger.error({ err }, "Error on messageEvent function");
}
};
/**
*
* @param {object} app - this is an express app
* you can register and handler same way you would do in express.
* the only difference is that in every req, you will have a req.nexmo variable containning a nexmo context
*
*/
const route = (app, express) => {
app.use(express.static(path.join(__dirname, "build")));
app.get("/", function (req, res) {
res.sendFile(path.join(__dirname, "build", "index.html"));
});
app.post("/api/login", async (req, res) => {
const {
generateBEToken,
generateUserToken,
logger,
csClient,
storageClient,
} = req.nexmo;
const { username } = req.body;
res.json({
user: username,
token: generateUserToken(username),
ws_url: WS_URL,
cs_url: CS_URL,
});
});
app.post("/api/subscribe", async (req, res) => {
const {
generateBEToken,
generateUserToken,
logger,
csClient,
storageClient,
} = req.nexmo;
try {
const { username } = req.body;
const resNewUser = await csClient({
url: `${CS_URL}/beta/users`,
method: "post",
data: {
name: username,
},
});
await storageClient.set(`user:${username}`, resNewUser.data.id);
const storageUser = await storageClient.get(`user:${username}`);
return res.json({ username, resNewUser: resNewUser.data, storageUser });
} catch (err) {
console.log("error", err);
logger.error({ err }, "ERROR");
throw err;
}
});
app.get("/api/users/:username", async (req, res) => {
const { logger, csClient, storageClient } = req.nexmo;
const { username } = req.params;
let user;
user = await storageClient.get(`user:${username}`);
if(user){
console.log(`user `, user)
user = JSON.parse(user)
}
if (!user) {
const userResponse = await csClient({
url: `${CS_URL}/v0.3/users?name=${username}`,
method: "get",
});
user = userResponse.data._embedded.users[0];
await storageClient.set(`user:${username}`, JSON.stringify(user));
}
res.json({ user });
});
app.post("/api/invite/:conversation_name/:username", async (req, res) => {
const { logger, csClient, storageClient } = req.nexmo;
const { username ,conversation_name} = req.params;
try{
let user;
user = await storageClient.get(`user:${username}`);
if(user){
console.log(`user `, user)
user = JSON.parse(user)
}
if (!user) {
const userResponse = await csClient({
url: `${CS_URL}/v0.3/users?name=${username}`,
method: "get",
});
user = userResponse.data._embedded.users[0];
await storageClient.set(`user:${username}`, JSON.stringify(user));
}
let conversation;
const conversationResponse = await csClient({
url: `${CS_URL}/v0.3/conversations`,
method: "post",
data:{
name: conversation_name
}
});
conversation = conversationResponse.data;
let member;
const memberResponse = await csClient({
url: `${CS_URL}/v0.3/conversations/${conversation.id}/members`,
method: "post",
data:{
"state": "invited",
"user": {
"name": username
},
"channel": {
"type": "app"
}
}
});
conversation = conversationResponse.data;
res.json({ user, conversation,member });
}catch(e){
res.status(500).json({ error: e });
}
});
};
module.exports = {
rtcEvent,
messageEvent,
route,
};