mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-01-07 20:42:44 +00:00
Merge pull request #3820 from G-Ambatte/addAdminFixScriptTool-#3801
Add script fix tool to Admin page #3801
This commit is contained in:
@@ -1,3 +1,5 @@
|
|||||||
|
require('./brewLookup.less');
|
||||||
|
|
||||||
const React = require('react');
|
const React = require('react');
|
||||||
const createClass = require('create-react-class');
|
const createClass = require('create-react-class');
|
||||||
const cx = require('classnames');
|
const cx = require('classnames');
|
||||||
@@ -12,22 +14,43 @@ const BrewLookup = createClass({
|
|||||||
},
|
},
|
||||||
getInitialState() {
|
getInitialState() {
|
||||||
return {
|
return {
|
||||||
query : '',
|
query : '',
|
||||||
foundBrew : null,
|
foundBrew : null,
|
||||||
searching : false,
|
searching : false,
|
||||||
error : null
|
error : null,
|
||||||
|
scriptCount : 0
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
handleChange(e){
|
handleChange(e){
|
||||||
this.setState({ query: e.target.value });
|
this.setState({ query: e.target.value });
|
||||||
},
|
},
|
||||||
lookup(){
|
lookup(){
|
||||||
this.setState({ searching: true, error: null });
|
this.setState({ searching: true, error: null, scriptCount: 0 });
|
||||||
|
|
||||||
request.get(`/admin/lookup/${this.state.query}`)
|
request.get(`/admin/lookup/${this.state.query}`)
|
||||||
.then((res)=>this.setState({ foundBrew: res.body }))
|
.then((res)=>{
|
||||||
|
const foundBrew = res.body;
|
||||||
|
const scriptCheck = foundBrew?.text.match(/(<\/?s)cript/g);
|
||||||
|
this.setState({
|
||||||
|
foundBrew : foundBrew,
|
||||||
|
scriptCount : scriptCheck?.length || 0,
|
||||||
|
});
|
||||||
|
})
|
||||||
.catch((err)=>this.setState({ error: err }))
|
.catch((err)=>this.setState({ error: err }))
|
||||||
.finally(()=>this.setState({ searching: false }));
|
.finally(()=>{
|
||||||
|
this.setState({
|
||||||
|
searching : false
|
||||||
|
});
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
async cleanScript(){
|
||||||
|
if(!this.state.foundBrew?.shareId) return;
|
||||||
|
|
||||||
|
await request.put(`/admin/clean/script/${this.state.foundBrew.shareId}`)
|
||||||
|
.catch((err)=>{ this.setState({ error: err }); return; });
|
||||||
|
|
||||||
|
this.lookup();
|
||||||
},
|
},
|
||||||
|
|
||||||
renderFoundBrew(){
|
renderFoundBrew(){
|
||||||
@@ -46,12 +69,23 @@ const BrewLookup = createClass({
|
|||||||
<dt>Share Link</dt>
|
<dt>Share Link</dt>
|
||||||
<dd><a href={`/share/${brew.shareId}`} target='_blank' rel='noopener noreferrer'>/share/{brew.shareId}</a></dd>
|
<dd><a href={`/share/${brew.shareId}`} target='_blank' rel='noopener noreferrer'>/share/{brew.shareId}</a></dd>
|
||||||
|
|
||||||
|
<dt>Created Time</dt>
|
||||||
|
<dd>{brew.createdAt ? Moment(brew.createdAt).toLocaleString() : 'No creation date'}</dd>
|
||||||
|
|
||||||
<dt>Last Updated</dt>
|
<dt>Last Updated</dt>
|
||||||
<dd>{Moment(brew.updatedAt).fromNow()}</dd>
|
<dd>{Moment(brew.updatedAt).fromNow()}</dd>
|
||||||
|
|
||||||
<dt>Num of Views</dt>
|
<dt>Num of Views</dt>
|
||||||
<dd>{brew.views}</dd>
|
<dd>{brew.views}</dd>
|
||||||
|
|
||||||
|
<dt>SCRIPT tags detected</dt>
|
||||||
|
<dd>{this.state.scriptCount}</dd>
|
||||||
</dl>
|
</dl>
|
||||||
|
{this.state.scriptCount > 0 &&
|
||||||
|
<div className='cleanButton'>
|
||||||
|
<button onClick={this.cleanScript}>CLEAN BREW</button>
|
||||||
|
</div>
|
||||||
|
}
|
||||||
</div>;
|
</div>;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|||||||
6
client/admin/brewUtils/brewLookup/brewLookup.less
Normal file
6
client/admin/brewUtils/brewLookup/brewLookup.less
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
.brewLookup {
|
||||||
|
.cleanButton {
|
||||||
|
display : inline-block;
|
||||||
|
width : 100%;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5,6 +5,10 @@ const Moment = require('moment');
|
|||||||
const templateFn = require('../client/template.js');
|
const templateFn = require('../client/template.js');
|
||||||
const zlib = require('zlib');
|
const zlib = require('zlib');
|
||||||
|
|
||||||
|
const HomebrewAPI = require('./homebrew.api.js');
|
||||||
|
const asyncHandler = require('express-async-handler');
|
||||||
|
const { splitTextStyleAndMetadata } = require('../shared/helpers.js');
|
||||||
|
|
||||||
process.env.ADMIN_USER = process.env.ADMIN_USER || 'admin';
|
process.env.ADMIN_USER = process.env.ADMIN_USER || 'admin';
|
||||||
process.env.ADMIN_PASS = process.env.ADMIN_PASS || 'password3';
|
process.env.ADMIN_PASS = process.env.ADMIN_PASS || 'password3';
|
||||||
|
|
||||||
@@ -66,23 +70,8 @@ router.post('/admin/cleanup', mw.adminOnly, (req, res)=>{
|
|||||||
});
|
});
|
||||||
|
|
||||||
/* Searches for matching edit or share id, also attempts to partial match */
|
/* Searches for matching edit or share id, also attempts to partial match */
|
||||||
router.get('/admin/lookup/:id', mw.adminOnly, async (req, res, next)=>{
|
router.get('/admin/lookup/:id', mw.adminOnly, asyncHandler(HomebrewAPI.getBrew('admin', false)), async (req, res, next)=>{
|
||||||
HomebrewModel.findOne({
|
return res.json(req.brew);
|
||||||
$or : [
|
|
||||||
{ editId: { $regex: req.params.id, $options: 'i' } },
|
|
||||||
{ shareId: { $regex: req.params.id, $options: 'i' } },
|
|
||||||
]
|
|
||||||
}).exec()
|
|
||||||
.then((brew)=>{
|
|
||||||
if(!brew) // No document found
|
|
||||||
return res.status(404).json({ error: 'Document not found' });
|
|
||||||
else
|
|
||||||
return res.json(brew);
|
|
||||||
})
|
|
||||||
.catch((err)=>{
|
|
||||||
console.error(err);
|
|
||||||
return res.status(500).json({ error: 'Internal Server Error' });
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
/* Find 50 brews that aren't compressed yet */
|
/* Find 50 brews that aren't compressed yet */
|
||||||
@@ -100,6 +89,25 @@ router.get('/admin/finduncompressed', mw.adminOnly, (req, res)=>{
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
/* Cleans `<script` and `</script>` from the "text" field of a brew */
|
||||||
|
router.put('/admin/clean/script/:id', asyncHandler(HomebrewAPI.getBrew('admin', false)), async (req, res)=>{
|
||||||
|
console.log(`[ADMIN] Cleaning script tags from ShareID ${req.params.id}`);
|
||||||
|
|
||||||
|
function cleanText(text){return text.replaceAll(/(<\/?s)cript/gi, '');};
|
||||||
|
|
||||||
|
const brew = req.brew;
|
||||||
|
|
||||||
|
const properties = ['text', 'description', 'title'];
|
||||||
|
properties.forEach((property)=>{
|
||||||
|
brew[property] = cleanText(brew[property]);
|
||||||
|
});
|
||||||
|
|
||||||
|
splitTextStyleAndMetadata(brew);
|
||||||
|
|
||||||
|
req.body = brew;
|
||||||
|
|
||||||
|
return await HomebrewAPI.updateBrew(req, res);
|
||||||
|
});
|
||||||
|
|
||||||
/* Compresses the "text" field of a brew to binary */
|
/* Compresses the "text" field of a brew to binary */
|
||||||
router.put('/admin/compress/:id', (req, res)=>{
|
router.put('/admin/compress/:id', (req, res)=>{
|
||||||
|
|||||||
@@ -87,8 +87,18 @@ const api = {
|
|||||||
// Get relevant IDs for the brew
|
// Get relevant IDs for the brew
|
||||||
const { id, googleId } = api.getId(req);
|
const { id, googleId } = api.getId(req);
|
||||||
|
|
||||||
|
const accessMap = {
|
||||||
|
edit : { editId: id },
|
||||||
|
share : { shareId: id },
|
||||||
|
admin : {
|
||||||
|
$or : [
|
||||||
|
{ editId: id },
|
||||||
|
{ shareId: id },
|
||||||
|
] }
|
||||||
|
};
|
||||||
|
|
||||||
// Try to find the document in the Homebrewery database -- if it doesn't exist, that's fine.
|
// Try to find the document in the Homebrewery database -- if it doesn't exist, that's fine.
|
||||||
let stub = await HomebrewModel.get(accessType === 'edit' ? { editId: id } : { shareId: id })
|
let stub = await HomebrewModel.get(accessMap[accessType])
|
||||||
.catch((err)=>{
|
.catch((err)=>{
|
||||||
if(googleId) {
|
if(googleId) {
|
||||||
console.warn(`Unable to find document stub for ${accessType}Id ${id}`);
|
console.warn(`Unable to find document stub for ${accessType}Id ${id}`);
|
||||||
@@ -295,9 +305,8 @@ const api = {
|
|||||||
|
|
||||||
req.params.id = currentTheme.theme;
|
req.params.id = currentTheme.theme;
|
||||||
req.params.renderer = currentTheme.renderer;
|
req.params.renderer = currentTheme.renderer;
|
||||||
}
|
} else {
|
||||||
//=== Static Themes ===//
|
//=== Static Themes ===//
|
||||||
else {
|
|
||||||
const localSnippets = `${req.params.renderer}_${req.params.id}`; // Just log the name for loading on client
|
const localSnippets = `${req.params.renderer}_${req.params.id}`; // Just log the name for loading on client
|
||||||
const localStyle = `@import url(\"/themes/${req.params.renderer}/${req.params.id}/style.css\");`;
|
const localStyle = `@import url(\"/themes/${req.params.renderer}/${req.params.id}/style.css\");`;
|
||||||
completeSnippets.push(localSnippets);
|
completeSnippets.push(localSnippets);
|
||||||
|
|||||||
Reference in New Issue
Block a user