subscribe.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. from misago.users.models import AUTO_SUBSCRIBE_NONE, AUTO_SUBSCRIBE_NOTIFY, AUTO_SUBSCRIBE_NOTIFY_AND_EMAIL
  2. from . import PostingEndpoint, PostingMiddleware
  3. from ...models import Subscription
  4. class SubscribeMiddleware(PostingMiddleware):
  5. def use_this_middleware(self):
  6. return self.mode != PostingEndpoint.EDIT
  7. def post_save(self, serializer):
  8. self.subscribe_new_thread()
  9. self.subscribe_replied_thread()
  10. def subscribe_new_thread(self):
  11. if self.mode != PostingEndpoint.START:
  12. return
  13. if self.user.subscribe_to_started_threads == AUTO_SUBSCRIBE_NONE:
  14. return
  15. self.user.subscription_set.create(
  16. category=self.thread.category,
  17. thread=self.thread,
  18. send_email=self.user.subscribe_to_started_threads == AUTO_SUBSCRIBE_NOTIFY_AND_EMAIL
  19. )
  20. def subscribe_replied_thread(self):
  21. if self.mode != PostingEndpoint.REPLY:
  22. return
  23. if self.user.subscribe_to_replied_threads == AUTO_SUBSCRIBE_NONE:
  24. return
  25. try:
  26. subscription = self.user.subscription_set.get(thread=self.thread)
  27. return
  28. except Subscription.DoesNotExist:
  29. pass
  30. # we are replying to thread again?
  31. if self.user.post_set.filter(thread=self.thread).count() > 1:
  32. return
  33. self.user.subscription_set.create(
  34. category=self.thread.category,
  35. thread=self.thread,
  36. send_email=self.user.subscribe_to_replied_threads == AUTO_SUBSCRIBE_NOTIFY_AND_EMAIL
  37. )