Skip to content

Commit

Permalink
Merge branch 'MDL-63466-master' of https://github.com/snake/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewnicols committed Oct 26, 2018
2 parents b272efb + b95e4c7 commit 621e7eb
Show file tree
Hide file tree
Showing 15 changed files with 1,314 additions and 38 deletions.
2 changes: 1 addition & 1 deletion lang/en/cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
$string['cachedef_groupdata'] = 'Course group information';
$string['cachedef_htmlpurifier'] = 'HTML Purifier - cleaned content';
$string['cachedef_langmenu'] = 'List of available languages';
$string['cachedef_message_time_last_message_between_users'] = 'Time created for most recent message between users';
$string['cachedef_message_time_last_message_in_conversation'] = 'Time created for most recent message in a conversation';
$string['cachedef_locking'] = 'Locking';
$string['cachedef_message_processors_enabled'] = "Message processors enabled status";
$string['cachedef_contextwithinsights'] = 'Context with insights';
Expand Down
1 change: 1 addition & 0 deletions lang/en/message.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
$string['contactblocked'] = 'Contact blocked';
$string['contactrequests'] = 'Contact requests';
$string['contacts'] = 'Contacts';
$string['conversationdoesntexist'] = 'Conversation does not exist';
$string['defaultmessageoutputs'] = 'Default message outputs';
$string['defaults'] = 'Defaults';
$string['deleteallconfirm'] = "Are you sure you would like to delete this entire conversation?";
Expand Down
9 changes: 8 additions & 1 deletion lib/classes/message/message.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,16 @@
* component string Component name. must exist in message_providers
* name string Message type name. must exist in message_providers
* userfrom object|int The user sending the message
* userto object|int The message recipient
* userto object|int The message recipient. This is mandatory for NOTIFICACIONS and 1:1 personal messages.
* subject string The message subject
* fullmessage string The full message in a given format
* fullmessageformat int The format if the full message (FORMAT_MOODLE, FORMAT_HTML, ..)
* fullmessagehtml string The full version (the message processor will choose with one to use)
* smallmessage string The small version of the message
*
* Required parameters of the $eventdata object for PERSONAL MESSAGES:
* convid int The conversation identifier where this message will be sent
*
* Optional parameters of the $eventdata object:
* notification bool Should the message be considered as a notification rather than a personal message
* contexturl string If this is a notification then you can specify a url to view the event.
Expand Down Expand Up @@ -71,6 +74,9 @@ class message {
/** @var object|int The user who is sending this message. */
private $userfrom;

/** @var int The conversation id where userfrom is sending this message. */
private $convid;

/** @var object|int The user who is receiving from which is sending this message. */
private $userto;

Expand Down Expand Up @@ -123,6 +129,7 @@ class message {
'component',
'name',
'userfrom',
'convid',
'userto',
'subject',
'fullmessage',
Expand Down
8 changes: 4 additions & 4 deletions lib/db/caches.php
Original file line number Diff line number Diff line change
Expand Up @@ -342,12 +342,12 @@
'staticaccelerationsize' => 3
),

// Caches the time of the last message between two users.
'message_time_last_message_between_users' => array(
// Caches the time of the last message in a conversation.
'message_time_last_message_in_conversation' => array(
'mode' => cache_store::MODE_APPLICATION,
'simplekeys' => true, // The id of the sender and recipient is used.
'simplekeys' => true, // The conversation id is used.
'simplevalues' => true,
'datasource' => '\core_message\time_last_message_between_users',
'datasource' => '\core_message\time_last_message_in_conversation',
),

// Caches font awesome icons.
Expand Down
9 changes: 9 additions & 0 deletions lib/db/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -1127,6 +1127,15 @@
'ajax' => true,
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE),
),
'core_message_get_conversation_messages' => array(
'classname' => 'core_message_external',
'methodname' => 'get_conversation_messages',
'classpath' => 'message/externallib.php',
'description' => 'Retrieve the conversation messages and relevant member information',
'type' => 'read',
'ajax' => true,
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE),
),
'core_message_unblock_user' => array(
'classname' => 'core_message_external',
'methodname' => 'unblock_user',
Expand Down
11 changes: 5 additions & 6 deletions lib/messagelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,11 +292,11 @@ function message_send(\core\message\message $eventdata) {

// Only cache messages, not notifications.
if (!$eventdata->notification) {
// Cache the timecreated value of the last message between these two users.
$cache = cache::make('core', 'message_time_last_message_between_users');
$key = \core_message\helper::get_last_message_time_created_cache_key($eventdata->userfrom->id,
$eventdata->userto->id);
$cache->set($key, $tabledata->timecreated);
if (!empty($eventdata->convid)) {
// Cache the timecreated value of the last message in this conversation.
$cache = cache::make('core', 'message_time_last_message_in_conversation');
$cache->set($eventdata->convid, $tabledata->timecreated);
}
}

// Store unread message just in case we get a fatal error any time later.
Expand All @@ -307,7 +307,6 @@ function message_send(\core\message\message $eventdata) {
return \core\message\manager::send_message($eventdata, $tabledata, $processorlist);
}


/**
* Updates the message_providers table with the current set of message providers
*
Expand Down
80 changes: 75 additions & 5 deletions message/classes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,7 @@ public static function get_non_contacts_with_unread_message_count($userid, $limi
/**
* Returns the messages to display in the message area.
*
* @deprecated since 3.6
* @param int $userid the current user
* @param int $otheruserid the other user
* @param int $limitfrom
Expand All @@ -615,13 +616,22 @@ public static function get_non_contacts_with_unread_message_count($userid, $limi
* @return array
*/
public static function get_messages($userid, $otheruserid, $limitfrom = 0, $limitnum = 0,
$sort = 'timecreated ASC', $timefrom = 0, $timeto = 0) {
$sort = 'timecreated ASC', $timefrom = 0, $timeto = 0) {
debugging('\core_message\api::get_messages() is deprecated, please use ' .
'\core_message\api::get_conversation_messages() instead.', DEBUG_DEVELOPER);

if (!empty($timefrom)) {
// Get the conversation between userid and otheruserid.
$userids = [$userid, $otheruserid];
if (!$conversationid = self::get_conversation_between_users($userids)) {
// This method was always used for individual conversations.
$conversation = self::create_conversation(self::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL, $userids);
$conversationid = $conversation->id;
}

// Check the cache to see if we even need to do a DB query.
$cache = \cache::make('core', 'message_time_last_message_between_users');
$key = helper::get_last_message_time_created_cache_key($otheruserid, $userid);
$lastcreated = $cache->get($key);
$cache = \cache::make('core', 'message_time_last_message_in_conversation');
$lastcreated = $cache->get($conversationid);

// The last known message time is earlier than the one being requested so we can
// just return an empty result set rather than having to query the DB.
Expand All @@ -633,21 +643,59 @@ public static function get_messages($userid, $otheruserid, $limitfrom = 0, $limi
$arrmessages = array();
if ($messages = helper::get_messages($userid, $otheruserid, 0, $limitfrom, $limitnum,
$sort, $timefrom, $timeto)) {

$arrmessages = helper::create_messages($userid, $messages);
}

return $arrmessages;
}

/**
* Returns the messages for the defined conversation.
*
* @param int $userid The current user.
* @param int $convid The conversation where the messages belong. Could be an object or just the id.
* @param int $limitfrom Return a subset of records, starting at this point (optional).
* @param int $limitnum Return a subset comprising this many records in total (optional, required if $limitfrom is set).
* @param string $sort The column name to order by including optionally direction.
* @param int $timefrom The time from the message being sent.
* @param int $timeto The time up until the message being sent.
* @return array of messages
*/
public static function get_conversation_messages(int $userid, int $convid, int $limitfrom = 0, int $limitnum = 0,
string $sort = 'timecreated ASC', int $timefrom = 0, int $timeto = 0) : array {

if (!empty($timefrom)) {
// Check the cache to see if we even need to do a DB query.
$cache = \cache::make('core', 'message_time_last_message_in_conversation');
$lastcreated = $cache->get($convid);

// The last known message time is earlier than the one being requested so we can
// just return an empty result set rather than having to query the DB.
if ($lastcreated && $lastcreated < $timefrom) {
return [];
}
}

$arrmessages = array();
if ($messages = helper::get_conversation_messages($userid, $convid, 0, $limitfrom, $limitnum, $sort, $timefrom, $timeto)) {
$arrmessages = helper::format_conversation_messages($userid, $convid, $messages);
}

return $arrmessages;
}

/**
* Returns the most recent message between two users.
*
* @deprecated since 3.6
* @param int $userid the current user
* @param int $otheruserid the other user
* @return \stdClass|null
*/
public static function get_most_recent_message($userid, $otheruserid) {
debugging('\core_message\api::get_most_recent_message() is deprecated, please use ' .
'\core_message\api::get_most_recent_conversation_message() instead.', DEBUG_DEVELOPER);

// We want two messages here so we get an accurate 'blocktime' value.
if ($messages = helper::get_messages($userid, $otheruserid, 0, 0, 2, 'timecreated DESC')) {
// Swap the order so we now have them in historical order.
Expand All @@ -659,6 +707,28 @@ public static function get_most_recent_message($userid, $otheruserid) {
return null;
}

/**
* Returns the most recent message in a conversation.
*
* @param int $convid The conversation identifier.
* @param int $currentuserid The current user identifier.
* @return \stdClass|null The most recent message.
*/
public static function get_most_recent_conversation_message(int $convid, int $currentuserid = 0) {
global $USER;

if (empty($currentuserid)) {
$currentuserid = $USER->id;
}

if ($messages = helper::get_conversation_messages($currentuserid, $convid, 0, 0, 1, 'timecreated DESC')) {
$convmessages = helper::format_conversation_messages($currentuserid, $convid, $messages);
return array_pop($convmessages['messages']);
}

return null;
}

/**
* Returns the profile information for a contact for a user.
*
Expand Down
Loading

0 comments on commit 621e7eb

Please sign in to comment.