-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
418 lines (370 loc) · 12.6 KB
/
server.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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
const serverless = require('serverless-http')
const cors = require('cors')
const bodyParser = require("body-parser")
const express = require("express")
const multer = require('multer')
const multerS3 = require('multer-s3');
const AWS = require('aws-sdk');
const dotenv = require("dotenv")
const Mixpanel = require('mixpanel');
const {verifyAuth} = require("./middleware")
var path = require('path')
const {
getNFTsByOwner,
getNFTByTokenId,
getSpotifyAuthTokens,
refreshSpotifyAccessToken,
mintNFTToAddress,
getUser,
createUser,
updateUser,
getIntegration,
createIntegration,
getCollectibles,
getCollectible,
createUserArtistCollectible,
createUserTrackCollectible,
getArtists,
getArtist,
getArtistCollector,
createArtistCollector,
createArtistCollectible,
getSpotifyTopArtists,
getSpotifyProfile,
getArtistCollectors,
getArtistCollectibles,
getCurrentAcheivement,
getCollectiblesBySk,
getCollections,
getCollection,
deleteCollection,
createCollection,
getSpotifySimilarArtists,
getSpotifyNewMusic,
getSpotifyArtist,
sendEmailTemplate,
getRandomCollectibleImageFromS3,
sortNftsFromSimpleHash
} = require("./utils.js")
// Set up express
const app = express();
const port = 8000;
// Init dotenv
dotenv.config();
// set up mixpanel
const mixpanel = Mixpanel.init(process.env.MIXPANEL_TOKEN)
// Set up S3
const S3 = new AWS.S3({
accessKeyId: process.env.AWS_ACCESS_ID,
secretAccessKey: process.env.AWS_ACCESS_SECRET
});
// Setup CORS and body parser
app.use(cors())
app.use(bodyParser.json());
// Profile Image Upload to S3
const upload = multer({
storage: multerS3({
s3: S3,
bucket: process.env.AWS_S3_USER_BUCKET_NAME,
key: function (req, file, cb) {
cb(null, `images/${req.params.pk}/${new Date().getTime()}${path.extname(file.originalname)}`);
}
})
});
/**
* User endpoints
* These endpoints are used to create, read, update, and delete users
*/
app.get("/account/user", verifyAuth, async (req, res) => {
const pk = req.query.pk;
const user = await getUser(pk)
res.json(user)
});
app.post("/account/user", verifyAuth, async (req, res) => {
const data = req.body;
const user = await createUser(data)
mixpanel.people.set(data.verifierId, {
walletAddresses: data.addresses,
$name: data.name,
$created: (new Date()).toISOString(),
$email: data.email
}, {
$ip: req.socket.remoteAddress
});
res.json(user)
});
app.put("/account/user", verifyAuth, async (req, res) => {
const data = req.body;
const user = await updateUser(data.verifierId, data)
mixpanel.track('Sign In', {
ip: req.socket.remoteAddress,
distinct_id: data.verifierId,
});
res.json(user)
});
app.post("/account/user/:pk/image/upload", upload.single('file'), async (req, res) => {
res.send({
message: "Uploaded!",
url: `${process.env.RADIA_USER_MEDIA_CDN}/${req.file.key}` //{url: req.file.location, name: req.file.key, type: req.file.mimetype, size: req.file.size}
});
});
/**
* Spotify integration endpoints. /integration/spotify/auth flow to get access token.
* Subsequent data endpoints require a Spotify access token.
*
* For more information, read https://developer.spotify.com/web-api
*/
app.get("/integration/spotify/auth", verifyAuth, async (req, res) => {
const code = req.query.code;
getSpotifyAuthTokens(code)
.then((json) => res.json(json))
.catch((err) => console.log("Error getting spotify Refresh Token: ", err));
});
app.get("/integration/spotify/artist", verifyAuth, async (req, res) => {
const refreshToken = req.query.refreshToken;
const freshToken = await refreshSpotifyAccessToken(refreshToken);
getSpotifyArtist(req.query.id, freshToken)
.then((token) => res.json(token))
.catch((err) => console.log("Error getting spotify artist:", err));
});
app.get("/integration/spotify/me", verifyAuth, async (req, res) => {
const refreshToken = req.query.refreshToken;
const freshToken = await refreshSpotifyAccessToken(refreshToken);
const spotify = await getSpotifyProfile(freshToken)
res.json(spotify)
});
/**
* User Integration endpoints
* These endpoints are used to create & read 3rd party integrations for a user
*/
app.get("/account/integration", verifyAuth, async (req, res) => {
const type = req.query.type;
const pk = req.query.pk;
const user = await getIntegration(type, pk)
res.json(user)
});
app.post("/account/integration/:type/:pk", verifyAuth, async (req, res) => {
const type = req.params.type;
const pk = req.params.pk;
const data = req.body;
const user = await createIntegration(type, pk, data)
mixpanel.people.set(pk, 'Integration', {spotify: true});
res.json(user)
});
/**
* Collectibles endpoints
* These endpoints are used to read collectibles
*/
app.get("/account/collectibles", verifyAuth, async (req, res) => {
const pk = req.query.pk;
const limit = req.query.limit;
const lastEvaluatedKey = req.query.lastEvaluatedKey;
const user = await getCollectibles(pk, limit, lastEvaluatedKey)
res.json(user)
});
app.get("/account/collectible", verifyAuth, async (req, res) => {
const pk = req.query.pk;
const sk = req.query.sk;
const collectible = await getCollectible(pk, sk)
res.json(collectible)
});
app.post("/account/collectible/artist/:pk", verifyAuth, async (req, res) => {
const pk = req.params.pk;
const artist = req.body.artist;
const achievement = req.body.achievement;
const streamedMilliseconds = req.body.streamedMilliseconds;
const user = req.body.user;
const statusName = req.body.status;
const transaction = req.body.transaction;
const collectible = await createUserArtistCollectible(pk, artist, achievement, streamedMilliseconds, user, statusName, transaction)
res.json(collectible)
});
app.post("/account/collectible/track/:pk", verifyAuth, async (req, res) => {
const pk = req.params.pk;
const artist = req.body.artist;
const track = req.body.track;
const achievement = req.body.achievement;
const user = req.body.user;
const statusName = req.body.status;
const transaction = req.body.transaction;
const collectible = await createUserTrackCollectible(pk, artist, track, achievement, user, statusName, transaction)
res.json(collectible)
});
/**
* Artists endpoints
* These endpoints are used to read artists
*/
app.get("/account/artists", verifyAuth, async (req, res) => {
const pk = req.query.pk;
const lastEvaluatedKey = req.query.lastEvaluatedKey;
const artists = await getArtists(pk, lastEvaluatedKey)
res.json(artists)
});
app.get("/account/artists/top", verifyAuth, async (req, res) => {
const refreshToken = req.query.refreshToken;
const freshToken = await refreshSpotifyAccessToken(refreshToken);
const artists = await getSpotifyTopArtists(freshToken)
res.json(artists)
});
app.get("/account/artists/similar", verifyAuth, async (req, res) => {
const refreshToken = req.query.refreshToken;
const id = req.query.id;
const freshToken = await refreshSpotifyAccessToken(refreshToken);
const artists = await getSpotifySimilarArtists(id, freshToken)
res.json(artists)
});
app.get("/account/artists/new-music", verifyAuth, async (req, res) => {
const refreshToken = req.query.refreshToken;
const nextUrl = req.query.nextUrl
const freshToken = await refreshSpotifyAccessToken(refreshToken);
const newMusic = await getSpotifyNewMusic(freshToken, nextUrl)
res.json(newMusic)
});
app.get("/artist/:id", verifyAuth, async (req, res) => {
const id = req.params.id;
const artist = await getArtist(id)
res.json(artist)
});
app.get("/collectibles/sk", verifyAuth, async (req, res) => {
const sk = req.query.sk;
const collectible = await getCollectiblesBySk(sk)
res.json(collectible)
});
app.get("/artist/collectibles/:id", verifyAuth, async (req, res) => {
const id = req.params.id;
const collectibles = await getArtistCollectibles(id)
res.json(collectibles)
});
app.post("/artist/collectible", verifyAuth, async (req, res) => {
const artist = req.body.artist;
const achievement = req.body.achievement;
const collector = await createArtistCollectible(artist, achievement)
res.json(collector)
});
app.get("/artist/collectors/:id", verifyAuth, async (req, res) => {
const id = req.params.id;
const collectors = await getArtistCollectors(id)
res.json(collectors)
});
app.get("/artist/collector/:id", verifyAuth, async (req, res) => {
const id = req.params.id;
const pk = req.query.pk;
const collector = await getArtistCollector(pk, id)
res.json(collector)
});
app.post("/artist/collector/:id/:pk", verifyAuth, async (req, res) => {
const id = req.params.id;
const pk = req.params.pk;
const user = req.body.user;
const collectibleCount = req.body.collectibleCount;
const collector = await createArtistCollector(id, pk, user, collectibleCount)
res.json(collector)
});
/**
* Collections and NFTs endpoints.
*
* Uses Simplehash to get NFTs for address. For more information, read https://simplehash.readme.io/reference/overview
*/
app.get("/account/collections", verifyAuth, async (req, res) => {
const pk = req.query.pk;
const collections = await getCollections(pk)
res.json(collections)
});
app.post("/account/collections/:pk", verifyAuth, async (req, res) => {
const pk = req.params.pk;
const data = req.body;
const collection = await createCollection(pk, data)
mixpanel.track('Create Collection', {
ip: req.socket.remoteAddress,
distinct_id: pk,
});
res.json(collection)
});
app.delete("/account/collections/:pk", verifyAuth, async (req, res) => {
const pk = req.params.pk;
const sk = req.body.sk;
const collection = await deleteCollection(pk, sk)
res.json(collection)
});
app.get("/account/collection", verifyAuth, async (req, res) => {
const pk = req.query.pk;
const sk = req.query.sk;
const collection = await getCollection(pk, sk)
res.json(collection)
});
app.get("/account/nfts", verifyAuth, async (req, res) => {
const nfts = await getNFTsByOwner(
req.query.chains,
req.query.addresses,
req.query.nextUrl
);
res.json(sortNftsFromSimpleHash(nfts));
});
app.get("/account/nft", verifyAuth, async (req, res) => {
const nft = await getNFTByTokenId(
req.query.chain,
req.query.contractAddress,
req.query.tokenId
);
res.json(nft);
});
/**
* ThirdWeb integration endpoints.
* These endpoints are used to integrate with the ThirdWeb SDK
*/
app.post("/nft/mint/spotify/track", verifyAuth, async (req, res) => {
const pk = req.body.pk;
const walletAddress = req.body.walletAddress;
const track = req.body.track;
let artists = track.artists.map((artist) => artist.name).join(", ");
artists = artists.replace(/,\s*$/, "");
const nftMetadata = {
name: `Streamed ${track.name} from ${artists} in first 24 hours of release`,
description: `${track.name} from ${artists} played at ${track.played_at}`,
image: getRandomCollectibleImageFromS3(), //TODO: this is random. At some point we should track what collectibles the user alreadt owns and not give them the same one.
track
};
console.log("This is our nft metadata: ", nftMetadata);
const result = await mintNFTToAddress(walletAddress, nftMetadata);
mixpanel.track('Claim Collectible (Track)', {
ip: req.socket.remoteAddress,
distinct_id: pk,
});
res.json(result);
});
app.post("/nft/mint/spotify/artist", verifyAuth, async (req, res) => {
const pk = req.body.pk;
const walletAddress = req.body.walletAddress;
const artist = req.body.artist;
const streamedMilliseconds = req.body.streamedMilliseconds;
const nftMetadata = {
name: `${getCurrentAcheivement(streamedMilliseconds)} of ${artist.name}`,
description: `${getCurrentAcheivement(streamedMilliseconds)} of ${artist.name} on Spotify.`,
image: getRandomCollectibleImageFromS3(), //TODO: this is random. At some point we should track what collectibles the user alreadt owns and not give them the same one.
artist
};
console.log("This is our nft metadata: ", nftMetadata);
const result = await mintNFTToAddress(walletAddress, nftMetadata);
mixpanel.track('Claim Collectible (Artist)', {
ip: req.socket.remoteAddress,
distinct_id: pk,
});
res.json(result);
});
/**
* Private Route: Requires x-api-key header
*/
app.post("/email/send", async (req, res) => {
const templateName = req.body.templateName;
const emailAddress = req.body.emailAddress;
const templateContent = req.body.templateContent;
const subject = req.body.subject;
const result = await sendEmailTemplate(emailAddress, templateName, templateContent, subject);
res.json(result);
});
// app.listen(port, () => {
// console.log(`Example app listening on port ${port}`);
// });
module.exports.handler = serverless(app, {
binary: ['image/png', 'image/gif', 'image/jpeg', 'image/jpg', 'image/svg+xml']
})