Compare commits

...

3 commits

5 changed files with 50 additions and 4 deletions

View file

@ -160,6 +160,7 @@ const MszMessages = () => {
if(msgsRecipient !== undefined) if(msgsRecipient !== undefined)
msgsRecipient.onUpdate(async info => { msgsRecipient.onUpdate(async info => {
msgsReply.setRecipient(typeof info.id === 'string' ? info.id : ''); msgsReply.setRecipient(typeof info.id === 'string' ? info.id : '');
msgsReply.setWarning(info.ban ? `${(typeof info.name === 'string' ? info.name : 'This user')} has been banned and will be unable to respond to your messages.` : undefined);
}); });
msgsReply.onSubmit(async form => { msgsReply.onSubmit(async form => {

View file

@ -11,6 +11,8 @@ const MszMessagesReply = function(element) {
const parserSelect = form.querySelector('.js-messages-reply-parser'); const parserSelect = form.querySelector('.js-messages-reply-parser');
const saveBtn = form.querySelector('.js-messages-reply-save'); const saveBtn = form.querySelector('.js-messages-reply-save');
const sendBtn = form.querySelector('.js-messages-reply-send'); const sendBtn = form.querySelector('.js-messages-reply-send');
const warnElem = form.querySelector('.js-reply-form-warning');
const warnText = warnElem instanceof Element ? warnElem.querySelector('.js-reply-form-warning-text') : undefined;
let submitHandler; let submitHandler;
form.addEventListener('submit', ev => { form.addEventListener('submit', ev => {
@ -133,6 +135,18 @@ const MszMessagesReply = function(element) {
return { return {
getElement: () => element, getElement: () => element,
setWarning: text => {
if(warnElem === undefined || warnText === undefined)
return;
if(text === undefined) {
warnElem.hidden = true;
warnText.textContent = '';
} else {
warnElem.hidden = false;
warnText.textContent = text;
}
},
setRecipient: userId => { setRecipient: userId => {
for(const field of form.elements) for(const field of form.elements)
if(field.name === 'recipient') { if(field.name === 'recipient') {

View file

@ -10,8 +10,9 @@ use Syokuhou\IConfig;
use Misuzu\{CSRF,Pagination,Perm,Template}; use Misuzu\{CSRF,Pagination,Perm,Template};
use Misuzu\Auth\AuthInfo; use Misuzu\Auth\AuthInfo;
use Misuzu\Parsers\Parser; use Misuzu\Parsers\Parser;
use Misuzu\Perms\Permissions;
use Misuzu\URLs\{URLInfo,URLRegistry}; use Misuzu\URLs\{URLInfo,URLRegistry};
use Misuzu\Users\UsersContext; use Misuzu\Users\{UsersContext,UserInfo};
class MessagesRoutes extends RouteHandler { class MessagesRoutes extends RouteHandler {
public const FOLDER_META = [ public const FOLDER_META = [
@ -26,7 +27,8 @@ class MessagesRoutes extends RouteHandler {
private URLRegistry $urls, private URLRegistry $urls,
private AuthInfo $authInfo, private AuthInfo $authInfo,
private MessagesContext $msgsCtx, private MessagesContext $msgsCtx,
private UsersContext $usersCtx private UsersContext $usersCtx,
private Permissions $perms
) {} ) {}
private bool $canSendMessages; private bool $canSendMessages;
@ -41,7 +43,8 @@ class MessagesRoutes extends RouteHandler {
if(!$globalPerms->check(Perm::G_MESSAGES_VIEW)) if(!$globalPerms->check(Perm::G_MESSAGES_VIEW))
return 403; return 403;
$this->canSendMessages = $globalPerms->check(Perm::G_MESSAGES_SEND); $this->canSendMessages = $globalPerms->check(Perm::G_MESSAGES_SEND)
&& !$this->usersCtx->hasActiveBan($this->authInfo->getUserInfo());
if($request->getMethod() === 'POST' && $request->isFormContent()) { if($request->getMethod() === 'POST' && $request->isFormContent()) {
$content = $request->getContent(); $content = $request->getContent();
@ -171,6 +174,7 @@ class MessagesRoutes extends RouteHandler {
return [ return [
'id' => $userInfo->getId(), 'id' => $userInfo->getId(),
'name' => $userInfo->getName(), 'name' => $userInfo->getName(),
'ban' => $this->usersCtx->hasActiveBan($userInfo),
'avatar' => $this->urls->format('user-avatar', [ 'avatar' => $this->urls->format('user-avatar', [
'user' => $userInfo->getId(), 'user' => $userInfo->getId(),
'res' => 200, 'res' => 200,
@ -247,6 +251,19 @@ class MessagesRoutes extends RouteHandler {
]); ]);
} }
private function checkCanReceiveMessages(UserInfo|string $userInfo): ?array {
$globalPerms = $this->perms->getPermissions('global', $userInfo);
if(!$globalPerms->check(Perm::G_MESSAGES_VIEW))
return [
'error' => [
'name' => 'msgs:recipient_cannot_recv',
'text' => 'This person is not allowed to receive messages.',
],
];
return null;
}
private function checkMessageFields(string $title, string $body, int $parser): ?array { private function checkMessageFields(string $title, string $body, int $parser): ?array {
if(!Parser::isValid($parser)) if(!Parser::isValid($parser))
return [ return [
@ -348,6 +365,10 @@ class MessagesRoutes extends RouteHandler {
]; ];
} }
$error = $this->checkCanReceiveMessages($recipientInfo);
if($error !== null)
return $error;
$replyToInfo = null; $replyToInfo = null;
if(!empty($replyTo)) { if(!empty($replyTo)) {
try { try {
@ -462,6 +483,10 @@ class MessagesRoutes extends RouteHandler {
], ],
]; ];
$error = $this->checkCanReceiveMessages($messageInfo->getRecipientId());
if($error !== null)
return $error;
$sentAt = $draft ? null : time(); $sentAt = $draft ? null : time();
$msgsDb->updateMessage( $msgsDb->updateMessage(

View file

@ -248,7 +248,8 @@ class MisuzuContext {
$this->urls, $this->urls,
$this->authInfo, $this->authInfo,
$this->messagesCtx, $this->messagesCtx,
$this->usersCtx $this->usersCtx,
$this->perms
)); ));
$routingCtx->register(new \Misuzu\Changelog\ChangelogRoutes( $routingCtx->register(new \Misuzu\Changelog\ChangelogRoutes(

View file

@ -40,6 +40,11 @@
<form class="messages-reply-form js-messages-reply-form"> <form class="messages-reply-form js-messages-reply-form">
{{ input_hidden('recipient', '') }} {{ input_hidden('recipient', '') }}
<div class="warning js-reply-form-warning" hidden>
<div class="warning__content">
<p class="js-reply-form-warning-text"></p>
</div>
</div>
<div class="messages-reply-subject"> <div class="messages-reply-subject">
{{ input_text('title', 'messages-reply-subject-input', '', 'text', 'Subject', true) }} {{ input_text('title', 'messages-reply-subject-input', '', 'text', 'Subject', true) }}
</div> </div>