webnotifier.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * MIT License
  3. *
  4. * Copyright (c) 2020 Alexey Edelev <semlanik@gmail.com>
  5. *
  6. * This file is part of gostfix project https://git.semlanik.org/semlanik/gostfix
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy of this
  9. * software and associated documentation files (the "Software"), to deal in the Software
  10. * without restriction, including without limitation the rights to use, copy, modify,
  11. * merge, publish, distribute, sublicense, and/or sell copies of the Software, and
  12. * to permit persons to whom the Software is furnished to do so, subject to the following
  13. * conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in all copies
  16. * or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  19. * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  20. * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  21. * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  22. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. */
  25. package web
  26. import (
  27. "fmt"
  28. "log"
  29. "net/http"
  30. "sync"
  31. "git.semlanik.org/semlanik/gostfix/common"
  32. "github.com/gorilla/websocket"
  33. )
  34. type websocketChannel struct {
  35. connection *websocket.Conn
  36. channel chan *common.Mail
  37. }
  38. type webNotifier struct {
  39. notifiers map[string]*websocketChannel
  40. notifiersLock sync.Mutex
  41. }
  42. func NewWebNotifier() *webNotifier {
  43. return &webNotifier{
  44. notifiers: make(map[string]*websocketChannel),
  45. }
  46. }
  47. func (wn *webNotifier) NotifyMaiboxUpdate(email string) {
  48. if channel, ok := wn.getNotifier(email); ok {
  49. channel.channel <- &common.Mail{} //TODO: Dummy notificator for now, later need to make separate interface to handle this
  50. }
  51. }
  52. func (wn *webNotifier) NotifyNewMail(email string, m common.Mail) {
  53. // if channel, ok := wn.getNotifier(email); ok {
  54. // channel.channel <- &m
  55. // }
  56. //TODO: this functionality needs JS support to create new mails from templates
  57. }
  58. var upgrader = websocket.Upgrader{
  59. ReadBufferSize: 1024,
  60. WriteBufferSize: 1024,
  61. }
  62. func (wn *webNotifier) handleNotifierRequest(w http.ResponseWriter, r *http.Request, email string) {
  63. fmt.Printf("New web socket session start %s\n", email)
  64. conn, err := upgrader.Upgrade(w, r, nil)
  65. if err != nil {
  66. http.Error(w, "Could not open websocket connection", http.StatusBadRequest)
  67. return
  68. }
  69. c := &websocketChannel{
  70. connection: conn,
  71. channel: make(chan *common.Mail, 10),
  72. }
  73. wn.addNotifier(email, c)
  74. conn.SetCloseHandler(func(code int, text string) error {
  75. fmt.Printf("Web socket session end %s\n", email)
  76. wn.removeNotifier(email)
  77. conn.Close()
  78. return nil
  79. })
  80. go wn.handleNotifications(c)
  81. }
  82. func (wn *webNotifier) handleNotifications(c *websocketChannel) {
  83. //Do nothing for now
  84. for {
  85. select {
  86. case newMail := <-c.channel:
  87. err := c.connection.WriteJSON(newMail)
  88. if err != nil {
  89. log.Println(err.Error())
  90. return
  91. }
  92. }
  93. }
  94. }
  95. func (wn *webNotifier) getNotifier(email string) (channel *websocketChannel, ok bool) {
  96. wn.notifiersLock.Lock()
  97. defer wn.notifiersLock.Unlock()
  98. channel, ok = wn.notifiers[email]
  99. return
  100. }
  101. func (wn *webNotifier) addNotifier(email string, channel *websocketChannel) {
  102. wn.notifiersLock.Lock()
  103. defer wn.notifiersLock.Unlock()
  104. wn.notifiers[email] = channel
  105. }
  106. func (wn *webNotifier) removeNotifier(email string) {
  107. wn.notifiersLock.Lock()
  108. defer wn.notifiersLock.Unlock()
  109. delete(wn.notifiers, email)
  110. }