Partially fixed bad send behavior.

This commit is contained in:
Michael Woods
2025-12-25 20:32:12 -05:00
parent bec626678e
commit d913674426
3 changed files with 43 additions and 9 deletions

View File

@@ -56,6 +56,8 @@ async def send_message(
username = current_user.username
users_dict = root.get('users', {})
# Prepare recipients
to_list = payload.to
to_tuple = tuple(to_list)
@@ -63,9 +65,28 @@ async def send_message(
to_tuple = ("ALL",)
is_bulletin = "ALL" in to_list
recipients = to_list if not is_bulletin else list(root.get('users', {}).keys())
# Create message using only supported core params
if is_bulletin:
# Bulletin: deliver to all registered users
valid_recipients = list(users_dict.keys())
failed_recipients = []
else:
# Private message: validate each recipient exists
valid_recipients = []
failed_recipients = []
for recip in to_list:
if users_dict.get(recip):
valid_recipients.append(recip)
else:
failed_recipients.append(recip)
if not valid_recipients:
raise HTTPException(
status_code=400,
detail=f"No valid recipients found. Failed: {', '.join(failed_recipients)}"
)
# Create message
new_msg = Message(
text=payload.text,
msg_from=username,
@@ -73,11 +94,11 @@ async def send_message(
attachments=()
)
# Deliver to recipients + always sender (sent folder)
# Deliver to valid recipients + always sender (sent folder)
messages_root = root.setdefault('messages', PersistentMapping())
delivered_to = set()
delivered_to = set([username]) # sender always gets copy
for recip in set(recipients) | {username}:
for recip in valid_recipients:
mailbox = messages_root.setdefault(recip, PersistentList())
mailbox.append(new_msg)
mailbox._p_changed = True
@@ -86,7 +107,7 @@ async def send_message(
messages_root._p_changed = True
transaction.commit()
return {
response = {
"status": "sent",
"message_id": str(new_msg.msg_id),
"from": username,
@@ -94,3 +115,9 @@ async def send_message(
"sent_at": new_msg.sent_at.isoformat() + "Z",
"recipients_delivered": len(delivered_to)
}
if failed_recipients:
response["warning"] = f"Some recipients not registered: {', '.join(failed_recipients)}"
response["failed_recipients"] = failed_recipients
return response

View File

@@ -103,6 +103,10 @@
});
if (response.ok) {
const result = await response.json();
let msg = 'Message sent!';
if (result.warning) msg += ` ${result.warning}`;
status.textContent = msg;
status.className = 'alert alert-success';
status.textContent = 'Message sent successfully!';
status.style.display = 'block';

View File

@@ -101,8 +101,11 @@
credentials: 'include' // sends Basic Auth
});
if (resp.ok) {
const data = await resp.json();
document.getElementById('composeAlert').innerHTML = '<div class="alert alert-success">Sent! ID: ' + data.message_id + '</div>';
const result = await response.json();
let msg = 'Message sent!';
if (result.warning) msg += ` ${result.warning}`;
status.textContent = msg;
document.getElementById('composeAlert').innerHTML = '<div class="alert alert-success">' + msg + '</div>';
setTimeout(() => location.reload(), 1500);
} else {
const err = await resp.json();