Skip to content

Commit

Permalink
feat: allow setting weird handle as atproto handle.
Browse files Browse the repository at this point in the history
  • Loading branch information
zicklag committed Jan 21, 2025
1 parent 31dcef2 commit aab9cfb
Show file tree
Hide file tree
Showing 9 changed files with 181 additions and 69 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
},
"devDependencies": {
"@atproto/api": "^0.13.18",
"@atproto/did": "^0.1.3",
"@codemirror/lang-html": "^6.4.9",
"@codemirror/lang-markdown": "^6.3.1",
"@codemirror/language": "^6.10.4",
Expand Down
10 changes: 10 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 41 additions & 20 deletions src/lib/dns/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import { RCode } from 'dinodns/common/core/utils';
import { redis } from '$lib/redis';
import { serverGlobals } from '$lib/server-globals';
import { usernames } from '$lib/usernames';
import { getAtProtoDid, profileLinkByUsername } from '$lib/leaf/profile';
import { isDid } from '@atproto/did';

const REDIS_USER_PREFIX = 'weird:users:names:';
const REDIS_DNS_RECORD_PREFIX = 'weird:dns:records:';
Expand Down Expand Up @@ -311,27 +313,46 @@ export async function startDnsServer() {
const suffix = usernames.publicSuffix(name);
switch (type) {
case 'TXT':
if (!name.startsWith('_weird.')) return returnAnswers(null);
if (!suffix) return returnAnswers(null);
const txtUsername = name.split('_weird.')[1];
if (!txtUsername) return returnAnswers(null);
const pubkey = await redis.hGet(REDIS_USER_PREFIX + txtUsername, 'subspace');
if (!pubkey) return returnAnswers(null);
returnAnswers([
// TODO: Resolve Iroh ticket / NodeID along with subspace
// {
// name,
// type,
// data: `server=${await leafClient.node_id()}`,
// ttl: DNS_TTL
// },
{
name,
type,
data: `subspace=${pubkey}`,
ttl: DNS_TTL
}
]);
if (name.startsWith('_weird')) {
const txtUsername = name.split('_weird.')[1];
if (!txtUsername) return returnAnswers(null);
const pubkey = await redis.hGet(REDIS_USER_PREFIX + txtUsername, 'subspace');
if (!pubkey) return returnAnswers(null);
returnAnswers([
// TODO: Resolve Iroh ticket / NodeID along with subspace
// {
// name,
// type,
// data: `server=${await leafClient.node_id()}`,
// ttl: DNS_TTL
// },
{
name,
type,
data: `subspace=${pubkey}`,
ttl: DNS_TTL
}
]);
} else if (name.startsWith('_atproto')) {
const txtUsername = name.split('atproto.')[1];
if (!txtUsername) return returnAnswers(null);
const profileLink = await profileLinkByUsername(txtUsername);
if (!profileLink) return returnAnswers(null);
const atprotoDid = await getAtProtoDid(profileLink);
if (!atprotoDid) return returnAnswers(null);
if (!isDid(atprotoDid)) return returnAnswers(null);
returnAnswers([
{
name,
type,
data: `did=${atprotoDid}`,
ttl: DNS_TTL
}
]);
} else {
return returnAnswers(null);
}
break;
case 'A':
if (!suffix) return returnAnswers(null);
Expand Down
25 changes: 14 additions & 11 deletions src/lib/leaf/profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ export interface Profile {
username: string;
server: string;
};
pubpage_theme?: string;
}

export class Tags extends Component {
Expand Down Expand Up @@ -191,22 +190,20 @@ export class MastodonProfile extends Component {
}
}

export class WeirdPubpageTheme extends Component {
export class AtprotoDid extends Component {
value: string = '';
constructor(s: string) {
super();
this.value = s;
}
static componentName(): string {
return 'WeirdPubpageTheme';
return 'AtProtoDid';
}
static borshSchema(): BorshSchema {
return BorshSchema.String;
}
static specification(): Component[] {
return [
new CommonMark(`The name of the theme selected by a user for their main Weird pubpage.`)
];
return [new CommonMark(`The DID associated to the user's AtProto / Bluesky account.`)];
}
}

Expand Down Expand Up @@ -261,9 +258,9 @@ export async function getProfile(link: ExactLink): Promise<Profile | undefined>
Tags,
WeirdCustomDomain,
MastodonProfile,
WeirdPubpageTheme,
WebLinks,
SocialLinks
SocialLinks,
AtprotoDid
);
return (
(ent && {
Expand All @@ -273,8 +270,7 @@ export async function getProfile(link: ExactLink): Promise<Profile | undefined>
// custom_domain: ent.get(WeirdCustomDomain)?.value,
social_links: ent.get(SocialLinks)?.value || [],
links: ent.get(WebLinks)?.value || [],
mastodon_profile: ent.get(MastodonProfile)?.value,
pubpage_theme: ent.get(WeirdPubpageTheme)?.value
mastodon_profile: ent.get(MastodonProfile)?.value
}) ||
undefined
);
Expand All @@ -288,13 +284,20 @@ export async function setRawProfile(link: ExactLink, profile: Profile) {
profile.display_name ? new Name(profile.display_name) : Name,
profile.bio ? new Description(profile.bio) : Description,
profile.mastodon_profile ? new MastodonProfile(profile.mastodon_profile) : MastodonProfile,
profile.pubpage_theme ? new WeirdPubpageTheme(profile.pubpage_theme) : WeirdPubpageTheme,
profile.links ? new WebLinks(profile.links) : WebLinks,
profile.social_links ? new SocialLinks(profile.social_links) : SocialLinks,
profile.tags ? new Tags(profile.tags) : Tags
]);
}

