pusher.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 .httppusher import HttpPusher
  17. logger = logging.getLogger(__name__)
  18. # We try importing this if we can (it will fail if we don't
  19. # have the optional email dependencies installed). We don't
  20. # yet have the config to know if we need the email pusher,
  21. # but importing this after daemonizing seems to fail
  22. # (even though a simple test of importing from a daemonized
  23. # process works fine)
  24. try:
  25. from synapse.push.emailpusher import EmailPusher
  26. from synapse.push.mailer import Mailer, load_jinja2_templates
  27. except Exception:
  28. pass
  29. class PusherFactory(object):
  30. def __init__(self, hs):
  31. self.hs = hs
  32. self.config = hs.config
  33. self.pusher_types = {"http": HttpPusher}
  34. logger.info("email enable notifs: %r", hs.config.email_enable_notifs)
  35. if hs.config.email_enable_notifs:
  36. self.mailers = {} # app_name -> Mailer
  37. self.notif_template_html, self.notif_template_text = load_jinja2_templates(
  38. self.config.email_template_dir,
  39. [
  40. self.config.email_notif_template_html,
  41. self.config.email_notif_template_text,
  42. ],
  43. apply_format_ts_filter=True,
  44. apply_mxc_to_http_filter=True,
  45. public_baseurl=self.config.public_baseurl,
  46. )
  47. self.pusher_types["email"] = self._create_email_pusher
  48. logger.info("defined email pusher type")
  49. def create_pusher(self, pusherdict):
  50. kind = pusherdict["kind"]
  51. f = self.pusher_types.get(kind, None)
  52. if not f:
  53. return None
  54. logger.debug("creating %s pusher for %r", kind, pusherdict)
  55. return f(self.hs, pusherdict)
  56. def _create_email_pusher(self, _hs, pusherdict):
  57. app_name = self._app_name_from_pusherdict(pusherdict)
  58. mailer = self.mailers.get(app_name)
  59. if not mailer:
  60. mailer = Mailer(
  61. hs=self.hs,
  62. app_name=app_name,
  63. template_html=self.notif_template_html,
  64. template_text=self.notif_template_text,
  65. )
  66. self.mailers[app_name] = mailer
  67. return EmailPusher(self.hs, pusherdict, mailer)
  68. def _app_name_from_pusherdict(self, pusherdict):
  69. data = pusherdict["data"]
  70. if isinstance(data, dict):
  71. brand = data.get("brand")
  72. if isinstance(brand, str):
  73. return brand
  74. return self.config.email_app_name