Commit Graph

900 Commits

Author SHA1 Message Date
Anders Kaseorg
f24a0a6b81 ruff: Fix RUF059 Unpacked variable is never used.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2025-09-30 16:47:54 -07:00
Sahil Batra
764f4aa2e0 groups: Use realm_for_sharding for limiting NamedUserGroup queries.
For get and filter queries of NamedUserGroup, realm_for_sharding
field is used instead of realm field, as directly using
realm_for_sharding field on NamedUserGroup makes the query faster
than using realm present on the base UserGroup table.
2025-09-23 12:15:53 -07:00
Anders Kaseorg
40a022dcc3 zephyr: Remove Zephyr mirroring support.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2025-09-16 11:18:18 -07:00
Mohammad Reza Kianifar
2aad468807 analytics: Support DM groups in count message type query.
To maintain API compatibility during and after the migration to direct
message groups for 1:1 and self messages, update the analytics count
query to treat DM groups of size 2 or less as private messages.
2025-07-21 10:39:40 -07:00
Alex Vandiver
3e5af466e4 push_notifications: Remove vestiges of base64 storage of tokens.
APNs tokens are provided by the client in hex, and we store them in
hex.  The existing code which attempts to "validate" them by parsing
them as base64 only works because base64 is a superset of hex.

Enforce that APNs tokens are hex, and remove all of the pieces of test
code which were incorrectly passing them in as base64 strings.
2025-07-16 16:59:58 -07:00
Alex Vandiver
a5a5791794 management: Skip hourly crons during deploys.
This is most important for `send_zulip_update_announcements`, which
can race with the version run as a post-deploy hook.  However, all of
these crons can tolerate being slightly delayed, and there's little
benefit to them taking CPU or possibly hitting odd borderline race
conditions when the deploy is in progress.

For safety, we only trust the deploy lockfile if it was created
within the last hour -- deploys should not take more than an hour, and
failing to ever run hourly crons is much worse than perhaps running
them during a real very-long deploy.
2025-07-15 09:07:36 -07:00
Lauryn Menard
4e97a6d80e analytics: Get user by delivery email when creating users in test set up.
In the analytics tests, where users are created as part of the set up
of the test, we want to get users by the delivery email and not the
visible-to-other-users email field.

Prep for setting default new user email visibility in realm creation
by the organization type.
2025-07-08 09:30:39 -07:00
Anders Kaseorg
e8fdae8f7b ruff: Fix PLR1733 Unnecessary lookup of dictionary value by key.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2025-06-25 11:49:02 -07:00
Lauryn Menard
3aadb86225 analytics: Update labels for mobile apps for Flutter launch.
Updates the labels in the"Messages set by client" chart so that
the Flutter app is no longer labeled as "beta".

The React Native app is now labeled as "old". And the older
Android and iOS apps are now labeled as "ancient".
2025-06-13 17:00:55 -07:00
bedo
c04558fe31 stream: Add subscriber_count field.
Fixes #34246.

Add subscriber_count field to Stream model to track number of
non-deactivated users subscribed to the channel.
2025-05-13 17:36:53 -07:00
Mateusz Mandera
74019706d7 populate_analytics_db: Create missing system group memberships. 2025-05-06 12:54:35 -07:00
Tim Abbott
e92d68fffe streams: Remove require_active in access_stream code path.
This was confusingly doing an assertion about the subscription being
active, not the channel. We could rename it to
require_active_subscription. But it was only passed with a non-default
value in b2cb443d24, and that call was
removed in 378062cc83.
2025-03-07 18:08:41 -08:00
Anders Kaseorg
22de8cfea3 counts: Fix current_month_accumulated_count_for_user bounds.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2025-02-28 15:15:21 -08:00
Mateusz Mandera
517538a296 management: Don't silence send_server_data_to_push_bouncer exceptions.
When these exceptions are thrown from the request-to-bouncer functions
inside of manage.py register_server/update_analytics_counts, they
shouldn't be silenced, merely calling maybe_mark_pushes_disabled in the
background.
This results in the occurrence of the error not being shown to the
user. Failure to upload analytics data when running these commands
should result in a loud, obvious error.

