11'use strict' ;
22
3+ // Add helper function for delayed logging
4+ function delayedLog ( message , data = null , delay = 1000 ) {
5+ setTimeout ( ( ) => {
6+ if ( data ) {
7+ console . log ( message , data ) ;
8+ } else {
9+ console . log ( message ) ;
10+ }
11+ } , delay ) ;
12+ }
13+
14+ // Add helper function for displaying messages
15+ function showMessage ( message , type = 'info' ) {
16+ if ( typeof Hm_Msgs !== 'undefined' ) {
17+ Hm_Msgs . add ( message , type ) ;
18+ } else if ( typeof hm_msgs !== 'undefined' && typeof hm_msgs . add === 'function' ) {
19+ hm_msgs . add ( message , type ) ;
20+ } else {
21+ console . log ( `[${ type . toUpperCase ( ) } ] ${ message } ` ) ;
22+ }
23+ }
24+
325var imap_delete_action = function ( event ) {
426 if ( ! hm_delete_prompt ( ) ) {
527 return false ;
@@ -1265,12 +1287,86 @@ $(function() {
12651287 }
12661288 setTimeout ( prefetch_imap_folders , 2 ) ;
12671289
1268- $ ( '#report_spam_message' ) . on ( "click" , function ( e ) { return imap_report_spam_message ( ) ; } ) ;
1290+ // Add spam report modal to the page
1291+ if ( ! $ ( '#spamReportModal' ) . length ) {
1292+ $ ( 'body' ) . append ( `
1293+ <div class="modal fade" id="spamReportModal" tabindex="-1" aria-labelledby="spamReportModalLabel" aria-hidden="true">
1294+ <div class="modal-dialog">
1295+ <div class="modal-content">
1296+ <div class="modal-header">
1297+ <h5 class="modal-title" id="spamReportModalLabel">Report Spam</h5>
1298+ <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
1299+ </div>
1300+ <div class="modal-body">
1301+ <div class="mb-3">
1302+ <label for="spamReportReason" class="form-label">Reason for reporting as spam:</label>
1303+ <textarea class="form-control" id="spamReportReason" rows="3">This message appears to be unsolicited commercial email or contains suspicious content.</textarea>
1304+ </div>
1305+ </div>
1306+ <div class="modal-footer">
1307+ <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
1308+ <button type="button" class="btn btn-danger" id="confirmSpamReport">Report as Spam</button>
1309+ </div>
1310+ </div>
1311+ </div>
1312+ </div>
1313+ ` ) ;
1314+ }
1315+
12691316 $ ( document ) . on ( 'click' , '.msg_report_spam' , function ( e ) {
12701317 e . preventDefault ( ) ;
1271- var uid = getMessageUidParam ( ) ;
1272- var detail = Hm_Utils . parse_folder_path ( getListPathParam ( ) , 'imap' ) ;
1273- return imap_report_spam_message ( false , uid , detail ) ;
1318+ var uid , detail ;
1319+
1320+ // Check if we're in message list view
1321+ var selected = $ ( 'input[type=checkbox]:checked' , $ ( '.message_table' ) ) . closest ( 'tr' ) ;
1322+ if ( selected . length > 0 ) {
1323+ // We're in message list view
1324+ uid = selected . data ( 'uid' ) ;
1325+ detail = Hm_Utils . parse_folder_path ( getListPathParam ( ) , 'imap' ) ;
1326+ } else {
1327+ // We're in message view
1328+ uid = getMessageUidParam ( ) ;
1329+ detail = Hm_Utils . parse_folder_path ( getListPathParam ( ) , 'imap' ) ;
1330+ }
1331+
1332+ if ( ! uid ) {
1333+ delayedLog ( 'Missing UID' , null , 0 ) ;
1334+ showMessage ( 'Could not determine message ID' , 'error' ) ;
1335+ return false ;
1336+ }
1337+
1338+ // Store the message details for the modal
1339+ $ ( '#spamReportModal' ) . data ( 'uid' , uid ) . data ( 'detail' , detail ) ;
1340+
1341+ // Show the modal
1342+ var modal = new bootstrap . Modal ( document . getElementById ( 'spamReportModal' ) ) ;
1343+ modal . show ( ) ;
1344+
1345+ return false ;
1346+ } ) ;
1347+
1348+ // Handle the confirm button click
1349+ $ ( document ) . on ( 'click' , '#confirmSpamReport' , function ( ) {
1350+ var modal = $ ( '#spamReportModal' ) ;
1351+ var uid = modal . data ( 'uid' ) ;
1352+ var detail = modal . data ( 'detail' ) ;
1353+ var reason = $ ( '#spamReportReason' ) . val ( ) . trim ( ) ;
1354+
1355+ if ( ! reason ) {
1356+ showMessage ( 'Please provide a reason for reporting this message as spam' , 'warning' ) ;
1357+ return ;
1358+ }
1359+
1360+ if ( ! uid ) {
1361+ showMessage ( 'Could not determine message ID' , 'error' ) ;
1362+ return ;
1363+ }
1364+
1365+ // Hide the modal
1366+ bootstrap . Modal . getInstance ( document . getElementById ( 'spamReportModal' ) ) . hide ( ) ;
1367+
1368+ // Call the report spam function with the reason
1369+ return imap_report_spam_message ( false , uid , detail , reason ) ;
12741370 } ) ;
12751371} ) ;
12761372
@@ -1313,44 +1409,49 @@ var imap_archive_message = function(state, supplied_uid, supplied_detail) {
13131409 return false ;
13141410} ;
13151411
1316- var imap_report_spam_message = function ( state , supplied_uid , supplied_detail ) {
1317- console . log ( 'Report Spam clicked - Starting process' ) ;
1318- var uid = getMessageUidParam ( ) ;
1319- var detail = Hm_Utils . parse_folder_path ( getListPathParam ( ) , 'imap' ) ;
1320- console . log ( 'Message details:' , { uid : uid , detail : detail } ) ;
1412+ var imap_report_spam_message = function ( state , supplied_uid , supplied_detail , reason ) {
1413+ delayedLog ( 'Report Spam clicked - Starting process' ) ;
1414+ var uid = supplied_uid || getMessageUidParam ( ) ;
1415+ var detail = supplied_detail || Hm_Utils . parse_folder_path ( getListPathParam ( ) , 'imap' ) ;
1416+ delayedLog ( 'Message details:' , { uid : uid , detail : detail , reason : reason } ) ;
13211417
1322- if ( supplied_uid ) {
1323- uid = supplied_uid ;
1324- }
1325- if ( supplied_detail ) {
1326- detail = supplied_detail ;
1418+ if ( ! uid ) {
1419+ delayedLog ( 'Missing UID' , null , 0 ) ;
1420+ showMessage ( 'Could not determine message ID' , 'error' ) ;
1421+ return false ;
13271422 }
1423+
13281424 if ( detail && uid ) {
1329- console . log ( 'Making AJAX request to report spam with params:' , {
1330- hook : 'ajax_imap_report_spam' ,
1331- uid : uid ,
1332- server_id : detail . server_id ,
1333- folder : detail . folder
1334- } ) ;
1335- Hm_Ajax . request (
1336- [ { 'name' : 'hm_ajax_hook' , 'value' : 'ajax_imap_report_spam' } ,
1425+ // Construct form data
1426+ var form_data = [
1427+ { 'name' : 'hm_ajax_hook' , 'value' : 'ajax_imap_report_spam' } ,
13371428 { 'name' : 'imap_msg_uid' , 'value' : uid } ,
13381429 { 'name' : 'imap_server_id' , 'value' : detail . server_id } ,
1339- { 'name' : 'folder' , 'value' : detail . folder } ] ,
1430+ { 'name' : 'folder' , 'value' : detail . folder }
1431+ ] ;
1432+
1433+ // Always include spam_reason, even if empty
1434+ form_data . push ( { 'name' : 'spam_reason' , 'value' : reason || 'No reason provided' } ) ;
1435+
1436+ delayedLog ( 'Making AJAX request to report spam with form data:' , form_data ) ;
1437+
1438+ Hm_Ajax . request (
1439+ form_data ,
13401440 function ( res ) {
1341- console . log ( 'Report spam AJAX response:' , res ) ;
1441+ delayedLog ( 'Report spam AJAX response:' , res ) ;
13421442 if ( ! res . imap_report_spam_error ) {
1443+ showMessage ( 'Message reported as spam and moved to junk folder' , 'success' ) ;
13431444 if ( Hm_Utils . get_from_global ( 'msg_uid' , false ) ) {
1344- console . log ( 'Message UID in global, returning' ) ;
1445+ delayedLog ( 'Message UID in global, returning' ) ;
13451446 return ;
13461447 }
13471448 var nlink = $ ( '.nlink' ) ;
13481449 if ( nlink . length && Hm_Utils . get_from_global ( 'auto_advance_email_enabled' ) ) {
1349- console . log ( 'Auto-advance enabled, redirecting to next message' ) ;
1450+ delayedLog ( 'Auto-advance enabled, redirecting to next message' ) ;
13501451 Hm_Utils . redirect ( nlink . attr ( 'href' ) ) ;
13511452 }
13521453 else {
1353- console . log ( 'Redirecting to message list' ) ;
1454+ delayedLog ( 'Redirecting to message list' ) ;
13541455 if ( ! hm_list_parent ( ) ) {
13551456 Hm_Utils . redirect ( "?page=message_list&list_path=" + getListPathParam ( ) ) ;
13561457 }
@@ -1359,12 +1460,20 @@ var imap_report_spam_message = function(state, supplied_uid, supplied_detail) {
13591460 }
13601461 }
13611462 } else {
1362- console . error ( 'Error in report spam response:' , res . imap_report_spam_error ) ;
1463+ delayedLog ( 'Error in report spam response:' , res . imap_report_spam_error ) ;
1464+ var errorMsg = 'Failed to report message as spam' ;
1465+ if ( res . router_user_msgs && res . router_user_msgs . text ) {
1466+ errorMsg += ': ' + res . router_user_msgs . text ;
1467+ }
1468+ showMessage ( errorMsg , 'error' ) ;
13631469 }
1364- }
1470+ } ,
1471+ true , // Show loading icon
1472+ true // Handle errors
13651473 ) ;
13661474 } else {
1367- console . error ( 'Missing required details:' , { detail : detail , uid : uid } ) ;
1475+ delayedLog ( 'Missing required details:' , { detail : detail , uid : uid } , 0 ) ;
1476+ showMessage ( 'Could not process spam report: Missing required details' , 'error' ) ;
13681477 }
13691478 return false ;
13701479} ;
0 commit comments