bewcloud/routes/contacts.tsx
Bruno Bernardino d86e65475c
Improve App Selection
This allows not enabling Dashboard and Files. It also sorts the apps in the menu according to the order in the `config.core.enabledApps` array.

Since this will require a major version upgrade (`v3.0.0`), I also took the opportunity to upgrade PostgreSQL. You can [follow this guide on how to upgrade PostgreSQL on Docker containers](https://news.onbrn.com/step-by-step-guide-upgrading-postgresql-docker-containers/).

Finally, this has some minor security improvements (confirming API endpoints won't work if their app is disabled in the config).

Closes #114
Closes #108
2025-12-01 12:25:21 +00:00

99 lines
2.8 KiB
TypeScript

import { Handlers, PageProps } from 'fresh/server.ts';
import { FreshContextState } from '/lib/types.ts';
import { AddressBook, Contact, ContactModel } from '/lib/models/contacts.ts';
import Contacts from '/islands/contacts/Contacts.tsx';
import { AppConfig } from '/lib/config.ts';
interface Data {
addressBookId: string;
userContacts: Contact[];
userAddressBooks: AddressBook[];
page: number;
contactsCount: number;
baseUrl: string;
search?: string;
}
export const handler: Handlers<Data, FreshContextState> = {
async GET(request, context) {
if (!context.state.user) {
return new Response('Redirect', { status: 303, headers: { 'Location': `/login` } });
}
const baseUrl = (await AppConfig.getConfig()).auth.baseUrl;
const contactsConfig = await AppConfig.getContactsConfig();
if (!contactsConfig.enableCardDavServer) {
throw new Error('CardDAV server is not enabled');
}
if (!(await AppConfig.isAppEnabled('contacts'))) {
throw new Error('Contacts app is not enabled');
}
const userId = context.state.user.id;
const searchParams = new URL(request.url).searchParams;
const page = parseInt(searchParams.get('page') || '1', 10);
const search = searchParams.get('search') || undefined;
let addressBookId = searchParams.get('addressBookId') || undefined;
let userAddressBooks = await ContactModel.listAddressBooks(userId);
// Create default address book if none exists
if (userAddressBooks.length === 0) {
await ContactModel.createAddressBook(userId, 'Contacts');
userAddressBooks = await ContactModel.listAddressBooks(userId);
}
if (!addressBookId) {
addressBookId = userAddressBooks[0].uid!;
}
if (!addressBookId) {
throw new Error('Invalid address book ID');
}
const userContacts = await ContactModel.list(userId, addressBookId);
const lowerCaseSearch = search?.toLowerCase();
const filteredContacts = lowerCaseSearch
? userContacts.filter((contact) =>
contact.firstName!.toLowerCase().includes(lowerCaseSearch) ||
contact.lastName?.toLowerCase().includes(lowerCaseSearch)
)
: userContacts;
const contactsCount = filteredContacts.length;
return await context.render({
addressBookId,
userContacts: filteredContacts,
userAddressBooks,
page,
contactsCount,
baseUrl,
search,
});
},
};
export default function ContactsPage({ data }: PageProps<Data, FreshContextState>) {
return (
<main>
<Contacts
initialAddressBookId={data?.addressBookId || ''}
initialContacts={data?.userContacts || []}
initialAddressBooks={data?.userAddressBooks || []}
baseUrl={data.baseUrl}
page={data?.page || 1}
contactsCount={data?.contactsCount || 0}
search={data?.search || ''}
/>
</main>
);
}