Failure of running register_server before this change:
```
./manage.py register_server
This command registers your server for the Mobile Push Notifications Service.
Doing so will share basic metadata with the service's maintainers:

* This server's configured hostname: zulipdev.com:9991
* This server's configured contact email address: desdemona+admin@zulip.com
* Metadata about each organization hosted by the server; see:

    <https://zulip.com/doc-permalinks/basic-metadata>

Use of this service is governed by the Zulip Terms of Service:

    <https://zulip.com/policies/terms>

Do you want to agree to the Zulip Terms of Service and proceed? [Y/n]

Mobile Push Notification Service registration successfully updated!
```

The occurrence of the error is not revealed to the user. Same concern
applies to the update_analytics_counts command.

After this change:
```
./manage.py register_server
This command registers your server for the Mobile Push Notifications Service.
Doing so will share basic metadata with the service's maintainers:
    <...>
Do you want to agree to the Zulip Terms of Service and proceed? [Y/n]

Traceback (most recent call last):
  File "/srv/zulip/./manage.py", line 150, in <module>
    execute_from_command_line(sys.argv)
  File "/srv/zulip/./manage.py", line 115, in execute_from_command_line
    utility.execute()
  File "/srv/zulip-venv-cache/bb36fc1fcb6d8c70a9a0bcb7bac45d78623a9ff4/zulip-py3-venv/lib/python3.10/site-packages/django/core/management/__init__.py", line 436, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/srv/zulip-venv-cache/bb36fc1fcb6d8c70a9a0bcb7bac45d78623a9ff4/zulip-py3-venv/lib/python3.10/site-packages/django/core/management/base.py", line 413, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/srv/zulip/zerver/lib/management.py", line 97, in execute
    super().execute(*args, **options)
  File "/srv/zulip-venv-cache/bb36fc1fcb6d8c70a9a0bcb7bac45d78623a9ff4/zulip-py3-venv/lib/python3.10/site-packages/django/core/management/base.py", line 459, in execute
    output = self.handle(*args, **options)
  File "/srv/zulip/zerver/management/commands/register_server.py", line 137, in handle
    send_server_data_to_push_bouncer(consider_usage_statistics=False, raise_on_error=True)
  File "/srv/zulip/zerver/lib/remote_server.py", line 453, in send_server_data_to_push_bouncer
    response = send_to_push_bouncer(
  File "/srv/zulip/zerver/lib/remote_server.py", line 233, in send_to_push_bouncer
    raise JsonableError(msg)
zerver.lib.exceptions.JsonableError: Duplicate registration detected.
```
2025-02-19 17:11:35 -08:00
Shubham Padia
35f9305acb stream: Modify flag to allow access for users with metadata access. 2025-02-11 15:09:16 -08:00
Aman Agrawal
2f5aea6604 message_summary: Add setting to put a monthly rate limit per user. 2025-02-06 12:03:24 -08:00
Mateusz Mandera
0caf815e36 do_activate_mirror_dummy_user: Assert user_profile is a mirror dummy.
If this is called on a user without is_mirror_dummy=True, that seems
certain to be a bug. Therefore, an assert is preferable in order to
catch this, rather than returning early with noop like some other
function such as do_deactivate_user.
2025-01-28 09:38:56 -08:00
Aman Agrawal
b047c4d322 message_summary: Add API endpoint to generate narrow summary.
This prototype API is disabled in production through settings not
configuring a default model.
2025-01-22 12:12:08 -08:00
Anders Kaseorg
653b0b0436 ruff: Partially reformat Python with Ruff 0.9 (2025 style).
These are the changes that are backwards compatible with the 2024
style.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
2025-01-14 09:42:16 -08:00
Anders Kaseorg
58822372d5 typos: Fix typos caught by typos and mwic.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2024-12-24 19:15:36 -08:00
Shubham Padia
eb943d54a9 streams: Add can_administer_channel_group as a stream setting.
We have not added current user as the default for new channels in this
commit.
2024-12-03 18:38:25 -08:00
Shubham Padia
6dcd4c4ca6 stream: Rename function to get defaults for stream permission groups. 2024-12-02 10:05:53 -08:00
Shubham Padia
9f6764a70a stream: Do not use hardcoded default values for group settings.
We were using admins group as a hardcoded value for the default of
`can_remove_subscribers_group`, now we use a function to get the value
of the default group.
2024-12-01 19:32:22 -08:00
Anders Kaseorg
5b486a74f5 test_counts: Remove mostly unused assert_table_count helper.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2024-10-20 18:16:27 -07:00
Lauryn Menard
10d161638e audit-log: Move subscription event types to AuditLogEventType enum.
Event types moved: SUBSCRIPTION_CREATED, SUBSCRIPTION_ACTIVATED,
SUBSCRIPTION_DEACTIVATED, SUBSCRIPTION_PROPERTY_CHANGED.
2024-09-09 11:50:13 -07:00
Lauryn Menard
caeeaf3c3f audit-log: Move user event types to AuditLogEventType enum.
Event types moved: USER_SOFT_ACTIVATED, USER_SOFT_DEACTIVATED
USER_PASSWORD_CHANGED, USER_AVATAR_SOURCE_CHANGED, USER_FULL_NAME_CHANGED
USER_EMAIL_CHANGED, USER_TERMS_OF_SERVICE_VERSION_CHANGED
USER_API_KEY_CHANGED, USER_BOT_OWNER_CHANGED,
USER_DEFAULT_SENDING_STREAM_CHANGED, USER_DEFAULT_REGISTER_STREAM_CHANGED
USER_DEFAULT_ALL_PUBLIC_STREAMS_CHANGED, USER_SETTING_CHANGED
USER_DIGEST_EMAIL_CREATED
2024-09-09 11:50:13 -07:00
Lauryn Menard
e5daa3470f audit-log: Move user event types to AuditLogEventType enum.
Event types moved: USER_CREATED, USER_ACTIVATED, USER_DEACTIVATED
USER_REACTIVATED, USER_ROLE_CHANGED, USER_DELETED
USER_DELETED_PRESERVING_MESSAGES
2024-09-09 11:50:13 -07:00
Anders Kaseorg
91ade25ba3 python: Simplify with str.removeprefix, str.removesuffix.
These are available in Python ≥ 3.9.
https://docs.python.org/3/library/stdtypes.html#str.removeprefix

