Commit Graph

938 Commits

Author SHA1 Message Date
Prakhar Pratyush
6ab6df96c8 push_notification: Send a list of push requests.
Earlier, we were passing a map `device_id_to_encrypted_data`
and http headers as separate fields to bouncer.

The downside of that approach is it restricts the bouncer to
process only one type of notice i.e. either notification for
a new message or removal of sent notification, because it
used to receive a fixed priority and push_type for all the
entries in the map.

Also, using map restricts the bouncer to receive only one
request per device_id. Server can't send multiple notices
to a device in a single call to bouncer.

Currently, the server isn't modelled in a way to make a
single call to the bouncer with:
* Both send-notification & remove-notification request data.
* Multiple send-notification request data to the same device.

This commit replaces the old protocol of sending data with
a list of objects where each object has the required data
for bouncer to send it to FCM or APNs.

This makes things a lot flexible and opens possibility for
server to batch requests in a different way if we'd like to.
2025-07-25 12:47:55 -07:00
Prakhar Pratyush
29ea8a07c2 push_notification: Add support to configure priority and push_type.
This commit adds support to let server configure:
* fcm_priority
* apns_priority
* apns_push_type

while sending E2EE push notifications.

The values of these fields will vary depending on whether the
send request is to send push notification for a message or
revoke an already sent notification.

Since, the bouncer receives encrypted data so it can't inspect
the payload to determine whether it is a removal request or not,
hence can't configure priority on its own.

The server needs to specify explicitly.

We're not simply sending a single 'is_removal' flag because
allowing the server to configure them separately will help in
future to support other types of notifications with a different
combination of priority and push_type, like whose aim is to notify
user about information other than a new message or removal request.

Fixes part of #35368.
2025-07-24 11:56:42 -07:00
Prakhar Pratyush
7e1afa0e8a push_notification: Send end-to-end encrypted push notifications.
This commit adds support to send encrypted push notifications
to devices registered to receive encrypted notifications.

URL: `POST /api/v1/remotes/push/e2ee/notify`
payload: `realm_uuid` and `device_id_to_encrypted_data`

The POST request needs to be authenticated with the server’s
API key.

Note: For Zulip Cloud, a background fact about the push bouncer is
that it runs on the same server and database as the main application;
it’s not a separate service. So, as an optimization we directly call
'send_e2ee_push_notifications' function and skip the HTTP request.
2025-07-22 17:08:55 -07:00
Alex Vandiver
2f4dd72076 push_notifications: Adjust APNs tokens to be case-insensitive in the database.
APNs apparently treats its tokens case-insensitively; FCM does not.
Adjust the `unique_together` to instead be separate partial
constraints, keyed on the `kind` of the PushDeviceToken.
2025-07-22 14:30:13 -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
Prakhar Pratyush
6a4b06b6f4 zerver: Add PushDevice model.
This commit adds a `PushDevice` model where each row
corresponds to an account on an install of the app
that has attempted to register with the bouncer to
receive mobile push notifications.

This is the core server table storing registrations
that are potentially registered with the mobile push
notifications bouncer service.
2025-07-14 14:52:38 -07:00
Prakhar Pratyush
3c6a3b0d77 zilencer: Add endpoint to register push device to bouncer.
This commit adds a zilencer endpoint to let self-hosted
servers register push devices to whom mobile push notifications
will be sent.

POST "/api/v1/remotes/push/e2ee/register"
Payload: realm_uuid, push_account_id, encrypted_push_registration,
bouncer_public_key

The post request needs to be authenticated with the server’s API key.

Note: For Zulip Cloud, a background fact about the push bouncer is
that it runs on the same server and database as the main application;
it’s not a separate service.
So, as an optimization, we plan to directly call the
`do_register_remote_push_device` function and skip the HTTP request.
2025-07-14 14:52:38 -07:00
Prakhar Pratyush
5facec1cc3 zilencer: Remove 'user_uuid' parameter of 'get_remote_realm_helper'.
The 'user_uuid' parameter of 'get_remote_realm_helper' was only
used for logging when realm lookup fails, but we've never made
use of that detail in practice.

