2023-10-02 15:55:38 -04:00
|
|
|
// import chardet from "chardet";
|
|
|
|
// import iconv from "iconv-lite";
|
|
|
|
import { GridFSBucketReadStream } from "mongodb";
|
|
|
|
import { stripHtml } from "string-strip-html";
|
2023-12-20 17:23:31 -05:00
|
|
|
import { IStory } from "@models/stories";
|
2023-12-29 19:06:55 -05:00
|
|
|
import { ficsHidden } from "@server/ficmas";
|
2023-12-06 21:30:22 -05:00
|
|
|
import { PreMiddlewareFunction, Query } from "mongoose";
|
2023-12-29 19:06:55 -05:00
|
|
|
import { IFicmas } from "@models/challenges/ficmas";
|
2023-10-02 15:55:38 -04:00
|
|
|
|
|
|
|
// const { encode, decode } = iconv;
|
|
|
|
|
|
|
|
export function countWords(string: string) {
|
|
|
|
return stripHtml(string).result.split(/W+/).length;
|
|
|
|
}
|
|
|
|
|
2023-12-29 20:11:07 -05:00
|
|
|
export function populate<T>(field: string, model: string): PreMiddlewareFunction<Query<T, T>> {
|
2023-10-02 15:55:38 -04:00
|
|
|
return function (next: () => any) {
|
2023-12-06 21:30:22 -05:00
|
|
|
this.populate(field, undefined, model);
|
2023-10-02 15:55:38 -04:00
|
|
|
next();
|
|
|
|
};
|
|
|
|
}
|
2023-12-29 20:11:07 -05:00
|
|
|
export function populateSelected<T>(field: string, model: string, selection: string): PreMiddlewareFunction<Query<T, T>> {
|
2023-10-02 15:55:38 -04:00
|
|
|
return function (next: () => any) {
|
2023-12-06 21:30:22 -05:00
|
|
|
this.populate(field, selection, model);
|
2023-10-02 15:55:38 -04:00
|
|
|
next();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function isFicmasHidden(story: IStory): boolean {
|
|
|
|
return (
|
2023-12-29 19:06:55 -05:00
|
|
|
((story.ficmas as IFicmas)?.year == new Date().getFullYear() &&
|
|
|
|
(story.ficmas as IFicmas)?.anniversary &&
|
2023-10-02 15:55:38 -04:00
|
|
|
new Date() < new Date(Date.parse("Aug 1 " + new Date().getFullYear()))) ||
|
2023-12-29 20:11:07 -05:00
|
|
|
((story.ficmas as IFicmas)?.year == new Date().getFullYear() && !(story.ficmas as IFicmas)?.anniversary && ficsHidden(Date.now()))
|
2023-10-02 15:55:38 -04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-12-30 17:01:43 -05:00
|
|
|
export function bufferizeStream(stream: GridFSBucketReadStream): Promise<Buffer> {
|
2023-10-02 15:55:38 -04:00
|
|
|
let chunks: Buffer[] = [];
|
2023-12-30 17:01:43 -05:00
|
|
|
|
2023-10-02 15:55:38 -04:00
|
|
|
return new Promise((res, rej) => {
|
|
|
|
stream.on("data", (c) => chunks.push(Buffer.from(c)));
|
|
|
|
stream.on("error", (err) => {
|
|
|
|
rej(err);
|
|
|
|
});
|
2023-12-30 17:01:43 -05:00
|
|
|
stream.on("end", () => res(Buffer.concat(chunks)));
|
2023-10-02 15:55:38 -04:00
|
|
|
});
|
|
|
|
}
|
2023-12-11 21:13:22 -05:00
|
|
|
|
2023-12-30 17:01:43 -05:00
|
|
|
export async function stringifyStream(stream: GridFSBucketReadStream): Promise<string> {
|
|
|
|
let str = await bufferizeStream(stream);
|
|
|
|
return str.toString("utf-8");
|
|
|
|
}
|
|
|
|
|
2023-12-11 21:13:22 -05:00
|
|
|
export function norm(text: string): string {
|
|
|
|
return text
|
|
|
|
.replace(/\n/g, "<br>")
|
|
|
|
.replace(/<br><p>/gm, "<p>")
|
|
|
|
.replace(/<p> <\/p>/gm, "")
|
|
|
|
.replace(/<p><br\s\/><b\s\/><\/p>/gm, "");
|
|
|
|
}
|