Skip to content

Commit

Permalink
Merge pull request #274 from esek/feat/latexifyservice
Browse files Browse the repository at this point in the history
HTML tags to Latex service added for future efterphest feature
  • Loading branch information
pontussjostedt authored Jul 23, 2024
2 parents 6161e4c + eca9fe2 commit 9017d90
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 2 deletions.
5 changes: 4 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -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
PDF_TO_PNG_BASE_URL=https://pdf-to-png.esek.se

#Latexify settings
LATEXIFY_URL=https://latexify.esek.se
5 changes: 5 additions & 0 deletions CHANGELOG.MD
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ekorre-ts",
"version": "1.11.0",
"version": "1.12.0",
"description": "E-Sektionens backend",
"main": "src/index.ts",
"scripts": {
Expand Down
9 changes: 9 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) ?? '',
};
Expand All @@ -77,6 +85,7 @@ const config = {
LU,
WIKI,
PDF_TO_PNG,
LATEXIFY,
JWT,
};

Expand Down
11 changes: 11 additions & 0 deletions src/models/generated/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -929,6 +929,7 @@ export type Query = {
latestElections: Array<Election>;
latestHehe: Array<Hehe>;
latestnews: Array<Article>;
latexify: Scalars['String'];
me: User;
meeting: Meeting;
meetings: Array<Meeting>;
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -1926,6 +1936,7 @@ export type QueryResolvers<ContextType = Context, ParentType extends ResolversPa
latestElections?: Resolver<Array<ResolversTypes['Election']>, ParentType, ContextType, Partial<QueryLatestElectionsArgs>>;
latestHehe?: Resolver<Array<ResolversTypes['Hehe']>, ParentType, ContextType, Partial<QueryLatestHeheArgs>>;
latestnews?: Resolver<Array<ResolversTypes['Article']>, ParentType, ContextType, Partial<QueryLatestnewsArgs>>;
latexify?: Resolver<ResolversTypes['String'], ParentType, ContextType, RequireFields<QueryLatexifyArgs, 'text'>>;
me?: Resolver<ResolversTypes['User'], ParentType, ContextType>;
meeting?: Resolver<ResolversTypes['Meeting'], ParentType, ContextType, RequireFields<QueryMeetingArgs, 'id'>>;
meetings?: Resolver<Array<ResolversTypes['Meeting']>, ParentType, ContextType, Partial<QueryMeetingsArgs>>;
Expand Down
1 change: 1 addition & 0 deletions src/resolvers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
15 changes: 15 additions & 0 deletions src/resolvers/latex.resolver.ts
Original file line number Diff line number Diff line change
@@ -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;
3 changes: 3 additions & 0 deletions src/schemas/latexify.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
type Query {
latexify(text: String!): String!
}
30 changes: 30 additions & 0 deletions src/services/latexify.service.ts
Original file line number Diff line number Diff line change
@@ -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<QueryLatexifyArgs, AxiosResponse>('/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');
}
};

0 comments on commit 9017d90

Please sign in to comment.