forked from HimaniBhaisare/lantern-notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
123 lines (109 loc) · 3.21 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
require('dotenv').config();
const express = require('express');
const admin = require('firebase-admin');
const socket = require('socket.io');
// Express setup
const app = express();
const NODE_ENV = process.env.NODE_ENV || 'development';
const port = process.env.PORT || 5000;
const server = app.listen(port, () =>
console.log(`Listening at http://localhost:${port}`),
);
app.use(express.static('public'));
app.use(express.json({ limit: '1mb' }));
const io = socket(server);
io.on('connection', (socket) => {
socket.on('userSession', (userSession) => {
let userId = userSession.userId;
socket.join(userId);
socket.to(userId).emit('userSession', userSession);
});
socket.on('collabSession', (currentSession) => {
let sessionId = currentSession.sessionId;
if (currentSession.action == 'subscribe') {
socket.join(sessionId);
socket.to(sessionId).emit('collabSession', currentSession);
} else if (currentSession.action == 'unsubscribe') {
socket.to(sessionId).emit('collabSession', currentSession);
socket.leave(sessionId);
}
});
});
const serviceAccount =
NODE_ENV == 'production'
? require('/etc/secrets/serviceAccountKey.json')
: JSON.parse(
Buffer.from(process.env.GOOGLE_FIREBASE_AUTH, 'base64').toString(
'utf-8',
),
);
// Firestore setup
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
});
const db = admin.firestore();
admin.firestore().settings({
ignoreUndefinedProperties: true,
});
async function updateNotes(userId, note) {
let folderRef = db
.collection('users')
.doc(userId)
.collection('folders')
.doc(note.folderId);
// Folder name can be later set from folderMetadata if adding a folder feature
await folderRef.set({ folderName: 'Default' });
let notesRef = db
.collection('users')
.doc(userId)
.collection('notes')
.doc(note.noteId);
await notesRef.set({
noteName: note.noteName,
mdContent: note.mdContent,
blockContent: note.blockContent,
noteType: note.noteType,
folderId: note.folderId,
});
}
async function updateUserProfile(user) {
let userRef = db.collection('users').doc(user.userId);
await userRef.set({
name: user.name,
email: user.email,
});
}
app.post('/notes', (req, res) => {
updateNotes(req.body.userId, req.body.note)
.then(() => res.json({ message: 'Database updated' }))
.catch((err) => console.log(err));
});
app.post('/deleteNote', (req, res) => {
db.collection('users')
.doc(req.body.userId)
.collection('notes')
.doc(req.body.noteId)
.delete()
.then(() => res.json({ message: 'Note deleted' }))
.catch((err) => console.log(err));
});
app.post('/users', (req, res) => {
updateUserProfile(req.body)
.then(() => res.json({ message: 'profile updated' }))
.catch((err) => console.log(err));
});
app.post('/notesList', (req, res) => {
let notes = {};
db.collection('users')
.doc(req.body.userId)
.collection('notes')
.orderBy(req.body.orderBy.sortBy, req.body.orderBy.direction)
.get()
.then((snapshot) => {
snapshot.forEach((note) => {
notes[note.id] = note.data();
});
res.json(notes);
})
.catch((err) => console.log(err));
});