mirror of
https://github.com/zulip/zulip.git
synced 2025-11-05 22:43:42 +00:00
python: Reformat with Black, except quotes.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
committed by
Tim Abbott
parent
5028c081cb
commit
11741543da
@@ -31,20 +31,22 @@ The following functions are for user-facing APIs
|
||||
where we'll want to support "subject" for a while.
|
||||
'''
|
||||
|
||||
|
||||
def get_topic_from_message_info(message_info: Dict[str, Any]) -> str:
|
||||
'''
|
||||
"""
|
||||
Use this where you are getting dicts that are based off of messages
|
||||
that may come from the outside world, especially from third party
|
||||
APIs and bots.
|
||||
|
||||
We prefer 'topic' to 'subject' here. We expect at least one field
|
||||
to be present (or the caller must know how to handle KeyError).
|
||||
'''
|
||||
"""
|
||||
if 'topic' in message_info:
|
||||
return message_info['topic']
|
||||
|
||||
return message_info['subject']
|
||||
|
||||
|
||||
def REQ_topic() -> Optional[str]:
|
||||
# REQ handlers really return a REQ, but we
|
||||
# lie to make the rest of the type matching work.
|
||||
@@ -55,6 +57,7 @@ def REQ_topic() -> Optional[str]:
|
||||
default=None,
|
||||
)
|
||||
|
||||
|
||||
'''
|
||||
TRY TO KEEP THIS DIVIDING LINE.
|
||||
|
||||
@@ -68,52 +71,71 @@ using "subject" in the DB sense, and nothing customer facing.
|
||||
DB_TOPIC_NAME = "subject"
|
||||
MESSAGE__TOPIC = 'message__subject'
|
||||
|
||||
|
||||
def topic_match_sa(topic_name: str) -> "ColumnElement[bool]":
|
||||
# _sa is short for SQLAlchemy, which we use mostly for
|
||||
# queries that search messages
|
||||
topic_cond = func.upper(column("subject", Text)) == func.upper(literal(topic_name))
|
||||
return topic_cond
|
||||
|
||||
|
||||
def topic_column_sa() -> "ColumnElement[str]":
|
||||
return column("subject", Text)
|
||||
|
||||
|
||||
def filter_by_exact_message_topic(query: QuerySet, message: Message) -> QuerySet:
|
||||
topic_name = message.topic_name()
|
||||
return query.filter(subject=topic_name)
|
||||
|
||||
|
||||
def filter_by_topic_name_via_message(query: QuerySet, topic_name: str) -> QuerySet:
|
||||
return query.filter(message__subject__iexact=topic_name)
|
||||
|
||||
|
||||
def messages_for_topic(stream_recipient_id: int, topic_name: str) -> QuerySet:
|
||||
return Message.objects.filter(
|
||||
recipient_id=stream_recipient_id,
|
||||
subject__iexact=topic_name,
|
||||
)
|
||||
|
||||
|
||||
def save_message_for_edit_use_case(message: Message) -> None:
|
||||
message.save(update_fields=[TOPIC_NAME, "content", "rendered_content",
|
||||
"rendered_content_version", "last_edit_time",
|
||||
"edit_history", "has_attachment", "has_image",
|
||||
"has_link", "recipient_id"])
|
||||
message.save(
|
||||
update_fields=[
|
||||
TOPIC_NAME,
|
||||
"content",
|
||||
"rendered_content",
|
||||
"rendered_content_version",
|
||||
"last_edit_time",
|
||||
"edit_history",
|
||||
"has_attachment",
|
||||
"has_image",
|
||||
"has_link",
|
||||
"recipient_id",
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
def user_message_exists_for_topic(user_profile: UserProfile,
|
||||
recipient_id: int,
|
||||
topic_name: str) -> bool:
|
||||
def user_message_exists_for_topic(
|
||||
user_profile: UserProfile, recipient_id: int, topic_name: str
|
||||
) -> bool:
|
||||
return UserMessage.objects.filter(
|
||||
user_profile=user_profile,
|
||||
message__recipient_id=recipient_id,
|
||||
message__subject__iexact=topic_name,
|
||||
).exists()
|
||||
|
||||
def update_messages_for_topic_edit(message: Message,
|
||||
propagate_mode: str,
|
||||
orig_topic_name: str,
|
||||
topic_name: Optional[str],
|
||||
new_stream: Optional[Stream],
|
||||
old_recipient_id: Optional[int],
|
||||
edit_history_event: Dict[str, Any],
|
||||
last_edit_time: datetime) -> List[Message]:
|
||||
|
||||
def update_messages_for_topic_edit(
|
||||
message: Message,
|
||||
propagate_mode: str,
|
||||
orig_topic_name: str,
|
||||
topic_name: Optional[str],
|
||||
new_stream: Optional[Stream],
|
||||
old_recipient_id: Optional[int],
|
||||
edit_history_event: Dict[str, Any],
|
||||
last_edit_time: datetime,
|
||||
) -> List[Message]:
|
||||
assert (new_stream and old_recipient_id) or (not new_stream and not old_recipient_id)
|
||||
|
||||
if old_recipient_id is not None:
|
||||
@@ -121,11 +143,11 @@ def update_messages_for_topic_edit(message: Message,
|
||||
else:
|
||||
recipient_id = message.recipient_id
|
||||
|
||||
propagate_query = Q(recipient_id = recipient_id, subject__iexact = orig_topic_name)
|
||||
propagate_query = Q(recipient_id=recipient_id, subject__iexact=orig_topic_name)
|
||||
if propagate_mode == 'change_all':
|
||||
propagate_query = propagate_query & ~Q(id = message.id)
|
||||
propagate_query = propagate_query & ~Q(id=message.id)
|
||||
if propagate_mode == 'change_later':
|
||||
propagate_query = propagate_query & Q(id__gt = message.id)
|
||||
propagate_query = propagate_query & Q(id__gt=message.id)
|
||||
|
||||
messages = Message.objects.filter(propagate_query).select_related()
|
||||
|
||||
@@ -160,6 +182,7 @@ def update_messages_for_topic_edit(message: Message,
|
||||
|
||||
return messages_list
|
||||
|
||||
|
||||
def generate_topic_history_from_db_rows(rows: List[Tuple[str, int]]) -> List[Dict[str, Any]]:
|
||||
canonical_topic_names: Dict[str, Tuple[int, str]] = {}
|
||||
|
||||
@@ -174,12 +197,12 @@ def generate_topic_history_from_db_rows(rows: List[Tuple[str, int]]) -> List[Dic
|
||||
|
||||
history = []
|
||||
for canonical_topic, (max_message_id, topic_name) in canonical_topic_names.items():
|
||||
history.append(dict(
|
||||
name=topic_name,
|
||||
max_id=max_message_id),
|
||||
history.append(
|
||||
dict(name=topic_name, max_id=max_message_id),
|
||||
)
|
||||
return sorted(history, key=lambda x: -x['max_id'])
|
||||
|
||||
|
||||
def get_topic_history_for_public_stream(recipient_id: int) -> List[Dict[str, Any]]:
|
||||
cursor = connection.cursor()
|
||||
query = '''
|
||||
@@ -201,9 +224,10 @@ def get_topic_history_for_public_stream(recipient_id: int) -> List[Dict[str, Any
|
||||
|
||||
return generate_topic_history_from_db_rows(rows)
|
||||
|
||||
def get_topic_history_for_stream(user_profile: UserProfile,
|
||||
recipient_id: int,
|
||||
public_history: bool) -> List[Dict[str, Any]]:
|
||||
|
||||
def get_topic_history_for_stream(
|
||||
user_profile: UserProfile, recipient_id: int, public_history: bool
|
||||
) -> List[Dict[str, Any]]:
|
||||
if public_history:
|
||||
return get_topic_history_for_public_stream(recipient_id)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user