From 829e765a16e600c78ea574560cf4755ddd014e10 Mon Sep 17 00:00:00 2001 From: Muncherkin <48158637+Muncherkin@users.noreply.github.com> Date: Wed, 17 Jul 2024 19:25:23 +0200 Subject: [PATCH 1/3] HTML tags to Latex service added for future efterphest feature --- .env.example | 5 ++++- src/config.ts | 9 +++++++++ src/models/generated/graphql.ts | 11 +++++++++++ src/resolvers/index.ts | 1 + src/resolvers/latex.resolver.ts | 15 +++++++++++++++ src/schemas/latexify.graphql | 3 +++ src/services/latexify.service.ts | 30 ++++++++++++++++++++++++++++++ 7 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 src/resolvers/latex.resolver.ts create mode 100644 src/schemas/latexify.graphql create mode 100644 src/services/latexify.service.ts diff --git a/.env.example b/.env.example index b6cd64b5..80995d61 100644 --- a/.env.example +++ b/.env.example @@ -30,4 +30,7 @@ SKIP_ACCESS_CHECKS=false POST_ACCESS_COOLDOWN_DAYS=90 # Pdf to png settings -PDF_TO_PNG_BASE_URL=https://pdf-to-png.esek.se \ No newline at end of file +PDF_TO_PNG_BASE_URL=https://pdf-to-png.esek.se + +#Latexify settings +LATEXIFY_URL= \ No newline at end of file diff --git a/src/config.ts b/src/config.ts index 50fdf7ba..2dbad31b 100644 --- a/src/config.ts +++ b/src/config.ts @@ -60,6 +60,14 @@ const PDF_TO_PNG = { URL: process.env.PDF_TO_PNG_BASE_URL ?? '', }; +/** + * Config for LaTeXify + * @param {string} URL - The base URL for the LaTeXify microservice + */ +const LATEXIFY = { + URL: process.env.LATEXIFY_URL ?? '', +}; + const JWT = { SECRET: (process.env.JWT_SECRET as string) ?? '', }; @@ -77,6 +85,7 @@ const config = { LU, WIKI, PDF_TO_PNG, + LATEXIFY, JWT, }; diff --git a/src/models/generated/graphql.ts b/src/models/generated/graphql.ts index 091e9f98..cc0f75c7 100644 --- a/src/models/generated/graphql.ts +++ b/src/models/generated/graphql.ts @@ -929,6 +929,7 @@ export type Query = { latestElections: Array; latestHehe: Array; latestnews: Array
; + latexify: Scalars['String']; me: User; meeting: Meeting; meetings: Array; @@ -1138,6 +1139,15 @@ export type QueryLatestnewsArgs = { }; +/** + * Queries and mutations that relies on an election being open + * does not take an `electionId` parameter. + */ +export type QueryLatexifyArgs = { + text: Scalars['String']; +}; + + /** * Queries and mutations that relies on an election being open * does not take an `electionId` parameter. @@ -1926,6 +1936,7 @@ export type QueryResolvers, ParentType, ContextType, Partial>; latestHehe?: Resolver, ParentType, ContextType, Partial>; latestnews?: Resolver, ParentType, ContextType, Partial>; + latexify?: Resolver>; me?: Resolver; meeting?: Resolver>; meetings?: Resolver, ParentType, ContextType, Partial>; diff --git a/src/resolvers/index.ts b/src/resolvers/index.ts index 97c2ad19..ba9d7707 100644 --- a/src/resolvers/index.ts +++ b/src/resolvers/index.ts @@ -11,3 +11,4 @@ export { default as meeting } from './meeting.resolver'; export { default as post } from './post.resolver'; export { default as ticket } from './ticket.resolver'; export { default as user } from './user.resolver'; +export { default as latexify } from './latex.resolver'; diff --git a/src/resolvers/latex.resolver.ts b/src/resolvers/latex.resolver.ts new file mode 100644 index 00000000..4ea8dc45 --- /dev/null +++ b/src/resolvers/latex.resolver.ts @@ -0,0 +1,15 @@ +import { hasAuthenticated } from '@/util'; +import { Resolvers } from '@generated/graphql'; +import { latexify } from '@service/latexify'; + +const latexifyResolver: Resolvers = { + Query: { + latexify: async (_, { text }, ctx) => { + await hasAuthenticated(ctx); + const res = await latexify(text); + return res; + }, + }, +}; + +export default latexifyResolver; diff --git a/src/schemas/latexify.graphql b/src/schemas/latexify.graphql new file mode 100644 index 00000000..cb81b093 --- /dev/null +++ b/src/schemas/latexify.graphql @@ -0,0 +1,3 @@ +type Query { + latexify(text: String!): String! +} \ No newline at end of file diff --git a/src/services/latexify.service.ts b/src/services/latexify.service.ts new file mode 100644 index 00000000..2877a895 --- /dev/null +++ b/src/services/latexify.service.ts @@ -0,0 +1,30 @@ +import config from '@/config'; +import { ServerError } from '@/errors/request.errors'; +import { QueryLatexifyArgs } from '@generated/graphql'; +import axios, { AxiosResponse } from 'axios'; + +const { + LATEXIFY: { URL }, +} = config; + +const api = axios.create({ + baseURL: URL, + headers: { + 'Content-Type': 'application/json', + }, +}); + +export const latexify = async (text: string) => { + try { + const res = await api.post('/latexify', { + text: text, + }); + if (res.status == 200) { + return res.data as string; + } else { + throw new ServerError(`${res.status}: ${res.statusText}`); + } + } catch { + throw new ServerError('Texten kunde inte konverteras till LaTeX'); + } +}; From 1fb0612d453f7d0543c2731b5263bb6290b43bd3 Mon Sep 17 00:00:00 2001 From: Muncherkin <48158637+Muncherkin@users.noreply.github.com> Date: Wed, 17 Jul 2024 19:34:18 +0200 Subject: [PATCH 2/3] CHANGELOG and package.json updated... --- CHANGELOG.MD | 5 +++++ package.json | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.MD b/CHANGELOG.MD index 877b01b4..8edf58e9 100644 --- a/CHANGELOG.MD +++ b/CHANGELOG.MD @@ -5,6 +5,11 @@ Alla märkbara ändringar ska dokumenteras i denna fil. Baserat på [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), och följer [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [1.12.0] - 2024-07-17 + +### Tillagt +- Service för LaTeXify med tillhörande graph, resolver och tillägg i .env.example + ## [1.11.0] - 2024-06-07 ### Tillagt diff --git a/package.json b/package.json index cda769c2..ae29aaf2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ekorre-ts", - "version": "1.11.0", + "version": "1.12.0", "description": "E-Sektionens backend", "main": "src/index.ts", "scripts": { From eca9fe269c7b8d2dd8e5eb8bf476ed4279daad70 Mon Sep 17 00:00:00 2001 From: Muncherkin <48158637+Muncherkin@users.noreply.github.com> Date: Tue, 23 Jul 2024 15:35:49 +0200 Subject: [PATCH 3/3] added reasonable default for latexify url --- .env.example | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.env.example b/.env.example index 80995d61..85baa050 100644 --- a/.env.example +++ b/.env.example @@ -33,4 +33,4 @@ POST_ACCESS_COOLDOWN_DAYS=90 PDF_TO_PNG_BASE_URL=https://pdf-to-png.esek.se #Latexify settings -LATEXIFY_URL= \ No newline at end of file +LATEXIFY_URL=https://latexify.esek.se \ No newline at end of file