Signed-off-by: Anders Kaseorg <anders@zulip.com>
2024-09-03 12:30:16 -07:00
Kenneth Rodrigues
00a54099a2 analytics: Migrate to @typed_endpoint. 2024-08-26 08:57:49 -07:00
Tim Abbott
54ea20cc5b migrations: Squash analytics migrations.
Created using manage.py squashmigrations, with my patch to the Django
migration optimizer to correctly collapse
AddConstraints/RemoveConstraints operations.
2024-08-23 17:15:35 -07:00
Tim Abbott
afecb2eca6 migrations: Add missing elidable tags on RunPython/RunSQL steps.
This helps the squashmigrations tool know that it can squash these.
2024-08-23 17:15:35 -07:00
roanster007
c6a06d4684 direct_message_group: Add new group_size field.
This commit adds a new `group_size` field to the `DirectMessageGroup`
model, and backfills its value to each of the existing direct message
groups.

Fixes part of #25713
2024-08-23 11:09:41 -07:00
Anders Kaseorg
c03839f42f mypy: Reenable explicit-override for models.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2024-08-01 17:39:04 -07:00
roanster007
7b3e163d55 refactor: Rename huddle to direct_message_group in non api files.
This commit completes rename of "huddle" to "direct_message_group"
in all the non API files.

Part of #28640
2024-07-31 23:25:56 -07:00
Alex Vandiver
94795500b5 analytics: Better indexed join to analytics_usercount, using realm_id.
analytics_usercount_property_realm_id_end_time_591dbec1_idx, added
back in b7df84d5a8, makes this lookup actually indexed.
195defb031 rewrote this query in a way which stopped using the
index.
2024-07-30 09:40:55 -07:00
Mateusz Mandera
4a93149435 settings: Rework how push notifications service is configured.
Instead of the PUSH_NOTIFICATIONS_BOUNCER_URL and
SUBMIT_USAGE_STATISTICS settings, we want servers to configure
individual ZULIP_SERVICE_* settings, while maintaining backward
compatibility with the old settings. Thus, if all the new
ZULIP_SERVICE_* are at their default False value, but the legacy
settings are activated, they need to be translated in computed_settings
to the modern way.
2024-07-17 17:14:06 -07:00
Anders Kaseorg
1e9b6445a9 ruff: Fix PLR6104 Use += to perform an augmented assignment directly.
This is a preview rule, not yet enabled by default.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
2024-07-14 13:49:51 -07:00
Anders Kaseorg
48202389b8 ruff: Bump target-version from py38 to py310.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2024-07-13 22:28:22 -07:00
Anders Kaseorg
3f29bc42b1 ruff: Fix B905 zip() without an explicit strict= parameter.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2024-07-13 22:28:22 -07:00
Anders Kaseorg
0fa5e7f629 ruff: Fix UP035 Import from collections.abc, typing instead.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2024-07-13 22:28:22 -07:00
Anders Kaseorg
531b34cb4c ruff: Fix UP007 Use X | Y for type annotations.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2024-07-13 22:28:22 -07:00
Anders Kaseorg
e08a24e47f ruff: Fix UP006 Use list instead of List for type annotation.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2024-07-13 22:28:22 -07:00
roanster007
02d0566dc5 refactor: Rename Huddle Django model class to DirectMessageGroup.
This commit renames the "Huddle" Django model class to
"DirectMessageGroup", while maintaining the same table --
"zerver_huddle".

