system-messages.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /* eslint-disable
  2. max-len,
  3. no-return-assign,
  4. */
  5. // TODO: This file was created by bulk-decaffeinate.
  6. // Fix any style issues and re-enable lint.
  7. /*
  8. * decaffeinate suggestions:
  9. * DS102: Remove unnecessary code created because of implicit returns
  10. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  11. */
  12. import App from '../base'
  13. const MESSAGE_POLL_INTERVAL = 15 * 60 * 1000
  14. // Controller for messages (array)
  15. App.controller('SystemMessagesController', [
  16. '$http',
  17. '$scope',
  18. function ($http, $scope) {
  19. $scope.messages = []
  20. function pollSystemMessages() {
  21. // Ignore polling if tab is hidden or browser is offline
  22. if (document.hidden || !navigator.onLine) {
  23. return
  24. }
  25. $http
  26. .get('/system/messages')
  27. .then(response => {
  28. // Ignore if content-type is anything but JSON, prevents a bug where
  29. // the user logs out in another tab, then a 302 redirect was returned,
  30. // which is transparently resolved by the browser to the login (HTML)
  31. // page.
  32. // This then caused an Angular error where it was attempting to loop
  33. // through the HTML as a string
  34. if (response.headers('content-type').includes('json')) {
  35. $scope.messages = response.data
  36. }
  37. })
  38. .catch(() => {
  39. // ignore errors
  40. })
  41. }
  42. pollSystemMessages()
  43. setInterval(pollSystemMessages, MESSAGE_POLL_INTERVAL)
  44. },
  45. ])
  46. export default App.controller('SystemMessageController', [
  47. '$scope',
  48. function ($scope) {
  49. $scope.hidden = $.localStorage(`systemMessage.hide.${$scope.message._id}`)
  50. $scope.protected = $scope.message._id === 'protected'
  51. $scope.htmlContent = $scope.message.content
  52. return ($scope.hide = function () {
  53. if (!$scope.protected) {
  54. // do not allow protected messages to be hidden
  55. $scope.hidden = true
  56. return $.localStorage(`systemMessage.hide.${$scope.message._id}`, true)
  57. }
  58. })
  59. },
  60. ])