pusher.py 2.4 KB

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