pusher.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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.pusher_types = {"http": HttpPusher}
  33. logger.info("email enable notifs: %r", hs.config.email_enable_notifs)
  34. if hs.config.email_enable_notifs:
  35. self.mailers = {} # app_name -> Mailer
  36. templates = load_jinja2_templates(
  37. config=hs.config,
  38. template_html_name=hs.config.email_notif_template_html,
  39. template_text_name=hs.config.email_notif_template_text,
  40. )
  41. self.notif_template_html, self.notif_template_text = templates
  42. self.pusher_types["email"] = self._create_email_pusher
  43. logger.info("defined email pusher type")
  44. def create_pusher(self, pusherdict):
  45. kind = pusherdict["kind"]
  46. f = self.pusher_types.get(kind, None)
  47. if not f:
  48. return None
  49. logger.debug("creating %s pusher for %r", kind, pusherdict)
  50. return f(self.hs, pusherdict)
  51. def _create_email_pusher(self, _hs, pusherdict):
  52. app_name = self._app_name_from_pusherdict(pusherdict)
  53. mailer = self.mailers.get(app_name)
  54. if not mailer:
  55. mailer = Mailer(
  56. hs=self.hs,
  57. app_name=app_name,
  58. template_html=self.notif_template_html,
  59. template_text=self.notif_template_text,
  60. )
  61. self.mailers[app_name] = mailer
  62. return EmailPusher(self.hs, pusherdict, mailer)
  63. def _app_name_from_pusherdict(self, pusherdict):
  64. if "data" in pusherdict and "brand" in pusherdict["data"]:
  65. app_name = pusherdict["data"]["brand"]
  66. else:
  67. app_name = self.hs.config.email_app_name
  68. return app_name