diff --git a/lang/en/message.php b/lang/en/message.php index 86b80854e77..84a38b13f8c 100644 --- a/lang/en/message.php +++ b/lang/en/message.php @@ -69,6 +69,7 @@ $string['emailtagline'] = 'This is a copy of a message sent to you at "{$a->site $string['enabled'] = 'Enabled'; $string['errorcallingprocessor'] = 'Error calling defined output'; $string['errorconversationdoesnotexist'] = 'Conversation does not exist'; +$string['errormessagetoolong'] = 'The message is longer than the maximum allowed.'; $string['errortranslatingdefault'] = 'Error translating default setting provided by plugin, using system defaults instead.'; $string['eventgroupmessagesent'] = 'Group message sent'; $string['eventnotificationviewed'] = 'Notification viewed'; diff --git a/message/classes/api.php b/message/classes/api.php index e3d830055e7..69ef6f50973 100644 --- a/message/classes/api.php +++ b/message/classes/api.php @@ -93,6 +93,11 @@ class api { */ const MESSAGE_CONVERSATION_DISABLED = 0; + /** + * The max message length. + */ + const MESSAGE_MAX_LENGTH = 4096; + /** * Handles searching for messages in the message area. * diff --git a/message/classes/helper.php b/message/classes/helper.php index c6472a4de8b..be7c59e1dc2 100644 --- a/message/classes/helper.php +++ b/message/classes/helper.php @@ -773,7 +773,8 @@ class helper { 'notification' => $notification ], 'isdrawer' => $isdrawer, - 'showemojipicker' => !empty($CFG->allowemojipicker) + 'showemojipicker' => !empty($CFG->allowemojipicker), + 'messagemaxlength' => api::MESSAGE_MAX_LENGTH, ]; if ($sendtouser || $conversationid) { diff --git a/message/externallib.php b/message/externallib.php index a6222196332..00cd91b2e14 100644 --- a/message/externallib.php +++ b/message/externallib.php @@ -88,6 +88,14 @@ class core_message_external extends external_api { 'messages' => $messages ]); + // Validate messages content before posting them. + foreach ($params['messages'] as $message) { + // Check message length. + if (strlen($message['text']) > \core_message\api::MESSAGE_MAX_LENGTH) { + throw new moodle_exception('errormessagetoolong', 'message'); + } + } + $messages = []; foreach ($params['messages'] as $message) { $createdmessage = \core_message\api::send_message_to_conversation($USER->id, $params['conversationid'], $message['text'], @@ -187,6 +195,12 @@ class core_message_external extends external_api { $errormessage = get_string('touserdoesntexist', 'message', $message['touserid']); } + // Check message length. + if ($success && strlen($message['text']) > \core_message\api::MESSAGE_MAX_LENGTH) { + $success = false; + $errormessage = get_string('errormessagetoolong', 'message'); + } + // TODO MDL-31118 performance improvement - edit the function so we can pass an array instead userid // Check if the recipient can be messaged by the sender. if ($success && !\core_message\api::can_send_message($tousers[$message['touserid']]->id, $USER->id)) { diff --git a/message/templates/message_drawer_view_conversation_footer_content.mustache b/message/templates/message_drawer_view_conversation_footer_content.mustache index 4c83458dd9c..cd8636efbc3 100644 --- a/message/templates/message_drawer_view_conversation_footer_content.mustache +++ b/message/templates/message_drawer_view_conversation_footer_content.mustache @@ -56,6 +56,7 @@ aria-label="{{#str}} writeamessage, core_message {{/str}}" placeholder="{{#str}} writeamessage, core_message {{/str}}" style="resize: none" + maxlength="{{messagemaxlength}}" >