There is no strong reason to keep that.
2025-07-14 14:52:38 -07:00
Prakhar Pratyush
e10d413476 RemotePushDevice: Migrate token_kind from IntegerChoices to TextChoices.
Improves code readability by using string values ("apns", "fcm")
instead of integers (1, 2).
2025-07-14 14:52:38 -07:00
Tim Abbott
71e8fcbb3a zilencer: Update comments on remotepushdevice. 2025-07-07 09:41:34 -07:00
Prakhar Pratyush
7cbd4e4880 zilencer: Add RemotePushDevice model.
This commit adds a `RemotePushDevice` model where each row
corresponds to an account on an install of the app registered
to receive mobile push notifications.
2025-07-06 21:11:26 -07:00
Prakhar Pratyush
e6bdf84363 validate_token: Move validate_token to avoid dependency cycle.
Prep commit to avoid depencency cycle.
2025-07-06 21:11:26 -07:00
Prakhar Pratyush
8b3cef554b settings: Add push_registration_encryption_keys map.
The `push_registration_encryption_keys` map stores the
assymetric key pair generated on bouncer.

The public key will be used by the client to encrypt
registration data and the bouncer will use the corresponding
private key to decrypt.

- Updated the `generate_secrets.py` script to generate the map
during installation in dev environment.
- Added a management command to add / remove key i.e. use it
for key rotation while retaining the older key-pair for a period
of time.
2025-07-06 21:11:26 -07:00
Anders Kaseorg
2539d9774e populate_db: Copy lists before shuffling them.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2025-06-25 11:49:02 -07:00
Sahil Batra
b655bd14ea messages: Use "\x07" as topic for DMs and group DMs.
This commit updates code to use "\x07" as value for
"subject" field of Message objects for DMs and group
DMs, so that we have a unique value for DMs and group
DMs which cannot be used for channel messages.

This helps in avoiding having an empty string value as
topic for DMs, which is also used for "general chat"
channel messages, as large number of DMs in the realm
resulted in PostgreSQL query planner thinking that there
are too many "general chat" messages and thus generated
bad query plans for operations like fetching
"general chat" messages in a stream or moving messages
to and from "general chat" topic.

This change as done for ArchivedMessage and
ScheduledMessage objects as well.

Note that the clients still get "subject" value as
an empty string "".

This commit also adds tests for checking that "\x07"
cannot be used as topic for channel messages.

Fixes #34360.
2025-06-19 10:44:37 -07:00
Sahil Batra
e243fc67fa populate_db: Use channel folder in dev database.
This commit creates "Engineering" channel folder and
adds "devel", "errors" and "test" channels to it.
2025-06-10 11:59:07 -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
userAdityaa
354a16fb0a migration: Rename 'populate_db' Client to 'ZulipDataImport'.
This commit:

* Creates a migration to rename any existing Client with
name="populate_db" to "ZulipDataImport".
* Updates populate_db.py to use ZulipDataImport for new
message creation

These changes should make code to identify imported messages
considerably more readable.

Fixes #33909.
2025-05-08 12:18:34 -07:00
PieterCK
0814fb88c1 api: Add a new endpoint for message reporting.
This adds a new API endpoint that enables users to report messages for
review by admins or moderators. Reports will be sent to the
`moderate_request_channel`, so it must be configured for this feature to
be enabled.

Fixes part of #20047.

Co-authored-by: Adam Sah <140002+asah@users.noreply.github.com>
2025-05-06 15:59:55 -07:00
Ethan Mayer
c12b94aea4 models: Refactor corporate/models.py into models package.
Fixes #34318.

Seperated models file into a package with component files.
2025-04-08 10:16:35 -07:00
Prakhar Pratyush
ce7d07a498 populate_db: Fix is_channel_message incorrectly set to True for DMs.
We introduced `is_channel_message` flag in 90d76b692b.

While populating messages in dev environment, we were not setting
it to False for DMs / group DMs.

This commit fixes that bug.
2025-03-19 11:38:11 -07:00
Prakhar Pratyush
c70a3ffb0a populate_db: Mark onboarding steps as seen for existing users.
This commit updates populate_db to mark all onboarding steps as seen
for existing users to avoid unnecessary popups during development.

Developers should create a new user in development environment
to test the onboarding steps.
2025-03-13 14:38:16 -07:00
Alex Vandiver
721fd26442 send_email: Set the Date header according to local enqueue time.
Email clients tend to sort emails by the "Date" header, which is not
when the email was received -- emails can be arbitrarily delayed
during relaying.  Messages without a Date header (as all Zulip
messages previously) have one inserted by the first mailserver they
encounter.  As there are now multiple email-sending queues, we would
like the view of the database, as presented by the emails that are
sent out, to be consistent based on the Date header, which may not be
the same as when the client gets the email in their inbox.

Insert a Date header of when the Zulip system inserted the data into
the local queue, as that encodes when the full information was pulled
from the database.  This also opens the door to multiple workers
servicing the email_senders queue, to limit backlogging during large
notifications, without having to worry about inconsistent delivery
order between those two workers.

