Jump to content

User:Pythoncoder/Scripts/OneClickAFCH.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
/*
**One-Click AFCH**
Adapted by Pythoncoder from [[MediaWiki:Gadget-afchelper.js/submissions.js]]

Adds a button to decline LLM-generated drafts in just one click.
Submitters will be notified and invited to the Teahouse if needed.
If you need to add a comment or a second decline reason, don't click the button!
I may update this in the future to add other common decline reasons.
This script will not work unless you have the AFC Helper Script installed.

The standard user script disclaimer applies: USE THIS AT YOUR OWN RISK!
You are responsible for any actions you take with this script.
Please don't get yourself blocked because you misused this script —
it would make me really sad :(
*/

function oneClickDecline() {
	
	let afchPage = new AFCH.Page( AFCH.consts.pagename );
    let afchSubmissionUnparsed = new AFCH.Submission( afchPage );

	$.when(
		afchSubmissionUnparsed.parse(),
	).then( ( afchSubmission ) => {
	    afchPage.getText( false ).done( ( text ) => {
	        let data = {
	            declineReason: ['ai'],
	            notifyUser: true,
	            inviteToTeahouse: true,
	        };
	        data.afchText = new AFCH.Text( text );
	
	        let isDecline = true; // true=decline, false=reject
	        let declineReason = data.declineReason[ 0 ];
	        let declineReason2 = null;
	        let newParams = {
	            decliner: mw.user.getName(),
	            declinets: '\x7B\x7Bsubst:REVISIONTIMESTAMP\x7D\x7D'
	        };
	
	        newParams[ '2' ] = declineReason;
	
	        // Update decline counts
	        let declineCounts = AFCH.userData.get( 'decline-counts', {} );
	
	        declineCounts[ declineReason ] = ( declineCounts[ declineReason ] || 1 ) + 1;
	
	        AFCH.userData.set( 'decline-counts', declineCounts );
	
	        // Now update the submission status
	        afchSubmission.setStatus( 'd', newParams );
	
	        data.afchText.updateAfcTemplates( afchSubmission.makeWikicode() );
	        data.afchText.cleanUp();
	
	
	        // Build edit summary. This is intentionally formatted a bit differently from the official AFCH script (spaced em dash lol) to make it easier to spot this script’s declines in case of bugs
	        let editSummary = 'Declining submission — ai (Submission appears to be a large language model output)';
	
	        afchPage.edit( {
	            contents: data.afchText.get(),
	            summary: editSummary
	        } );
	
	        if ( data.notifyUser ) {
	            afchSubmission.getSubmitter().done( ( submitter ) => {
	                const userTalk = new AFCH.Page( ( new mw.Title( submitter, 3 ) ).getPrefixedText() ),
	                    shouldTeahouse = data.inviteToTeahouse ? $.Deferred() : false;
	
	                // Check categories on the page to ensure that if the user has already been
	                // invited to the Teahouse, we don't invite them again.
	                if ( data.inviteToTeahouse ) {
	                    userTalk.getCategories( true ).done( ( categories ) => {
	                        let hasTeahouseCat = false,
	                            teahouseCategories = [
	                                'Category:Wikipedians who have received a Teahouse invitation',
	                                'Category:Wikipedians who have received a Teahouse invitation through AfC'
	                            ];
	
	                        $.each( categories, ( _, cat ) => {
	                            if ( teahouseCategories.indexOf( cat ) !== -1 ) {
	                                hasTeahouseCat = true;
	                                return false;
	                            }
	                        } );
	
	                        shouldTeahouse.resolve( !hasTeahouseCat );
	                    } );
	                }
	
	                $.when( shouldTeahouse ).then( ( teahouse ) => {
	                    let message = AFCH.msg.get( 'declined-submission', {
	                        $1: AFCH.consts.pagename,
	                        $2: afchSubmission.shortTitle,
	                        $3: 'no',
	                        $4: declineReason,
	                        $5: newParams[ '3' ] || '',
	                        $6: declineReason2 || '',
	                        $7: newParams.details2 || '',
	                        $8: ''
	                    } );
	
	                    if ( teahouse ) {
	                        message += '\n\n' + AFCH.msg.get( 'teahouse-invite' );
	                    }
	
	                    AFCH.actions.notifyUser( submitter, {
	                        message: message,
	                        summary: 'Notification: Your [[' + AFCH.consts.pagename + '|Articles for Creation submission]] has been declined'
	                    } );
	                } );
	            } );
	        }
	
	        // Log AfC if enabled and CSD if necessary
	        afchSubmission.getSubmitter().done( ( submitter ) => {
	            AFCH.actions.logAfc( {
	                title: afchPage.rawTitle,
	                actionType: isDecline ? 'decline' : 'reject',
	                declineReason: declineReason,
	                declineReason2: declineReason2,
	                submitter: submitter
	            } );
	        } );
	    } );
	});
}

function checkForDeclineButton(i) {
	if (mw.config.get('wgNamespaceNumber') !== 2 && mw.config.get('wgNamespaceNumber') !== 118) {
		// Only run in userspace and draftspace
		return;
	} else if (document.getElementById('1click_ai') === null && document.getElementById('afchDecline') !== null) {
		htmlString = '<span id="1click_ai" style="display: inline-block; background-color: #f33; font-size: 24px; padding: 12px 24px; margin: 12px 0; border-radius: 8px; cursor: pointer">Decline AI</div>';
		let elem = document.createElement('div');
		elem.innerHTML = htmlString;
		document.getElementById('bodyContent').insertBefore(elem, document.querySelector('#mw-content-text'));
		document.getElementById('1click_ai').addEventListener('click', oneClickDecline);
	} else {
		setTimeout(function(i) {checkForDeclineButton(i+1)}, 1000*i*i);
	}
}

checkForDeclineButton(1);