@@ -35,8 +35,7 @@ export class SyncServer extends DurableObject<Env> { |
35 | 35 | ws.close(code, "Durable Object is closing WebSocket") |
36 | 36 | } |
37 | 37 |
|
38 | | - async publish(secret: string, key: string, content: any) { |
39 | | - if (secret !== (await this.getSecret())) throw new Error("Invalid secret") |
| 38 | + async publish(key: string, content: any) { |
40 | 39 | const sessionID = await this.getSessionID() |
41 | 40 | if ( |
42 | 41 | !key.startsWith(`session/info/${sessionID}`) && |
@@ -76,6 +75,10 @@ export class SyncServer extends DurableObject<Env> { |
76 | 75 | .map(([key, content]) => ({ key, content })) |
77 | 76 | } |
78 | 77 |
|
| 78 | + public async assertSecret(secret: string) { |
| 79 | + if (secret !== (await this.getSecret())) throw new Error("Invalid secret") |
| 80 | + } |
| 81 | + |
79 | 82 | private async getSecret() { |
80 | 83 | return this.ctx.storage.get<string>("secret") |
81 | 84 | } |
@@ -84,15 +87,19 @@ export class SyncServer extends DurableObject<Env> { |
84 | 87 | return this.ctx.storage.get<string>("sessionID") |
85 | 88 | } |
86 | 89 |
|
87 | | - async clear(secret: string) { |
88 | | - await this.assertSecret(secret) |
| 90 | + async clear() { |
| 91 | + const sessionID = await this.getSessionID() |
| 92 | + const list = await this.env.Bucket.list({ |
| 93 | + prefix: `session/message/${sessionID}/`, |
| 94 | + limit: 1000, |
| 95 | + }) |
| 96 | + for (const item of list.objects) { |
| 97 | + await this.env.Bucket.delete(item.key) |
| 98 | + } |
| 99 | + await this.env.Bucket.delete(`session/info/${sessionID}`) |
89 | 100 | await this.ctx.storage.deleteAll() |
90 | 101 | } |
91 | 102 |
|
92 | | - private async assertSecret(secret: string) { |
93 | | - if (secret !== (await this.getSecret())) throw new Error("Invalid secret") |
94 | | - } |
95 | | - |
96 | 103 | static shortName(id: string) { |
97 | 104 | return id.substring(id.length - 8) |
98 | 105 | } |
@@ -134,7 +141,17 @@ export default { |
134 | 141 | const secret = body.secret |
135 | 142 | const id = env.SYNC_SERVER.idFromName(SyncServer.shortName(sessionID)) |
136 | 143 | const stub = env.SYNC_SERVER.get(id) |
137 | | - await stub.clear(secret) |
| 144 | + await stub.assertSecret(secret) |
| 145 | + await stub.clear() |
| 146 | + return new Response(JSON.stringify({}), { |
| 147 | + headers: { "Content-Type": "application/json" }, |
| 148 | + }) |
| 149 | + } |
| 150 | + |
| 151 | + if (request.method === "POST" && method === "share_delete_admin") { |
| 152 | + const id = env.SYNC_SERVER.idFromName("oVF8Rsiv") |
| 153 | + const stub = env.SYNC_SERVER.get(id) |
| 154 | + await stub.clear() |
138 | 155 | return new Response(JSON.stringify({}), { |
139 | 156 | headers: { "Content-Type": "application/json" }, |
140 | 157 | }) |
@@ -150,7 +167,8 @@ export default { |
150 | 167 | const name = SyncServer.shortName(body.sessionID) |
151 | 168 | const id = env.SYNC_SERVER.idFromName(name) |
152 | 169 | const stub = env.SYNC_SERVER.get(id) |
153 | | - await stub.publish(body.secret, body.key, body.content) |
| 170 | + await stub.assertSecret(body.secret) |
| 171 | + await stub.publish(body.key, body.content) |
154 | 172 | return new Response(JSON.stringify({}), { |
155 | 173 | headers: { "Content-Type": "application/json" }, |
156 | 174 | }) |
|