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.

notification.service.js 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import { remote } from '../utils/db.js'
  2. import { currentProfile } from '../services/login.service.js'
  3. /**
  4. * Base notifier class
  5. * @param {number} profileId needed to listen for events for this profile
  6. */
  7. class Toaster {
  8. constructor(profileId) {
  9. this.url = `${remote}/notification/${profileId}/subscribe`
  10. this.source = null
  11. this.source = new EventSource(this.url)
  12. this.listenFor('end', message => this.source.close)
  13. }
  14. listenFor(event, callback) {
  15. this.source.addEventListener(event, callback)
  16. }
  17. stop() {
  18. this.source.close()
  19. }
  20. }
  21. /**
  22. * Example extension that listens for 'stonk' events
  23. */
  24. class StonkAlert extends Toaster {
  25. constructor(profileId, waveCb) {
  26. super(profileId)
  27. this.event = 'stonk'
  28. this.stonks = {}
  29. this.listenFor(`${profileId}.${this.event}`, message => {
  30. const parsed = JSON.parse(message.data)
  31. if (parsed.name === 'REVEALED_INFO') {
  32. this._appendTagsToGrouping(parsed)
  33. }
  34. this.stonks[parsed.name] = parsed
  35. waveCb(this._formatToast(parsed), parsed.type)
  36. })
  37. }
  38. _formatToast(parsed) {
  39. if (parsed.revealed_info) {
  40. return `${parsed.name}: ${parsed.revealed_info} at ${parsed.type}: ${parsed.url}`
  41. } else if (parsed.url) {
  42. return `${parsed.name}: ${parsed.profile_id}: visit: ${parsed.url}`
  43. } else {
  44. return `${parsed.name}: ${parsed.profile_id} ${parsed.order} at ${parsed.price}`
  45. }
  46. }
  47. _appendTagsToGrouping(parsed) {
  48. const foundGrouping = currentProfile.groupings.find(
  49. grouping => grouping.grouping_id === parsed.grouping_id,
  50. )
  51. if (foundGrouping) {
  52. const tagFromNotification = {
  53. is_active: 1,
  54. tag_category: 'reveal',
  55. profile_id: parsed.profile_id,
  56. tag_description: parsed.description,
  57. tag_id: parsed.tag,
  58. }
  59. const target_desc = parsed.description
  60. tagFromNotification[target_desc] = parsed.revealed_info
  61. foundGrouping.profile.reveal.push(tagFromNotification)
  62. foundGrouping.revealedFromNotification.value.push(
  63. tagFromNotification,
  64. )
  65. }
  66. }
  67. }
  68. export { Toaster, StonkAlert }