2023-03-09 10:05:32 -07:00
|
|
|
export const useUser = async (force = false) => {
|
2024-07-06 20:57:32 -07:00
|
|
|
const user = useState("user", () => {});
|
2023-03-09 10:05:32 -07:00
|
|
|
|
|
|
|
|
if (!user.value || force || (user.value && Date.now() - user.value.lastUpdated > 300000)) {
|
2024-07-06 20:57:32 -07:00
|
|
|
user.value = await initUser();
|
2023-03-09 10:05:32 -07:00
|
|
|
}
|
|
|
|
|
|
2024-07-06 20:57:32 -07:00
|
|
|
return user;
|
|
|
|
|
};
|
2023-03-09 10:05:32 -07:00
|
|
|
|
|
|
|
|
export const initUser = async () => {
|
2024-07-06 20:57:32 -07:00
|
|
|
const auth = (await useAuth()).value;
|
2023-03-09 10:05:32 -07:00
|
|
|
|
|
|
|
|
const user = {
|
2024-08-15 23:21:30 -07:00
|
|
|
collections: [],
|
2023-03-09 10:05:32 -07:00
|
|
|
follows: [],
|
2024-08-15 23:21:30 -07:00
|
|
|
subscriptions: [],
|
2023-03-09 10:05:32 -07:00
|
|
|
lastUpdated: 0,
|
2024-07-06 20:57:32 -07:00
|
|
|
};
|
2023-03-09 10:05:32 -07:00
|
|
|
|
|
|
|
|
if (auth.user && auth.user.id) {
|
|
|
|
|
try {
|
2024-08-15 23:21:30 -07:00
|
|
|
const headers = {
|
|
|
|
|
Authorization: auth.token,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const [follows, collections, subscriptions] = await Promise.all([
|
|
|
|
|
useBaseFetch(`user/${auth.user.id}/follows`, { headers }, true),
|
|
|
|
|
useBaseFetch(`user/${auth.user.id}/collections`, { apiVersion: 3, headers }, true),
|
|
|
|
|
useBaseFetch(`billing/subscriptions`, { internal: true, headers }, true),
|
2024-07-06 20:57:32 -07:00
|
|
|
]);
|
2023-03-09 10:05:32 -07:00
|
|
|
|
2024-07-06 20:57:32 -07:00
|
|
|
user.collections = collections;
|
|
|
|
|
user.follows = follows;
|
2024-08-15 23:21:30 -07:00
|
|
|
user.subscriptions = subscriptions;
|
2024-07-06 20:57:32 -07:00
|
|
|
user.lastUpdated = Date.now();
|
2023-03-09 10:05:32 -07:00
|
|
|
} catch (err) {
|
2024-07-06 20:57:32 -07:00
|
|
|
console.error(err);
|
2023-03-09 10:05:32 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-06 20:57:32 -07:00
|
|
|
return user;
|
|
|
|
|
};
|
2023-03-09 10:05:32 -07:00
|
|
|
|
2023-12-27 13:27:50 -05:00
|
|
|
export const initUserCollections = async () => {
|
2024-07-06 20:57:32 -07:00
|
|
|
const auth = (await useAuth()).value;
|
|
|
|
|
const user = (await useUser()).value;
|
2023-03-09 10:05:32 -07:00
|
|
|
|
|
|
|
|
if (auth.user && auth.user.id) {
|
|
|
|
|
try {
|
2024-07-06 20:57:32 -07:00
|
|
|
user.collections = await useBaseFetch(`user/${auth.user.id}/collections`, { apiVersion: 3 });
|
2023-03-09 10:05:32 -07:00
|
|
|
} catch (err) {
|
2024-07-06 20:57:32 -07:00
|
|
|
console.error(err);
|
2023-03-09 10:05:32 -07:00
|
|
|
}
|
|
|
|
|
}
|
2024-07-06 20:57:32 -07:00
|
|
|
};
|
2023-03-09 10:05:32 -07:00
|
|
|
|
|
|
|
|
export const initUserFollows = async () => {
|
2024-07-06 20:57:32 -07:00
|
|
|
const auth = (await useAuth()).value;
|
|
|
|
|
const user = (await useUser()).value;
|
2023-03-09 10:05:32 -07:00
|
|
|
|
|
|
|
|
if (auth.user && auth.user.id) {
|
|
|
|
|
try {
|
2024-07-06 20:57:32 -07:00
|
|
|
user.follows = await useBaseFetch(`user/${auth.user.id}/follows`);
|
2023-03-09 10:05:32 -07:00
|
|
|
} catch (err) {
|
2024-07-06 20:57:32 -07:00
|
|
|
console.error(err);
|
2023-03-09 10:05:32 -07:00
|
|
|
}
|
|
|
|
|
}
|
2024-07-06 20:57:32 -07:00
|
|
|
};
|
2023-03-09 10:05:32 -07:00
|
|
|
|
|
|
|
|
export const initUserProjects = async () => {
|
2024-07-06 20:57:32 -07:00
|
|
|
const auth = (await useAuth()).value;
|
|
|
|
|
const user = (await useUser()).value;
|
2023-03-09 10:05:32 -07:00
|
|
|
|
|
|
|
|
if (auth.user && auth.user.id) {
|
|
|
|
|
try {
|
2024-07-06 20:57:32 -07:00
|
|
|
user.projects = await useBaseFetch(`user/${auth.user.id}/projects`);
|
2023-03-09 10:05:32 -07:00
|
|
|
} catch (err) {
|
2024-07-06 20:57:32 -07:00
|
|
|
console.error(err);
|
2023-03-09 10:05:32 -07:00
|
|
|
}
|
|
|
|
|
}
|
2024-07-06 20:57:32 -07:00
|
|
|
};
|
2023-03-09 10:05:32 -07:00
|
|
|
|
2023-12-27 13:27:50 -05:00
|
|
|
export const userCollectProject = async (collection, projectId) => {
|
2024-07-06 20:57:32 -07:00
|
|
|
const user = (await useUser()).value;
|
|
|
|
|
await initUserCollections();
|
2023-12-27 13:27:50 -05:00
|
|
|
|
2024-07-06 20:57:32 -07:00
|
|
|
const collectionId = collection.id;
|
2024-01-07 16:58:30 -08:00
|
|
|
|
2024-07-06 20:57:32 -07:00
|
|
|
const latestCollection = user.collections.find((x) => x.id === collectionId);
|
2024-01-07 16:58:30 -08:00
|
|
|
if (!latestCollection) {
|
2024-07-06 20:57:32 -07:00
|
|
|
throw new Error("This collection was not found. Has it been deleted?");
|
2024-01-07 16:58:30 -08:00
|
|
|
}
|
|
|
|
|
|
2024-07-06 20:57:32 -07:00
|
|
|
const add = !latestCollection.projects.includes(projectId);
|
2023-12-27 13:27:50 -05:00
|
|
|
const projects = add
|
2024-01-07 16:58:30 -08:00
|
|
|
? [...latestCollection.projects, projectId]
|
2024-07-06 20:57:32 -07:00
|
|
|
: [...latestCollection.projects].filter((x) => x !== projectId);
|
2023-12-27 13:27:50 -05:00
|
|
|
|
2024-07-06 20:57:32 -07:00
|
|
|
const idx = user.collections.findIndex((x) => x.id === latestCollection.id);
|
2023-12-27 13:27:50 -05:00
|
|
|
if (idx >= 0) {
|
2024-07-06 20:57:32 -07:00
|
|
|
user.collections[idx].projects = projects;
|
2023-12-27 13:27:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await useBaseFetch(`collection/${collection.id}`, {
|
2024-07-06 20:57:32 -07:00
|
|
|
method: "PATCH",
|
2023-12-27 13:27:50 -05:00
|
|
|
body: {
|
|
|
|
|
new_projects: projects,
|
|
|
|
|
},
|
|
|
|
|
apiVersion: 3,
|
2024-07-06 20:57:32 -07:00
|
|
|
});
|
|
|
|
|
};
|
2023-12-27 13:27:50 -05:00
|
|
|
|
2023-03-09 10:05:32 -07:00
|
|
|
export const userFollowProject = async (project) => {
|
2024-07-06 20:57:32 -07:00
|
|
|
const user = (await useUser()).value;
|
2023-03-09 10:05:32 -07:00
|
|
|
|
Project, Search, User redesign (#1281)
* New project page
* fix silly icon tailwind classes
* Start new versions page, add new ButtonStyled component
* Pagination and finish mocking up versions page functionality
* green download button
* hover animation
* New Modal, Avatar refactor, subpages in NavTabs
* lint
* Download modal
* New user page + fix lint
* fix ui lint
* Download animation fix
* Versions filter + finish project page
* Improve consistency of buttons on home page
* Fix ButtonStyled breaking
* Fix margin on version summary
* finish search, new modals, user + project page mobile
* fix gallery image pages
* New project header
* Fix gallery tab showing improperly
* Use auto direction + position for all popouts
* Preliminary user page
* test to see if this fixes login stuff
* remove extra slash
* Add version actions, move download button on versions page
* Listed -> public
* Shorten download modal selector height
* Fix user menu open direction
* Change breakpoint for header collapse
* Only underline title
* Tighten padding on stats a little
* New nav
* Make mobile breakpoint more consistent
* fix header breakpoint regression
* Add sign in button
* Fix edit icon color
* Fix margin at top of screen
* Fix user bios and ad width
* Fix user nav showing when there's only one type of project
* Fix plural projects on user page & extract i18n
* Remove ads on mobile for now
* Fix overflow menu showing hidden items
* NavTabs on mobile
* Fix navbar z index
* Search filter overhaul + negative filters
* fix no-max-height
* port version filters, fix following/collections, lint
* hide promos
* ui lint
* Disable modal background animation to reduce reported motion sickness
* Hide install with modrinth app button on mobile
---------
Signed-off-by: Geometrically <18202329+Geometrically@users.noreply.github.com>
Co-authored-by: Prospector <prospectordev@gmail.com>
2024-08-20 23:03:16 -07:00
|
|
|
if (user.follows.find((x) => x.id === project.id)) {
|
|
|
|
|
user.follows = user.follows.filter((x) => x.id !== project.id);
|
|
|
|
|
project.followers--;
|
|
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
useBaseFetch(`project/${project.id}/follow`, {
|
|
|
|
|
method: "DELETE",
|
|
|
|
|
});
|
2024-07-06 20:57:32 -07:00
|
|
|
});
|
Project, Search, User redesign (#1281)
* New project page
* fix silly icon tailwind classes
* Start new versions page, add new ButtonStyled component
* Pagination and finish mocking up versions page functionality
* green download button
* hover animation
* New Modal, Avatar refactor, subpages in NavTabs
* lint
* Download modal
* New user page + fix lint
* fix ui lint
* Download animation fix
* Versions filter + finish project page
* Improve consistency of buttons on home page
* Fix ButtonStyled breaking
* Fix margin on version summary
* finish search, new modals, user + project page mobile
* fix gallery image pages
* New project header
* Fix gallery tab showing improperly
* Use auto direction + position for all popouts
* Preliminary user page
* test to see if this fixes login stuff
* remove extra slash
* Add version actions, move download button on versions page
* Listed -> public
* Shorten download modal selector height
* Fix user menu open direction
* Change breakpoint for header collapse
* Only underline title
* Tighten padding on stats a little
* New nav
* Make mobile breakpoint more consistent
* fix header breakpoint regression
* Add sign in button
* Fix edit icon color
* Fix margin at top of screen
* Fix user bios and ad width
* Fix user nav showing when there's only one type of project
* Fix plural projects on user page & extract i18n
* Remove ads on mobile for now
* Fix overflow menu showing hidden items
* NavTabs on mobile
* Fix navbar z index
* Search filter overhaul + negative filters
* fix no-max-height
* port version filters, fix following/collections, lint
* hide promos
* ui lint
* Disable modal background animation to reduce reported motion sickness
* Hide install with modrinth app button on mobile
---------
Signed-off-by: Geometrically <18202329+Geometrically@users.noreply.github.com>
Co-authored-by: Prospector <prospectordev@gmail.com>
2024-08-20 23:03:16 -07:00
|
|
|
} else {
|
|
|
|
|
user.follows = user.follows.concat(project);
|
|
|
|
|
project.followers++;
|
|
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
useBaseFetch(`project/${project.id}/follow`, {
|
|
|
|
|
method: "POST",
|
|
|
|
|
});
|
2024-07-06 20:57:32 -07:00
|
|
|
});
|
Project, Search, User redesign (#1281)
* New project page
* fix silly icon tailwind classes
* Start new versions page, add new ButtonStyled component
* Pagination and finish mocking up versions page functionality
* green download button
* hover animation
* New Modal, Avatar refactor, subpages in NavTabs
* lint
* Download modal
* New user page + fix lint
* fix ui lint
* Download animation fix
* Versions filter + finish project page
* Improve consistency of buttons on home page
* Fix ButtonStyled breaking
* Fix margin on version summary
* finish search, new modals, user + project page mobile
* fix gallery image pages
* New project header
* Fix gallery tab showing improperly
* Use auto direction + position for all popouts
* Preliminary user page
* test to see if this fixes login stuff
* remove extra slash
* Add version actions, move download button on versions page
* Listed -> public
* Shorten download modal selector height
* Fix user menu open direction
* Change breakpoint for header collapse
* Only underline title
* Tighten padding on stats a little
* New nav
* Make mobile breakpoint more consistent
* fix header breakpoint regression
* Add sign in button
* Fix edit icon color
* Fix margin at top of screen
* Fix user bios and ad width
* Fix user nav showing when there's only one type of project
* Fix plural projects on user page & extract i18n
* Remove ads on mobile for now
* Fix overflow menu showing hidden items
* NavTabs on mobile
* Fix navbar z index
* Search filter overhaul + negative filters
* fix no-max-height
* port version filters, fix following/collections, lint
* hide promos
* ui lint
* Disable modal background animation to reduce reported motion sickness
* Hide install with modrinth app button on mobile
---------
Signed-off-by: Geometrically <18202329+Geometrically@users.noreply.github.com>
Co-authored-by: Prospector <prospectordev@gmail.com>
2024-08-20 23:03:16 -07:00
|
|
|
}
|
2024-07-06 20:57:32 -07:00
|
|
|
};
|
2023-07-20 11:19:42 -07:00
|
|
|
export const resendVerifyEmail = async () => {
|
2025-08-13 21:48:52 +01:00
|
|
|
// const { injectNotificationManager } = await import("@modrinth/ui");
|
|
|
|
|
// const { addNotification } = injectNotificationManager();
|
2023-07-20 11:19:42 -07:00
|
|
|
|
2024-07-06 20:57:32 -07:00
|
|
|
startLoading();
|
2023-07-20 11:19:42 -07:00
|
|
|
try {
|
2024-07-06 20:57:32 -07:00
|
|
|
await useBaseFetch("auth/email/resend_verify", {
|
|
|
|
|
method: "POST",
|
|
|
|
|
});
|
2023-07-20 11:19:42 -07:00
|
|
|
|
2024-07-06 20:57:32 -07:00
|
|
|
const auth = await useAuth();
|
2025-08-13 21:48:52 +01:00
|
|
|
addNotification({
|
2024-07-06 20:57:32 -07:00
|
|
|
title: "Email sent",
|
2023-07-20 11:19:42 -07:00
|
|
|
text: `An email with a link to verify your account has been sent to ${auth.value.user.email}.`,
|
2024-07-06 20:57:32 -07:00
|
|
|
type: "success",
|
|
|
|
|
});
|
2023-07-20 11:19:42 -07:00
|
|
|
} catch (err) {
|
2025-08-13 21:48:52 +01:00
|
|
|
addNotification({
|
2024-07-06 20:57:32 -07:00
|
|
|
title: "An error occurred",
|
2023-07-20 11:19:42 -07:00
|
|
|
text: err.data.description,
|
2024-07-06 20:57:32 -07:00
|
|
|
type: "error",
|
|
|
|
|
});
|
2023-07-20 11:19:42 -07:00
|
|
|
}
|
2024-07-06 20:57:32 -07:00
|
|
|
stopLoading();
|
|
|
|
|
};
|
2023-07-20 11:19:42 -07:00
|
|
|
|
|
|
|
|
export const logout = async () => {
|
2024-07-06 20:57:32 -07:00
|
|
|
startLoading();
|
|
|
|
|
const auth = await useAuth();
|
2023-07-20 11:19:42 -07:00
|
|
|
try {
|
|
|
|
|
await useBaseFetch(`session/${auth.value.token}`, {
|
2024-07-06 20:57:32 -07:00
|
|
|
method: "DELETE",
|
|
|
|
|
});
|
|
|
|
|
} catch {
|
|
|
|
|
/* empty */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await useAuth("none");
|
|
|
|
|
useCookie("auth-token").value = null;
|
|
|
|
|
stopLoading();
|
|
|
|
|
};
|