Skip to content

Commit

Permalink
feat(search): ✨ only show edit page action if user has rights
Browse files Browse the repository at this point in the history
That only works for the main namespace fow now.
  • Loading branch information
alistair3149 committed Sep 6, 2023
1 parent bfea966 commit 6f3d82d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 11 deletions.
38 changes: 28 additions & 10 deletions resources/skins.citizen.search/searchAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ const htmlHelper = require( './htmlHelper.js' )();

function searchAction() {
return {
userRights: undefined,
getUserRights: async function () {
// Get and cache user rights
this.userRights = await mw.user.getRights();
return this.userRights;
},
init: function ( typeaheadEl, itemGroupData ) {
const actionData = {
type: 'action',
Expand All @@ -11,7 +17,7 @@ function searchAction() {
itemGroupData.items = itemGroupData.items.map( ( item ) => ( { ...item, ...actionData } ) );
typeaheadEl.append( htmlHelper.getItemGroupElement( itemGroupData ) );
},
render: function ( typeaheadEl, searchQuery ) {
render: async function ( typeaheadEl, searchQuery ) {
const itemGroupData = {
id: 'action',
items: []
Expand All @@ -26,15 +32,6 @@ function searchAction() {
msg: 'citizen-search-fulltext'
} );

// Edit/create page
// TODO: Check if user has right, and whether the page exists
itemGroupData.items.push( {
// id: 'editpage',
link: `${config.wgScriptPath}/index.php?title=${searchQuery.valueHtml}&action=edit`,
icon: 'edit',
msg: 'citizen-search-editpage'
} );

// MediaSearch
if ( config.isMediaSearchExtensionEnabled ) {
itemGroupData.items.push( {
Expand All @@ -45,6 +42,24 @@ function searchAction() {
} );
}

/*
For some reason title.exists() always returns null
const title = mw.Title.newFromUserInput( searchQuery.value );
console.log( title.exists() );
*/

const userRights = this.userRights ?? await this.getUserRights();
if ( userRights.includes( 'createpage', 'edit' ) ) {
// Edit/create page
// TODO: Check whether the page exists
itemGroupData.items.push( {
// id: 'editpage',
link: `${config.wgScriptPath}/index.php?title=${searchQuery.valueHtml}&action=edit`,
icon: 'edit',
msg: 'citizen-search-editpage'
} );
}

if ( !typeaheadEl.querySelector( '.citizen-typeahead-item-group[data-group="action"]' ) ) {
this.init( typeaheadEl, itemGroupData );
}
Expand All @@ -58,6 +73,9 @@ function searchAction() {
label: mw.message( item.msg )
} );
} );
},
clear: function ( typeaheadEl ) {
typeaheadEl.querySelector( '.citizen-typeahead-item-group[data-group="action"]' ).remove();
}
};
}
Expand Down
1 change: 1 addition & 0 deletions resources/skins.citizen.search/typeahead.js
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,7 @@ function updateTypeaheadItems() {
searchAction.render( typeahead.element, searchQuery );
getSuggestions();
} else {
searchAction.clear( typeahead.element );
typeahead.items.clear();
presult.render( typeahead.element );
typeahead.items.set();
Expand Down
4 changes: 3 additions & 1 deletion skin.json
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,9 @@
"searchsuggest-search"
],
"dependencies": [
"mediawiki.storage"
"mediawiki.storage",
"mediawiki.user",
"mediawiki.util"
],
"targets": [
"desktop",
Expand Down

0 comments on commit 6f3d82d

Please sign in to comment.