scheduledmessages: Add funcs to verify and schedule messages.

We add two functions:

1.) check_schedule_message(): This function is responsible for
doing the essential initial checkes to verify the validity of
the message. These checkes include things like if user is
allowed to send messages to some stream or not or if the user is
a super_user. All this is basically done by further calling
check_message() with appropriate parameters. This is on the same
lines as is check_send_message().

2.) do_schedule_messages(): This function is responsible for
creating ScheduleMessage table rows for a list of messages that
are to be scheduled. This basically accumulates the ScheduleMessage
objects in a list and then bulk creates the rows.
This commit is contained in:
Aditya Bansal 2018-01-02 01:12:48 +05:30 committed by showell
parent 6c36cf3c82
commit c64766e55e
1 changed files with 40 additions and 1 deletions

View File

@ -55,7 +55,7 @@ from zerver.lib.user_groups import create_user_group, access_user_group_by_id
from zerver.models import Realm, RealmEmoji, Stream, UserProfile, UserActivity, \
RealmDomain, \
Subscription, Recipient, Message, Attachment, UserMessage, RealmAuditLog, \
UserHotspot, \
UserHotspot, ScheduledMessage, \
Client, DefaultStream, DefaultStreamGroup, UserPresence, PushDeviceToken, \
ScheduledEmail, MAX_SUBJECT_LENGTH, \
MAX_MESSAGE_LENGTH, get_client, get_stream, get_personal_recipient, get_huddle, \
@ -1020,6 +1020,26 @@ def get_service_bot_events(sender: UserProfile, service_bot_tuples: List[Tuple[i
return event_dict
def do_schedule_messages(messages: Sequence[Optional[MutableMapping[str, Any]]]) -> List[int]:
scheduled_messages = [] # type: List[ScheduledMessage]
for message in messages:
scheduled_message = ScheduledMessage()
scheduled_message.sender = message['message'].sender
scheduled_message.recipient = message['message'].recipient
scheduled_message.subject = message['message'].subject
scheduled_message.content = message['message'].content
scheduled_message.sending_client = message['message'].sending_client
scheduled_message.stream = message['stream']
scheduled_message.realm = message['realm']
scheduled_message.scheduled_timestamp = message['deliver_at']
scheduled_messages.append(scheduled_message)
ScheduledMessage.objects.bulk_create(scheduled_messages)
return [scheduled_message.id for scheduled_message in scheduled_messages]
def do_send_messages(messages_maybe_none: Sequence[Optional[MutableMapping[str, Any]]],
email_gateway: Optional[bool]=False) -> List[int]:
# Filter out messages which didn't pass internal_prep_message properly
@ -1659,6 +1679,25 @@ def check_send_message(sender: UserProfile, client: Client, message_type_name: T
forwarder_user_profile, local_id, sender_queue_id)
return do_send_messages([message])[0]
def check_schedule_message(sender: UserProfile, client: Client,
message_type_name: Text, message_to: Sequence[Text],
topic_name: Optional[Text], message_content: Text,
deliver_at: datetime.datetime,
realm: Optional[Realm]=None,
forwarder_user_profile: Optional[UserProfile]=None
) -> int:
addressee = Addressee.legacy_build(
sender,
message_type_name,
message_to,
topic_name)
message = check_message(sender, client, addressee,
message_content, realm=realm,
forwarder_user_profile=forwarder_user_profile)
message['deliver_at'] = deliver_at
return do_schedule_messages([message])[0]
def check_stream_name(stream_name: Text) -> None:
if stream_name.strip() == "":
raise JsonableError(_("Invalid stream name '%s'" % (stream_name)))