Messages which are sent synchronously via `send_email()` get a Date
header of when we attempt to send the message; this is, in practice,
no different from Django's default behaviour of doing so, but makes
the behaviour slightly more consistent.
2025-03-10 16:48:08 -07:00
Mateusz Mandera
1532b87910 zilencer: Log bouncer-side info when RemoteRealm creation fails.
This should also be logged on the bouncer side, to give us better
debugging information when self-hosters run into this error.
2025-02-19 17:11:35 -08:00
Alex Vandiver
a7d513e5ec users: Remove unnecessary get_api_key helper.
Using the column name is clearer.
2025-02-13 12:40:53 -08:00
Vector73
2ef9695ef0 settings: Migrate wildcard_mention_policy to group-based permission.
Added new group-based permission setting
`can_mention_many_users_group` to replace `wildcard_mention_policy`.
2025-02-12 23:21:33 -08:00
Mateusz Mandera
ddcc36c3aa register_server: Improve UX with the "hostname already in use" error.
An even better way than the current json error message recommending the
--registration-transfer option is to return an appropriate error code
and have that get picked up by the register_server command.

The register_server command can then display a more comprehensive,
better formatted error message with proper whitespaces and a pointer to
the documentation.
2025-01-30 14:32:36 -08:00
Mateusz Mandera
7390eb2ed0 zilencer: Rename registration takeover to registration transfer.
This is the final naming that we want, compared to the naming we merged
in #32399.
Includes renaming the API endpoints, but that should be fine as the
original PR was just merged and this isn't deployed anywhere.
2025-01-30 14:32:36 -08:00
Mateusz Mandera
685e49d34d zilencer: Improve "hostname already exists" error in registration.
Users most likely to run into this will be the ones who are moving to a
new server, but keeping their original domain and thus just need to
transfer the registration.
2025-01-28 11:10:50 -08:00
Mateusz Mandera
4e22a79e6a zilencer: Add flow for a server to reclaim its registration.
If the server controls the registration's hostname, it can reclaim its
registration credentials. This is useful, because self-hosted admins
frequently lose the credentials when moving their Zulip server to a
different machine / deployment method.

The flow is the following:
1. The host sends a POST request to
   /api/v1/remotes/server/register/takeover.
2. The bouncer responds with a signed token.
3. The host prepares to serve this token at /api/v1/zulip-services/verify and
   sends a POST to /remotes/server/register/verify_challenge endpoint of
   the bouncer.
4. Upon receiving the POST request, the bouncer GETS
   https://{hostname}/api/v1/zulip-services/verify, verifies the secret and
   responds to the original POST with the registration credentials.
5. The host can now save these credentials to it zulip-secrets.conf file
   and thus regains its push notifications registration.

Includes a global rate limit on the usage of the /verify_challenge
endpoint, as it causes us to make outgoing requests.
2025-01-28 11:10:50 -08:00
Lauryn Menard
d3bc9bec1d billing: Rename migrate_customer_to_legacy_plan.
Renames migrate_customer_to_legacy_plan to
create_complimentary_access_plan for how this
function is currently used.

Prep for adding a complimentary access plan
for Zulip Cloud.
2025-01-21 15:40:27 -08:00
Lauryn Menard
757dd3e2a2 billing: Delete add_server_to_legacy_plan in favor of support actions.
As we can now add a complimentary access plan (formerly legacy plan)
to a remote server or remote realm via the support view, we no
longer use or need this management command for our billing admin.
2025-01-21 15:40:27 -08:00
Anders Kaseorg
f223251ffe requirements: Upgrade Python requirements.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2025-01-14 09:42:16 -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
Mateusz Mandera
99ea0255da validate_hostname_or_raise_error: Improve hostname validation.
Obviously, URLs with a path or query are not valid hostnames.
2025-01-13 18:34:20 -08:00
Mateusz Mandera
4196859cd6 zilencer: Extract validate_hostname_or_raise_error function. 2025-01-13 18:34:20 -08:00
Sahil Batra
2d09cd899e message: Use new setting for checking stream posting permissions. 2025-01-07 12:17:09 -08:00
Lauryn Menard
e65f3cf657 corporate: Create license ledger for automanaged plan migrations.
If we move a paid plan from a remote server to a remote realm, and
the plan has automated license management, then we create an updated
license ledger entry when we move the plan for the remote realm
billing data so that we have an accurate user count for licenses
when the plan is next invoiced.
2024-12-31 16:54:40 -08:00
Lauryn Menard
c01787f41a corporate: Clean up vestiges of migrating plans to multiple realms.
In commit ea863bab5b, handle_customer_migration_from_server_to_realm
was updated to only move a remote server's plan in the case that there
is only one remote realm on the server.
2024-12-31 16:54:40 -08:00
Prakhar Pratyush
3bad36ef8c queue: Rename queue_json_publish to queue_json_publish_rollback_unsafe.
This commit renames the 'queue_json_publish' function to
'queue_json_publish_rollback_unsafe' to reflect the fact that it doesn't
wait for the db transaction (within which it gets called, if any)
to commit and sends event irrespective of commit or rollback.

