Skip to content

Commit

Permalink
MDL-63303 message: add enter to send user preference
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanwyllie committed Nov 15, 2018
1 parent f074d6f commit 8c8939c
Show file tree
Hide file tree
Showing 12 changed files with 93 additions and 10 deletions.
1 change: 1 addition & 0 deletions lang/en/message.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@
$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.';
Expand Down
2 changes: 1 addition & 1 deletion message/amd/build/message_drawer_events.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion message/amd/build/message_drawer_view_conversation.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion message/amd/build/message_drawer_view_settings.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions message/amd/src/message_drawer_events.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
21 changes: 20 additions & 1 deletion message/amd/src/message_drawer_view_conversation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
}
});
};

/**
Expand Down
35 changes: 33 additions & 2 deletions message/amd/src/message_drawer_view_settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -89,6 +94,10 @@ function(
}, []);

Repository.savePreferences(loggedInUserId, preferences)
.then(function() {
PubSub.publish(MessageDrawerEvents.PREFERENCES_UPDATED, preferences);
return;
})
.catch(Notification.exception);
}
);
Expand All @@ -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);
}
);
Expand Down
6 changes: 5 additions & 1 deletion message/classes/privacy/provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,11 @@ public static function get_metadata(collection $items) : collection {
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,
Expand Down
11 changes: 10 additions & 1 deletion message/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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' => [
Expand Down Expand Up @@ -898,7 +906,8 @@ function core_message_before_standard_top_of_body_html() {
],
'settings' => [
'privacy' => $choices,
'emailenabled' => $emailenabled
'emailenabled' => $emailenabled,
'entertosend' => $entertosend
]
]);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
17 changes: 16 additions & 1 deletion message/templates/message_drawer_view_settings_body.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -75,6 +75,21 @@
</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>
4 changes: 3 additions & 1 deletion message/tests/privacy_provider_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ public function test_export_user_preferences() {
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.
Expand All @@ -175,10 +176,11 @@ public function test_export_user_preferences() {
$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') {
Expand Down

0 comments on commit 8c8939c

Please sign in to comment.