-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from Shunseii/feat/flashcards
Feat/flashcards
- Loading branch information
Showing
26 changed files
with
6,842 additions
and
4,806 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
name: Update Meilisearch Indexes for All Users | ||
|
||
# This workflow updates the settings for all indexes for all users. | ||
|
||
on: workflow_dispatch | ||
|
||
jobs: | ||
update-indexes: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v3 | ||
|
||
- name: Update Meilisearch Indexes | ||
run: ./scripts/update-indexes.sh | ||
env: | ||
MEILISEARCH_HOST: ${{ secrets.MEILISEARCH_HOST }} | ||
MEILISEARCH_MASTER_KEY: ${{ secrets.MEILISEARCH_MASTER_KEY }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { MeiliSearch } from "meilisearch"; | ||
|
||
export const meilisearchClient = new MeiliSearch({ | ||
host: process.env.MEILISEARCH_HOST!, | ||
apiKey: process.env.MEILISEARCH_API_KEY!, | ||
}); | ||
|
||
/** | ||
* Creates a new index for a user and configures it | ||
* with the necessary universal settings. | ||
* | ||
* @param userId The user's id. | ||
*/ | ||
export const createUserIndex = async (userId: string) => { | ||
const { taskUid: createTaskUid } = | ||
await meilisearchClient.createIndex(userId); | ||
|
||
await meilisearchClient.waitForTask(createTaskUid); | ||
|
||
const userIndex = meilisearchClient.index(userId); | ||
|
||
const { taskUid: updateTaskUid } = await userIndex.updateSettings({ | ||
filterableAttributes: ["flashcard.due_timestamp"], | ||
}); | ||
|
||
await meilisearchClient.waitForTask(updateTaskUid); | ||
}; |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import { router, protectedProcedure } from "../trpc"; | ||
import { meilisearchClient } from "../clients/meilisearch"; | ||
import { Card, createEmptyCard } from "ts-fsrs"; | ||
import { z } from "zod"; | ||
|
||
export type Flashcard = Card & { | ||
id: string; | ||
content: string; | ||
translation: string; | ||
due: string; | ||
last_review: string | null; | ||
due_timestamp: number; | ||
last_review_timestamp: number | null; | ||
}; | ||
|
||
const FlashcardSchema = z.object({ | ||
id: z.string(), | ||
content: z.string(), | ||
translation: z.string(), | ||
elapsed_days: z.number(), | ||
lapses: z.number(), | ||
reps: z.number(), | ||
scheduled_days: z.number(), | ||
stability: z.number(), | ||
state: z.number(), | ||
difficulty: z.number(), | ||
due: z.string(), | ||
due_timestamp: z.number(), | ||
last_review: z.string().nullable(), | ||
last_review_timestamp: z.number().nullable(), | ||
}); | ||
|
||
export const flashcardRouter = router({ | ||
today: protectedProcedure | ||
.output( | ||
z.object({ | ||
flashcards: z.array(FlashcardSchema), | ||
}), | ||
) | ||
.query(async ({ ctx }) => { | ||
const { user } = ctx; | ||
|
||
/** | ||
* Current timestamp in seconds | ||
*/ | ||
const now = Math.floor(new Date().getTime() / 1000); | ||
|
||
const { results } = await meilisearchClient.index(user.id).getDocuments({ | ||
filter: [ | ||
`flashcard.due_timestamp NOT EXISTS OR flashcard.due_timestamp <= ${now}`, | ||
], | ||
limit: 1000, | ||
}); | ||
|
||
const flashcards: Flashcard[] = results.map( | ||
({ id, word, translation, flashcard }) => { | ||
const card = flashcard ?? getEmptyFlashcard(id); | ||
|
||
return { | ||
id, | ||
content: word, | ||
translation, | ||
...card, | ||
}; | ||
}, | ||
); | ||
|
||
return { | ||
flashcards, | ||
}; | ||
}), | ||
|
||
update: protectedProcedure | ||
.input(FlashcardSchema) | ||
.mutation(async ({ ctx, input }) => { | ||
const { user } = ctx; | ||
|
||
const { id, ...flashcard } = input; | ||
|
||
const { taskUid } = await meilisearchClient | ||
.index(user.id) | ||
.updateDocuments([ | ||
{ | ||
id, | ||
flashcard, | ||
}, | ||
]); | ||
|
||
await meilisearchClient.index(user.id).waitForTask(taskUid); | ||
|
||
return { | ||
id, | ||
...flashcard, | ||
}; | ||
}), | ||
}); | ||
|
||
const getEmptyFlashcard = (id: string): Flashcard => { | ||
return createEmptyCard(new Date(), (c) => { | ||
const due = new Date(c.due); | ||
const last_review = c.last_review ? new Date(c.last_review) : null; | ||
|
||
const due_timestamp = Math.floor(due.getTime() / 1000); | ||
const last_review_timestamp = last_review | ||
? Math.floor(last_review.getTime() / 1000) | ||
: null; | ||
|
||
return { | ||
...c, | ||
id, | ||
due: due.toISOString(), | ||
last_review: last_review?.toISOString() ?? null, | ||
due_timestamp, | ||
last_review_timestamp, | ||
} as Flashcard; | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.