From b4c2dfd8b2a42c06501622ac3c727a23a8edd7aa Mon Sep 17 00:00:00 2001 From: natehuby76 <86365949+natehuby76@users.noreply.github.com> Date: Wed, 25 Nov 2026 10:14:02 -0500 Subject: [PATCH] unfollow the staff member who closes a ticket --- services/companion-bot/case-actions.mjs | 11 ++++++++++- services/companion-bot/case-actions.test.mjs | 18 ++++++++++++++++-- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/services/companion-bot/case-actions.mjs b/services/companion-bot/case-actions.mjs index e88c448873..185557141d 100644 --- a/services/companion-bot/case-actions.mjs +++ b/services/companion-bot/case-actions.mjs @@ -317,9 +317,18 @@ export async function caseAction(storage,env,b){ } const post=await botPost(env,channel,payload); if(nextStatus==='Closed'){ + let followNotice=''; + if(action==='status' && /^\d{1,20}$/.test(actor)){ + try{ + const membership=await fetch('https://discord.com/api/v10/channels/'+channel+'/thread-members/'+actor,{ + method:'DELETE',headers:{Authorization:'Bot '+env.DISCORD_BOT_TOKEN},redirect:'manual',signal:AbortSignal.timeout(5000) + }); + if(!membership.ok && membership.status!==404)followNotice=' Discord could not unfollow you.'; + }catch{followNotice=' Discord could not confirm that you unfollowed.';} + } try{await discordTags(env,'/channels/'+channel,{archived:true});} catch{return result('Tagged Closed, but archiving failed. Check Manage Threads, then choose Closed again.');} - return result(post.ok?'Closed and archived. The original thread link still works.':'Closed and archived. Discord did not confirm the status card.'); + return result((post.ok?'Closed and archived. The original thread link still works.':'Closed and archived. Discord did not confirm the status card.')+followNotice); } return result(post.ok?'Updated this report.':'Could not confirm the update message. Refresh the thread.'); } diff --git a/services/companion-bot/case-actions.test.mjs b/services/companion-bot/case-actions.test.mjs index c9796abbb9..5c66ff0e5a 100644 --- a/services/companion-bot/case-actions.test.mjs +++ b/services/companion-bot/case-actions.test.mjs @@ -86,7 +86,13 @@ test('closing archives after the status card and reopening restores the same thr const store={get:k=>data.get(k),put:(k,v)=>data.set(k,v),transaction:fn=>fn(store)}; const thread={id:'4',type:11,parent_id:supportForum,applied_tags:['42','1565236988913913887'],thread_metadata:{archived:false}}; const events=[]; + let unfollowStatus=204; t.mock.method(globalThis,'fetch',async(url,options)=>{ + if(options.method==='DELETE'){ + assert.equal(url,'https://discord.com/api/v10/channels/4/thread-members/3'); + events.push('unfollow closer'); + return new Response(null,{status:unfollowStatus}); + } if(options.method==='PATCH'){ const patch=JSON.parse(options.body); if(patch.applied_tags)thread.applied_tags=patch.applied_tags; @@ -101,10 +107,18 @@ test('closing archives after the status card and reopening restores the same thr }); const act=(action,value,id)=>caseAction(store,env,{channel:'4',user:'1',platform:'ios',actor:'3',action,value,interaction:id}); await act('status','Closed','1'); - assert.deepEqual(events,[{applied_tags:['42','1565237052524466207']},0x3498db,{archived:true}]); + assert.deepEqual(events,[{applied_tags:['42','1565237052524466207']},0x3498db,'unfollow closer',{archived:true}]); assert.match((await (await act('working',null,'2')).json()).message,/closed or archived/); await act('status','Open','3'); - assert.deepEqual(events.slice(3),[{applied_tags:['42','1565236988913913887'],archived:false},0xe74c3c]); + assert.deepEqual(events.slice(4),[{applied_tags:['42','1565236988913913887'],archived:false},0xe74c3c]); + unfollowStatus=403; + assert.match((await (await act('status','Closed','4')).json()).message,/archived.*could not unfollow you/); + assert.equal(thread.thread_metadata.archived,true); + await act('status','Open','5'); + events.length=0; + await caseAction(store,env,{channel:'4',user:'1',platform:'ios',actor:'1',action:'fulfilled',interaction:'6'}); + assert.equal(events.includes('unfollow closer'),false); + assert.equal(thread.thread_metadata.archived,true); }); test('the overview is staff-only and reads archived tickets without posting',async t=>{