not really known
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

391 lines
9.8 KiB

  1. // Interface to server
  2. define(["settings"], function(preferences) {
  3. var server = {};
  4. // Set header util
  5. var setHeader = function(request, token) {
  6. request.setHeaders({ 'x-key' : token.x_key, 'x-access-token' : token.access_token });
  7. }
  8. // Get error code util
  9. var getErrorCode = function(response) {
  10. if (!response || !response.xhrResponse || !response.xhrResponse.body) {
  11. return 404;
  12. }
  13. try {
  14. var body = JSON.parse(response.xhrResponse.body);
  15. return body.code;
  16. } catch(e) {
  17. return 500;
  18. }
  19. }
  20. // Session expired
  21. var sessionExpired = function() {
  22. if (util.getClientType() == constant.webAppType) {
  23. // For web app, force deconnection
  24. util.cleanDatastore(null, function() {
  25. util.restartApp();
  26. });
  27. return true;
  28. } else {
  29. // For app, display a message and mark token expired
  30. window.setTimeout(function() {
  31. humane.log(l10n.get("SessionExpired"));
  32. if (app.toolbar && app.toolbar.showServerWarning) {
  33. app.toolbar.showServerWarning(true);
  34. }
  35. }, 500);
  36. var token = preferences.getToken();
  37. token.expired = true;
  38. preferences.setToken(token);
  39. return false;
  40. }
  41. }
  42. // Retrieve current server name
  43. server.getServer = function() {
  44. var info = preferences.getServer();
  45. if (info != null && info.url) {
  46. return info.url;
  47. }
  48. return (util.getClientType() == constant.webAppType) ? util.getCurrentServerUrl() : constant.http + "localhost";
  49. }
  50. // Retrieve current server URL
  51. server.getServerUrl = function() {
  52. var serverUrl = server.getServer();
  53. if (serverUrl.length && serverUrl[serverUrl.length-1] == '/') {
  54. serverUrl = serverUrl.substr(0, serverUrl.length-1);
  55. }
  56. return serverUrl;
  57. }
  58. // Get URL to retrieve activities from server
  59. server.getActivitiesUrl = function() {
  60. var params = preferences.getToken();
  61. if (!params) {
  62. sessionExpired();
  63. return "";
  64. }
  65. return server.getServerUrl() + constant.dynamicInitActivitiesURL + "?x_key="+params.x_key+"&access_token="+params.access_token;
  66. }
  67. // Get server information
  68. server.getServerInformation = function(serverurl, response, error) {
  69. var serverUrl = serverurl;
  70. if (serverUrl.length && serverUrl[serverurl.length-1] == '/') {
  71. serverUrl = serverUrl.substr(0, serverUrl.length-1);
  72. }
  73. var ajax = new enyo.Ajax({
  74. url: serverUrl + constant.serverInformationURL,
  75. method: "GET",
  76. handleAs: "json"
  77. });
  78. ajax.response(function(inSender, inResponse) {
  79. if (response) response(inSender, inResponse);
  80. });
  81. ajax.error(function(inResponse) {
  82. if (error) error(inResponse, getErrorCode(inResponse));
  83. });
  84. ajax.go();
  85. }
  86. // Get activities
  87. server.getActivities = function(response, error) {
  88. var ajax = new enyo.Ajax({
  89. url: server.getServerUrl() + constant.dynamicInitActivitiesURL,
  90. method: "GET",
  91. handleAs: "json"
  92. });
  93. setHeader(ajax, preferences.getToken());
  94. ajax.response(function(inSender, inResponse) {
  95. var newResponse = {data: inResponse};
  96. if (response) response(inSender, newResponse);
  97. });
  98. ajax.error(function(inResponse) {
  99. var code = getErrorCode(inResponse);
  100. if (code == 3) {
  101. if (sessionExpired()) {
  102. return;
  103. }
  104. }
  105. if (error) error(inResponse, code);
  106. });
  107. ajax.go();
  108. }
  109. // Get user information
  110. server.getUser = function(userId, response, error, optserver) {
  111. var ajax = new enyo.Ajax({
  112. url: (optserver ? optserver : server.getServerUrl()) + constant.initNetworkURL + userId,
  113. method: "GET",
  114. handleAs: "json"
  115. });
  116. setHeader(ajax, preferences.getToken());
  117. ajax.response(response);
  118. ajax.error(function(inResponse) {
  119. var code = getErrorCode(inResponse);
  120. if (code == 3) {
  121. if (sessionExpired()) {
  122. return;
  123. }
  124. }
  125. if (error) error(inResponse, code);
  126. });
  127. ajax.go();
  128. }
  129. // Create a new user
  130. server.postUser = function(user, response, error) {
  131. var ajax = new enyo.Ajax({
  132. url: server.getServerUrl() + constant.signupURL,
  133. method: "POST",
  134. handleAs: "json",
  135. postBody: {
  136. user: JSON.stringify(user)
  137. }
  138. });
  139. ajax.response(function(inSender, inResponse) {
  140. var newuser = {"name": user.name, "password": user.password};
  141. server.loginUser(newuser, function(loginSender, loginResponse) {
  142. preferences.setToken({'x_key': inResponse._id, 'access_token': loginResponse.token});
  143. });
  144. if (response) response(inSender, inResponse);
  145. });
  146. ajax.error(function(inResponse) {
  147. var code = getErrorCode(inResponse);
  148. if (code == 3) {
  149. if (sessionExpired()) {
  150. return;
  151. }
  152. }
  153. if (error) error(inResponse, code);
  154. });
  155. ajax.go();
  156. }
  157. // Create a new user
  158. server.loginUser = function(user, response, error) {
  159. var ajax = new enyo.Ajax({
  160. url: server.getServerUrl() + constant.loginURL,
  161. method: "POST",
  162. handleAs: "json",
  163. postBody: {
  164. user: JSON.stringify(user)
  165. }
  166. });
  167. ajax.response(response);
  168. ajax.error(function(inResponse) {
  169. var code = getErrorCode(inResponse);
  170. if (code == 3) {
  171. if (sessionExpired()) {
  172. return;
  173. }
  174. }
  175. if (error) error(inResponse, code);
  176. });
  177. ajax.go();
  178. }
  179. // Update user
  180. server.putUser = function(userId, settings, response, error) {
  181. var ajax = new enyo.Ajax({
  182. url: server.getServerUrl() + constant.initNetworkURL + userId,
  183. method: "PUT",
  184. handleAs: "json",
  185. postBody: {
  186. user: JSON.stringify(settings)
  187. }
  188. });
  189. setHeader(ajax, preferences.getToken());
  190. ajax.response(response);
  191. ajax.error(function(inResponse) {
  192. var code = getErrorCode(inResponse);
  193. if (code == 3) {
  194. if (sessionExpired()) {
  195. return;
  196. }
  197. }
  198. if (error) error(inResponse, code);
  199. });
  200. ajax.go();
  201. }
  202. // Get journal content optionally filter by typeactivity, time, favorite, ...
  203. server.getJournal = function(journalId, request, response, error) {
  204. var typeactivity = request.typeactivity;
  205. var stime = request.stime;
  206. var favorite = request.favorite;
  207. var field = request.field;
  208. var limit = request.limit;
  209. var offset = request.offset;
  210. var title = request.title;
  211. var sort = request.sort;
  212. var url = server.getServerUrl() + constant.sendCloudURL + journalId + "?limit="+limit;
  213. var params = {};
  214. if (typeactivity !== undefined) {
  215. params.aid = typeactivity;
  216. }
  217. if (stime !== undefined) {
  218. params.stime = stime;
  219. }
  220. if (favorite !== undefined) {
  221. params.favorite = favorite;
  222. }
  223. if (offset !== undefined) {
  224. params.offset = offset;
  225. }
  226. if (title !== undefined) {
  227. params.title = title;
  228. }
  229. if (sort !== undefined) {
  230. params.sort = sort;
  231. } else {
  232. params.sort = "-timestamp";
  233. }
  234. var ajax = new enyo.Ajax({
  235. url: url,
  236. method: "GET",
  237. handleAs: "json"
  238. });
  239. setHeader(ajax, preferences.getToken());
  240. ajax.response(response);
  241. ajax.error(function(inResponse) {
  242. var code = getErrorCode(inResponse);
  243. if (code == 3) {
  244. if (sessionExpired()) {
  245. return;
  246. }
  247. }
  248. if (error) error(inResponse, code);
  249. });
  250. ajax.go(params);
  251. }
  252. // Get an entry in a journal
  253. server.getJournalEntry = function(journalId, objectId, response, error) {
  254. var params = {};
  255. var ajax = new enyo.Ajax({
  256. url: server.getServerUrl() + constant.sendCloudURL + journalId,
  257. method: "GET",
  258. handleAs: "json"
  259. });
  260. params.oid = objectId;
  261. params.fields="text,metadata";
  262. setHeader(ajax, preferences.getToken());
  263. ajax.response(response);
  264. ajax.error(function(inResponse) {
  265. var code = getErrorCode(inResponse);
  266. if (code == 3) {
  267. if (sessionExpired()) {
  268. return;
  269. }
  270. }
  271. if (error) error(inResponse, code);
  272. });
  273. ajax.go(params);
  274. }
  275. // Add an entry in a journal
  276. server.postJournalEntry = function(journalId, entry, response, error) {
  277. var token = preferences.getToken();
  278. entry.metadata.user_id = token.x_key;
  279. var ajax = new enyo.Ajax({
  280. url: server.getServerUrl() + constant.sendCloudURL + journalId,
  281. method: "POST",
  282. handleAs: "json",
  283. postBody: {
  284. journal: JSON.stringify(entry)
  285. }
  286. });
  287. setHeader(ajax, token);
  288. ajax.response(response);
  289. ajax.error(function(inResponse) {
  290. var code = getErrorCode(inResponse);
  291. if (code == 3) {
  292. if (sessionExpired()) {
  293. return;
  294. }
  295. }
  296. if (error) error(inResponse, code);
  297. });
  298. ajax.go();
  299. }
  300. // Update an entry in a journal
  301. server.putJournalEntry = function(journalId, objectId, entry, response, error) {
  302. var params = {};
  303. var token = preferences.getToken();
  304. entry.metadata.user_id = token.x_key;
  305. var ajax = new enyo.Ajax({
  306. url: server.getServerUrl() + constant.sendCloudURL + journalId,
  307. method: "PUT",
  308. handleAs: "json",
  309. postBody: {
  310. journal: JSON.stringify(entry)
  311. }
  312. });
  313. setHeader(ajax, token);
  314. ajax.response(response);
  315. ajax.error(function(inResponse) {
  316. var code = getErrorCode(inResponse);
  317. if (code == 3) {
  318. if (sessionExpired()) {
  319. return;
  320. }
  321. }
  322. if (error) error(inResponse, code);
  323. });
  324. params.oid = objectId;
  325. ajax.go(params);
  326. }
  327. // Delete an entry in a journal
  328. server.deleteJournalEntry = function(journalId, objectId, response, error) {
  329. var params = {};
  330. var ajax = new enyo.Ajax({
  331. url: server.getServerUrl() + constant.sendCloudURL + journalId,
  332. method: "DELETE",
  333. handleAs: "json"
  334. });
  335. setHeader(ajax, preferences.getToken());
  336. ajax.response(response);
  337. ajax.error(function(inResponse) {
  338. var code = getErrorCode(inResponse);
  339. if (code == 3) {
  340. if (sessionExpired()) {
  341. return;
  342. }
  343. }
  344. if (error) error(inResponse, code);
  345. });
  346. params.oid = objectId;
  347. params.type = 'partial';
  348. ajax.go(params);
  349. }
  350. // Create a new user
  351. server.postStats = function(stats, response, error) {
  352. var ajax = new enyo.Ajax({
  353. url: server.getServerUrl() + constant.statsURL,
  354. method: "POST",
  355. handleAs: "json",
  356. postBody: {
  357. stats: JSON.stringify(stats)
  358. }
  359. });
  360. setHeader(ajax, preferences.getToken());
  361. ajax.response(function(inSender, inResponse) {
  362. if (response) response(inSender, inResponse);
  363. });
  364. ajax.error(function(inResponse) {
  365. var code = getErrorCode(inResponse);
  366. if (error) error(inResponse, code);
  367. });
  368. ajax.go();
  369. }
  370. return server;
  371. });