system-messages.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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', ($http, $scope) => {
  16. $scope.messages = []
  17. function pollSystemMessages() {
  18. // Ignore polling if tab is hidden or browser is offline
  19. if (document.hidden || !navigator.onLine) {
  20. return
  21. }
  22. $http
  23. .get('/system/messages')
  24. .then(response => {
  25. // Ignore if content-type is anything but JSON, prevents a bug where
  26. // the user logs out in another tab, then a 302 redirect was returned,
  27. // which is transparently resolved by the browser to the login (HTML)
  28. // page.
  29. // This then caused an Angular error where it was attempting to loop
  30. // through the HTML as a string
  31. if (response.headers('content-type').includes('json')) {
  32. $scope.messages = response.data
  33. }
  34. })
  35. .catch(() => {
  36. // ignore errors
  37. })
  38. }
  39. pollSystemMessages()
  40. setInterval(pollSystemMessages, MESSAGE_POLL_INTERVAL)
  41. })
  42. export default App.controller(
  43. 'SystemMessageController',
  44. function ($scope, $sce) {
  45. $scope.hidden = $.localStorage(`systemMessage.hide.${$scope.message._id}`)
  46. $scope.protected = $scope.message._id === 'protected'
  47. $scope.htmlContent = $scope.message.content
  48. return ($scope.hide = function () {
  49. if (!$scope.protected) {
  50. // do not allow protected messages to be hidden
  51. $scope.hidden = true
  52. return $.localStorage(`systemMessage.hide.${$scope.message._id}`, true)
  53. }
  54. })
  55. }
  56. )