Several things about mail storage weren't quite right.

This commit is contained in:
zotlabs 2017-06-22 17:31:08 -07:00
parent 37b9a809a6
commit 3b4f70ae83
3 changed files with 98 additions and 41 deletions

View file

@ -12,10 +12,6 @@ class Conversations {
if(argc() > 1) {
switch(argv(1)) {
case 'combined':
$mailbox = 'combined';
$header = t('Conversations');
break;
case 'inbox':
$mailbox = 'inbox';
$header = t('Received Messages');

View file

@ -8,6 +8,7 @@ use Zotlabs\Lib as Zlib;
require_once('include/bbcode.php');
require_once('include/oembed.php');
require_once('include/crypto.php');
require_once('include/message.php');
require_once('include/feedutils.php');
require_once('include/photo/photo_driver.php');
require_once('include/permissions.php');
@ -2932,6 +2933,8 @@ function mail_store($arr) {
return 0;
}
$channel = channelx_by_n($arr['channel_id']);
if(! $arr['mail_obscured']) {
if((strpos($arr['body'],'<') !== false) || (strpos($arr['body'],'>') !== false))
$arr['body'] = escape_tags($arr['body']);
@ -2962,11 +2965,34 @@ function mail_store($arr) {
$arr['mail_flags'] = ((x($arr,'mail_flags')) ? intval($arr['mail_flags']) : 0 );
$arr['mail_raw'] = ((x($arr,'mail_raw')) ? intval($arr['mail_raw']) : 0 );
if(! $arr['parent_mid']) {
if($arr['parent_mid']) {
$parent_item = q("select * from mail where mid = '%s' and channel_id = %d limit 1",
dbesc($arr['parent_mid']),
intval($arr['channel_id'])
);
if(($parent_item) && (! $arr['conv_guid'])) {
$arr['conv_guid'] = $parent_item[0]['conv_guid'];
}
}
else {
logger('mail_store: missing parent');
$arr['parent_mid'] = $arr['mid'];
}
if($arr['from_xchan'] === $channel['channel_hash'])
$conversant = $arr['to_xchan'];
else
$conversant = $arr['from_xchan'];
if(! $arr['conv_guid']) {
$x = create_conversation($channel,$conversant,(($arr['title']) ? base64url_decode(str_rot47($arr['title'])) : ''));
$arr['conv_guid'] = (($x) ? $x['guid'] : '');
}
$r = q("SELECT id FROM mail WHERE mid = '%s' AND channel_id = %d LIMIT 1",
dbesc($arr['mid']),
intval($arr['channel_id'])
@ -3031,6 +3057,14 @@ function mail_store($arr) {
Zlib\Enotify::submit($notif_params);
}
if($arr['conv_guid']) {
$c = q("update conv set updated = '%s' where guid = '%s' and uid = %d",
dbesc(datetime_convert()),
dbesc($arr['conv_guid']),
intval($arr['channel_id'])
);
}
call_hooks('post_mail_end',$arr);
return $current_post;
}

View file

@ -104,38 +104,9 @@ function send_message($uid = 0, $recipient = '', $body = '', $subject = '', $rep
// create a new conversation
$conv_guid = random_string();
$recip = q("select * from xchan where xchan_hash = '%s' limit 1",
dbesc($recipient)
);
if($recip)
$recip_handle = $recip[0]['xchan_addr'];
$sender_handle = channel_reddress($channel);
$handles = $recip_handle . ';' . $sender_handle;
if($subject)
$nsubject = str_rot47(base64url_encode($subject));
$r = q("insert into conv (uid,guid,creator,created,updated,subject,recips) values(%d, '%s', '%s', '%s', '%s', '%s', '%s') ",
intval(local_channel()),
dbesc($conv_guid),
dbesc($sender_handle),
dbesc(datetime_convert()),
dbesc(datetime_convert()),
dbesc($nsubject),
dbesc($handles)
);
$r = q("select * from conv where guid = '%s' and uid = %d limit 1",
dbesc($conv_guid),
intval(local_channel())
);
if($r) {
$retconv = $r[0];
$retconv['subject'] = base64url_decode(str_rot47($retconv['subject']));
$retconv = create_conversation($channel,$recipient,$subject);
if($retconv) {
$conv_guid = $retconv['guid'];
}
}
@ -146,7 +117,6 @@ function send_message($uid = 0, $recipient = '', $body = '', $subject = '', $rep
);
if($r) {
$retconv = $r[0];
$retconv['subject'] = base64url_decode(str_rot47($retconv['subject']));
}
}
@ -155,6 +125,12 @@ function send_message($uid = 0, $recipient = '', $body = '', $subject = '', $rep
return $ret;
}
$c = q("update conv set updated = '%s' where guid = '%s' and uid = %d",
dbesc(datetime_convert()),
dbesc($conv_guid),
intval(local_channel())
);
// generate a unique message_id
do {
@ -284,6 +260,49 @@ function send_message($uid = 0, $recipient = '', $body = '', $subject = '', $rep
}
function create_conversation($channel,$recipient,$subject) {
// create a new conversation
$conv_guid = random_string();
$recip = q("select * from xchan where xchan_hash = '%s' limit 1",
dbesc($recipient)
);
if($recip)
$recip_handle = $recip[0]['xchan_addr'];
$sender_handle = channel_reddress($channel);
$handles = $recip_handle . ';' . $sender_handle;
if($subject)
$nsubject = str_rot47(base64url_encode($subject));
$r = q("insert into conv (uid,guid,creator,created,updated,subject,recips) values(%d, '%s', '%s', '%s', '%s', '%s', '%s') ",
intval($channel['channel_id']),
dbesc($conv_guid),
dbesc($sender_handle),
dbesc(datetime_convert()),
dbesc(datetime_convert()),
dbesc($nsubject),
dbesc($handles)
);
$r = q("select * from conv where guid = '%s' and uid = %d limit 1",
dbesc($conv_guid),
intval($channel['channel_id'])
);
return $r[0];
}
function private_messages_list($uid, $mailbox = '', $start = 0, $numitems = 0) {
$where = '';
@ -315,6 +334,8 @@ function private_messages_list($uid, $mailbox = '', $start = 0, $numitems = 0) {
break;
case 'combined':
default:
$parents = q("SELECT parent_mid FROM mail WHERE mid = parent_mid AND channel_id = %d ORDER BY created DESC",
dbesc($local_channel)
);
@ -326,15 +347,21 @@ function private_messages_list($uid, $mailbox = '', $start = 0, $numitems = 0) {
}
$r = null;
if($parents) {
foreach($parents as $parent) {
$all[] = q("SELECT * FROM mail WHERE parent_mid = '%s' AND channel_id = %d ORDER BY created DESC",
$all = q("SELECT * FROM mail WHERE parent_mid = '%s' AND channel_id = %d ORDER BY created DESC limit 1",
dbesc($parent['parent_mid']),
dbesc($local_channel)
);
if($all) {
foreach($all as $single) {
$r[] = $single;
}
}
}
foreach($all as $single)
$r[] = $single[0];
}
else {
$r = q($sql);