Skip to content

Commit

Permalink
feat: PMD-83 & PMD-84 & PMD-85 (#725)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bas950 authored Oct 6, 2022
1 parent f193f78 commit 645e5bb
Show file tree
Hide file tree
Showing 23 changed files with 271 additions and 72 deletions.
2 changes: 1 addition & 1 deletion master/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion worker/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
"build": "tsc && devScript --copyOnly"
},
"dependencies": {
"@keyv/redis": "^2.5.1",
"@sentry/node": "^6.19.2",
"@sentry/tracing": "^6.19.2",
"apollo-datasource": "^3.3.1",
"apollo-mongodb-datasource": "^1.3.3",
"apollo-server-cache-redis": "^3.3.1",
"apollo-server-caching": "^3.3.0",
"apollo-server-core": "^3.6.6",
Expand All @@ -27,6 +27,7 @@
"language-flag-colors": "^2.1.1",
"lodash": "^4.17.21",
"mongodb": "^4.4.1",
"mongodb-caching": "^1.1.0",
"p-event": "^4.2.0",
"source-map-support": "^0.5.21",
"validator": "^13.7.0"
Expand Down
65 changes: 50 additions & 15 deletions worker/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions worker/src/generic/ffUpdates.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import MongoDataSource from "apollo-mongodb-datasource";
import { RouteGenericInterface, RouteHandlerMethod } from "fastify/types/route";
import { IncomingMessage, Server, ServerResponse } from "http";
import MongoDBCaching from "mongodb-caching";

import { dSources } from "../util/dataSources";

export class FFUpdates extends MongoDataSource {
export class FFUpdates extends MongoDBCaching {
getAll() {
return this.find();
}
Expand All @@ -19,6 +19,7 @@ const handler: RouteHandlerMethod<
> = async (req, res) => {
const updates = await dSources.ffUpdates.getAll();

//@ts-ignore
for (const u of updates) delete u._id;

return res.send({
Expand Down
2 changes: 2 additions & 0 deletions worker/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,14 @@ async function run() {
]
};

//@ts-ignore
v3Server = new ApolloServer({
...apolloGenericSettings,
typeDefs: await v3TypeDefs,
resolvers: await v3Resolvers
});

//@ts-ignore
v4Server = new ApolloServer({
...apolloGenericSettings,
typeDefs: await v4TypeDefs,
Expand Down
85 changes: 66 additions & 19 deletions worker/src/util/dataSources.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Db } from "mongodb";

import { baseRedisCache, mongodb } from "..";
import { mongodb } from "..";
import { FFUpdates } from "../generic/ffUpdates";
import { Benefits } from "../v3/fields/benefits";
import { BetaUsers } from "../v3/fields/betaUsers";
Expand All @@ -21,25 +21,72 @@ export let pmdDb: Db, dSources: ReturnType<typeof dataSources>;
export default function dataSources() {
pmdDb = mongodb.db("PreMiD");

const keyvDefaultOptions = {
uri: process.env.REDIS_URL || "redis://localhost:6379",
ttl: 60 * 1000
};

const sources = {
usage: new Usage(pmdDb.collection("science"), baseRedisCache),
presences: new Presences(pmdDb.collection("presences"), baseRedisCache),
versions: new Versions(pmdDb.collection("versions"), baseRedisCache),
discordUsers: new DiscordUsers(
pmdDb.collection("discordUsers"),
baseRedisCache
),
langFiles: new LangFiles(pmdDb.collection("langFiles"), baseRedisCache),
strings: new Strings(pmdDb.collection("strings"), baseRedisCache),
sponsors: new Sponsors(pmdDb.collection("sponsors"), baseRedisCache),
partners: new Partners(pmdDb.collection("partners"), baseRedisCache),
jobs: new Jobs(pmdDb.collection("jobs"), baseRedisCache),
downloads: new Downloads(pmdDb.collection("downloads"), baseRedisCache),
alphaUsers: new AlphaUsers(pmdDb.collection("alphaUsers"), baseRedisCache),
betaUsers: new BetaUsers(pmdDb.collection("betaUsers"), baseRedisCache),
credits: new Credits(pmdDb.collection("credits"), baseRedisCache),
benefits: new Benefits(pmdDb.collection("benefits"), baseRedisCache),
ffUpdates: new FFUpdates(pmdDb.collection("ffUpdates"), baseRedisCache)
usage: new Usage(pmdDb.collection("science"), {
...keyvDefaultOptions,
namespace: "science"
}),
presences: new Presences(pmdDb.collection("presences"), {
...keyvDefaultOptions,
namespace: "presences"
}),
versions: new Versions(pmdDb.collection("versions"), {
...keyvDefaultOptions,
namespace: "versions"
}),
discordUsers: new DiscordUsers(pmdDb.collection("discordUsers"), {
...keyvDefaultOptions,
namespace: "discordUsers"
}),
langFiles: new LangFiles(pmdDb.collection("langFiles"), {
...keyvDefaultOptions,
namespace: "langFiles"
}),
strings: new Strings(pmdDb.collection("strings"), {
...keyvDefaultOptions,
namespace: "strings"
}),
sponsors: new Sponsors(pmdDb.collection("sponsors"), {
...keyvDefaultOptions,
namespace: "sponsors"
}),
partners: new Partners(pmdDb.collection("partners"), {
...keyvDefaultOptions,
namespace: "partners"
}),
jobs: new Jobs(pmdDb.collection("jobs"), {
...keyvDefaultOptions,
namespace: "jobs"
}),
downloads: new Downloads(pmdDb.collection("downloads"), {
...keyvDefaultOptions,
namespace: "downloads"
}),
alphaUsers: new AlphaUsers(pmdDb.collection("alphaUsers"), {
...keyvDefaultOptions,
namespace: "alphaUsers"
}),
betaUsers: new BetaUsers(pmdDb.collection("betaUsers"), {
...keyvDefaultOptions,
namespace: "betaUsers"
}),
credits: new Credits(pmdDb.collection("credits"), {
...keyvDefaultOptions,
namespace: "credits"
}),
benefits: new Benefits(pmdDb.collection("benefits"), {
...keyvDefaultOptions,
namespace: "benefits"
}),
ffUpdates: new FFUpdates(pmdDb.collection("ffUpdates"), {
...keyvDefaultOptions,
namespace: "ffUpdates"
})
};

dSources = sources;
Expand Down
7 changes: 5 additions & 2 deletions worker/src/v3/fields/addBetaUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ export async function resolver(
}

if (
(await betaUsers.count()) > Math.floor((await discordUsers.count()) * 1)
(await betaUsers.countDocuments()) >
Math.floor((await discordUsers.countDocuments()) * 1)
) {
res.message = "error.noslots";
return res;
Expand All @@ -53,7 +54,9 @@ export async function resolver(
}

await betaUsers.add(user.id);
await betaUsers.delete("findOne", { userId: user.id });
await betaUsers.keyv.delete(
"findOne-" + betaUsers.getCacheKey({ userId: user.id }, undefined)
);

res.success = true;
res.message = "User recieved beta access.";
Expand Down
4 changes: 2 additions & 2 deletions worker/src/v3/fields/benefits.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import MongoDataSource from "apollo-mongodb-datasource";
import { gql } from "apollo-server-core";
import MongoDBCaching from "mongodb-caching";

export const schema = gql`
type Query {
Expand All @@ -13,7 +13,7 @@ export const schema = gql`
}
`;

export class Benefits extends MongoDataSource {
export class Benefits extends MongoDBCaching {
getAll() {
return this.find();
}
Expand Down
6 changes: 3 additions & 3 deletions worker/src/v3/fields/betaUsers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import MongoDataSource from "apollo-mongodb-datasource";
import { gql } from "apollo-server-core";
import MongoDBCaching from "mongodb-caching";

import { pmdDb } from "../../util/dataSources";

Expand All @@ -13,7 +13,7 @@ export const schema = gql`
}
`;

export class BetaUsers extends MongoDataSource {
export class BetaUsers extends MongoDBCaching {
async has(userId: string) {
return (await this.findOne({ userId })) !== null;
}
Expand All @@ -29,5 +29,5 @@ export function resolver(
_1: any,
{ dataSources: { betaUsers } }: { dataSources: { betaUsers: BetaUsers } }
) {
return { number: betaUsers.count() };
return { number: betaUsers.countDocuments() };
}
4 changes: 2 additions & 2 deletions worker/src/v3/fields/credits.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import MongoDataSource from "apollo-mongodb-datasource";
import { gql } from "apollo-server-core";
import { shuffle } from "lodash";
import MongoDBCaching from "mongodb-caching";

export const schema = gql`
type Query {
Expand Down Expand Up @@ -33,7 +33,7 @@ export const schema = gql`
}
`;

export class Credits extends MongoDataSource {
export class Credits extends MongoDBCaching {
async getAll() {
return (await this.find()).map(this.transformEntry);
}
Expand Down
Loading

0 comments on commit 645e5bb

Please sign in to comment.