Просмотр исходного кода

Temporarily add admin unshare api

Frank 7 месяцев назад
Родитель
Сommit
4a5a93b3f8
1 измененных файлов с 28 добавлено и 10 удалено
  1. 28 10
      packages/function/src/api.ts

+ 28 - 10
packages/function/src/api.ts

@@ -35,8 +35,7 @@ export class SyncServer extends DurableObject<Env> {
     ws.close(code, "Durable Object is closing WebSocket")
   }
 
-  async publish(secret: string, key: string, content: any) {
-    if (secret !== (await this.getSecret())) throw new Error("Invalid secret")
+  async publish(key: string, content: any) {
     const sessionID = await this.getSessionID()
     if (
       !key.startsWith(`session/info/${sessionID}`) &&
@@ -76,6 +75,10 @@ export class SyncServer extends DurableObject<Env> {
       .map(([key, content]) => ({ key, content }))
   }
 
+  public async assertSecret(secret: string) {
+    if (secret !== (await this.getSecret())) throw new Error("Invalid secret")
+  }
+
   private async getSecret() {
     return this.ctx.storage.get<string>("secret")
   }
@@ -84,15 +87,19 @@ export class SyncServer extends DurableObject<Env> {
     return this.ctx.storage.get<string>("sessionID")
   }
 
-  async clear(secret: string) {
-    await this.assertSecret(secret)
+  async clear() {
+    const sessionID = await this.getSessionID()
+    const list = await this.env.Bucket.list({
+      prefix: `session/message/${sessionID}/`,
+      limit: 1000,
+    })
+    for (const item of list.objects) {
+      await this.env.Bucket.delete(item.key)
+    }
+    await this.env.Bucket.delete(`session/info/${sessionID}`)
     await this.ctx.storage.deleteAll()
   }
 
-  private async assertSecret(secret: string) {
-    if (secret !== (await this.getSecret())) throw new Error("Invalid secret")
-  }
-
   static shortName(id: string) {
     return id.substring(id.length - 8)
   }
@@ -134,7 +141,17 @@ export default {
       const secret = body.secret
       const id = env.SYNC_SERVER.idFromName(SyncServer.shortName(sessionID))
       const stub = env.SYNC_SERVER.get(id)
-      await stub.clear(secret)
+      await stub.assertSecret(secret)
+      await stub.clear()
+      return new Response(JSON.stringify({}), {
+        headers: { "Content-Type": "application/json" },
+      })
+    }
+
+    if (request.method === "POST" && method === "share_delete_admin") {
+      const id = env.SYNC_SERVER.idFromName("oVF8Rsiv")
+      const stub = env.SYNC_SERVER.get(id)
+      await stub.clear()
       return new Response(JSON.stringify({}), {
         headers: { "Content-Type": "application/json" },
       })
@@ -150,7 +167,8 @@ export default {
       const name = SyncServer.shortName(body.sessionID)
       const id = env.SYNC_SERVER.idFromName(name)
       const stub = env.SYNC_SERVER.get(id)
-      await stub.publish(body.secret, body.key, body.content)
+      await stub.assertSecret(body.secret)
+      await stub.publish(body.key, body.content)
       return new Response(JSON.stringify({}), {
         headers: { "Content-Type": "application/json" },
       })