rhythia-api 162.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/index.ts +1 -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/index.ts
CHANGED
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(
|