Fixes part of #28640.
2024-07-07 21:31:30 -07:00
Alex Vandiver
f52a93bc14 upload: Stop requiring callers pass in the file size.
This can be calculated because we have the contents.
2024-07-07 14:40:07 -07:00
Alex Vandiver
0a296b2a6e upload: Start storing content-type for new uploads. 2024-07-07 14:40:07 -07:00
Mateusz Mandera
00b8cce50e push_notifs: Rename PushDeviceToken.GCM to FCM. 2024-06-17 18:22:59 -07:00
Alex Vandiver
50c3dd88e6 models: Migrate ids of all non-Message-related tables to bigint.
Migrate all `ids` of anything which does not have a foreign key from
the Message or UserMessage table (and would thus require walking
those) to be `bigint`.  This is done by removing explicit
`BigAutoField`s, trading them for explicit `AutoField`s on the tables
to not be migrated, while updating `DEFAULT_AUTO_FIELD` to the new
default.

In general, the tables adjusted in this commit are small tables -- at
least compared to Messages and UserMessages.

Many-to-many tables without their own model class are adjusted by a
custom Operation, since they do not automatically pick up migrations
when `DEFAULT_AUTO_FIELD` changes[^1].

Note that this does multiple scans over tables to update foreign
keys[^2].  Large installs may wish to hand-optimize this using the
output of `./manage.py sqlmigrate` to join multiple `ALTER TABLE`
statements into one, to speed up the migration.  This is unfortunately
not possible to do generically, as constraint names may differ between
installations.

This leaves the following primary keys as non-`bigint`:
- `auth_group.id`
- `auth_group_permissions.id`
- `auth_permission.id`
- `django_content_type.id`
- `django_migrations.id`
- `otp_static_staticdevice.id`
- `otp_static_statictoken.id`
- `otp_totp_totpdevice.id`
- `two_factor_phonedevice.id`
- `zerver_archivedmessage.id`
- `zerver_client.id`
- `zerver_message.id`
- `zerver_realm.id`
- `zerver_recipient.id`
- `zerver_userprofile.id`

[^1]: https://code.djangoproject.com/ticket/32674
[^2]: https://code.djangoproject.com/ticket/24203
2024-06-05 11:48:27 -07:00
Alex Vandiver
4f4725f810 analytics: Migrate models' id columns to bigint.
This helps prevent wraparound on exceedingly large and old installs,
particularly Zulip Cloud.  These are relatively simple migrations
since they are not referenced by any other tables; however, they are
quite large, and are actively used from Django by running servers,
making this not a migration which is possible to run without stopping
the server.

Use the escape hatch in the previous commit to temporarily pause
analytics writes while the migration happens.  This should make the
migration transparent to users, at the small cost of an artificial dip
in statistics (specifically, to push notification counts, and unread
message counts) while the migration runs.
2024-06-05 11:48:27 -07:00
Alex Vandiver
6c17cca208 zilencer: Drop unwanted data that old servers might still send. 2024-06-03 12:35:35 -07:00
Alex Vandiver
09e9c75ec6 analytics: Remove active_users and active_users_log metrics.
Both of these are inaccurate, not currently used anywhere, and have
been superseded by the `active_users_audit` metric.
2024-06-03 12:35:35 -07:00