-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
151 lines (139 loc) · 4.28 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/////////////////////////////////////////////////
// SERVER
/////////////////////////////////////////////////
/*
Mainly json parsing and editing, to act like a database.
We use json files instead of a "real" database so the user can easily transfert
his conversations, contacts and key to another device.
The user is fully responsible for the security of these json files.
*/
var express = require('express');
var fs = require('fs');
var url = require('url');
var app = express();
//Serves static files from web folder; i.e. all UI COMPONENTS
app.use(express.static('web'));
function getJsonFromFile(path) {
return JSON.parse(fs.readFileSync(path, 'utf8'));
}
////////////////////////////////////////////////
//Contact
const CONTACTS_FILE = "contacts.json"
// WRITE
app.get('/c/:theAddress/:prettyName', function (req, res){
writeContact(req.params.theAddress.toString(), req.params.prettyName.toString());
res.send(JSON.stringify({ "succeed":true}));
}
);
function writeContact(address, prettyName) {
var jObj;
try {
jObj = getJsonFromFile(CONTACTS_FILE);
}
catch (e) {
console.log(e);
}
jObj.push({"address":address, "name":prettyName});
fs.writeFile(CONTACTS_FILE, JSON.stringify(jObj), function (err) {
if (err) throw err;
});
}
//GET
app.get('/getName/:theAddress', function(req, res){
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify({ "name":getName(req.params.theAddress)}));
});
function getName(address){
var name = address;
var jsonContent = getJsonFromFile(CONTACTS_FILE);
for (var i = 0; i < jsonContent.length; i++) {
if(jsonContent[i].address == address){
name = jsonContent[i].name;
}
}
return name;
}
////////////////////////////////////////////////
//Key
const KEYS_FILE = "keys.json"
// WRITE
app.get('/addKey/:convId/:key', function (req, res){
writeKey(req.params.convId.toString(), req.params.key.toString());
res.send(JSON.stringify({ "succeed":true}));
}
);
function writeKey(convId, key) {
var jObj;
try {
jObj = getJsonFromFile(KEYS_FILE);
}
catch (e) {
console.log(e);
}
jObj.push({"convId":convId, "key":key});
fs.writeFile(KEYS_FILE, JSON.stringify(jObj), function (err) {
if (err) throw err;
});
}
// GET
app.get('/getKey/:id', function(req, res){
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify({ "key":getKey(req.params.id)}));
});
// Returns the key for the given conversation
function getKey(id){
var jsonContent = getJsonFromFile(KEYS_FILE);
var key = "";
for (var i = 0; i < jsonContent.length; i++) {
if(jsonContent[i].convId == id){
key = jsonContent[i].key;
}
}
return key;
}
////////////////////////////////////////////////
//Conversation
const CONVS_FILE = "convs.json";
app.get('/d/:id/:prettyName', function (req, res){
writeConv(req.params.id.toString(), req.params.prettyName.toString());
res.send(JSON.stringify({"succeed":true}));
}
);
function writeConv(id, prettyName) {
var jObj;
try {
jObj = getJsonFromFile(CONVS_FILE);
}
catch (e) {
console.log(e);
}
jObj.push({"id":id, "name":prettyName});
fs.writeFile(CONVS_FILE, JSON.stringify(jObj), function (err) {
if (err) throw err;
});
}
app.get('/getConvName/:id', function(req, res){
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify({ "name":getConvName(req.params.id)}));
});
function getConvName(id){
var name = id;
var jsonContent = getJsonFromFile(CONVS_FILE);
for (var i = 0; i < jsonContent.length; i++) {
if(jsonContent[i].id == id){
name = jsonContent[i].name;
}
}
return name;
}
/////////////////////////////////////////////////
// GET THE WHOLE JSON OBJECT AND SEND IT
/////////////////////////////////////////////////
////////////////////////////////////////////////
//Conversation
app.get('/getWholeObjConv/', function(req, res){
res.setHeader('Content-Type', 'application/json');
res.send(fs.readFileSync('convs.json', 'utf8'));
});
console.log("TALARIA\nOfficial Website : http://arsent.ch/talaria\nServer is listening on port 5000");
var server = app.listen(5000);