Mailstream: respect blocked/ignored/collapsed contact settings

This commit is contained in:
Matthew Exon 2024-07-09 20:12:09 +01:00
parent ca1de575c6
commit 6b6f536c95

View file

@ -129,21 +129,35 @@ function mailstream_send_hook(array $data)
return; return;
} }
$contact = DBA::selectFirst('contact', [], ['id' => $item['contact-id'], 'self' => false]); $author = DBA::selectFirst('contact', ['nick', 'blocked', 'uri-id'], ['id' => $data['author-id'], 'self' => false]);
if (!DBA::isResult($contact)) { if (!DBA::isResult($author)) {
Logger::error('mailstream_send_hook could not find contact', ['guid' => $item['guid'], 'contact-id' => $item['contact-id']]); Logger::error('mailstream_send_hook could not find author', ['guid' => $item['guid'], 'author-id' => $data['author-id']]);
return; return;
} }
if ($contact['blocked']) { if ($author['blocked']) {
Logger::error('mailstream_send_hook contact is blocked', ['guid' => $item['guid'], 'contact-id' => $item['contact-id']]); Logger::info('mailstream_send_hook author is blocked', ['guid' => $item['guid'], 'author-id' => $data['author-id']]);
return; return;
} }
if (array_key_exists('ignored', $contact) && $contact['ignored']) { $collapsed = false;
Logger::error('mailstream_send_hook contact is ignored', ['guid' => $item['guid'], 'contact-id' => $item['contact-id']]); $user_contact = DBA::selectFirst('user-contact', ['cid', 'blocked', 'ignored', 'collapsed'], ['uid' => $item['uid'], 'uri-id' => $item['author-uri-id']]);
return; if (!DBA::isResult($user_contact)) {
$user_contact = DBA::selectFirst('user-contact', ['cid', 'blocked', 'ignored', 'collapsed'], ['uid' => $item['uid'], 'cid' => $item['author-id']]);
}
if (DBA::isResult($user_contact)) {
if ($user_contact['blocked']) {
Logger::info('mailstream_send_hook author is blocked', ['guid' => $item['guid'], 'cid' => $user_contact['cid']]);
return;
}
if ($user_contact['ignored']) {
Logger::info('mailstream_send_hook author is ignored', ['guid' => $item['guid'], 'cid' => $user_contact['cid']]);
return;
}
if ($user_contact['collapsed']) {
$collapsed = true;
}
} }
if (!mailstream_send($data['message_id'], $item, $user)) { if (!mailstream_send($data['message_id'], $item, $user, $collapsed)) {
Logger::debug('mailstream_send_hook send failed, will retry', $data); Logger::debug('mailstream_send_hook send failed, will retry', $data);
if (!Worker::defer()) { if (!Worker::defer()) {
Logger::error('mailstream_send_hook failed and could not defer', $data); Logger::error('mailstream_send_hook failed and could not defer', $data);
@ -199,6 +213,7 @@ function mailstream_post_hook(array &$item)
$send_hook_data = [ $send_hook_data = [
'uid' => $item['uid'], 'uid' => $item['uid'],
'contact-id' => $item['contact-id'], 'contact-id' => $item['contact-id'],
'author-id' => $item['author-id'],
'uri' => $item['uri'], 'uri' => $item['uri'],
'message_id' => $message_id, 'message_id' => $message_id,
'tries' => 0, 'tries' => 0,
@ -383,10 +398,11 @@ function mailstream_subject(array $item): string
* @param string $message_id ID of the message (RFC 1036) * @param string $message_id ID of the message (RFC 1036)
* @param array $item content of the item * @param array $item content of the item
* @param array $user results from the user table * @param array $user results from the user table
* @param bool $collapsed true if the content should be hidden
* *
* @return bool True if this message has been completed. False if it should be retried. * @return bool True if this message has been completed. False if it should be retried.
*/ */
function mailstream_send(string $message_id, array $item, array $user): bool function mailstream_send(string $message_id, array $item, array $user, bool $collapsed): bool
{ {
if (!is_array($item)) { if (!is_array($item)) {
Logger::error('mailstream_send item is empty', ['message_id' => $message_id]); Logger::error('mailstream_send item is empty', ['message_id' => $message_id]);
@ -405,10 +421,16 @@ function mailstream_send(string $message_id, array $item, array $user): bool
require_once (dirname(__file__) . '/phpmailer/class.phpmailer.php'); require_once (dirname(__file__) . '/phpmailer/class.phpmailer.php');
$item['body'] = Post\Media::addAttachmentsToBody($item['uri-id'], $item['body']); if ($collapsed) {
$item['body'] = DI::l10n()->t('Content from %s is collapsed', $item['author-name']);
} else {
$item['body'] = Post\Media::addAttachmentsToBody($item['uri-id'], $item['body']);
}
$attachments = []; $attachments = [];
mailstream_do_images($item, $attachments); if (!$collapsed) {
mailstream_do_images($item, $attachments);
}
$frommail = DI::config()->get('mailstream', 'frommail'); $frommail = DI::config()->get('mailstream', 'frommail');
if ($frommail == '') { if ($frommail == '') {
$frommail = 'friendica@localhost.local'; $frommail = 'friendica@localhost.local';