register the companion referral message command

This commit is contained in:
natehuby76
2026-11-25 00:20:37 -05:00
parent df47158568
commit 3ab9f67c83
3 changed files with 31 additions and 2 deletions
+1 -1
View File
@@ -1 +1 @@
{"protected_inventory_sha256": "db7ac87a7ed19617ed34a01dd70014a99296ca8c06c419fbcf4e590d7859add0", "schema_version": 1}
{"protected_inventory_sha256": "990c033383dd223b6c93eb56e40576d6f83454dc2706bd9f239abdfa04799ce2", "schema_version": 1}
+20
View File
@@ -139,6 +139,25 @@ async function recoverReview(store,env,key,record){
});
if(reserved)await deliverReview(store,env,key,record);
}
async function ensureReferralCommand(store,env){
const key='refer-command-v1';
const saved=await store.get(key);
if(saved?.id||saved?.retryAfter>Date.now())return;
await store.put(key,{retryAfter:Date.now()+300000});
try{
const app='1574857862872703088';
const response=await fetch('https://discord.com/api/v10/applications/'+app+'/guilds/'+guild+'/commands',{
method:'POST',redirect:'manual',signal:AbortSignal.timeout(10000),
headers:{Authorization:'Bot '+env.DISCORD_BOT_TOKEN,'Content-Type':'application/json'},
body:JSON.stringify({name:command,type:3})
});
if(!response.ok)throw Error('HTTP '+response.status);
const registered=await response.json();
if(!id(registered.id)||registered.name!==command||registered.type!==3||registered.guild_id!==guild||registered.application_id!==app)
throw Error('Invalid command receipt');
await store.put(key,{id:registered.id});
}catch(error){console.warn('Discord referral command registration pending',error.message);}
}
export async function pollReferrals(env){
if(!configured(env))return;
const res=await object(env).fetch('https://reports/referrals/scan',{method:'POST'});
@@ -149,6 +168,7 @@ export async function referralStore(path,request,store,env){
env=await resolveSource(store,env);
await staffDestination(env);
if(path.endsWith('/scan')){
await ensureReferralCommand(store,env);
const start=await store.get('start');
if(!start){await store.put('start',Date.now());return Response.json({ready:true});}
const {threads}=await discordTags(env,'/guilds/'+guild+'/threads/active');
+10 -1
View File
@@ -29,6 +29,7 @@ test('scan resolves the sample, skips old tickets, and sends a single private ca
const guild='1387432184121393333',forum='1574085782967292000',review='1575000066366644244',sample='1574085782967292086';
const bot='123456789123456789',user='987654321987654321';
const fresh=String(BigInt(Date.now()+1000-1420070400000)<<22n);
let registrations=0;
const posts=[];
const env={DRIVING_SAMPLE_THREAD_ID:sample,COMPANION_REVIEW_THREAD_ID:review,DISCORD_BOT_TOKEN:'fixture',REPORT_MAINTAINER_ID:'524762496373096459',ANDROID_REPORT_MAINTAINER_ID:'420178305073938433',FIRESTAR_DISCORD_USER_ID:'123456789123456780'};
const rows=new Map();
@@ -36,6 +37,13 @@ test('scan resolves the sample, skips old tickets, and sends a single private ca
const thread=id=>({id,type:11,guild_id:guild,parent_id:forum});
t.mock.method(globalThis,'fetch',async(url,opts)=>{
const path=new URL(url).pathname.replace('/api/v10','');
if(path==='/applications/1574857862872703088/guilds/'+guild+'/commands'){
assert.equal(opts.method,'POST');
const payload=JSON.parse(opts.body);
assert.deepEqual(payload,{name:'Refer to Companion Support',type:3});
registrations++;
return Response.json({...payload,id:sample,guild_id:guild,application_id:'1574857862872703088'});
}
if(opts.method==='POST'){posts.push({path,body:JSON.parse(opts.body)});return Response.json({id:'1'});}
if(path==='/channels/'+sample)return Response.json(thread(sample));
if(path==='/channels/'+forum)return Response.json({id:forum,type:15,guild_id:guild});
@@ -45,6 +53,7 @@ test('scan resolves the sample, skips old tickets, and sends a single private ca
throw Error('Unexpected access '+path);
});
for(let i=0;i<3;i++)await referralStore('/referrals/scan',{},store,env);
assert.equal(registrations,1);
assert.equal(posts.length,1);
assert.ok(posts[0].body.flags & 4096);
assert.deepEqual(posts[0].body.allowed_mentions,{parse:[],users:[env.REPORT_MAINTAINER_ID,env.ANDROID_REPORT_MAINTAINER_ID,env.FIRESTAR_DISCORD_USER_ID]});
@@ -181,7 +190,7 @@ test('lost acknowledgements recover the existing review card and Companion ticke
const source='1574085782967292086',review='1575000066366644244',guild='1387432184121393333',forum='1574085782967292000',bot='1574857862872703088',dest='1574999999999999999',support='1565235002734809168';
const key='report:'+source,title='A Companion report';
const record={state:'offering',category:'Phone app',source,reporter:source,observed:'Android app'};
const rows=new Map([['start',1],['source:'+source,{DRIVING_FORUM_ID:forum}],[key,record]]);
const rows=new Map([['refer-command-v1',{id:dest}],['start',1],['source:'+source,{DRIVING_FORUM_ID:forum}],[key,record]]);
const store={get:k=>rows.get(k),put:(k,v)=>rows.set(k,v),transaction:fn=>fn(store)};
const env={COMPANION_REVIEW_THREAD_ID:review,DRIVING_SAMPLE_THREAD_ID:source,DISCORD_BOT_TOKEN:'test',REPORTS:{idFromName:k=>k,get:()=>({fetch:()=>new Response('',{status:503})})}};
t.mock.method(globalThis,'fetch',async(url,o)=>{