This implements a basic CardDav UI, titled "Contacts". It allows creating new contacts with a first name + last name, and editing their first and last names, main email, main phone, and notes. You can also import and export VCF (VCARD) files. It also allows editing the VCARD directly, for power users. Additionally, you can choose, create, or delete address books, and if there's no address book created yet in your CardDav server (first-time setup), it'll automatically create one, titled "Contacts". Finally, there are some dependency updates and a fix for the config not allowing disabling the `cardDav` or the `calDav` server. Related to #56
47 lines
1.2 KiB
TypeScript
47 lines
1.2 KiB
TypeScript
import { Handlers } from 'fresh/server.ts';
|
|
|
|
import { FreshContextState } from '/lib/types.ts';
|
|
import { Contact, ContactModel } from '/lib/models/contacts.ts';
|
|
|
|
interface Data {}
|
|
|
|
export interface RequestBody {
|
|
contactId: string;
|
|
addressBookId: string;
|
|
}
|
|
|
|
export interface ResponseBody {
|
|
success: boolean;
|
|
contacts: Contact[];
|
|
}
|
|
|
|
export const handler: Handlers<Data, FreshContextState> = {
|
|
async POST(request, context) {
|
|
if (!context.state.user) {
|
|
return new Response('Unauthorized', { status: 401 });
|
|
}
|
|
|
|
const requestBody = await request.clone().json() as RequestBody;
|
|
|
|
if (!requestBody.contactId || !requestBody.addressBookId) {
|
|
return new Response('Bad request', { status: 400 });
|
|
}
|
|
|
|
const userId = context.state.user.id;
|
|
|
|
const contact = await ContactModel.get(userId, requestBody.addressBookId, requestBody.contactId);
|
|
|
|
if (!contact) {
|
|
return new Response('Not found', { status: 404 });
|
|
}
|
|
|
|
await ContactModel.delete(userId, contact.url);
|
|
|
|
const contacts = await ContactModel.list(userId, requestBody.addressBookId);
|
|
|
|
const responseBody: ResponseBody = { success: true, contacts };
|
|
|
|
return new Response(JSON.stringify(responseBody));
|
|
},
|
|
};
|