rhythia-api 161.0.0 → 163.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/api/getProfile.ts +14 -1
- package/api/getUserScores.ts +6 -2
- package/index.ts +2 -0
- package/package.json +1 -1
- package/types/database.ts +15 -0
- package/utils/requestUtils.ts +15 -1
package/api/getProfile.ts
CHANGED
|
@@ -32,6 +32,7 @@ export const Schema = {
|
|
|
32
32
|
squares_hit: z.number().nullable(),
|
|
33
33
|
total_score: z.number().nullable(),
|
|
34
34
|
position: z.number().nullable(),
|
|
35
|
+
is_online: z.boolean(),
|
|
35
36
|
})
|
|
36
37
|
.optional(),
|
|
37
38
|
}),
|
|
@@ -51,7 +52,7 @@ export async function handler(
|
|
|
51
52
|
req: Request
|
|
52
53
|
): Promise<NextResponse<(typeof Schema)["output"]["_type"]>> {
|
|
53
54
|
let profiles: Database["public"]["Tables"]["profiles"]["Row"][] = [];
|
|
54
|
-
|
|
55
|
+
let isOnline = false;
|
|
55
56
|
// Fetch by id
|
|
56
57
|
if (data.id !== undefined && data.id !== null) {
|
|
57
58
|
let { data: queryData, error } = await supabase
|
|
@@ -108,6 +109,17 @@ export async function handler(
|
|
|
108
109
|
profiles = queryData;
|
|
109
110
|
}
|
|
110
111
|
}
|
|
112
|
+
|
|
113
|
+
const { data: activityData } = await supabase
|
|
114
|
+
.from("profileActivities")
|
|
115
|
+
.select("*")
|
|
116
|
+
.eq("uid", user.id)
|
|
117
|
+
.single();
|
|
118
|
+
|
|
119
|
+
//last 30 minutes
|
|
120
|
+
if (activityData && activityData.last_activity) {
|
|
121
|
+
isOnline = Date.now() - activityData.last_activity < 1800000;
|
|
122
|
+
}
|
|
111
123
|
}
|
|
112
124
|
|
|
113
125
|
const user = profiles[0];
|
|
@@ -123,6 +135,7 @@ export async function handler(
|
|
|
123
135
|
user: {
|
|
124
136
|
...user,
|
|
125
137
|
position: (playersWithMorePoints || 0) + 1,
|
|
138
|
+
is_online: isOnline,
|
|
126
139
|
},
|
|
127
140
|
});
|
|
128
141
|
}
|
package/api/getUserScores.ts
CHANGED
|
@@ -7,6 +7,7 @@ export const Schema = {
|
|
|
7
7
|
input: z.strictObject({
|
|
8
8
|
session: z.string(),
|
|
9
9
|
id: z.number(),
|
|
10
|
+
limit: z.number().default(10),
|
|
10
11
|
}),
|
|
11
12
|
output: z.object({
|
|
12
13
|
error: z.string().optional(),
|
|
@@ -65,6 +66,9 @@ export async function handler(
|
|
|
65
66
|
data: (typeof Schema)["input"]["_type"],
|
|
66
67
|
req: Request
|
|
67
68
|
): Promise<NextResponse<(typeof Schema)["output"]["_type"]>> {
|
|
69
|
+
if (data.limit > 100) {
|
|
70
|
+
return NextResponse.json({ error: "Limit breached" });
|
|
71
|
+
}
|
|
68
72
|
let { data: scores1, error: errorlast } = await supabase
|
|
69
73
|
.from("scores")
|
|
70
74
|
.select(
|
|
@@ -80,7 +84,7 @@ export async function handler(
|
|
|
80
84
|
.eq("userId", data.id)
|
|
81
85
|
.eq("passed", true)
|
|
82
86
|
.order("created_at", { ascending: false })
|
|
83
|
-
.limit(
|
|
87
|
+
.limit(data.limit);
|
|
84
88
|
|
|
85
89
|
let { data: scores2, error: errorsp } = await supabase
|
|
86
90
|
.from("scores")
|
|
@@ -116,7 +120,7 @@ export async function handler(
|
|
|
116
120
|
const values = Object.values(hashMap);
|
|
117
121
|
let vals = values
|
|
118
122
|
.sort((a, b) => b.awarded_sp - a.awarded_sp)
|
|
119
|
-
.slice(0,
|
|
123
|
+
.slice(0, data.limit)
|
|
120
124
|
.map((e) => e.score);
|
|
121
125
|
|
|
122
126
|
return NextResponse.json({
|
package/index.ts
CHANGED
|
@@ -425,6 +425,7 @@ export const Schema = {
|
|
|
425
425
|
squares_hit: z.number().nullable(),
|
|
426
426
|
total_score: z.number().nullable(),
|
|
427
427
|
position: z.number().nullable(),
|
|
428
|
+
is_online: z.boolean(),
|
|
428
429
|
})
|
|
429
430
|
.optional(),
|
|
430
431
|
}),
|
|
@@ -539,6 +540,7 @@ export const Schema = {
|
|
|
539
540
|
input: z.strictObject({
|
|
540
541
|
session: z.string(),
|
|
541
542
|
id: z.number(),
|
|
543
|
+
limit: z.number().default(10),
|
|
542
544
|
}),
|
|
543
545
|
output: z.object({
|
|
544
546
|
error: z.string().optional(),
|
package/package.json
CHANGED
package/types/database.ts
CHANGED
|
@@ -179,6 +179,21 @@ export type Database = {
|
|
|
179
179
|
},
|
|
180
180
|
]
|
|
181
181
|
}
|
|
182
|
+
profileActivities: {
|
|
183
|
+
Row: {
|
|
184
|
+
last_activity: number | null
|
|
185
|
+
uid: string
|
|
186
|
+
}
|
|
187
|
+
Insert: {
|
|
188
|
+
last_activity?: number | null
|
|
189
|
+
uid: string
|
|
190
|
+
}
|
|
191
|
+
Update: {
|
|
192
|
+
last_activity?: number | null
|
|
193
|
+
uid?: string
|
|
194
|
+
}
|
|
195
|
+
Relationships: []
|
|
196
|
+
}
|
|
182
197
|
profiles: {
|
|
183
198
|
Row: {
|
|
184
199
|
about_me: string | null
|
package/utils/requestUtils.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { NextResponse } from "next/server";
|
|
2
|
-
import { ZodObject } from "zod";
|
|
2
|
+
import { set, ZodObject } from "zod";
|
|
3
3
|
import { getUserBySession } from "./getUserBySession";
|
|
4
|
+
import { supabase } from "./supabase";
|
|
4
5
|
|
|
5
6
|
interface Props<
|
|
6
7
|
K = (...args: any[]) => Promise<NextResponse<any>>,
|
|
@@ -21,6 +22,7 @@ export async function protectedApi({
|
|
|
21
22
|
try {
|
|
22
23
|
const toParse = await request.json();
|
|
23
24
|
const data = schema.input.parse(toParse);
|
|
25
|
+
setActivity(data);
|
|
24
26
|
if (authorization) {
|
|
25
27
|
const authorizationResponse = await authorization(data);
|
|
26
28
|
if (authorizationResponse) {
|
|
@@ -33,6 +35,18 @@ export async function protectedApi({
|
|
|
33
35
|
}
|
|
34
36
|
}
|
|
35
37
|
|
|
38
|
+
export async function setActivity(data: Record<string, any>) {
|
|
39
|
+
if (data.session) {
|
|
40
|
+
const user = (await supabase.auth.getUser(data.session)).data.user;
|
|
41
|
+
if (user) {
|
|
42
|
+
await supabase.from("profileActivities").upsert({
|
|
43
|
+
uid: user.id,
|
|
44
|
+
last_activity: Date.now(),
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
36
50
|
export async function validUser(data) {
|
|
37
51
|
if (!data.session) {
|
|
38
52
|
return NextResponse.json(
|