2025-09-27 18:39:09 +00:00
|
|
|
import { createDAVClient } from '/lib/models/dav.js';
|
Basic CardDav UI (Contacts)
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
2025-08-10 06:48:16 +00:00
|
|
|
import { AppConfig } from '/lib/config.ts';
|
Remove fresh
This implements a huge change, where Fresh is removed as a framework and serving files, allowing more control over importing, bundling, and serving files and components.
The biggest challenge was to continue making sure that there weren't too many places to look into for import versions, and `PasswordlessPasskeyLogin.tsx` became a prototype in migrating a component to fully SSR, no need for frontend parsing (via Babel) or bundling (via a custom-script, downloading frontend dependencies from esm.sh). Still, there are too many components to migrate like that, and it's all working, so I likely won't even attempt it unless there's some bug, new feature, or security vulnerability to address that warrants a rewrite of those.
This also updates all dependencies (except `@libs/xml` because that still causes some breaking in DAV endpoints), including Deno!
All other advantages can be seen in the related issues, and the breaking change this (v4.0.0) introduces is related simply to `config.email.tlsMode` (which had a deprecation warning throughout v3), and because, while I tested many things exhaustively, it's not impossible something broke that I didn't see.
Closes #141
Closes #132
2026-02-20 10:54:31 +00:00
|
|
|
import { parseVCard } from '/public/ts/utils/contacts.ts';
|
Basic CardDav UI (Contacts)
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
2025-08-10 06:48:16 +00:00
|
|
|
|
|
|
|
|
interface DAVObject extends Record<string, any> {
|
|
|
|
|
data?: string;
|
|
|
|
|
displayName?: string;
|
|
|
|
|
ctag?: string;
|
|
|
|
|
url: string;
|
|
|
|
|
uid?: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface Contact extends DAVObject {
|
|
|
|
|
firstName?: string;
|
|
|
|
|
lastName?: string;
|
|
|
|
|
middleNames?: string[];
|
|
|
|
|
title?: string;
|
|
|
|
|
email?: string;
|
|
|
|
|
phone?: string;
|
|
|
|
|
notes?: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface AddressBook extends DAVObject {}
|
|
|
|
|
|
|
|
|
|
const contactsConfig = await AppConfig.getContactsConfig();
|
|
|
|
|
|
|
|
|
|
async function getClient(userId: string) {
|
|
|
|
|
const client = await createDAVClient({
|
|
|
|
|
serverUrl: contactsConfig.cardDavUrl,
|
|
|
|
|
credentials: {},
|
|
|
|
|
authMethod: 'Custom',
|
|
|
|
|
// deno-lint-ignore require-await
|
|
|
|
|
authFunction: async () => {
|
|
|
|
|
return {
|
|
|
|
|
'X-Remote-User': userId,
|
|
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
fetchOptions: {
|
|
|
|
|
timeout: 15_000,
|
|
|
|
|
},
|
|
|
|
|
defaultAccountType: 'carddav',
|
|
|
|
|
rootUrl: `${contactsConfig.cardDavUrl}/`,
|
|
|
|
|
principalUrl: `${contactsConfig.cardDavUrl}/${userId}/`,
|
|
|
|
|
homeUrl: `${contactsConfig.cardDavUrl}/${userId}/`,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return client;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export class ContactModel {
|
|
|
|
|
static async list(
|
|
|
|
|
userId: string,
|
|
|
|
|
addressBookId: string,
|
|
|
|
|
): Promise<Contact[]> {
|
|
|
|
|
const client = await getClient(userId);
|
|
|
|
|
|
|
|
|
|
const addressBookUrl = `${contactsConfig.cardDavUrl}/${userId}/${addressBookId}/`;
|
|
|
|
|
|
|
|
|
|
const davContacts: DAVObject[] = await client.fetchVCards({
|
|
|
|
|
addressBook: {
|
|
|
|
|
url: addressBookUrl,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const contacts: Contact[] = davContacts.map((davContact) => {
|
|
|
|
|
return {
|
|
|
|
|
...davContact,
|
|
|
|
|
...parseVCard(davContact.data || '')[0],
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return contacts;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static async get(
|
|
|
|
|
userId: string,
|
|
|
|
|
addressBookId: string,
|
|
|
|
|
contactId: string,
|
|
|
|
|
): Promise<Contact | undefined> {
|
|
|
|
|
const contacts = await this.list(userId, addressBookId);
|
|
|
|
|
|
|
|
|
|
return contacts.find((contact) => contact.uid === contactId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static async create(
|
|
|
|
|
userId: string,
|
|
|
|
|
addressBookId: string,
|
|
|
|
|
contactId: string,
|
|
|
|
|
vCard: string,
|
|
|
|
|
): Promise<void> {
|
|
|
|
|
const client = await getClient(userId);
|
|
|
|
|
|
|
|
|
|
const addressBookUrl = `${contactsConfig.cardDavUrl}/${userId}/${addressBookId}/`;
|
|
|
|
|
|
|
|
|
|
await client.createVCard({
|
|
|
|
|
addressBook: {
|
|
|
|
|
url: addressBookUrl,
|
|
|
|
|
},
|
|
|
|
|
vCardString: vCard,
|
|
|
|
|
filename: `${contactId}.vcf`,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static async update(
|
|
|
|
|
userId: string,
|
|
|
|
|
contactUrl: string,
|
|
|
|
|
vCard: string,
|
|
|
|
|
): Promise<void> {
|
|
|
|
|
const client = await getClient(userId);
|
|
|
|
|
|
|
|
|
|
await client.updateVCard({
|
|
|
|
|
vCard: {
|
|
|
|
|
url: contactUrl,
|
|
|
|
|
data: vCard,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static async delete(
|
|
|
|
|
userId: string,
|
|
|
|
|
contactUrl: string,
|
|
|
|
|
): Promise<void> {
|
|
|
|
|
const client = await getClient(userId);
|
|
|
|
|
|
|
|
|
|
await client.deleteVCard({
|
|
|
|
|
vCard: {
|
|
|
|
|
url: contactUrl,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static async listAddressBooks(
|
|
|
|
|
userId: string,
|
|
|
|
|
): Promise<AddressBook[]> {
|
|
|
|
|
const client = await getClient(userId);
|
|
|
|
|
|
2025-08-27 15:44:01 +00:00
|
|
|
const davAddressBooks: DAVObject[] = await client.fetchAddressBooks();
|
Basic CardDav UI (Contacts)
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
2025-08-10 06:48:16 +00:00
|
|
|
|
|
|
|
|
const addressBooks: AddressBook[] = davAddressBooks.map((davAddressBook) => {
|
|
|
|
|
const uid = davAddressBook.url.split('/').filter(Boolean).pop()!;
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
...davAddressBook,
|
|
|
|
|
uid,
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return addressBooks;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static async createAddressBook(
|
|
|
|
|
userId: string,
|
|
|
|
|
name: string,
|
|
|
|
|
): Promise<void> {
|
|
|
|
|
const addressBookId = crypto.randomUUID();
|
|
|
|
|
const addressBookUrl = `${contactsConfig.cardDavUrl}/${userId}/${addressBookId}/`;
|
|
|
|
|
|
|
|
|
|
// For some reason this sends invalid XML
|
|
|
|
|
// await client.makeCollection({
|
|
|
|
|
// url: addressBookUrl,
|
|
|
|
|
// props: {
|
|
|
|
|
// displayName: name,
|
|
|
|
|
// },
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// Make "manual" request (https://www.rfc-editor.org/rfc/rfc6352.html#page-14)
|
|
|
|
|
const xmlBody = `<?xml version="1.0" encoding="utf-8"?>
|
|
|
|
|
<d:mkcol xmlns:d="DAV:" xmlns:card="urn:ietf:params:xml:ns:carddav">
|
|
|
|
|
<d:set>
|
|
|
|
|
<d:prop>
|
|
|
|
|
<d:displayname>${encodeURIComponent(name)}</d:displayname>
|
|
|
|
|
<d:resourcetype>
|
|
|
|
|
<d:collection/>
|
|
|
|
|
<card:addressbook/>
|
|
|
|
|
</d:resourcetype>
|
|
|
|
|
</d:prop>
|
|
|
|
|
</d:set>
|
|
|
|
|
</d:mkcol>`;
|
|
|
|
|
|
|
|
|
|
await fetch(addressBookUrl, {
|
|
|
|
|
method: 'MKCOL',
|
|
|
|
|
headers: {
|
|
|
|
|
'Content-Type': 'application/xml; charset=utf-8',
|
|
|
|
|
'X-Remote-User': userId,
|
|
|
|
|
},
|
|
|
|
|
body: xmlBody,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static async deleteAddressBook(
|
|
|
|
|
userId: string,
|
|
|
|
|
addressBookId: string,
|
|
|
|
|
): Promise<void> {
|
|
|
|
|
const client = await getClient(userId);
|
|
|
|
|
|
|
|
|
|
const addressBookUrl = `${contactsConfig.cardDavUrl}/${userId}/${addressBookId}/`;
|
|
|
|
|
|
|
|
|
|
await client.deleteObject({
|
|
|
|
|
url: addressBookUrl,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|