Commit e724aa6c authored by Richard Mansfield's avatar Richard Mansfield
Browse files

Don't include message content in user message email notifications

Change user message notification url to allow replies
Quote original message when sending a user message as a reply
parent 0389b811
......@@ -234,7 +234,12 @@ $string['sendmessage'] = 'Send message';
$string['messagesent'] = 'Message sent!';
$string['messagenotsent'] = 'Failed to send message';
$string['newusermessage'] = 'New message from %s';
$string['newusermessageemailbody'] = '%s has sent you a message. To view this message, visit
%s';
$string['sendmessageto'] = 'Send message to %s';
$string['viewmessage'] = 'View Message';
$string['Reply'] = 'Reply';
$string['denyfriendrequest'] = 'Deny Friend Request';
$string['sendfriendshiprequest'] = 'Send %s a friendship request';
......
......@@ -332,6 +332,11 @@ abstract class ActivityType {
return $this->subject;
}
// rewrite the url with the internal notification id?
protected function update_url() {
return false;
}
abstract function get_required_parameters();
public function notify_user($user) {
......@@ -348,20 +353,32 @@ abstract class ActivityType {
if (empty($user->method)) {
$user->method = 'internal';
}
// always do internal
$this->internalid = call_static_method('PluginNotificationInternal', 'notify_user', $user, $userdata);
if ($this->update_url()) {
$userdata->internalid = $this->internalid;
$userdata->url = $this->url;
}
if ($user->method != 'internal') {
$method = $user->method;
safe_require('notification', $method);
try {
call_static_method(generate_class_name('notification', $method), 'notify_user', $user, $userdata);
$user->markasread = true; // if we're doing something else, don't generate unread internal ones.
$userdata->internalid = $this->internalid;
}
catch (Exception $e) {
$user->markasread = false; // if we fail (eg email falls over), don't mark it as read...
// @todo Catalyst IT Ltd
}
}
// always do internal
call_static_method('PluginNotificationInternal', 'notify_user', $user, $userdata);
if (isset($userdata->internalid)) {
call_static_method('PluginNotificationInternal', 'update_notification', $user, $userdata);
}
}
public function notify_users() {
......@@ -553,9 +570,6 @@ class ActivityTypeUsermessage extends ActivityType {
public function __construct($data, $cron=false) {
parent::__construct($data, $cron);
$this->users = activity_get_users($this->get_id(), array($this->userto));
if (empty($this->url)) {
$this->url = get_config('wwwroot') . 'user/view.php?id=' . $this->userfrom;
}
}
public function get_subject($user) {
......@@ -566,6 +580,11 @@ class ActivityTypeUsermessage extends ActivityType {
return $this->subject;
}
protected function update_url() {
$this->url = get_config('wwwroot') . 'user/sendmessage.php?id=' . $this->userfrom . '&replyto=' . $this->internalid;
return true;
}
public function get_required_parameters() {
return array('message', 'userto', 'userfrom');
}
......
......@@ -913,7 +913,7 @@ function send_user_message($to, $message, $from=null) {
}
$messagepref = get_account_preference($to->id, 'messages');
if ((is_friend($from->id, $to->id) && $messagepref == 'friends') || $messagepref == 'allow' || $from->get('admin')) {
if ($messagepref == 'allow' || ($messagepref == 'friends' && is_friend($from->id, $to->id)) || $from->get('admin')) {
require_once('activity.php');
activity_occurred('usermessage',
array(
......
......@@ -45,10 +45,16 @@ class PluginNotificationEmail extends PluginNotification {
$messagebody .= $separator . "\n\n";
$messagebody .= get_string_from_language($lang, 'subject') . ': ' . $data->subject . "\n\n";
$messagebody .= $data->message;
if (!empty($data->url)) {
$messagebody .= "\n\n" . get_string_from_language($lang, 'referurl', 'notification.email', $data->url);
if ($data->activityname == 'usermessage') {
// Do not include the message body in user messages when they are sent by email
// because it encourages people to reply to the email.
$messagebody .= get_string_from_language($lang, 'newusermessageemailbody', 'group', display_name($data->userfrom), $data->url);
}
else {
$messagebody .= $data->message;
if (!empty($data->url)) {
$messagebody .= "\n\n" . get_string_from_language($lang, 'referurl', 'notification.email', $data->url);
}
}
$messagebody .= "\n\n$separator";
......
......@@ -46,7 +46,7 @@ class PluginNotificationInternal extends PluginNotification {
$toinsert->url = $data->url;
}
insert_record('notification_internal_activity', $toinsert);
return insert_record('notification_internal_activity', $toinsert, 'id', true);
}
/**
......@@ -57,6 +57,31 @@ class PluginNotificationInternal extends PluginNotification {
return count_records('notification_internal_activity', 'usr', $userid, 'read', 0);
}
public static function update_notification($user, $data) {
$toupdate = new StdClass;
if (empty($data->internalid)) {
throw new Exception("No id passed to internal notifiction update");
}
$toupdate->id = $data->internalid;
$toupdate->type = $data->type;
$toupdate->usr = $user->id;
if (!empty($user->markasread)) {
$toupdate->read = 1;
}
else {
$toupdate->read = 0;
}
$toupdate->message = $data->message;
$toupdate->subject = $data->subject;
$toupdate->ctime = db_format_timestamp(time());
if (!empty($data->url)) {
$toupdate->url = $data->url;
}
update_record('notification_internal_activity', $toupdate);
}
public static function get_event_subscriptions() {
$subscriptions = array(
(object)array(
......
......@@ -5,6 +5,14 @@
{include file="user/simpleuser.tpl" user=$user}
{if $replyto}
<h4>{$replyto->subject|escape}:</h4>
<br>
{foreach from=$replyto->lines item=line}
{$line|escape}<br>
{/foreach}
{/if}
{$form}
{include file="columnleftend.tpl"}
......
......@@ -32,6 +32,25 @@ require('searchlib.php');
safe_require('search', 'internal');
$id = param_integer('id');
$replytoid = param_integer('replyto', null);
if (!is_null($replytoid)) {
$replyto = get_record_sql('
SELECT
a.subject, a.message, a.url
FROM {notification_internal_activity} a
JOIN {activity_type} t ON a.type = t.id
WHERE t.name = ? AND a.id = ? AND a.usr = ?',
array('usermessage', $replytoid, $USER->get('id')));
if ($replyto) {
// Make sure the message was sent by the user being replied to
$bits = parse_url($replyto->url);
parse_str($bits['query'], $params);
if (empty($params['id']) || $params['id'] != $id) {
$replyto = false;
}
}
}
$returnto = param_alpha('returnto', 'myfriends');
$user = get_record('usr', 'id', $id, 'deleted', 0);
......@@ -40,23 +59,35 @@ if (!$user || !can_send_message($USER->to_stdclass(), $id)) {
throw new AccessDeniedException(get_string('cantmessageuser', 'group'));
}
define('TITLE', get_string('sendmessageto', 'group', display_name($id)));
$user->introduction = get_field('artefact', 'title', 'artefacttype', 'introduction', 'owner', $id);
$quote = '';
if ($replyto) {
$replyto->lines = split("\n", $replyto->message);
foreach ($replyto->lines as $line) {
$quote .= "\n> " . wordwrap($line, 75, "\n> ");
}
define('TITLE', get_string('viewmessage', 'group'));
}
else {
define('TITLE', get_string('sendmessageto', 'group', display_name($id)));
}
$form = pieform(array(
'name' => 'sendmessage',
'autofocus' => false,
'elements' => array(
'message' => array(
'type' => 'textarea',
'title' => get_string('message'),
'cols' => 50,
'rows' => 4,
'title' => $replyto ? get_string('Reply', 'group') : get_string('message'),
'cols' => 80,
'rows' => 10,
'defaultvalue' => $quote,
),
'submit' => array(
'type' => 'submitcancel',
'value' => array(get_string('sendmessage', 'group'), get_string('cancel')),
'value' => array($replyto ? get_string('Reply', 'group') : get_string('sendmessage', 'group'), get_string('cancel')),
'goto' => get_config('wwwroot') . ($returnto == 'find' ? 'user/find.php' : ($returnto == 'view' ? 'user/view.php?id=' . $id : 'user/myfriends.php')),
)
)
......@@ -66,6 +97,7 @@ $smarty = smarty();
$smarty->assign('heading', TITLE);
$smarty->assign('form', $form);
$smarty->assign('user', $user);
$smarty->assign('replyto', $replyto);
$smarty->display('user/sendmessage.tpl');
function sendmessage_submit(Pieform $form, $values) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment