MDL-63303 message: add enter to send user preference
authorRyan Wyllie <ryan@moodle.com>
Thu, 8 Nov 2018 07:26:21 +0000 (15:26 +0800)
committerRyan Wyllie <ryan@moodle.com>
Thu, 15 Nov 2018 06:43:21 +0000 (14:43 +0800)
12 files changed:
lang/en/message.php
message/amd/build/message_drawer_events.min.js
message/amd/build/message_drawer_view_conversation.min.js
message/amd/build/message_drawer_view_settings.min.js
message/amd/src/message_drawer_events.js
message/amd/src/message_drawer_view_conversation.js
message/amd/src/message_drawer_view_settings.js
message/classes/privacy/provider.php
message/lib.php
message/templates/message_drawer_view_conversation_footer.mustache
message/templates/message_drawer_view_settings_body.mustache
message/tests/privacy_provider_test.php

index 6679814..77a748e 100644 (file)
@@ -233,6 +233,7 @@ $string['unknownuser'] = 'Unknown user';
 $string['unreadnotification'] = 'Unread notification: {$a}';
 $string['unreadnewgroupconversationmessage'] = 'New message from {$a->name} in {$a->conversationname}';
 $string['unreadnewmessage'] = 'New message from {$a}';
+$string['useentertosend'] = 'Use enter to send';
 $string['usercantbemessaged'] = 'You can\'t message {$a} due to their message preferences. Try adding them as a contact.';
 $string['userisblockingyou'] = 'This user has blocked you from sending messages to them';
 $string['userisblockingyounoncontact'] = '{$a} only accepts messages from their contacts.';
