Commit Graph

188 Commits

Author SHA1 Message Date
Shubham Padia
4d02a082a0 stream: Users with permission to administer can unsubscribe others.
We've also converted the function to check for permission to unsubscribe
others to accept a list of streams instead of checking each stream one
by one.
2025-02-11 15:09:16 -08:00
Shubham Padia
0f51b029a0 stream: User with just can add permissions should be able to add. 2025-02-11 15:09:16 -08:00
Shubham Padia
9e09a240d7 stream: Pass is_subscribed to check_basic_stream_access.
Earlier, we were passing the whole subscription object to the function
in order to check if the user was subscribed or not. In the future
commits, we want to check that without fetching and passing the complete
subscription object and this commit will help us do that.
2025-02-11 15:09:16 -08:00
Shubham Padia
98c5243c9b stream: Refactor to use functions for administer and add subscribers. 2025-02-11 15:09:16 -08:00
Tim Abbott
bd8b845a4d mention: Use filter_stream_authorization.
In preparation for accessing the messages in channels to link topics
in them, we need to check channel access.
2025-02-03 18:48:24 -08:00
Shubham Padia
6852142b00 stream: Rename can_administer_channel to reflect what it checks.
We've added a comment highlighting that the function does not check
whether a user has access to the channel or not. Adding `accessible` to
the function name further emphasises that.
2025-01-27 11:26:06 -08:00
Shubham Padia
bf5bdacf51 stream: Use dataclass for filter_stream_authorization return type. 2025-01-27 11:26:06 -08:00
Shubham Padia
e912ada51e stream: Rename can_subscribe_others_to_all_streams.
Rename `can_subscribe_others_to_all_streams` to
`can_subscribe_others_to_all_accessible_streams` so it's clear that we
are not attempting to check basic access in this function.
2025-01-27 11:26:06 -08:00
bedo
21199beb73 stream_delete_event: Include only stream IDs in the event.
Fixes #32369

Migrate stream delete event to include only stream ids in the form of
"stream_ids": [1,...], because clients only need the ids.

While keep sending ids in the form of "streams": [{stream_id: 1},...]
for compatibility with all clients other than web.
2025-01-24 17:58:32 -08:00
Shubham Padia
a76042ce39 invite: Any combination of default streams should be subscribe-able.
Fixes #32706.

A user with permission to invite users should be able to subscribe users
to any of the default streams whether they have the permission to do so
or not for each of those default streams or not. This should only happen
in the invite code path, and not the subscribe code path.

This commit also adds the ability to pick and chose default streams if
you do not have the permission to subscribe to any other channels.

Before this, if you did not have the permission to subscribe any other
channels, only the checkbox to subscribe to all the default streams at
once was available to you.

For the stream pill typeahead, we don't show streams that the user
cannot subscribe other users to. For more details, see

   https://chat.zulip.org/#narrow/channel/101-design/topic/can.20subscribe.20other.20users.20permission.20invite
2025-01-22 14:27:06 -08:00
Shubham Padia
41c74314c0 streams: Use can_add_subscribers_group for permission check.
The function to check relevant permissions does so for multiple streams
at once to save us database query counts. Doing it one by one for every
stream would become very expensive.
We've also added `insufficient_permission_streams` to the filter
functions return type for streams for which the current user does not
have permission to subscribe other users.
2025-01-22 14:27:06 -08:00
Shubham Padia
97996b9929 streams: Add can_add_subscribers_group as a setting.
We're not using this setting to check the permissions yet.
2025-01-22 14:27:06 -08:00
Mateusz Mandera
e41ee8abf5 streams: Tweak query in update_stream_active_status_for_realm.
The NOT EXISTS structure might be better optimized by the Postgres query
planner and might lead to slightly better performance than the id NOT IN
(<subquery>) structure.
2025-01-09 11:56:11 -08:00
Sahil Batra
b4b571d60c streams: Remove stream_post_policy field from Stream. 2025-01-07 12:17:37 -08:00
Sahil Batra
1d7a77b9a8 streams: Pass stream_post_policy value according to the group setting.
The stream and subscription objects now have stream_post_policy value
set according to the can_send_message_group setting representing the
superset of users who have permission to post in the channel.
2025-01-07 12:17:09 -08:00
Sahil Batra
2d09cd899e message: Use new setting for checking stream posting permissions. 2025-01-07 12:17:09 -08:00
Sahil Batra
101e94b052 streams: Add group based setting to control posting permissions. 2025-01-07 12:17:07 -08:00
Shubham Padia
75d994f5b4 streams: Use can_administer_channel_group for checking permissions.
We're not using OrganizationAdministratorRequiredError anymore and the
new error message will be `Insufficient Permission`.
2024-12-03 18:38:25 -08:00
Shubham Padia
4b3d1a5aac streams: Creator should be able to administer new channels.
There are cases when importing from slack where the stream creator can
technically be none, that is why we have named the default group string
to `stream_creator_or_nobody`. If stream creator is not present, we
default back to nobody. See
https://chat.zulip.org/#narrow/channel/3-backend/topic/Default.20can_administer_channel_group.20for.20imported.20realms/near/1983634
for mode details.
2024-12-03 18:38:25 -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
Sahil Batra
0f3d459d58 streams: Minor optimization in get_setting_values_for_group_settings.
Return early without doing membership query when there are no
anonymous groups.
2024-12-02 17:29:56 -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
343741f621 stream: Fetch system groups in one query for group setting defaults.
Right now, the number of queries has remained the same, but when we add
more settings in the future, we won't be increasing the number of
queries when iterating over stream permission group settings.
2024-12-01 19:32:22 -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
Aman Agrawal
50256f4831 stream: Add field to track active status of stream. 2024-11-27 17:31:06 -08:00
Sahil Batra
f197d881ca streams: Fix handling empty groups when computing setting values.
Since "Nobody" and other user-defined empty groups has no
members or subgroups, we need a UserGroup query.

