2024-04-11 13:39:07 +02:00
|
|
|
import type { InferSelectModel } from "drizzle-orm";
|
|
|
|
|
import { db } from "~drizzle/db";
|
|
|
|
|
import type { application } from "~drizzle/schema";
|
2024-04-14 12:53:21 +02:00
|
|
|
import type { Application as APIApplication } from "~types/mastodon/application";
|
2023-09-12 22:48:10 +02:00
|
|
|
|
2024-04-11 13:39:07 +02:00
|
|
|
export type Application = InferSelectModel<typeof application>;
|
2023-09-12 22:48:10 +02:00
|
|
|
|
2023-11-11 03:36:06 +01:00
|
|
|
/**
|
|
|
|
|
* Retrieves the application associated with the given access token.
|
|
|
|
|
* @param token The access token to retrieve the application for.
|
|
|
|
|
* @returns The application associated with the given access token, or null if no such application exists.
|
|
|
|
|
*/
|
|
|
|
|
export const getFromToken = async (
|
2024-04-07 07:30:49 +02:00
|
|
|
token: string,
|
2023-11-11 03:36:06 +01:00
|
|
|
): Promise<Application | null> => {
|
2024-04-11 13:39:07 +02:00
|
|
|
const result = await db.query.token.findFirst({
|
|
|
|
|
where: (tokens, { eq }) => eq(tokens.accessToken, token),
|
|
|
|
|
with: {
|
2024-04-07 07:30:49 +02:00
|
|
|
application: true,
|
|
|
|
|
},
|
|
|
|
|
});
|
2023-11-11 03:36:06 +01:00
|
|
|
|
2024-04-11 13:39:07 +02:00
|
|
|
return result?.application || null;
|
2023-11-11 03:36:06 +01:00
|
|
|
};
|
2023-09-22 00:42:59 +02:00
|
|
|
|
2023-11-11 03:36:06 +01:00
|
|
|
/**
|
|
|
|
|
* Converts this application to an API application.
|
|
|
|
|
* @returns The API application representation of this application.
|
|
|
|
|
*/
|
2023-11-20 03:42:40 +01:00
|
|
|
export const applicationToAPI = (app: Application): APIApplication => {
|
2024-04-07 07:30:49 +02:00
|
|
|
return {
|
|
|
|
|
name: app.name,
|
|
|
|
|
website: app.website,
|
2024-04-11 13:39:07 +02:00
|
|
|
vapid_key: app.vapidKey,
|
2024-04-07 07:30:49 +02:00
|
|
|
};
|
2023-11-11 03:36:06 +01:00
|
|
|
};
|