index f11f606..f4f20c4 100644 (file)
Binary files a/message/amd/build/message_drawer_events.min.js and b/message/amd/build/message_drawer_events.min.js differ
index 6e19ab7..c42999e 100644 (file)
Binary files a/message/amd/build/message_drawer_view_conversation.min.js and b/message/amd/build/message_drawer_view_conversation.min.js differ
index 2d465d3..7afce0c 100644 (file)
Binary files a/message/amd/build/message_drawer_view_settings.min.js and b/message/amd/build/message_drawer_view_settings.min.js differ
index 7362ba9..02c37bc 100644 (file)
@@ -35,6 +35,7 @@ define([], function() {
         CONVERSATION_READ: 'message-drawer-conversation-read',
         CONVERSATION_SET_FAVOURITE: 'message-drawer-conversation-set-favourite',
         CONVERSATION_UNSET_FAVOURITE: 'message-drawer-conversation-unset-favourite',
+        PREFERENCES_UPDATED: 'message-drawer-preferences-updated',
         ROUTE_CHANGED: 'message-drawer-route-change',
         SHOW: 'message-drawer-show',
         HIDE: 'message-drawer-hide',
index 77e49ea..8aa2e60 100644 (file)
@@ -1225,7 +1225,8 @@ function(
             CustomEvents.events.activate
         ]);
         CustomEvents.define(footer, [
-            CustomEvents.events.activate
+            CustomEvents.events.activate,
+            CustomEvents.events.enter
         ]);
         CustomEvents.define(messagesContainer, [
             CustomEvents.events.scrollTop,
@@ -1273,6 +1274,13 @@ function(
             footer.on(CustomEvents.events.activate, selector, handlerFunction);
         });
 
+        footer.on(CustomEvents.events.enter, SELECTORS.MESSAGE_TEXT_AREA, function(e, data) {
+            var enterToSend = footer.attr('data-enter-to-send');
+            if (enterToSend == true) {
+                handleSendMessage(e, data);
+            }
+        });
+
         PubSub.subscribe(MessageDrawerEvents.ROUTE_CHANGED, function(newRouteData) {
             if (newMessagesPollTimer) {
                 if (newRouteData.route == MessageDrawerRoutes.VIEW_CONVERSATION) {
@@ -1282,6 +1290,17 @@ function(
                 }
             }
         });
+
+        PubSub.subscribe(MessageDrawerEvents.PREFERENCES_UPDATED, function(preferences) {
+            var filteredPreferences = preferences.filter(function(preference) {
+                return preference.type == 'message_entertosend';
+            });
+            var enterToSendPreference = filteredPreferences.length ? filteredPreferences[0] : null;
+
+            if (enterToSendPreference) {
+                footer.attr('data-enter-to-send', enterToSendPreference.value);
+            }
+        });
     };
 
     /**
index 3df6cb4..cd7eda6 100644 (file)
@@ -25,22 +25,27 @@ define(
     'jquery',
     'core/notification',
     'core/str',
+    'core/pubsub',
     'core_message/message_repository',
     'core/custom_interaction_events',
+    'core_message/message_drawer_events'
 ],
 function(
     $,
     Notification,
     Str,
+    PubSub,
     Repository,
-    CustomEvents
+    CustomEvents,
+    MessageDrawerEvents
 ) {
 
     var SELECTORS = {
         SETTINGS: '[data-region="settings"]',
         PREFERENCE_CONTROL: '[data-region="preference-control"]',
         PRIVACY_PREFERENCE: '[data-preference="blocknoncontacts"] input[type="radio"]',
-        EMAIL_ENABLED_PREFERENCE: '[data-preference="emailnotifications"] input[type="checkbox"]'
+        EMAIL_ENABLED_PREFERENCE: '[data-preference="emailnotifications"] input[type="checkbox"]',
+        ENTER_TO_SEND_PREFERENCE: '[data-preference="entertosend"] input[type="checkbox"]',
     };
 
     var PREFERENCES_EMAIL = {
@@ -89,6 +94,10 @@ function(
                 }, []);
 
                 Repository.savePreferences(loggedInUserId, preferences)
+                    .then(function() {
+                        PubSub.publish(MessageDrawerEvents.PREFERENCES_UPDATED, preferences);
+                        return;
+                    })
                     .catch(Notification.exception);
             }
         );
@@ -103,6 +112,28 @@ function(
                 ];
 
                 Repository.savePreferences(loggedInUserId, preferences)
+                    .then(function() {
+                        PubSub.publish(MessageDrawerEvents.PREFERENCES_UPDATED, preferences);
+                        return;
+                    })
+                    .catch(Notification.exception);
+            }
+        );
+
+        settingsContainer.on(CustomEvents.events.activate, SELECTORS.ENTER_TO_SEND_PREFERENCE, function(e) {
+                var newValue = $(e.target).prop('checked');
+                var preferences = [
+                    {
+                        type: 'message_entertosend',
+                        value: newValue
+                    }
+                ];
+
+                Repository.savePreferences(loggedInUserId, preferences)
+                    .then(function() {
+                        PubSub.publish(MessageDrawerEvents.PREFERENCES_UPDATED, preferences);
+                        return;
+                    })
                     .catch(Notification.exception);
             }
         );
index 5c24544..b3a0e8a 100644 (file)
@@ -158,7 +158,11 @@ class provider implements
     public static function export_user_preferences(int $userid) {
         $preferences = get_user_preferences(null, null, $userid);
         foreach ($preferences as $name => $value) {
-            if ((substr($name, 0, 16) == 'message_provider') || ($name == 'message_blocknoncontacts')) {
+            if (
+                (substr($name, 0, 16) == 'message_provider') ||
+                ($name == 'message_blocknoncontacts') ||
+                ($name == 'message_entertosend')
+            ) {
                 writer::export_user_preference(
                     'core_message',
                     $name,
index fc4bebe..da474fa 100644 (file)
@@ -736,6 +736,11 @@ function core_message_user_preferences() {
             return $value;
         }
     );
+    $preferences['message_entertosend'] = array(
+        'type' => PARAM_BOOL,
+        'null' => NULL_NOT_ALLOWED,
+        'default' => false
+    );
     $preferences['/^message_provider_([\w\d_]*)_logged(in|off)$/'] = array('isregex' => true, 'type' => PARAM_NOTAGS,
         'null' => NULL_NOT_ALLOWED, 'default' => 'none',
         'permissioncallback' => function ($user, $preferencename) {
@@ -859,6 +864,9 @@ function core_message_before_standard_top_of_body_html() {
     $emailloggedoff = get_user_preferences('message_provider_moodle_instantmessage_loggedoff', 'none', $USER->id);
     $emailenabled = $emailloggedin == 'email' && $emailloggedoff == 'email';
 
+    // Enter to send.
+    $entertosend = get_user_preferences('message_entertosend', false, $USER->id);
+
     return $renderer->render_from_template('core_message/message_drawer', [
         'contactrequestcount' => $requestcount,
         'loggedinuser' => [
@@ -898,7 +906,8 @@ function core_message_before_standard_top_of_body_html() {
         ],
         'settings' => [
             'privacy' => $choices,
-            'emailenabled' => $emailenabled
+            'emailenabled' => $emailenabled,
+            'entertosend' => $entertosend
         ]
     ]);
 }
index 585678a..722a75b 100644 (file)
@@ -39,6 +39,7 @@
     class="hidden border-top bg-white position-relative"
     aria-hidden="true"
     data-region="view-conversation"
+    data-enter-to-send="{{settings.entertosend}}"
 >
     <div class="hidden p-2" data-region="content-messages-footer-container">
         {{> core_message/message_drawer_view_conversation_footer_content }}
index 74a838c..92443cf 100644 (file)
@@ -60,7 +60,7 @@
                 </div>
             {{/privacy}}
         </div>
-        <h3 class="mb-2 mt-5 h6 font-weight-bold">{{#str}}categoryemail, admin{{/str}}</h3>
+        <h3 class="mb-2 mt-4 h6 font-weight-bold">{{#str}}categoryemail, admin{{/str}}</h3>
         <div
             data-region="preference-control"
             data-preference="emailnotifications"
                 </label>
             </span>
         </div>
+        <h3 class="mb-2 mt-4 h6 font-weight-bold">{{#str}} general, core {{/str}}</h3>
+        <div
+            data-region="preference-control"
+            data-preference="entertosend"
+        >
+            <span class="switch">
+                <input type="checkbox"
+                    id="enter-to-send-{{uniqid}}"
+                    {{#entertosend}}checked{{/entertosend}}
+                >
+                <label for="enter-to-send-{{uniqid}}">
+                    {{#str}} useentertosend, core_message {{/str}}
+                </label>
+            </span>
+        </div>
     </div>
 {{/settings}}
 </div>
\ No newline at end of file
index 5005483..d069f65 100644 (file)
@@ -161,6 +161,7 @@ class core_message_privacy_provider_testcase extends \core_privacy\tests\provide
         set_user_preference('message_provider_moodle_instantmessage_loggedin', 'airnotifier', $USER->id);
         set_user_preference('message_provider_moodle_instantmessage_loggedoff', 'popup', $USER->id);
         set_user_preference('message_blocknoncontacts', \core_message\api::MESSAGE_PRIVACY_ONLYCONTACTS, $USER->id);
+        set_user_preference('message_entertosend', true, $USER->id);
         set_user_preference('message_provider_moodle_instantmessage_loggedoff', 'inbound', $user->id);
 
         // Set an unrelated preference.
@@ -175,10 +176,11 @@ class core_message_privacy_provider_testcase extends \core_privacy\tests\provide
         $prefs = (array) $writer->get_user_preferences('core_message');
 
         // Check only 3 preferences exist.
-        $this->assertCount(3, $prefs);
+        $this->assertCount(4, $prefs);
         $this->assertArrayHasKey('message_provider_moodle_instantmessage_loggedin', $prefs);
         $this->assertArrayHasKey('message_provider_moodle_instantmessage_loggedoff', $prefs);
         $this->assertArrayHasKey('message_blocknoncontacts', $prefs);
+        $this->assertArrayHasKey('message_entertosend', $prefs);
 
         foreach ($prefs as $key => $pref) {
             if ($key == 'message_provider_moodle_instantmessage_loggedin') {