Since we are doing UserGroup query, updated the code to
check whether the setting is set to anonymous group or
not just after doing UserGroup query and not in membership
queries.

Also, added tests to check query count when setting is set
to anonymous group.
2024-11-22 10:48:41 -08:00
Sahil Batra
b6f895f61c streams: Optimize DB queries for computing permission setting values.
This commit updates code to use union so that we can compute values
for permission settings using a single DB query.
2024-11-21 11:28:54 -08:00
Sahil Batra
d1e76a9281 streams: Allow admins to unsubscribe others irrespective of setting. 2024-11-21 11:28:54 -08:00
Sahil Batra
d5a391a56b streams: Optmize code to send events on creating stream.
This commit updates code to effectively compute the setting
values when creating stream object to be sent in stream creation
events.
2024-11-18 11:55:19 -08:00
Sahil Batra
7adc83d2a0 streams: Optimize code for computing stream objects.
This commit updates code to optimize code for computing stream
objects to be sent with stream creation event and in response
for 'GET /streams/{stream_id}' endpoint by optimizing number
of queries while computing values for group permission settings.

This change does not benefit much currently as we only have one
stream group permission setting, but is important before we add
more stream permission settings.

There are a couple of places left where we can still optimize
the code and that would be done in further commits.
2024-11-18 11:55:19 -08:00
Sahil Batra
2cc0f482e1 streams: Refactor code to compute setting group values.
This commit adds a new function to compute setting group
values for a list of streams, so we can avoid having duplicate
code for computing setting group IDs from streams.
2024-11-18 11:55:19 -08:00
Shubham Padia
b6ebf143cc streams: Backend changes to support anonymous groups.
can_remove_subscribers_group setting can now be set to
anonymous user groups.

Co-authored-by: Sahil Batra <sahil@zulip.com>
2024-11-16 17:11:08 -08:00
Lauryn Menard
3ebc507ddb signups: Move logic for realm admin notification to corporate app.
As we would like to send similar notifications for other billing
state changes (for all BillingSession types), it makes sense to
move the logic for creating and sending these admin realm internal
messages to the BillingSession framework in the corporate app.

In the case that a channel with the specified name does not exist,
we now send direct messages to the admin realm administrators with
the channel, topic and message so that the information is not lost
and so that the channel for these messages can be created.
2024-11-05 08:56:24 -08:00
sanchi-t
c73038edea streams: Restrict access to archived streams. 2024-10-25 16:06:43 -07:00
Sanchit Sharma
795b2ba14e do_deactivate_stream: Remove unnecessary mutations.
Streams should not be marked as private, and subscribers
of the deactivated stream should not be removed.

Update the confirmation message when archiving a stream.
2024-10-25 16:06:42 -07:00
sanchi-t
af7ebde9e4 subscription: Include archived channels in streams list.
`is_archived` field is added to the stream and types.

Include a new `archived_channeels` client capability, to allow clients
to access data on archived channels, without breaking
backwards-compatibility for existing clients that don't know how to
handle these.

