webnotifier.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. "encoding/json"
  28. "fmt"
  29. "log"
  30. "net/http"
  31. "sync"
  32. "git.semlanik.org/semlanik/gostfix/common"
  33. "github.com/gorilla/websocket"
  34. )
  35. type websocketChannel struct {
  36. connection *websocket.Conn
  37. channel chan *common.MailMetadata
  38. }
  39. type webNotifier struct {
  40. server *Server
  41. notifiers map[string]*websocketChannel
  42. notifiersLock sync.Mutex
  43. }
  44. func NewWebNotifier() *webNotifier {
  45. return &webNotifier{
  46. notifiers: make(map[string]*websocketChannel),
  47. }
  48. }
  49. func (wn *webNotifier) NotifyMaiboxUpdate(email string) {
  50. if channel, ok := wn.getNotifier(email); ok {
  51. channel.channel <- &common.MailMetadata{} //TODO: Dummy notificator for now, later need to make separate interface to handle this
  52. }
  53. }
  54. func (wn *webNotifier) NotifyNewMail(email string, m common.MailMetadata) {
  55. if channel, ok := wn.getNotifier(email); ok {
  56. channel.channel <- &m
  57. }
  58. //TODO: this functionality needs JS support to create new mails from templates
  59. }
  60. var upgrader = websocket.Upgrader{
  61. ReadBufferSize: 1024,
  62. WriteBufferSize: 1024,
  63. }
  64. func (wn *webNotifier) handleNotifierRequest(w http.ResponseWriter, r *http.Request, email string) {
  65. fmt.Printf("New web socket session start %s\n", email)
  66. conn, err := upgrader.Upgrade(w, r, nil)
  67. if err != nil {
  68. log.Printf("Could not upgrade websocket %s\n", err)
  69. http.Error(w, "Could not open websocket connection", http.StatusBadRequest)
  70. return
  71. }
  72. c := &websocketChannel{
  73. connection: conn,
  74. channel: make(chan *common.MailMetadata, 10),
  75. }
  76. wn.addNotifier(email, c)
  77. conn.SetCloseHandler(func(code int, text string) error {
  78. fmt.Printf("Web socket session end %s\n", email)
  79. wn.removeNotifier(email)
  80. conn.Close()
  81. return nil
  82. })
  83. go wn.handleNotifications(c)
  84. }
  85. func (wn *webNotifier) handleNotifications(c *websocketChannel) {
  86. for {
  87. select {
  88. case newMail := <-c.channel:
  89. out, err := json.Marshal(&struct {
  90. Type string `json:"type"`
  91. Data interface{} `json:"data"`
  92. }{
  93. Type: "mail",
  94. Data: &struct {
  95. Folder string `json:"folder"`
  96. HTML string `json:"html"`
  97. }{
  98. Folder: newMail.Folder,
  99. HTML: wn.server.templater.ExecuteMailList([]*common.MailMetadata{newMail}),
  100. },
  101. })
  102. err = c.connection.WriteMessage(websocket.TextMessage, out)
  103. if err != nil {
  104. log.Println(err.Error())
  105. return
  106. }
  107. }
  108. }
  109. }
  110. func (wn *webNotifier) getNotifier(email string) (channel *websocketChannel, ok bool) {
  111. wn.notifiersLock.Lock()
  112. defer wn.notifiersLock.Unlock()
  113. channel, ok = wn.notifiers[email]
  114. return
  115. }
  116. func (wn *webNotifier) addNotifier(email string, channel *websocketChannel) {
  117. wn.notifiersLock.Lock()
  118. defer wn.notifiersLock.Unlock()
  119. wn.notifiers[email] = channel
  120. }
  121. func (wn *webNotifier) removeNotifier(email string) {
  122. wn.notifiersLock.Lock()
  123. defer wn.notifiersLock.Unlock()
  124. delete(wn.notifiers, email)
  125. }