MDL-36941 core: convert existing api to use new table structure

Also deprecated the following functions -

1. message_move_userfrom_unread2read - It is not necessary
   for us to mark a message as read on user deletion.
2. message_get_blocked_users - Horrible logic used to
   determine if a user is blocked via reference on some
   randomly chosen 'isblocked' variable.
3. message_get_contacts - The same as above. This can be
   done in a much nicer way.
4. message_mark_message_read - We want two functions to do
   this to avoid confusing messages and notifications.
5. message_can_delete_message - This assumed the variable
   $message contained the 'useridto' property, which
   was present in the old table structure. We do not want
   future usages where a query is done on the new table
   and is simply passed as this won't contain this property.
6. message_delete_message - Same as above.
This commit is contained in:
Mark Nelson 2018-01-04 15:01:37 +08:00
parent 4cd439887a
commit 883ce42127
29 changed files with 1714 additions and 1096 deletions

View file

@ -69,34 +69,21 @@ class api {
$disabled = $user->emailstop;
}
if ($disabled) {
// Notifications are disabled, no need to run giant queries.
// Notifications are disabled.
return array();
}
$sql = "SELECT r.id, r.useridfrom, r.useridto,
r.subject, r.fullmessage, r.fullmessageformat,
r.fullmessagehtml, r.smallmessage, r.notification, r.contexturl,
r.contexturlname, r.timecreated, r.timeuserfromdeleted, r.timeusertodeleted,
r.component, r.eventtype, r.timeread
FROM {message_read} r
WHERE r.notification = 1
AND r.id IN (SELECT messageid FROM {message_popup} WHERE isread = 1)
AND r.useridto = :useridto1
UNION ALL
SELECT u.id, u.useridfrom, u.useridto,
u.subject, u.fullmessage, u.fullmessageformat,
u.fullmessagehtml, u.smallmessage, u.notification, u.contexturl,
u.contexturlname, u.timecreated, u.timeuserfromdeleted, u.timeusertodeleted,
u.component, u.eventtype, 0 as timeread
FROM {message} u
WHERE u.notification = 1
AND u.id IN (SELECT messageid FROM {message_popup} WHERE isread = 0)
AND u.useridto = :useridto2
$sql = "SELECT n.id, n.useridfrom, n.useridto,
n.subject, n.fullmessage, n.fullmessageformat,
n.fullmessagehtml, n.smallmessage, n.contexturl,
n.contexturlname, n.timecreated, n.component,
n.eventtype, n.timeread
FROM {notifications} n
WHERE n.id IN (SELECT messageid FROM {message_popup})
AND n.useridto = :useridto1
ORDER BY timecreated $sort, timeread $sort, id $sort";
$notifications = [];
// Use recordset here to ensure records with the same id aren't ignored because
// we can have id clashes between the message and message_read tables.
$records = $DB->get_recordset_sql($sql, $params, $offset, $limit);
foreach ($records as $record) {
$notifications[] = (object) $record;
@ -122,9 +109,9 @@ class api {
return $DB->count_records_sql(
"SELECT count(id)
FROM {message}
WHERE id IN (SELECT messageid FROM {message_popup} WHERE isread = 0)
AND useridto = ?",
FROM {notifications}
WHERE id IN (SELECT messageid FROM {message_popup} WHERE isread = 0)
AND useridto = ?",
[$useridto]
);
}

View file

@ -57,16 +57,7 @@ class popup_notification implements templatable, renderable {
}
public function export_for_template(\renderer_base $output) {
global $USER;
$context = clone $this->notification;
if ($context->useridto == $USER->id && $context->timeusertodeleted) {
$context->deleted = true;
} else {
$context->deleted = false;
}
$context->timecreatedpretty = get_string('ago', 'message', format_time(time() - $context->timecreated));
$context->text = message_format_message_text($context);
$context->read = $context->timeread ? true : false;

View file

@ -117,6 +117,9 @@ class message_popup_external extends external_api {
$notificationoutput = new \message_popup\output\popup_notification($notification);
$notificationcontext = $notificationoutput->export_for_template($renderer);
// Keep this for BC.
$notificationcontext->deleted = false;
$notificationcontexts[] = $notificationcontext;
}
}

View file

@ -55,6 +55,7 @@ class message_output_popup extends message_output {
$procmessage = new stdClass();
$procmessage->unreadmessageid = $eventdata->savedmessageid;
$procmessage->processorid = $processorid;
$procmessage->notification = $eventdata->notification;
//save this message for later delivery
$DB->insert_record('message_working', $procmessage);
@ -122,7 +123,6 @@ class message_output_popup extends message_output {
global $DB;
if ($record = $DB->get_record('message_popup', ['messageid' => $event->other['messageid']])) {
// The id can change when the moving to the message_read table.
$record->messageid = $event->objectid;
$record->isread = 1;
$DB->update_record('message_popup', $record);

View file

@ -50,7 +50,7 @@ trait message_popup_test_helper {
$record->smallmessage = $message;
$record->timecreated = $timecreated ? $timecreated : time();
$id = $DB->insert_record('message', $record);
$id = $DB->insert_record('notifications', $record);
$popup = new stdClass();
$popup->messageid = $id;
@ -89,7 +89,10 @@ trait message_popup_test_helper {
$record->timecreated = $timecreated ? $timecreated : time();
$record->timeread = $timeread ? $timeread : time();
$id = $DB->insert_record('message_read', $record);
$id = $DB->insert_record('notifications', $record);
// Mark it as read.
\core_message\api::mark_notification_as_read($userto->id, $id);
$popup = new stdClass();
$popup->messageid = $id;

View file

@ -13,7 +13,7 @@ Feature: Message popover unread messages
And I send "Test message" message to "Student 1" user
And I log out
Scenario: Notification popover shows correct unread count
Scenario: Message popover shows correct unread count
When I log in as "student2"
And I send "Test message 2" message to "Student 1" user
And I log out
@ -38,7 +38,7 @@ Feature: Message popover unread messages
# Confirm the message was loaded in the messaging page.
And I should see "Test message" in the "[data-region='message-text']" "css_element"
Scenario: Mark all notifications as read
Scenario: Mark all messages as read
When I log in as "student1"
# Open the popover.
And I open the message popover