-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
161 lines (146 loc) · 5.07 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
152
153
154
155
156
157
158
159
160
"use strict"
var express = require('express');
var request = require('request')
var bodyParser = require('body-parser');
var Promise = require('promise')
var app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
const urlRocketMap = process.env.urlRocketMap || "localhost:3000";
//const urlRocketMap = process.env.urlRocketMap || "http://54.202.112.224:3000"
const portRocketMap = process.env.portRocketMap || 8000
const usernameRocketMap = process.env.usernameRocketMap || "ido@webiks.com"
const passwordRocketMap = process.env.passwordRocketMap || "123456789"
app.listen(portRocketMap, function () {
login()
.then((credentials) => {
app.set('headers', {
"X-Auth-Token": credentials.authToken,
"X-User-Id": credentials.userId
}
)
console.log("app ready on " + portRocketMap)
}).catch((err) => { console.log("couldn't log in" + err) })
});
function login() {
console.log("trying to log in")
console.log(usernameRocketMap +passwordRocketMap)
return new Promise((resolve, reject) => {
var options = {
url: urlRocketMap + "/api/v1/login",
method: 'POST',
form: { user: usernameRocketMap, password: passwordRocketMap }
}
request(options, function (err, httpResponse, body) {
var bodyParse = JSON.parse(body)
console.log(bodyParse)
console.log("login is " + bodyParse.status)
if (bodyParse.status == "error") {
reject(err)
}
else {
console.log("credentials are - userID: " + bodyParse.data.userId + " authToken: " + bodyParse.data.authToken)
const credentials = {
authToken: bodyParse.data.authToken,
userId: bodyParse.data.userId
}
resolve(credentials)
}
});
})
}
app.get('/', function (req, res) {
res.send("hello rocket.map.app")
})
app.post('/addNewLocation', function (req, res) {
console.log("trying to add location")
const headers = Object.assign({}, req.app.get('headers'), { "Content-type": "application/json" })
var options = {
url: urlRocketMap + "/api/v1/users.create",
method: 'POST',
headers: headers,
form: {
name: "location",
email: "no2@email.com",
password: "123456789",
username: "locaiton2",
customFields: { "location": "tel-aviv" }
}
}
request(options, function (err, res, body) {
var bodyParse = JSON.parse(body)
console.log(bodyParse)
})
});
app.post('/getLocation', function (req, res) {
console.log("trying to get user data")
if (!userId) { userId = credentials.userId }
var options = {
url: urlRocketMap + "/api/v1/users.info?userId=" + userId,
method: 'GET',
headers: req.app.get('headers')
}
request(options, function (err, res, body) {
var bodyParse = JSON.parse(body)
console.log(bodyParse)
})
})
app.post('/updateLocation', function (req, res) {
var user_id = req.data.user_id
var text = req.data.text
var lng = str.substring(17, 25)
var lat = str.substring(20, 29)
console.log(text + lng +lat)
location = "lng=" + lng + "&lat=" + lat
console.log("update_location")
const headers = Object.assign({}, req.app.get('headers'), { "Content-type": "application/json" })
if (!userId) { userId = credentials.userId }
var options = {
url: urlRocketMap + "/api/v1/users.update",
method: 'POST',
headers: headers,
form: { userId: user_id, data: { customFields: { twitter: "locati" }, name: location } }
}
request(options, function (err, res, body) {
var bodyParse = JSON.parse(body)
console.log(bodyParse)
})
});
app.post('/getAllLocations', function (req, res) {
var roomId;
var headers = req.app.get('headers')
getAllUsers(headers, roomId).then(allusers => {
getUsersLocation(headers, allusers)
})
});
function getUsersLocation(headers, allusers) {
console.log("tyring to get all users locations")
allusers.forEach((username) => {
console.log(username)
var options = {
url: urlRocketMap + "/api/v1/users.info?username=" + username,
method: 'GET',
headers: headers
}
request(options, function (err, res, body) {
var bodyParse = JSON.parse(body)
console.log(body)
})
})
}
function getAllUsers(headers, roomId) {
console.log("tyring to get all username")
if (!roomId) { roomId = "GENERAL" }
var options = {
url: urlRocketMap + "/api/v1/channels.info?roomId=" + roomId,
method: 'GET',
headers: headers
}
return new Promise((resolve, reject) => {
request(options, function (err, res, body) {
var bodyParse = JSON.parse(body)
console.log(bodyParse)
resolve(bodyParse.channel.usernames)
})
})
}