Also, included `exclude_archived` parameter to `/get-streams`,
which defaults to `true` as basic clients may not be interested
in archived streams.
2024-10-25 16:06:40 -07:00
Sahil Batra
7a6135371e settings: Handle guests separately for group-based settings.
This commit adds code to handle guests separately for group
based settings, where guest will only have permission if
that particular setting can be set to "role:everyone" group
even if the guest user is part of the group which is used
for that setting. This is to make sure that guests do not
get permissions for actions that we generally do not want
guests to have.

Currently the guests do not have permission for most of them
except for "Who can delete any message", where guest could
delete a message if the setting was set to a user defined
group with guest being its member. But this commit still
update the code to use the new function for all the settings
as we want to have a consistent pattern of how to check whether
a user has permission for group-based settings.
2024-09-18 11:51:11 -07:00
Lauryn Menard
df1e9093a9 audit-log: Move stream/channel event types to AuditLogEventType enum.
Renamed event types below in the enum class to use channel instead of
stream.

Event types moved: STREAM_CREATED, STREAM_DEACTIVATED, STREAM_NAME_CHANGED
STREAM_REACTIVATED, STREAM_MESSAGE_RETENTION_DAYS_CHANGED
STREAM_PROPERTY_CHANGED, STREAM_GROUP_BASED_SETTING_CHANGED
2024-09-09 11:50:13 -07:00
Sahil Batra
09102c5780 streams: Check permission to create web-public streams based on group setting. 2024-08-01 22:49:33 -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
Alex Vandiver
d5a4941691 django: Switch to .alias() instead .annotate() where possible.
When using the sub-expression purely for filtering, and not for
accessing the value in the resultset, .alias() is potentially faster
since it does not pull the value in as well.
2024-07-11 09:26:23 -07:00
Alex Vandiver
03864db5a2 streams: Move enforcement of stripped-stream-names lower.
This catches things like trailing spaces in internationalized
default-stream names.
2024-06-05 10:44:53 -07:00
Prakhar Pratyush
c798d192dc message_send: Update do_send_messages codepath to send event on commit.
Earlier, we were using 'send_event' & 'queue_json_publish' in
'do_send_messages' which can lead to a situation where we enqueue
events but the transaction fails at a later stage.

Events should not be sent until we know we're not rolling back.
2024-05-19 23:18:43 -07:00
Lauryn Menard
62e17c987f exceptions: Add IncompatibleParametersError as a JsonableError.
Creates an IncompatibleParametersError to be used in cases where
there are two (or more) optional parameters for an endpoint that
are incompatible with each other, e.g. there's a parameter for a
user name and a user ID but only one should be sent in the request
to identify the user.

Documents the error on the /api/rest-error-handling article.

Updates the PATCH users/me/subscriptions/muted_topics endpoint to
use this error when both the stream and stream_id parameters are
passed (note this endpoint is currently deprecated).
2024-04-29 16:58:47 -07:00
Sahil Batra
a96c8b8352 groups: Use NamedUserGroup for all queries. 2024-04-26 17:03:09 -07:00
Lauryn Menard
91ffb548cc streams: Update translated errors for stream to channel rename.
Updates translated JsonableError strings that relate to streams
to use channel instead of stream. Separated from other error string
updates as this is a dense area of changes for this rename.

Part of stream to channel rename project.
2024-04-24 14:35:05 -07:00
Lauryn Menard
abc6f5b8a2 i18n: Update translated string variables for stream to channel rename.
Generally updates variables that appear in translated strings that use
"stream" to instead use "channel".

Two exceptions are ErrorCode.STREAM_DOES_NOT_EXIST JsonableErrors as
changing the variable would also change the fields returned by these
errors to clients.

Changes to context variables in emails and variables in onboarding
welcome bot messages are addressed in separate commits.

Part of stream to channel rename project.
2024-04-24 14:35:05 -07:00
tnmkr
5128d8f9af streams: Add creator field.
Adds nullable creator field, containing a reference to the user who
created the stream. When creating a stream, acting user is set as
the creator of the stream. Since API calls to create streams always
have an acting user, this field should always be set when streams
are created using the API.

Because streams can be created with no acting user, this field is
nullable. We try to backfill existing streams using RealmAuditLog table,
but not all streams are guaranteed to have a recorded create log. Thus
this new field is left null when it cannot be backfilled. We also set
this field to null when the creator user is deleted.
2024-04-20 19:03:43 -07:00