server(theme): update theme's install count immediately on install

This commit is contained in:
Abdullah Atta
2026-01-03 14:38:00 +05:00
parent bf4d168c96
commit 603b7da9e6
2 changed files with 16 additions and 3 deletions

View File

@@ -19,7 +19,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
import { z } from "zod";
import { InstallsCounter } from "./constants";
import { findTheme, getThemes } from "./orama";
import { findTheme, getThemes, updateTotalInstalls } from "./orama";
import { syncThemes } from "./sync";
import { publicProcedure, router } from "./trpc";
import { THEME_COMPATIBILITY_VERSION } from "@notesnook/theme";
@@ -41,7 +41,12 @@ export const ThemesAPI = router({
const theme = await findTheme(id, compatibilityVersion);
if (!theme) return;
if (userId) await InstallsCounter.increment(theme.id, userId);
if (userId) {
updateTotalInstalls(
theme,
await InstallsCounter.increment(theme.id, userId)
);
}
return theme;
}),
updateTheme: publicProcedure

View File

@@ -16,7 +16,7 @@ GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { Orama, SearchParams, create, search } from "@orama/orama";
import { Orama, SearchParams, create, search, update } from "@orama/orama";
import { CompiledThemeDefinition, ThemeMetadata } from "./sync";
import { ThemeQuerySchema } from "./schemas";
@@ -54,6 +54,14 @@ export async function findTheme(
return results.hits[0].document as CompiledThemeDefinition;
}
export async function updateTotalInstalls(
theme: CompiledThemeDefinition,
totalInstalls: number
) {
if (!ThemesDatabase) await initializeDatabase();
await update(ThemesDatabase!, theme.id, { ...theme, totalInstalls });
}
export async function getThemes(query: (typeof ThemeQuerySchema)["_type"]) {
if (!ThemesDatabase) await initializeDatabase();