tangled
alpha
login
or
join now
t1c.dev
/
rocksky
forked from
rocksky.app/rocksky
2
fork
atom
A decentralized music tracking and discovery platform built on AT Protocol 🎵
2
fork
atom
overview
issues
pulls
pipelines
[api] remove refresh sessions logic
tsiry-sandratraina.com
9 months ago
db5cbd56
1987d487
-64
3 changed files
expand all
collapse all
unified
split
rockskyapi
rocksky-auth
src
bsky
app.ts
index.ts
sessions.ts
-36
rockskyapi/rocksky-auth/src/bsky/app.ts
···
1
1
import { BlobRef } from "@atproto/lexicon";
2
2
import { isValidHandle } from "@atproto/syntax";
3
3
import { equals } from "@xata.io/client";
4
4
-
import chalk from "chalk";
5
4
import { ctx } from "context";
6
5
import { desc, eq } from "drizzle-orm";
7
6
import { Hono } from "hono";
···
71
70
cli = ctx.kv.get(`cli:${handle}`);
72
71
ctx.kv.delete(`cli:${handle}`);
73
72
74
74
-
await fetch(`http://localhost:8000/refresh/${did}`, {
75
75
-
method: "POST",
76
76
-
headers: {
77
77
-
"Content-Type": "application/json",
78
78
-
},
79
79
-
body: JSON.stringify({}),
80
80
-
});
81
81
-
82
73
const token = jwt.sign(
83
74
{
84
75
did,
···
110
101
return c.redirect(`${env.FRONTEND_URL}?did=${did}&cli=${cli}`);
111
102
});
112
103
113
113
-
app.post("/refresh/:did", async (c) => {
114
114
-
const cache = await ctx.redis.get(`refresh:${c.req.param("did")}`);
115
115
-
if (cache) {
116
116
-
return c.text(cache);
117
117
-
}
118
118
-
119
119
-
const did = c.req.param("did");
120
120
-
setInterval(
121
121
-
() => {
122
122
-
createAgent(ctx.oauthClient, did).catch(() => {});
123
123
-
console.log(`Refreshing agent for ${chalk.greenBright(did)}`);
124
124
-
},
125
125
-
// every 5 minutes
126
126
-
5 * 60 * 1000
127
127
-
);
128
128
-
await ctx.redis.set(`refresh:${did}`, did);
129
129
-
return c.text("ok");
130
130
-
});
131
131
-
132
104
app.get("/profile", async (c) => {
133
105
requestCounter.add(1, { method: "GET", route: "/profile" });
134
106
const bearer = (c.req.header("authorization") || "").split(" ")[1]?.trim();
···
148
120
c.status(401);
149
121
return c.text("Unauthorized");
150
122
}
151
151
-
152
152
-
await fetch(`http://localhost:8000/refresh/${did}`, {
153
153
-
method: "POST",
154
154
-
headers: {
155
155
-
"Content-Type": "application/json",
156
156
-
},
157
157
-
body: JSON.stringify({}),
158
158
-
});
159
123
160
124
const { data: profileRecord } = await agent.com.atproto.repo.getRecord({
161
125
repo: agent.assertDid,
-3
rockskyapi/rocksky-auth/src/index.ts
···
14
14
} from "lovedtracks/lovedtracks.service";
15
15
import { scrobbleTrack } from "nowplaying/nowplaying.service";
16
16
import { rateLimiter } from "ratelimiter";
17
17
-
import { refreshSessions } from "sessions";
18
17
import subscribe from "subscribers";
19
18
import { saveTrack } from "tracks/tracks.service";
20
19
import { trackSchema } from "types/track";
···
33
32
import webscrobbler from "./webscrobbler/app";
34
33
35
34
subscribe(ctx);
36
36
-
37
37
-
refreshSessions();
38
35
39
36
const app = new Hono();
40
37
const { injectWebSocket, upgradeWebSocket } = createNodeWebSocket({ app });
-25
rockskyapi/rocksky-auth/src/sessions.ts
···
1
1
-
import chalk from "chalk";
2
2
-
import { ctx } from "context";
3
3
-
import { createAgent } from "lib/agent";
4
4
-
5
5
-
export async function refreshSessions() {
6
6
-
const keys = await ctx.redis.keys("refresh:*");
7
7
-
return new Promise<void>((resolve) => {
8
8
-
for (const key of keys) {
9
9
-
const did = key.split("refresh:")[1];
10
10
-
setTimeout(() => {
11
11
-
setInterval(
12
12
-
async () => {
13
13
-
createAgent(ctx.oauthClient, did).catch(() => {});
14
14
-
console.log(`Refreshing agent for ${chalk.greenBright(did)}`);
15
15
-
},
16
16
-
// every 5 minutes
17
17
-
5 * 60 * 1000
18
18
-
);
19
19
-
}, 5 * 1000);
20
20
-
}
21
21
-
22
22
-
console.log(`Refreshed ${chalk.greenBright(keys.length)} sessions`);
23
23
-
resolve();
24
24
-
});
25
25
-
}