routes.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. op-mattermost provides an integration for Mattermost and Open Project.
  3. Copyright (C) 2020 to present , Girish M
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <https://www.gnu.org/licenses/>
  14. */
  15. require('./message');
  16. module.exports = (app, axios) => {
  17. const opURL = process.env.OP_URL;
  18. const mmURL = process.env.MM_URL;
  19. const intURL = process.env.INT_URL;
  20. const UIActions = require('./uiActions');
  21. const uiActions = new UIActions(opURL, mmURL, intURL);
  22. const RateLimit = require('express-rate-limit');
  23. const limiter = RateLimit({
  24. windowMs: 1*60*1000,
  25. max: 100,
  26. standardHeaders: true,
  27. legacyHeaders: false
  28. });
  29. app.use(limiter);
  30. app.get('/', (req, res) => {
  31. res.send("Hello there! Good to see you here :) We don't know what to show here yet!").status(200);
  32. });
  33. app.post('/', (req, res) => {
  34. const {command, token, text} = req.body;
  35. if(token === process.env.MATTERMOST_SLASH_TOKEN) {
  36. console.log("Request Body to / ", JSON.stringify(req.body, null, 2));
  37. if(command === "/op") {
  38. switch (text) {
  39. case 'lt':
  40. uiActions.showSelProject(req, res, axios, "showSelWP");
  41. break;
  42. case 'cwp':
  43. uiActions.showSelProject(req, res, axios, "createWP");
  44. break;
  45. case 'tl':
  46. uiActions.getTimeLog(req, res, axios);
  47. break;
  48. case 'dtl':
  49. uiActions.showTimeLogSel(req, res, axios);
  50. break;
  51. case 'dwp':
  52. uiActions.showDelWPSel(req, res, axios);
  53. break;
  54. case 'bye':
  55. uiActions.showByeMsg(req, res);
  56. break;
  57. case 'sub':
  58. uiActions.notificationSubscribe(req, res, axios);
  59. break;
  60. default:
  61. uiActions.showMenuBtn(req, res, axios);
  62. break;
  63. }
  64. }
  65. else {
  66. res.send("*I don't understand ", command, ". Let's try again...* \n `/op`").status(500);
  67. }
  68. }
  69. else {
  70. res.send("Invalid slash token").status(400);
  71. }
  72. });
  73. app.post('/createTimeLog', (req, res) => {
  74. console.log("Create time log request: ", JSON.stringify(req.body, null, 2));
  75. uiActions.showSelProject(req, res, axios, "showSelWP");
  76. });
  77. app.post('/projSel', (req, res) => {
  78. console.log("Project submit request: ", JSON.stringify(req.body, null, 2));
  79. switch (req.body.context.action) {
  80. case 'showSelWP':
  81. uiActions.showSelWP(req, res, axios, "showTimeLogDlg", 'update');
  82. break;
  83. case 'createWP':
  84. uiActions.createWP(req, res, axios);
  85. break;
  86. default:
  87. res.send("Invalid action type").status(400);
  88. break;
  89. }
  90. });
  91. app.post('/wpSel', (req, res) => {
  92. console.log("Work package submit request: ", JSON.stringify(req.body, null, 2));
  93. switch (req.body.context.action) {
  94. case 'showTimeLogDlg':
  95. uiActions.loadTimeLogDlg(req, res, axios);
  96. break;
  97. case 'cnfDelWP':
  98. uiActions.showCnfDelWP(req, res, axios);
  99. break;
  100. default:
  101. res.send("Invalid action type").status(400);
  102. break;
  103. }
  104. });
  105. app.post('/logTime', (req, res) => {
  106. console.log("Time log submit request: ", JSON.stringify(req.body, null, 2));
  107. uiActions.handleSubmission(req, res, axios);
  108. });
  109. app.get('/getLogo', (req, res) => {
  110. console.log("Logo image request: ", JSON.stringify(req.body, null, 2));
  111. res.sendFile(__dirname + '/op_logo.png');
  112. });
  113. app.post('/getTimeLog', (req, res) => {
  114. console.log("Request to getTimeLog: ", JSON.stringify(req.body, null, 2));
  115. uiActions.getTimeLog(req, res, axios, 'update');
  116. });
  117. app.post('/delTimeLog', (req, res) => {
  118. console.log("Request to delTimeLog: ", JSON.stringify(req.body, null, 2));
  119. switch (req.body.context.action) {
  120. case "delSelTimeLog":
  121. uiActions.delTimeLog(req, res, axios);
  122. break;
  123. case "cnfDelTimeLog":
  124. uiActions.cnfDelTimeLog(req, res);
  125. break;
  126. default:
  127. uiActions.showTimeLogSel(req, res, axios, 'update');
  128. break;
  129. }
  130. });
  131. app.post('/createWP', (req, res) => {
  132. console.log("Request to createWP: ", JSON.stringify(req.body, null, 2));
  133. uiActions.showSelProject(req, res, axios, "createWP");
  134. });
  135. app.post('/saveWP', (req, res) => {
  136. console.log("Work package save request: ", JSON.stringify(req.body, null, 2));
  137. uiActions.saveWP(req, res, axios);
  138. })
  139. app.post('/delWP', (req, res) => {
  140. console.log("Work package delete request: ", JSON.stringify(req.body, null, 2));
  141. switch (req.body.context.action) {
  142. case 'delWP':
  143. uiActions.delWP(req, res, axios);
  144. break;
  145. default:
  146. uiActions.showDelWPSel(req, res, axios, 'update');
  147. break;
  148. }
  149. });
  150. app.post('/notifyChannel', (req, res) => {
  151. console.log("Notify channel request: ", JSON.stringify(req.body, null, 2));
  152. uiActions.notifyChannel(req, res, axios);
  153. });
  154. app.post('/subscribe', (req, res) => {
  155. console.log("Request to subscribe handler: ", JSON.stringify(req.body, null, 2));
  156. uiActions.notificationSubscribe(req, res, axios);
  157. });
  158. app.post('/bye', (req, res) => {
  159. console.log("Request to showBye handler: ", JSON.stringify(req.body, null, 2));
  160. uiActions.showByeMsg(req, res, 'update');
  161. });
  162. }