Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

users: Add is_deleted field to UserProfile and expose in API.

Agnish Bauri54011fdeec0a
diff --git a/api_docs/changelog.md b/api_docs/changelog.md
index 0a6a15b7aad..d7a7cca9e8a 100644
--- a/api_docs/changelog.md
+++ b/api_docs/changelog.md
@@ -20,6 +20,18 @@ format used by the Zulip server that they are interacting with.
## Changes in Zulip 12.0
+**Feature level 490**
+
+* [`GET /users`](/api/get-users), [`GET /users/{user_id}`](/api/get-user),
+ [`GET /users/{email}`](/api/get-user-by-email),
+ [`GET /users/me`](/api/get-own-user): Added `is_deleted` field to
+ returned user objects.
+* [`POST /register`](/api/register-queue): Added `is_deleted` field
+ in the user objects returned in the `realm_users` and
+ `realm_non_active_users` fields.
+* [`GET /events`](/api/get-events): Added `is_deleted` field to
+ user objects sent in `realm_user` events.
+
**Feature level 489**
* [`GET /messages`](/api/get-messages),
diff --git a/version.py b/version.py
index 5af02ba9191..a34590c28ff 100644
--- a/version.py
+++ b/version.py
@@ -33,7 +33,7 @@ DESKTOP_WARNING_VERSION = "5.9.3"
# https://zulip.readthedocs.io/en/latest/documentation/api.html#step-by-step-guide
# Also available at docs/documentation/api.md.
-API_FEATURE_LEVEL = 489
+API_FEATURE_LEVEL = 490
# 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/web/src/state_data.ts b/web/src/state_data.ts
index c6f121a0874..d47b51a81ab 100644
--- a/web/src/state_data.ts
+++ b/web/src/state_data.ts
@@ -216,6 +216,7 @@ export const user_schema = z.intersection(
is_owner: z.boolean(),
is_admin: z.boolean(),
is_guest: z.boolean(),
+ is_deleted: z.optional(z.literal(true)),
is_moderator: z.optional(z.boolean()),
role: z.number(),
timezone: z.optional(z.string()),
diff --git a/zerver/actions/create_user.py b/zerver/actions/create_user.py
index 564f1a485d2..55848cbb00d 100644
--- a/zerver/actions/create_user.py
+++ b/zerver/actions/create_user.py
@@ -712,6 +712,10 @@ def do_reactivate_user(user_profile: UserProfile, *, acting_user: UserProfile |
raise JsonableError(
_("Cannot activate a placeholder account; ask the user to sign up, instead.")
)
+
+ if user_profile.is_deleted: # nocoverage
+ raise JsonableError(_("Cannot reactivate a deleted user"))
+
change_user_is_active(user_profile, True)
event_time = timezone_now()
diff --git a/zerver/actions/users.py b/zerver/actions/users.py
index aabdfaa67b5..3be5a98f257 100644
--- a/zerver/actions/users.py
+++ b/zerver/actions/users.py
@@ -143,6 +143,7 @@ def do_delete_user_core(
full_name=f"Deleted User {user_id}",
bot_owner=user_profile.bot_owner,
is_mirror_dummy=True,
+ is_deleted=True,
tos_version=user_profile.tos_version,
# These required arguments should get default values configured on the UserProfile model,
# to overwrite the values of user_profile.
diff --git a/zerver/lib/cache.py b/zerver/lib/cache.py
index 580c8197201..6ab1b604d20 100644
--- a/zerver/lib/cache.py
+++ b/zerver/lib/cache.py
@@ -536,6 +536,7 @@ realm_user_dict_fields: list[str] = [
"long_term_idle",
"email_address_visibility",
"is_imported_stub",
+ "is_deleted",
]
diff --git a/zerver/lib/create_user.py b/zerver/lib/create_user.py
index ada3039aea1..b7e59639801 100644
--- a/zerver/lib/create_user.py
+++ b/zerver/lib/create_user.py
@@ -114,6 +114,7 @@ def create_user_profile(
tos_version: str | None,
timezone: str,
default_language: str,
+ is_deleted: bool = False,
force_date_joined: datetime | None = None,
is_imported_stub: bool = False,
*,
@@ -137,6 +138,7 @@ def create_user_profile(
bot_type=bot_type,
bot_owner=bot_owner,
is_mirror_dummy=is_mirror_dummy,
+ is_deleted=is_deleted,
tos_version=tos_version,
timezone=timezone,
default_language=default_language,
@@ -166,6 +168,7 @@ def create_user(
timezone: str = "",
avatar_source: str | None = None,
is_mirror_dummy: bool = False,
+ is_deleted: bool = False,
default_language: str | None = None,
default_sending_stream: Stream | None = None,
default_events_register_stream: Stream | None = None,
@@ -204,6 +207,7 @@ def create_user(
tos_version,
timezone,
default_language,
+ is_deleted,
force_date_joined=force_date_joined,
email_address_visibility=user_email_address_visibility,
)
diff --git a/zerver/lib/types.py b/zerver/lib/types.py
index b482b3983c3..842fe0ec2a8 100644
--- a/zerver/lib/types.py
+++ b/zerver/lib/types.py
@@ -410,6 +410,7 @@ class RawUserDict(TypedDict):
long_term_idle: bool
email_address_visibility: int
is_imported_stub: bool
+ is_deleted: bool
class RemoteRealmDictValue(TypedDict):
diff --git a/zerver/lib/users.py b/zerver/lib/users.py
index 6571ba84c28..ea47fb14f7f 100644
--- a/zerver/lib/users.py
+++ b/zerver/lib/users.py
@@ -599,6 +599,7 @@ class APIUserDict(TypedDict):
is_system_bot: NotRequired[bool]
max_message_id: NotRequired[int]
is_imported_stub: bool
+ is_deleted: NotRequired[bool]
def format_user_row(
@@ -646,6 +647,9 @@ def format_user_row(
is_imported_stub=row["is_imported_stub"],
)
+ if row["is_deleted"]:
+ result["is_deleted"] = True
+
if acting_user is None:
# Remove data about other users which are not useful to spectators
# or can reveal personal information about a user.
@@ -1045,6 +1049,7 @@ def user_profile_to_user_row(user_profile: UserProfile) -> RawUserDict:
long_term_idle=user_profile.long_term_idle,
email_address_visibility=user_profile.email_address_visibility,
is_imported_stub=user_profile.is_imported_stub,
+ is_deleted=user_profile.is_deleted,
)
diff --git a/zerver/migrations/0797_userprofile_is_deleted.py b/zerver/migrations/0797_userprofile_is_deleted.py
new file mode 100644
index 00000000000..6c252cd56ef
--- /dev/null
+++ b/zerver/migrations/0797_userprofile_is_deleted.py
@@ -0,0 +1,17 @@
+# Generated by Django 5.2.11 on 2026-03-08 09:22
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+ dependencies = [
+ ("zerver", "0796_fix_corrupted_named_user_group_creator"),
+ ]
+
+ operations = [
+ migrations.AddField(
+ model_name="userprofile",
+ name="is_deleted",
+ field=models.BooleanField(db_default=False, default=False),
+ ),
+ ]
diff --git a/zerver/models/users.py b/zerver/models/users.py
index 213078383d0..0163e3a5f0a 100644
--- a/zerver/models/users.py
+++ b/zerver/models/users.py
@@ -549,6 +549,11 @@ class UserProfile(AbstractBaseUser, PermissionsMixin, UserBaseSettings):
# See also `long_term_idle`.
is_active = models.BooleanField(default=True, db_index=True)
+ # Deleted users are a subset of deactivated users whose metadata
+ # has been removed as well. They cannot be reactivated, to ensure
+ # is_deleted=True, is_active=True is impossible.
+ is_deleted = models.BooleanField(db_default=False, default=False)
+
is_bot = models.BooleanField(default=False, db_index=True)
bot_type = models.PositiveSmallIntegerField(null=True, db_index=True)
bot_owner = models.ForeignKey("self", null=True, on_delete=models.SET_NULL)
diff --git a/zerver/openapi/zulip.yaml b/zerver/openapi/zulip.yaml
index b65efd14e63..37e08652425 100644
--- a/zerver/openapi/zulip.yaml
+++ b/zerver/openapi/zulip.yaml
@@ -29091,6 +29091,7 @@ components:
nullable: true
avatar_version: {}
is_imported_stub: {}
+ is_deleted: {}
profile_data:
$ref: "#/components/schemas/ProfileData"
UserBase:
@@ -29252,6 +29253,16 @@ components:
**Changes**: New in Zulip 12.0 (feature level 433).
example: false
+ is_deleted:
+ type: boolean
+ description: |
+ A boolean specifying whether the user has been permanently
+ deleted. Deleted users are a subset of deactivated users
+ (`is_active=false`) who have had their account data removed.
+
+ This field is only present when `true`.
+
+ **Changes**: New in Zulip 12.0 (feature level 490).
ProfileData:
type: object
description: |
diff --git a/zerver/tests/test_users.py b/zerver/tests/test_users.py
index 07be00a88b2..6d5e7f91e1c 100644
--- a/zerver/tests/test_users.py
+++ b/zerver/tests/test_users.py
@@ -3989,6 +3989,7 @@ class DeleteUserTest(ZulipTestCase):
self.assertEqual(hamlet.delivery_email, f"deleteduser{hamlet_user_id}@zulip.testserver")
self.assertEqual(hamlet.is_mirror_dummy, True)
+ self.assertEqual(hamlet.is_deleted, True)
self.assertEqual(hamlet.is_active, False)
self.assertEqual(hamlet.date_joined, hamlet_date_joined)
@@ -4028,6 +4029,11 @@ class DeleteUserTest(ZulipTestCase):
self.assertEqual(ScheduledEmail.objects.count(), 0)
+ # Verify is_deleted is exposed in the API.
+ self.login("iago")
+ result = orjson.loads(self.client_get(f"/json/users/{hamlet_user_id}").content)
+ self.assertEqual(result["user"]["is_deleted"], True)
+
@override_settings(PREFER_DIRECT_MESSAGE_GROUP=True)
def test_do_delete_user_preserving_messages(self) -> None:
"""