subscribe.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. from django.contrib.auth import get_user_model
  2. from misago.threads.models import Subscription
  3. from . import PostingEndpoint, PostingMiddleware
  4. UserModel = get_user_model()
  5. class SubscribeMiddleware(PostingMiddleware):
  6. def use_this_middleware(self):
  7. return self.mode != PostingEndpoint.EDIT
  8. def post_save(self, serializer):
  9. self.subscribe_new_thread()
  10. self.subscribe_replied_thread()
  11. def subscribe_new_thread(self):
  12. if self.mode != PostingEndpoint.START:
  13. return
  14. if self.user.subscribe_to_started_threads == UserModel.SUBSCRIBE_NONE:
  15. return
  16. self.user.subscription_set.create(
  17. category=self.thread.category,
  18. thread=self.thread,
  19. send_email=self.user.subscribe_to_started_threads == UserModel.SUBSCRIBE_ALL,
  20. )
  21. def subscribe_replied_thread(self):
  22. if self.mode != PostingEndpoint.REPLY:
  23. return
  24. if self.user.subscribe_to_replied_threads == UserModel.SUBSCRIBE_NONE:
  25. return
  26. try:
  27. return self.user.subscription_set.get(thread=self.thread)
  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 == UserModel.SUBSCRIBE_ALL,
  37. )