export async function setAtprotoDid(link: ExactLink, did?: string): Promise<void> {
await leafClient.update_components(link, [did ? new AtprotoDid(did) : AtprotoDid]);
}

export async function getAtProtoDid(link: ExactLink): Promise<string | undefined> {
return (await leafClient.get_components(link, AtprotoDid))?.get(AtprotoDid)?.value;
}

export async function setCustomDomain(userId: string, domain?: string): Promise<void> {
const link = await profileLinkById(userId);

Expand Down
8 changes: 6 additions & 2 deletions src/routes/(app)/[username]/+layout.server.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getProfile, listChildren } from '$lib/leaf/profile';
import { getAtProtoDid, getProfile, listChildren } from '$lib/leaf/profile';
import { error, redirect } from '@sveltejs/kit';
import type { LayoutServerLoad } from '../$types';
import { getSession, getUserInfoFromSession } from '$lib/rauthy/server';
Expand All @@ -23,7 +23,8 @@ export const load: LayoutServerLoad = async ({ fetch, params, request, url }) =>
const subspace = await usernames.getSubspace(fullUsername);
if (!subspace) return error(404, `User not found: ${fullUsername}`);

const profile = (await getProfile(subspace_link(subspace, null))) || {
const profileLink = subspace_link(subspace, null);
const profile = (await getProfile(profileLink)) || {
tags: [],
links: [],
social_links: []
Expand Down Expand Up @@ -64,8 +65,11 @@ export const load: LayoutServerLoad = async ({ fetch, params, request, url }) =>
? await getUserInfoFromSession(fetch, request, sessionInfo)
: undefined;

const atprotoDid = await getAtProtoDid(profileLink);

return {
userInfo,
atprotoDid,
providers,
profile,
verifiedLinks: await verifiedLinks.get(fullUsername),
Expand Down
1 change: 1 addition & 0 deletions src/routes/(app)/[username]/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
component: { ref: ManageAccountModal },
userInfo: data.userInfo,
providers: data.providers,
atprotoDid: data.atprotoDid,
async response(r) {
if ('error' in r) {
error = r.error;
Expand Down
7 changes: 1 addition & 6 deletions src/routes/(app)/[username]/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export const actions = {
link.url = 'https://' + link.url;
}
}

let bio = data.get('bio')?.toString() || undefined;
if (bio === '') {
bio = undefined;
Expand All @@ -68,19 +69,13 @@ export const actions = {
}) ||
undefined;

let pubpage_theme = data.get('subsite_theme')?.toString() || undefined;
if (pubpage_theme === '') {
pubpage_theme = undefined;
}

const profile: Profile = {
display_name,
tags,
links,
social_links,
bio,
mastodon_profile,
pubpage_theme
};

try {
Expand Down
Loading

0 comments on commit aab9cfb

Please sign in to comment.