Skip to content

Commit

Permalink
refactor: use new types from Notion
Browse files Browse the repository at this point in the history
This is a work in progress.
  • Loading branch information
aalemayhu committed Dec 27, 2022
1 parent 0c4913c commit 2c6c24f
Show file tree
Hide file tree
Showing 93 changed files with 1,120 additions and 722 deletions.
2 changes: 1 addition & 1 deletion src/lib/anki/getDeckname.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import getDeckName from './getDeckname';

describe('getDeckname', () => {
it('has no parent', () => {
expect(getDeckName(undefined, 'test')).toBe('test');
expect(getDeckName('', 'test')).toBe('test');
});
it('has parent', () => {
expect(getDeckName('parent', 'test')).toBe('parent::test');
Expand Down
5 changes: 1 addition & 4 deletions src/lib/anki/getDeckname.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
* In the real world this does not really matter but adding this note in case that assumption
* changes.
*/
export default function getDeckName(
parent: string | undefined,
name: string
): string {
export default function getDeckName(parent: string, name: string): string {
if (parent && parent !== name) {
return `${parent}::${name}`;
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/anki/zip.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import JSZip from 'jszip';
import { unzipSync, strFromU8 } from 'fflate';
import { strFromU8, unzipSync } from 'fflate';

import { renderToStaticMarkup } from 'react-dom/server';

Expand Down
51 changes: 35 additions & 16 deletions src/lib/jobs/helpers/deleteOldUploads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,53 @@ import { Knex } from 'knex';

import { TIME_21_MINUTES_AS_SECONDS } from '../../constants';
import StorageHandler from '../../storage/StorageHandler';
import { Upload } from '../../storage/types';
import { S3 } from 'aws-sdk';

export const MS_21 = TIME_21_MINUTES_AS_SECONDS * 1000;

const isFileOld = (file: S3.Object) => {
const now = new Date();
if (file.LastModified) {
return now.getMilliseconds() - file.LastModified.getMilliseconds() > MS_21;
}
return false;
};

const hasOwner = async (
key: S3.ObjectKey | undefined,
db: Knex
): Promise<boolean> => {
if (!key) {
return false;
}
const upload = (await db('uploads')
.where('key', key)
.returning('owner')) as Upload;
return Boolean(upload.owner);
};

export default async function deleteOldUploads(db: Knex) {
const s = new StorageHandler();
const files = await s.getContents();
const now = new Date();

if (!files) {
return;
}

for (const file of files) {
/* @ts-ignore */
if (now - new Date(file.LastModified) > MS_21) {
const upload = await db('uploads')
.where('key', file.Key)
.returning('owner');
/* @ts-ignore */
if (upload.owner) {
console.info('file has an owner, skipping');
continue;
} else {
await s.delete(file);
console.debug(
`Delete **** which was last modified on ${file.LastModified}`
);
}
if (!file || !isFileOld(file)) {
continue;
}

if (await hasOwner(file.Key, db)) {
console.info('file has an owner, skipping');
continue;
}

await s.delete(file);
console.debug(
`Delete **** which was last modified on ${file.LastModified}`
);
}
}
9 changes: 5 additions & 4 deletions src/lib/misc/TokenHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ class TokenHandler {
return false;
}
const user = await DB('users').where({ reset_token: token }).first();
/* @ts-ignore */
return user && user.reset_token;
}

Expand Down Expand Up @@ -135,13 +134,15 @@ class TokenHandler {
jwt.sign(
{ userId },
process.env.SECRET!,
// TODO: let user decide expiry
// { expiresIn: "1d" },
/* @ts-ignore */
(error, token) => {
(error: Error | null, token: string | undefined) => {
if (error) {
reject(error);
} else {
} else if (token) {
resolve(token);
} else {
reject(new Error('Token is undefined'));
}
}
);
Expand Down
4 changes: 2 additions & 2 deletions src/lib/notion/BlockHandler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import Workspace from '../parser/WorkSpace';
import BlockHandler from './BlockHandler';
import { pageId as examplId } from '../../test/test-utils';
import MockNotionAPI from './_mock/MockNotionAPI';
import { getToggleBlocks } from './helpers/getToggleBlocks';

dotenv.config({ path: 'test/.env' });
const api = new MockNotionAPI(process.env.NOTION_KEY!);
Expand Down Expand Up @@ -67,8 +68,7 @@ describe('BlockHandler', () => {
'07a7b319183642b9afecdcc4c456f73d',
true
);
/* @ts-ignore */
const topLevelToggles = blocks.results.filter((t) => t.type === 'toggle');
const topLevelToggles = getToggleBlocks(blocks.results);
expect(topLevelToggles.length).toEqual(14);
});

Expand Down
30 changes: 19 additions & 11 deletions src/lib/notion/BlockHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import fs from 'fs';
import {
AudioBlockObjectResponse,
BlockObjectResponse,
ColumnBlockObjectResponse,
FileBlockObjectResponse,
GetBlockResponse,
ImageBlockObjectResponse,
Expand All @@ -26,7 +27,6 @@ import getInputCard from './helpers/getInputCard';
import getColumn from './helpers/getColumn';
import isColumnList from './helpers/isColumnList';
import isTesting from './helpers/isTesting';
import renderFront from './helpers/renderFront';
import perserveNewlinesIfApplicable from './helpers/preserveNewlinesIfApplicable';
import getDeckName from '../anki/getDeckname';
import getUniqueFileName from '../misc/getUniqueFileName';
Expand All @@ -37,6 +37,7 @@ import { getImageUrl } from './helpers/getImageUrl';
import { getAudioUrl } from './helpers/getAudioUrl';
import { getFileUrl } from './helpers/getFileUrl';
import { isFullBlock, isFullPage } from '@notionhq/client';
import { blockToStaticMarkup } from './helpers/blockToStaticMarkup';

interface Finder {
parentType: string;
Expand Down Expand Up @@ -121,12 +122,11 @@ class BlockHandler {
block: GetBlockResponse,
handleChildren?: boolean
): Promise<string | null> {
let response: ListBlockChildrenResponse | null;

let response2: ListBlockChildrenResponse | null;
try {
response = await this.api.getBlocks(block.id, this.useAll);
const requestChildren = response.results;
return await renderBack(this, requestChildren, response, handleChildren);
response2 = await this.api.getBlocks(block.id, this.useAll);
const requestChildren = response2.results;
return await renderBack(this, requestChildren, response2, handleChildren);
} catch (e: unknown) {
captureException(e);
return null;
Expand All @@ -153,16 +153,26 @@ class BlockHandler {

for (const block of flashcardBlocks) {
// Assume it's a basic card then check for children
const name = await renderFront(block, this);
const name = await blockToStaticMarkup(
this,
block as BlockObjectResponse
);
let back: null | string = '';
if (isColumnList(block) && rules.useColums()) {
const secondColumn = await getColumn(block.id, this, 1);
if (secondColumn) {
back = await BlockColumn(secondColumn, this);
back = await BlockColumn(
secondColumn as ColumnBlockObjectResponse,
this
);
}
} else {
back = await this.getBackSide(block);
}
if (!name) {
console.debug('name is not valid for front, skipping', name, back);
continue;
}
const ankiNote = new Note(name, back || '');
ankiNote.media = this.exporter.media;
let isBasicType = true;
Expand Down Expand Up @@ -344,9 +354,7 @@ class BlockHandler {
path.join(__dirname, '../../templates/notion.css'),
'utf8'
);
// @ts-ignore
const block = sd[sd.type];
let subDeckName = getSubDeckName(block);
let subDeckName = getSubDeckName(sd);

decks.push(
new Deck(
Expand Down
Loading

0 comments on commit 2c6c24f

Please sign in to comment.