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.
 
 
 
 
 
 

81 lines
2.8 KiB

  1. # Copyright 2014-2016 OpenMarket Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import logging
  15. from typing import TYPE_CHECKING, Callable, Dict, Optional
  16. from synapse.push import Pusher, PusherConfig
  17. from synapse.push.emailpusher import EmailPusher
  18. from synapse.push.httppusher import HttpPusher
  19. from synapse.push.mailer import Mailer
  20. if TYPE_CHECKING:
  21. from synapse.server import HomeServer
  22. logger = logging.getLogger(__name__)
  23. class PusherFactory:
  24. def __init__(self, hs: "HomeServer"):
  25. self.hs = hs
  26. self.config = hs.config
  27. self.pusher_types: Dict[str, Callable[[HomeServer, PusherConfig], Pusher]] = {
  28. "http": HttpPusher
  29. }
  30. logger.info("email enable notifs: %r", hs.config.email.email_enable_notifs)
  31. if hs.config.email.email_enable_notifs:
  32. self.mailers: Dict[str, Mailer] = {}
  33. self._notif_template_html = hs.config.email.email_notif_template_html
  34. self._notif_template_text = hs.config.email.email_notif_template_text
  35. self.pusher_types["email"] = self._create_email_pusher
  36. logger.info("defined email pusher type")
  37. def create_pusher(self, pusher_config: PusherConfig) -> Optional[Pusher]:
  38. kind = pusher_config.kind
  39. f = self.pusher_types.get(kind, None)
  40. if not f:
  41. return None
  42. logger.debug("creating %s pusher for %r", kind, pusher_config)
  43. return f(self.hs, pusher_config)
  44. def _create_email_pusher(
  45. self, _hs: "HomeServer", pusher_config: PusherConfig
  46. ) -> EmailPusher:
  47. app_name = self._app_name_from_pusherdict(pusher_config)
  48. mailer = self.mailers.get(app_name)
  49. if not mailer:
  50. mailer = Mailer(
  51. hs=self.hs,
  52. app_name=app_name,
  53. template_html=self._notif_template_html,
  54. template_text=self._notif_template_text,
  55. )
  56. self.mailers[app_name] = mailer
  57. return EmailPusher(self.hs, pusher_config, mailer)
  58. def _app_name_from_pusherdict(self, pusher_config: PusherConfig) -> str:
  59. data = pusher_config.data
  60. if isinstance(data, dict):
  61. brand = data.get("brand")
  62. if isinstance(brand, str):
  63. return brand
  64. return self.config.email.email_app_name