In most of the cases we don't want to send event in the case of
rollbacks, so the caller should be aware that calling the function
directly is rollback unsafe.

Fixes part of #30489.
2024-12-06 09:23:02 -08:00
Anders Kaseorg
9e9e951a4a corporate: Fix misuse of timezone_now() as parameter default.
Python parameter defaults are only evaluated once at the function
definition site, not at each call site.  So these defaults were
incorrectly evaluating to the server’s startup time rather than the
current time.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
2024-11-10 19:14:32 -08:00
Prakhar Pratyush
9c9866461a transaction: Add durable=True to the outermost db transactions.
This commit adds `durable=True` to the outermost db transactions
created in the following:
* confirm_email_change
* handle_upload_pre_finish_hook
* deliver_scheduled_emails
* restore_data_from_archive
* do_change_realm_subdomain
* do_create_realm
* do_deactivate_realm
* do_reactivate_realm
* do_delete_user
* do_delete_user_preserving_messages
* create_stripe_customer
* process_initial_upgrade
* do_update_plan
* request_sponsorship
* upload_message_attachment
* register_remote_server
* do_soft_deactivate_users
* maybe_send_batched_emails

It helps to avoid creating unintended savepoints in the future.

This is as a part of our plan to explicitly mark all the
transaction.atomic calls with either 'savepoint=False' or
'durable=True' as required.

* 'savepoint=True' is used in special cases.
2024-11-05 17:58:47 -08:00
Prakhar Pratyush
c4f74f470d remote_server_post_analytics: Add durable=True to outermost transaction.
This commit adds 'durable=True' to the outermost transaction
in 'remote_server_post_analytics'.

It also adds 'savepoint=False' to inner transaction.atomic
decorator to avoid creating savepoint.

This is as a part of our plan to explicitly mark all the
transaction.atomic decorators with either 'savepoint=False' or
'durable=True' as required.

* 'savepoint=True' is used in special cases.
2024-11-01 16:41:15 -07:00
Prakhar Pratyush
0fb5657131 transaction: Add durable=True to outermost transaction.atomic decorator.
This commit adds 'durable=True' to the outermost transactions
of the following functions:
* do_create_multiuse_invite_link
* do_revoke_user_invite
* do_revoke_multi_use_invite
* sync_ldap_user_data
* do_reactivate_remote_server
* do_deactivate_remote_server
* bulk_handle_digest_email
* handle_customer_migration_from_server_to_realm
* add_reaction
* remove_reaction
* deactivate_user_group

It helps to avoid creating unintended savepoints in the future.

This is as a part of our plan to explicitly mark all the
transaction.atomic decorators with either 'savepoint=False' or
'durable=True' as required.

* 'savepoint=True' is used in special cases.
2024-11-01 16:41:15 -07:00
Tim Abbott
400ca2c8dd zilencer: Avoid repeated emails about locally deleted realms. 2024-10-15 13:26:48 -07:00
Shubham Padia
12ebd97f1f settings: Add group_creator as default for can_manage_group.
We create an unnamed user group with just the group creator as it's
member when trying to set the default. The pattern I've followed across
most of the acting_user additions is to just put the user declared
somewhere before the check_add_user_group and see if the test passes.
If it does not, then I'll look at what kind of user it needs to be set
to `acting_user`.
2024-10-01 17:35:14 -07:00
Anders Kaseorg
f0f048de69 corporate: Import corporate.lib.stripe lazily.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2024-09-24 18:18:26 -07:00
Tim Abbott
9d74abee0b tools: Skip checks in recursive management calls. 2024-09-24 14:26:34 -07:00
Mateusz Mandera
8505de7b19 populate_db: Rename message_type to recipient_type. 2024-09-10 12:50:28 -07:00
Lauryn Menard
d610e09397 audit-logs: Use AuditLogEventType enum name for string method.
Reorders audit log string methods to have the following pattern:
"event_type event_time (id): modified_object". And the event type
is the name for the AuditLogEventType enum.
2024-09-09 11:50:13 -07:00