Eliminate the parse_usermessage_flags interface.

This commit puts the guts of parse_usermessage_flags into
UserMessage.flags_list_for_flags, since it was slightly faster
than the old implementation and produced the same results.
(Both algorithms were super fast, actually.)

And then all callers use the model method now.
This commit is contained in:
Steve Howell 2017-11-07 09:40:39 -08:00 committed by Tim Abbott
parent ae0b27a7ed
commit 4108797218
2 changed files with 10 additions and 20 deletions

View File

@ -1318,18 +1318,19 @@ class AbstractUserMessage(models.Model):
return self.flags_list_for_flags(flags) return self.flags_list_for_flags(flags)
@staticmethod @staticmethod
def flags_list_for_flags(flags): def flags_list_for_flags(val):
# type: (int) -> List[str] # type: (int) -> List[str]
''' '''
This function is highly optimized, because it actually slows down This function is highly optimized, because it actually slows down
sending messages in a naive implementation. sending messages in a naive implementation.
''' '''
names = AbstractUserMessage.ALL_FLAGS flags = []
return [ mask = 1
names[i] for flag in UserMessage.ALL_FLAGS:
for i in range(len(names)) if val & mask:
if flags & (2 ** i) flags.append(flag)
] mask <<= 1
return flags
def __str__(self): def __str__(self):
# type: () -> Text # type: () -> Text
@ -1347,17 +1348,6 @@ class UserMessage(AbstractUserMessage):
message = models.ForeignKey(Message, on_delete=CASCADE) # type: Message message = models.ForeignKey(Message, on_delete=CASCADE) # type: Message
def parse_usermessage_flags(val):
# type: (int) -> List[str]
flags = []
mask = 1
for flag in UserMessage.ALL_FLAGS:
if val & mask:
flags.append(flag)
mask <<= 1
return flags
class AbstractAttachment(models.Model): class AbstractAttachment(models.Model):
file_name = models.TextField(db_index=True) # type: Text file_name = models.TextField(db_index=True) # type: Text
# path_id is a storage location agnostic representation of the path of the file. # path_id is a storage location agnostic representation of the path of the file.

View File

@ -36,7 +36,7 @@ from zerver.lib.validator import \
check_list, check_int, check_dict, check_string, check_bool check_list, check_int, check_dict, check_string, check_bool
from zerver.models import Message, UserProfile, Stream, Subscription, \ from zerver.models import Message, UserProfile, Stream, Subscription, \
Realm, RealmDomain, Recipient, UserMessage, bulk_get_recipients, get_personal_recipient, \ Realm, RealmDomain, Recipient, UserMessage, bulk_get_recipients, get_personal_recipient, \
get_stream, parse_usermessage_flags, email_to_domain, get_realm, get_active_streams, \ get_stream, email_to_domain, get_realm, get_active_streams, \
get_user_including_cross_realm, get_stream_recipient get_user_including_cross_realm, get_stream_recipient
from sqlalchemy import func from sqlalchemy import func
@ -739,7 +739,7 @@ def get_messages_backend(request, user_profile,
for row in query_result: for row in query_result:
message_id = row[0] message_id = row[0]
flags = row[1] flags = row[1]
user_message_flags[message_id] = parse_usermessage_flags(flags) user_message_flags[message_id] = UserMessage.flags_list_for_flags(flags)
message_ids.append(message_id) message_ids.append(message_id)
search_fields = dict() # type: Dict[int, Dict[str, Text]] search_fields = dict() # type: Dict[int, Dict[str, Text]]