pusher.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. from .httppusher import HttpPusher
  16. import logging
  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 = {
  33. "http": HttpPusher,
  34. }
  35. logger.info("email enable notifs: %r", hs.config.email_enable_notifs)
  36. if hs.config.email_enable_notifs:
  37. self.mailers = {} # app_name -> Mailer
  38. templates = load_jinja2_templates(hs.config)
  39. self.notif_template_html, self.notif_template_text = templates
  40. self.pusher_types["email"] = self._create_email_pusher
  41. logger.info("defined email pusher type")
  42. def create_pusher(self, pusherdict):
  43. logger.info("trying to create_pusher for %r", pusherdict)
  44. if pusherdict['kind'] in self.pusher_types:
  45. logger.info("found pusher")
  46. return self.pusher_types[pusherdict['kind']](self.hs, pusherdict)
  47. def _create_email_pusher(self, _hs, pusherdict):
  48. app_name = self._app_name_from_pusherdict(pusherdict)
  49. mailer = self.mailers.get(app_name)
  50. if not mailer:
  51. mailer = Mailer(
  52. hs=self.hs,
  53. app_name=app_name,
  54. notif_template_html=self.notif_template_html,
  55. notif_template_text=self.notif_template_text,
  56. )
  57. self.mailers[app_name] = mailer
  58. return EmailPusher(self.hs, pusherdict, mailer)
  59. def _app_name_from_pusherdict(self, pusherdict):
  60. if 'data' in pusherdict and 'brand' in pusherdict['data']:
  61. app_name = pusherdict['data']['brand']
  62. else:
  63. app_name = self.hs.config.email_app_name
  64. return app_name