From 88ec312b21671f57a8e383ed40668087c2d9275e Mon Sep 17 00:00:00 2001 From: Ujjawal Modi Date: Sat, 5 Aug 2023 18:20:19 +0530 Subject: [PATCH] events: Send invites changes event to non-admin users also. Earlier whenever a new invitation is created a event was sent to only admin users. So, if invites by a non-admins user are changed the invite panel does not live update. This commit makes changes to also send event to non-admin user if invites by them are changed. --- api_docs/changelog.md | 5 +++++ version.py | 2 +- zerver/actions/create_user.py | 2 +- zerver/actions/invites.py | 17 +++++++++++------ zerver/openapi/zulip.yaml | 8 ++++++-- 5 files changed, 24 insertions(+), 10 deletions(-) diff --git a/api_docs/changelog.md b/api_docs/changelog.md index 019911374a..83ede5b2c7 100644 --- a/api_docs/changelog.md +++ b/api_docs/changelog.md @@ -42,6 +42,11 @@ format used by the Zulip server that they are interacting with. reusable invitation links they have created. Previously, only admin users could create and revoke reusable invitation links. +* [`GET /events`](/api/get-events): When the set of invitations in an + organization changes, an `invites_changed` event is now sent to the + creator of the changed invitation, as well as all admin users. + Previously, this event was only sent to admin users. + **Feature level 208** * [`POST /users/me/subscriptions`](/api/subscribe), diff --git a/version.py b/version.py index f57744c92b..734b4932ae 100644 --- a/version.py +++ b/version.py @@ -33,7 +33,7 @@ DESKTOP_WARNING_VERSION = "5.9.3" # Changes should be accompanied by documentation explaining what the # new level means in api_docs/changelog.md, as well as "**Changes**" # entries in the endpoint's documentation in `zulip.yaml`. -API_FEATURE_LEVEL = 208 +API_FEATURE_LEVEL = 209 # Bump the minor PROVISION_VERSION to indicate that folks should provision # only when going from an old version of the code to a newer version. Bump diff --git a/zerver/actions/create_user.py b/zerver/actions/create_user.py index 9f7b220123..4da6872adc 100644 --- a/zerver/actions/create_user.py +++ b/zerver/actions/create_user.py @@ -273,7 +273,7 @@ def process_new_human_user( ).update(status=confirmation_settings.STATUS_REVOKED) if prereg_user is not None and prereg_user.referred_by is not None: - notify_invites_changed(user_profile.realm) + notify_invites_changed(user_profile.realm, changed_invite_referrer=prereg_user.referred_by) notify_new_user(user_profile) # Clear any scheduled invitation emails to prevent them diff --git a/zerver/actions/invites.py b/zerver/actions/invites.py index fbaf23a89c..607832703e 100644 --- a/zerver/actions/invites.py +++ b/zerver/actions/invites.py @@ -36,10 +36,15 @@ from zerver.models import ( from zerver.tornado.django_api import send_event -def notify_invites_changed(realm: Realm) -> None: +def notify_invites_changed( + realm: Realm, *, changed_invite_referrer: Optional[UserProfile] = None +) -> None: event = dict(type="invites_changed") admin_ids = [user.id for user in realm.get_admin_users_and_bots()] - send_event(realm, event, admin_ids) + recipient_ids = admin_ids + if changed_invite_referrer and changed_invite_referrer.id not in recipient_ids: + recipient_ids.append(changed_invite_referrer.id) + send_event(realm, event, recipient_ids) def do_send_confirmation_email( @@ -321,7 +326,7 @@ def do_invite_users( skipped, sent_invitations=True, ) - notify_invites_changed(user_profile.realm) + notify_invites_changed(user_profile.realm, changed_invite_referrer=user_profile) def get_invitation_expiry_date(confirmation_obj: Confirmation) -> Optional[int]: @@ -446,7 +451,7 @@ def do_create_multiuse_invite_link( invite.streams.set(streams) invite.invited_as = invited_as invite.save() - notify_invites_changed(referred_by.realm) + notify_invites_changed(referred_by.realm, changed_invite_referrer=referred_by) return create_confirmation_link( invite, Confirmation.MULTIUSE_INVITE, validity_in_minutes=invite_expires_in_minutes ) @@ -466,7 +471,7 @@ def do_revoke_user_invite(prereg_user: PreregistrationUser) -> None: Confirmation.objects.filter(content_type=content_type, object_id=prereg_user.id).delete() prereg_user.delete() clear_scheduled_invitation_emails(email) - notify_invites_changed(realm) + notify_invites_changed(realm, changed_invite_referrer=prereg_user.referred_by) def do_revoke_multi_use_invite(multiuse_invite: MultiuseInvite) -> None: @@ -479,7 +484,7 @@ def do_revoke_multi_use_invite(multiuse_invite: MultiuseInvite) -> None: ).delete() multiuse_invite.status = confirmation_settings.STATUS_REVOKED multiuse_invite.save(update_fields=["status"]) - notify_invites_changed(realm) + notify_invites_changed(realm, changed_invite_referrer=multiuse_invite.referred_by) def do_resend_user_invite_email(prereg_user: PreregistrationUser) -> int: diff --git a/zerver/openapi/zulip.yaml b/zerver/openapi/zulip.yaml index 6fd106cd5a..030f2cff02 100644 --- a/zerver/openapi/zulip.yaml +++ b/zerver/openapi/zulip.yaml @@ -955,10 +955,14 @@ paths: } - type: object description: | - A simple event sent to organization administrators when the - set of invitations changes; this tells clients they need to refetch + A simple event sent when the set of invitations changes. + This event is sent to organization administrators and the creator of + the changed invitation; this tells clients they need to refetch data from `GET /invites` if they are displaying UI containing active invitations. + + **Changes**: Before Zulip 8.0 (feature level 209), this event was + only sent to organization administrators. properties: id: $ref: "#/components/schemas/EventIdSchema"