forked from unimatrix/web
14 lines
2.1 KiB
JavaScript
14 lines
2.1 KiB
JavaScript
import {randomUUID} from 'node:crypto';import {fileURLToPath} from 'node:url';import {resolve} from 'node:path';
|
|
import {openDatabase} from '../node/mysql.mjs';import {OFFICIAL_ACCOUNT} from '../node/official-account.mjs';
|
|
export async function adoptAutomatedNewsAuthor(db,{actorId,apply=false}={}){
|
|
if(![OFFICIAL_ACCOUNT.ownerId,'f7b88778-f0bc-41cc-9227-d8ef2f52264d'].includes(actorId))throw Error('Pinned Owner or Dev actor required.');
|
|
return db.transaction(async tx=>{
|
|
const account=await tx.first("SELECT id FROM site_accounts WHERE id=? AND username=? AND role='official_account' AND password_hash NOT LIKE '!%' FOR SHARE",[OFFICIAL_ACCOUNT.id,OFFICIAL_ACCOUNT.username]);if(!account)throw Error('Official publishing identity unavailable.');
|
|
const actor=await tx.first("SELECT p.principal_kind FROM org_control_principals p JOIN site_accounts a ON a.id=p.user_id WHERE p.user_id=? AND a.password_hash NOT LIKE '!%' FOR SHARE",[actorId]);if(!['owner','service'].includes(actor?.principal_kind))throw Error('Current control actor required.');
|
|
const rows=await tx.query("SELECT id,source_key FROM website_posts WHERE author_id='release-register' AND LEFT(source_key,8)='release:' FOR UPDATE");
|
|
if(!apply)return {applied:false,automatedPosts:rows.length};
|
|
for(const p of rows){await tx.query("UPDATE website_posts SET author_id=?,version=version+1 WHERE id=? AND author_id='release-register'",[OFFICIAL_ACCOUNT.id,p.id]);await tx.query('INSERT INTO account_audit(id,actor_id,action,details,created_at) VALUES(?,?,?,?,?)',[randomUUID(),actorId,'automated-news-author',JSON.stringify({postId:p.id,source:p.source_key,previousAuthor:'release-register',authorId:OFFICIAL_ACCOUNT.id}),Date.now()]);}
|
|
return {applied:true,updated:rows.length,manualPostsChanged:false,newArticlesCreated:false};
|
|
});
|
|
}
|
|
if(process.argv[1]&&fileURLToPath(import.meta.url)===resolve(process.argv[1])){const db=await openDatabase(process.env);try{console.log(JSON.stringify(await adoptAutomatedNewsAuthor(db,{actorId:process.argv.find(a=>a.startsWith('--actor='))?.slice(8),apply:process.argv.includes('--apply')})));}finally{await db.close();}}
|

Staff