Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Push notification via celery on post creation #207

Merged
merged 4 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion feeds/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@

from feeds.models import Comment


check_org_email = import_string(settings.CHECK_ORG_EMAIL)
PendingEmail = import_string(settings.PENDING_EMAIL)
EMAIL_TYPE = import_string(settings.EMAIL_TYPE)
TEMPLATE_MODEL = import_string(settings.TEMPLATE_MODEL)
CustomUser = import_string(settings.CUSTOM_USER_MODEL)


logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -53,3 +53,14 @@ def notify_user_via_email(self, comment_id):
body=email_body,
email_type=EMAIL_TYPE.html
)


@shared_task(bind=True)
def notify_user_via_push_notification(self, creator_id, message, user_ids, object_type, poll_id, extra_context):
from feeds.utils import push_notification

creator = CustomUser.objects.get(id=creator_id)
accessible_users = CustomUser.objects.filter(id__in=user_ids)
for usr in accessible_users:
push_notification(creator, message, usr, object_type=object_type, object_id=poll_id,
extra_context=extra_context)
15 changes: 10 additions & 5 deletions feeds/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

from .constants import POST_TYPE, SHARED_WITH
from .models import Comment, Post
from feeds.tasks import notify_user_via_email
from feeds.tasks import notify_user_via_email, notify_user_via_push_notification


DEPARTMENT_MODEL = import_string(settings.DEPARTMENT_MODEL)
Expand Down Expand Up @@ -307,10 +307,15 @@ def notify_new_post_poll_created(poll, is_post=False):
user_name = get_user_name(creator)
message = _("'%s' created a new post." % user_name) if is_post else _("'%s' started a new poll." % user_name)
object_type = NOTIFICATION_OBJECT_TYPE
accessible_users = list(set(accessible_users))
for usr in accessible_users:
push_notification(creator, message, usr, object_type=object_type, object_id=poll.id,
extra_context={"redirect_screen": "Poll"})

notify_user_via_push_notification.delay(
creator.id,
message,
list({user.id for user in accessible_users}),
object_type,
poll.id,
extra_context={"redirect_screen": "Poll"}
)


def notify_flagged_post(post, user, reason):
Expand Down