-
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
Expand file tree
/
Copy pathtypes.py
More file actions
394 lines (318 loc) · 10.6 KB
/
types.py
File metadata and controls
394 lines (318 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
from collections.abc import Callable
from dataclasses import dataclass, field
from datetime import datetime
from enum import IntEnum
from typing import TYPE_CHECKING, Any, TypeAlias, TypeVar
if TYPE_CHECKING:
from zerver.models import Stream
from django_stubs_ext import StrPromise
from typing_extensions import NotRequired, TypedDict
# See zerver/lib/validator.py for more details of Validators,
# including many examples
ResultT = TypeVar("ResultT")
Validator: TypeAlias = Callable[[str, object], ResultT]
ExtendedValidator: TypeAlias = Callable[[str, str, object], str]
RealmUserValidator: TypeAlias = Callable[[int, object, bool], list[int]]
ProfileDataElementValue: TypeAlias = str | list[int]
class ProfileDataElementBase(TypedDict, total=False):
id: int
name: str
type: int
hint: str
display_in_profile_summary: bool
required: bool
editable_by_user: bool
field_data: str
order: int
class ProfileDataElement(ProfileDataElementBase):
value: ProfileDataElementValue | None
rendered_value: str | None
class ProfileDataElementUpdateDict(TypedDict):
id: int
value: ProfileDataElementValue
ProfileData: TypeAlias = list[ProfileDataElement]
FieldElement: TypeAlias = tuple[
int, StrPromise, Validator[ProfileDataElementValue], Callable[[Any], Any], str
]
ExtendedFieldElement: TypeAlias = tuple[
int, StrPromise, ExtendedValidator, Callable[[Any], Any], str
]
UserFieldElement: TypeAlias = tuple[int, StrPromise, RealmUserValidator, Callable[[Any], Any], str]
ProfileFieldData: TypeAlias = dict[str, dict[str, str] | str]
class UserDisplayRecipient(TypedDict):
email: str
full_name: str
id: int
is_mirror_dummy: bool
DisplayRecipientT: TypeAlias = str | list[UserDisplayRecipient]
class LinkifierDict(TypedDict):
pattern: str
url_template: str
id: int
class Unset:
"""In most API endpoints, we use a default value of `None"` to encode
parameters that the client did not pass, which is nicely Pythonic.
However, that design does not work for those few endpoints where
we want to allow clients to pass an explicit `null` (which
JSON-decodes to `None`).
We use this type as an explicit sentinel value for such endpoints.
TODO: Can this be merged with the _NotSpecified class, which is
currently an internal implementation detail of the typed_endpoint?
"""
UNSET = Unset()
class EditHistoryEvent(TypedDict, total=False):
"""
Database format for edit history events.
"""
# user_id is null for precisely those edit history events
# predating March 2017, when we started tracking the person who
# made edits, which is still years after the introduction of topic
# editing support in Zulip.
user_id: int | None
timestamp: int
prev_stream: int
stream: int
prev_topic: str
topic: str
prev_content: str
prev_rendered_content: str | None
prev_rendered_content_version: int | None
class FormattedEditHistoryEvent(TypedDict, total=False):
"""
Extended format used in the edit history endpoint.
"""
# See EditHistoryEvent for details on when this can be null.
user_id: int | None
timestamp: int
prev_stream: int
stream: int
prev_topic: str
topic: str
prev_content: str
content: str
prev_rendered_content: str | None
rendered_content: str | None
content_html_diff: str
class UserTopicDict(TypedDict, total=False):
"""Dictionary containing fields fetched from the UserTopic model that
are needed to encode the UserTopic object for the API.
"""
stream_id: int
stream__name: str
topic_name: str
last_updated: int
visibility_policy: int
@dataclass
class UserGroupMembersDict:
direct_members: list[int]
direct_subgroups: list[int]
# This next batch of types is for Stream/Subscription objects.
class RawStreamDict(TypedDict):
"""Dictionary containing fields fetched from the Stream model that
are needed to encode the stream for the API.
"""
can_add_subscribers_group_id: int
can_administer_channel_group_id: int
can_send_message_group_id: int
can_remove_subscribers_group_id: int
can_subscribe_group_id: int
creator_id: int | None
date_created: datetime
deactivated: bool
description: str
first_message_id: int | None
is_recently_active: bool
history_public_to_subscribers: bool
id: int
invite_only: bool
is_web_public: bool
message_retention_days: int | None
name: str
rendered_description: str
stream_post_policy: int
class RawSubscriptionDict(TypedDict):
"""Dictionary containing fields fetched from the Subscription model
that are needed to encode the subscription for the API.
"""
active: bool
audible_notifications: bool | None
color: str
desktop_notifications: bool | None
email_notifications: bool | None
is_muted: bool
pin_to_top: bool
push_notifications: bool | None
recipient_id: int
wildcard_mentions_notify: bool | None
class SubscriptionStreamDict(TypedDict):
"""Conceptually, the union of RawSubscriptionDict and RawStreamDict
(i.e. containing all the user's personal settings for the stream
as well as the stream's global settings), with a few additional
computed fields.
"""
audible_notifications: bool | None
can_add_subscribers_group: int | UserGroupMembersDict
can_administer_channel_group: int | UserGroupMembersDict
can_send_message_group: int | UserGroupMembersDict
can_remove_subscribers_group: int | UserGroupMembersDict
can_subscribe_group: int | UserGroupMembersDict
color: str
creator_id: int | None
date_created: int
description: str
desktop_notifications: bool | None
email_notifications: bool | None
first_message_id: int | None
is_recently_active: bool
history_public_to_subscribers: bool
in_home_view: bool
invite_only: bool
is_announcement_only: bool
is_archived: bool
is_muted: bool
is_web_public: bool
message_retention_days: int | None
name: str
pin_to_top: bool
push_notifications: bool | None
rendered_description: str
stream_id: int
stream_post_policy: int
stream_weekly_traffic: int | None
subscribers: NotRequired[list[int]]
wildcard_mentions_notify: bool | None
class NeverSubscribedStreamDict(TypedDict):
is_archived: bool
can_add_subscribers_group: int | UserGroupMembersDict
can_administer_channel_group: int | UserGroupMembersDict
can_send_message_group: int | UserGroupMembersDict
can_remove_subscribers_group: int | UserGroupMembersDict
can_subscribe_group: int | UserGroupMembersDict
creator_id: int | None
date_created: int
description: str
first_message_id: int | None
is_recently_active: bool
history_public_to_subscribers: bool
invite_only: bool
is_announcement_only: bool
is_web_public: bool
message_retention_days: int | None
name: str
rendered_description: str
stream_id: int
stream_post_policy: int
stream_weekly_traffic: int | None
subscribers: NotRequired[list[int]]
class DefaultStreamDict(TypedDict):
"""Stream information provided to Zulip clients as a dictionary via API.
It should contain all the fields specified in `zerver.models.Stream.API_FIELDS`
with few exceptions and possible additional fields.
"""
is_archived: bool
can_add_subscribers_group: int | UserGroupMembersDict
can_administer_channel_group: int | UserGroupMembersDict
can_send_message_group: int | UserGroupMembersDict
can_remove_subscribers_group: int | UserGroupMembersDict
can_subscribe_group: int | UserGroupMembersDict
creator_id: int | None
date_created: int
description: str
first_message_id: int | None
is_recently_active: bool
history_public_to_subscribers: bool
invite_only: bool
is_web_public: bool
message_retention_days: int | None
name: str
rendered_description: str
stream_id: int # `stream_id` represents `id` of the `Stream` object in `API_FIELDS`
stream_post_policy: int
# Computed fields not specified in `Stream.API_FIELDS`
is_announcement_only: bool
is_default: NotRequired[bool]
class APIStreamDict(DefaultStreamDict):
stream_weekly_traffic: int | None
class APISubscriptionDict(APIStreamDict):
"""Similar to StreamClientDict, it should contain all the fields specified in
`zerver.models.Subscription.API_FIELDS` and several additional fields.
"""
audible_notifications: bool | None
color: str
desktop_notifications: bool | None
email_notifications: bool | None
is_muted: bool
pin_to_top: bool
push_notifications: bool | None
wildcard_mentions_notify: bool | None
# Computed fields not specified in `Subscription.API_FIELDS`
in_home_view: bool
subscribers: list[int]
@dataclass
class SubscriptionInfo:
subscriptions: list[SubscriptionStreamDict]
unsubscribed: list[SubscriptionStreamDict]
never_subscribed: list[NeverSubscribedStreamDict]
class RealmPlaygroundDict(TypedDict):
id: int
name: str
pygments_language: str
url_template: str
@dataclass
class GroupPermissionSetting:
require_system_group: bool
allow_internet_group: bool
allow_nobody_group: bool
allow_everyone_group: bool
default_group_name: str
default_for_system_groups: str | None = None
allowed_system_groups: list[str] = field(default_factory=list)
@dataclass
class ServerSupportedPermissionSettings:
realm: dict[str, GroupPermissionSetting]
stream: dict[str, GroupPermissionSetting]
group: dict[str, GroupPermissionSetting]
class RawUserDict(TypedDict):
id: int
full_name: str
email: str
avatar_source: str
avatar_version: int
is_active: bool
role: int
is_bot: bool
timezone: str
date_joined: datetime
bot_owner_id: int | None
delivery_email: str
bot_type: int | None
long_term_idle: bool
email_address_visibility: int
class RemoteRealmDictValue(TypedDict):
can_push: bool
expected_end_timestamp: int | None
class AnalyticsDataUploadLevel(IntEnum):
NONE = 0
BASIC = 1
BILLING = 2
ALL = 3
@dataclass
class StreamMessageEditRequest:
is_content_edited: bool
is_topic_edited: bool
is_stream_edited: bool
is_message_moved: bool
topic_resolved: bool
topic_unresolved: bool
content: str
target_topic_name: str
target_stream: "Stream"
orig_content: str
orig_topic_name: str
orig_stream: "Stream"
propagate_mode: str
@dataclass
class DirectMessageEditRequest:
content: str
orig_content: str
is_content_edited: bool