`;
}
-export function articleListHtml(articleEvents: object[]) {
- if (articleEvents.length === 0) return "";
+export function articleListHtml(articles: Article[]) {
+ if (articles.length === 0) return "";
let html = "";
- for (const articleEvent of articleEvents) {
- html += articleListItemHtml(articleEvent);
+ for (const article of articles) {
+ html += articleListItemHtml(article);
}
return `
@@ -198,10 +184,8 @@ export function articleListHtml(articleEvents: object[]) {
`;
}
-export function profilePageHtml(profileEvent: object, articleEvents: object[]) {
- const profile = JSON.parse(profileEvent.content);
- const name = profile.name || "Anonymous";
- const title = `${name} on Nostr`;
+export function profilePageHtml(profile: Profile, articles: Article[]) {
+ const title = `${profile.name} on Nostr`;
const body = `
@@ -214,9 +198,9 @@ export function profilePageHtml(profileEvent: object, articleEvents: object[]) {
- ${articleListHtml(articleEvents)}
+ ${articleListHtml(articles)}
`;
- return htmlLayout(title, body);
+ return htmlLayout(title, body, profile);
}
diff --git a/ldap.ts b/ldap.ts
index a9db92e..8f59bd9 100644
--- a/ldap.ts
+++ b/ldap.ts
@@ -1,26 +1,17 @@
-import { load } from "@std/dotenv";
import { Client } from "ldapts";
import { log } from "./log.ts";
+import config from "./config.ts";
-const dirname = new URL(".", import.meta.url).pathname;
-await load({ envPath: `${dirname}/.env`, export: true });
-
-const config = {
- url: Deno.env.get("LDAP_URL"),
- bindDN: Deno.env.get("LDAP_BIND_DN"),
- password: Deno.env.get("LDAP_PASSWORD"),
- searchDN: Deno.env.get("LDAP_SEARCH_DN"),
-};
-
-const client = new Client({ url: config.url });
+const { ldap } = config;
+const client = new Client({ url: ldap.url });
export async function lookupPubkeyByUsername(username: string) {
let pubkey;
try {
- await client.bind(config.bindDN, config.password);
+ await client.bind(ldap.bindDN, ldap.password);
- const { searchEntries } = await client.search(config.searchDN, {
+ const { searchEntries } = await client.search(ldap.searchDN, {
filter: `(cn=${username})`,
attributes: ["nostrKey"],
});
@@ -39,9 +30,9 @@ export async function lookupUsernameByPubkey(pubkey: string) {
let username;
try {
- await client.bind(config.bindDN, config.password);
+ await client.bind(ldap.bindDN, ldap.password);
- const { searchEntries } = await client.search(config.searchDN, {
+ const { searchEntries } = await client.search(ldap.searchDN, {
filter: `(nostrKey=${pubkey})`,
attributes: ["cn"],
});
diff --git a/main.ts b/main.ts
index 140f12d..c069280 100644
--- a/main.ts
+++ b/main.ts
@@ -3,8 +3,9 @@ import { log } from "./log.ts";
import naddrHandler from "./handlers/naddr.ts";
import nprofileHandler from "./handlers/nprofile.ts";
import npubHandler from "./handlers/npub.ts";
-import usernameHandler from "./handlers/username.ts";
+import userProfileHandler from "./handlers/user-profile.ts";
import userEventHandler from "./handlers/user-event.ts";
+import userAtomFeedHandler from "./handlers/user-atom-feed.ts";
const router = new Router();
@@ -18,7 +19,23 @@ router.get("/:path", async (ctx: ctx) => {
} else if (path.startsWith("npub")) {
await npubHandler(ctx);
} else if (path.startsWith("@") || path.startsWith("~")) {
- await usernameHandler(ctx);
+ await userProfileHandler(ctx);
+ } else {
+ ctx.response.status = 404;
+ ctx.response.body = "Not Found";
+ }
+
+ log(
+ `${ctx.request.method} ${ctx.request.url} - ${ctx.response.status}`,
+ "gray",
+ );
+});
+
+router.get("/:user/:kind.atom", async (ctx: ctx) => {
+ const { user } = ctx.params;
+
+ if (user.startsWith("@") || user.startsWith("~") || kind === "articles") {
+ await userAtomFeedHandler(ctx);
} else {
ctx.response.status = 404;
ctx.response.body = "Not Found";
diff --git a/models/article.ts b/models/article.ts
index 03e1b70..d24b539 100644
--- a/models/article.ts
+++ b/models/article.ts
@@ -1,19 +1,47 @@
+import { nip19 } from "@nostr/tools";
import { NEvent } from "../nostr.ts";
import { render as renderMarkdown } from "@deno/gfm";
export default class Article {
- private event: NEvent;
+ event: NEvent;
constructor(event: NEvent) {
this.event = event;
}
- get identifier(): string | null {
+ get identifier(): string {
const tag = this.event.tags.find((t) => t[0] === "d");
- return tag ? tag[1] : null;
+ return tag ? tag[1] : "";
+ }
+
+ get title(): string {
+ const tag = this.event.tags.find((t) => t[0] === "title");
+ return tag ? tag[1] : "Untitled";
+ }
+
+ get summary(): string {
+ const tag = this.event.tags.find((t) => t[0] === "summary");
+ return tag ? tag[1] : "";
+ }
+
+ get publishedAt(): number {
+ const tag = this.event.tags.find((t) => t[0] === "published_at");
+ return tag ? parseInt(tag[1]) : this.event.created_at;
+ }
+
+ get updatedAt(): number {
+ return this.event.created_at;
}
get html(): string {
return renderMarkdown(this.event.content);
}
+
+ get naddr(): string {
+ return nip19.naddrEncode({
+ identifier: this.identifier,
+ pubkey: this.event.pubkey,
+ kind: this.event.kind,
+ });
+ }
}
diff --git a/models/profile.ts b/models/profile.ts
new file mode 100644
index 0000000..3345b37
--- /dev/null
+++ b/models/profile.ts
@@ -0,0 +1,54 @@
+import { nip19 } from "@nostr/tools";
+import { NEvent } from "../nostr.ts";
+
+export interface ProfileData {
+ name: string;
+ about?: string;
+ picture?: string;
+ nip05?: string;
+ lud16?: string;
+}
+
+export default class Profile {
+ event: NEvent;
+ private data: ProfileData;
+ username?: string;
+
+ constructor(event: NEvent, username?: string) {
+ this.event = event;
+ this.data = JSON.parse(event.content);
+ this.username = username;
+ }
+
+ get updatedAt(): number {
+ return this.event.created_at;
+ }
+
+ get name(): string {
+ return this.data.name || "Anonymous";
+ }
+
+ get about(): string {
+ return this.data.about || "";
+ }
+
+ get picture(): string | undefined {
+ return this.data.picture;
+ }
+
+ get nip05(): string | undefined {
+ return this.data.nip05;
+ }
+
+ get lud16(): string | undefined {
+ return this.data.lud16;
+ }
+
+ get pubkey(): string {
+ return this.event.pubkey;
+ }
+
+ get npub(): string {
+ return nip19.npubEncode(this.pubkey);
+ }
+}
diff --git a/nostr.ts b/nostr.ts
index 5d7c330..fe60f12 100644
--- a/nostr.ts
+++ b/nostr.ts
@@ -1,6 +1,17 @@
import { NRelay1 } from "@nostrify/nostrify";
+import config from "./config.ts";
-export const relay = new NRelay1("wss://nostr.kosmos.org");
+export interface NEvent {
+ content: string;
+ created_at: number;
+ id: string;
+ kind: number;
+ pubkey: string;
+ sig: string;
+ tags: Array<[string, string, string?]>;
+}
+
+export const relay = new NRelay1(config.home_relay_url);
export async function fetchReplaceableEvent(
pubkey: string,
diff --git a/tests/dates_test.ts b/tests/dates_test.ts
new file mode 100644
index 0000000..2ba85b2
--- /dev/null
+++ b/tests/dates_test.ts
@@ -0,0 +1,19 @@
+import { describe, it } from "@std/testing/bdd";
+import { expect } from "@std/expect";
+import { localizeDate } from "../dates.ts";
+
+describe("Dates", () => {
+ describe("#localizeDate", () => {
+ it("returns a human-readable date for timestamp", () => {
+ const date = localizeDate(1726402055);
+ expect(date).toEqual("September 15, 2024");
+ });
+ });
+
+ describe("#isoDate", () => {
+ it("returns an ISO 8601 date for the timestamp", () => {
+ const date = localizeDate(1726402055);
+ expect(date).toEqual("September 15, 2024");
+ });
+ });
+});
diff --git a/tests/fixtures/article-1.json b/tests/fixtures/article-1.json
index 208d0d8..412ee0e 100644
--- a/tests/fixtures/article-1.json
+++ b/tests/fixtures/article-1.json
@@ -1 +1,29 @@
-{"content":"This week, it finally happened: I still had a Lightning channel open with a node that hadn't been online for the better part of a year now, so I decided to close the channel unilaterally. But force-closing a channel means you have to broadcast the latest commitment transaction, the pre-set fee of which was only ~1 sat/vB for this one.\n\nWith LND, if the channel is created as an [anchor channel](https://lightning.engineering/posts/2021-01-28-lnd-v0.12/) (by default only since version 0.12), then the commitment transaction contains small extra outputs (currently 330 sats), which let either channel partner spend one of them into a child transaction that can be created with higher fees to pay for the parent transaction (CPFP). LND even has a built-in command for that: `lncli wallet bumpclosefee`\n\nHowever, this channel was created in the old-school way, and was thus stuck with its low fee. In fact, even the local bitcoin node refused to accept the transaction into its own mempool, so the bitcoin p2p network didn't even know it existed. So how do we get out of this pickle?\n\n## The solution\n\nEnter the [mempool.space Accelerator](https://mempool.space/accelerator). It is essentially an automated way to create agreements with various mining pools to mine your low-fee transaction in exchange for an out-of-band payment. Mempool.space coordinates these agreements and out-of-band payments with miners and gets a share from the overall fee for that.\n\nNow, if you're in the same situation as I was, you might search for the ID of your closing transaction and find that mempool.space cannot find it. Remember how the local bitcoin node (with mostly default settings) didn't accept it in the first place?\n\n### 1. Get the transaction to be broadcast\n\nIn your `bitcoin.conf`, add the following line:\n\n minrelaytxfee=0\n\nThis sets the minimum fee to 0, meaning it will accept and broadcast your transactions, no matter how low the fee is. Restart `bitcoind` and wait a little bit. LND will retry broadcasting the closing transaction every minute or so until it succeeds. At some point you should be able to find it on mempool.space.\n\n### 2. Use the Accelerator to confirm it\n\nOnce you can see the transaction on [mempool.space](https://mempool.space), you can just click the \"Accelerate\" button next to the ETA. This will bring you to a page that shows you the estimated share of miners that will include your transaction in their blocks, as well as some acceleration fee options for various transaction fee levels, which you can pay for via the Lightning Network, of course.\n\nIf you haven't looked into this service before (which I had), then the fees might be a bit of a surprise to you. This thing is **not** cheap! Bumping my fee from 1 sat/vB to ~9 sats/vB cost a whopping 51,500 sats (~31 USD that day). Bumping it higher only seemed to add the difference in the transaction fee itself, so the service seems to have cost a flat 50K sats at the time.\n\nUnfortunately, this channel wasn't particularly large, so the acceleration fee amounted to ~9% of my remaining channel balance. But 91% of something is better than 100% of nothing, so I actually felt pretty good about it.\n\nNext, you will see something like this:\n\n\n\nTime to lean back and let the miners work for you. In my case, the ETA was eerily precise. It told me that it would take ~56 minutes to confirm the transaction, and almost exactly an hour later it was mined.\n\n### 3. Wait\n\nNow that our transaction is confirmed, our channel is not closed immediately, of course. The [time lock of the HTLC](https://docs.lightning.engineering/the-lightning-network/multihop-payments/hash-time-lock-contract-htlc) protects our channel partner from us broadcasting an old channel state in which our balance might be higher than in the latest state.\n\nIn my case, it was set to 144 blocks, i.e. ~24 hours. So I checked back the next day, et voilá: channel closed and balance restored. 🥳","created_at":1729462158,"id":"b45714c3965549c11dde7228071313bfc53a4df1896d939fb767ed0b6fcdab3a","kind":30023,"pubkey":"1f79058c77a224e5be226c8f024cacdad4d741855d75ed9f11473ba8eb86e1cb","sig":"23d929aa05921f5b10cc4ca1d05cd99b9d595888a94c394ffa1850b97c39b1b3d3d7b7472df959f2675b9f936b3334a55073c8931c52cdcfea18c19cdb9cdcb6","tags":[["d","1726396758485"],["title","How to confirm a stuck Lightning channel closing transaction with the mempool.space Accelerator"],["summary",""],["published_at","1726402055"],["published_at","1726402055"],["alt","This is a long form article, you can read it in https://habla.news/a/naddr1qvzqqqr4gupzq8meqkx80g3yuklzymy0qfx2ekk56aqc2ht4ak03z3em4r4cdcwtqqxnzdejxcenjd3hx5urgwp4676hkz"],["published_at","1726402055"],["published_at","1726402055"],["published_at","1726402055"],["t","lightning"],["t","lightning network"],["t","howto"],["published_at","1726402055"]]}
+{
+ "content": "This week, it finally happened: I still had a Lightning channel open with a node that hadn't been online for the better part of a year now, so I decided to close the channel unilaterally. But force-closing a channel means you have to broadcast the latest commitment transaction, the pre-set fee of which was only ~1 sat/vB for this one.\n\nWith LND, if the channel is created as an [anchor channel](https://lightning.engineering/posts/2021-01-28-lnd-v0.12/) (by default only since version 0.12), then the commitment transaction contains small extra outputs (currently 330 sats), which let either channel partner spend one of them into a child transaction that can be created with higher fees to pay for the parent transaction (CPFP). LND even has a built-in command for that: `lncli wallet bumpclosefee`\n\nHowever, this channel was created in the old-school way, and was thus stuck with its low fee. In fact, even the local bitcoin node refused to accept the transaction into its own mempool, so the bitcoin p2p network didn't even know it existed. So how do we get out of this pickle?\n\n## The solution\n\nEnter the [mempool.space Accelerator](https://mempool.space/accelerator). It is essentially an automated way to create agreements with various mining pools to mine your low-fee transaction in exchange for an out-of-band payment. Mempool.space coordinates these agreements and out-of-band payments with miners and gets a share from the overall fee for that.\n\nNow, if you're in the same situation as I was, you might search for the ID of your closing transaction and find that mempool.space cannot find it. Remember how the local bitcoin node (with mostly default settings) didn't accept it in the first place?\n\n### 1. Get the transaction to be broadcast\n\nIn your `bitcoin.conf`, add the following line:\n\n minrelaytxfee=0\n\nThis sets the minimum fee to 0, meaning it will accept and broadcast your transactions, no matter how low the fee is. Restart `bitcoind` and wait a little bit. LND will retry broadcasting the closing transaction every minute or so until it succeeds. At some point you should be able to find it on mempool.space.\n\n### 2. Use the Accelerator to confirm it\n\nOnce you can see the transaction on [mempool.space](https://mempool.space), you can just click the \"Accelerate\" button next to the ETA. This will bring you to a page that shows you the estimated share of miners that will include your transaction in their blocks, as well as some acceleration fee options for various transaction fee levels, which you can pay for via the Lightning Network, of course.\n\nIf you haven't looked into this service before (which I had), then the fees might be a bit of a surprise to you. This thing is **not** cheap! Bumping my fee from 1 sat/vB to ~9 sats/vB cost a whopping 51,500 sats (~31 USD that day). Bumping it higher only seemed to add the difference in the transaction fee itself, so the service seems to have cost a flat 50K sats at the time.\n\nUnfortunately, this channel wasn't particularly large, so the acceleration fee amounted to ~9% of my remaining channel balance. But 91% of something is better than 100% of nothing, so I actually felt pretty good about it.\n\nNext, you will see something like this:\n\n\n\nTime to lean back and let the miners work for you. In my case, the ETA was eerily precise. It told me that it would take ~56 minutes to confirm the transaction, and almost exactly an hour later it was mined.\n\n### 3. Wait\n\nNow that our transaction is confirmed, our channel is not closed immediately, of course. The [time lock of the HTLC](https://docs.lightning.engineering/the-lightning-network/multihop-payments/hash-time-lock-contract-htlc) protects our channel partner from us broadcasting an old channel state in which our balance might be higher than in the latest state.\n\nIn my case, it was set to 144 blocks, i.e. ~24 hours. So I checked back the next day, et voilá: channel closed and balance restored. 🥳",
+ "created_at": 1729462158,
+ "id": "b45714c3965549c11dde7228071313bfc53a4df1896d939fb767ed0b6fcdab3a",
+ "kind": 30023,
+ "pubkey": "1f79058c77a224e5be226c8f024cacdad4d741855d75ed9f11473ba8eb86e1cb",
+ "sig": "23d929aa05921f5b10cc4ca1d05cd99b9d595888a94c394ffa1850b97c39b1b3d3d7b7472df959f2675b9f936b3334a55073c8931c52cdcfea18c19cdb9cdcb6",
+ "tags": [
+ ["d", "1726396758485"],
+ [
+ "title",
+ "How to confirm a stuck Lightning channel closing transaction with the mempool.space Accelerator"
+ ],
+ ["summary", ""],
+ ["published_at", "1726402055"],
+ ["published_at", "1726402055"],
+ [
+ "alt",
+ "This is a long form article, you can read it in https://habla.news/a/naddr1qvzqqqr4gupzq8meqkx80g3yuklzymy0qfx2ekk56aqc2ht4ak03z3em4r4cdcwtqqxnzdejxcenjd3hx5urgwp4676hkz"
+ ],
+ ["published_at", "1726402055"],
+ ["published_at", "1726402055"],
+ ["published_at", "1726402055"],
+ ["t", "lightning"],
+ ["t", "lightning network"],
+ ["t", "howto"],
+ ["published_at", "1726402055"]
+ ]
+}
diff --git a/tests/fixtures/profile-1.json b/tests/fixtures/profile-1.json
new file mode 100644
index 0000000..d63a0f0
--- /dev/null
+++ b/tests/fixtures/profile-1.json
@@ -0,0 +1,9 @@
+{
+ "content": "{\"name\":\"Râu Cao ⚡\",\"nip05\":\"raucao@kosmos.org\",\"about\":\"Traveling full-time since 2010. Working on open-source software daily. Currently integrating Nostr features into Kosmos accounts.\",\"picture\":\"https://storage.kosmos.org/raucao/public/shares/240604-1441-fuerte-256.png\",\"lud16\":\"raucao@kosmos.org\",\"banner\":\"https://storage.kosmos.org/raucao/public/shares/240604-1517-1500x500.jpg\"}",
+ "created_at": 1728814592,
+ "id": "d437964cdd87f4b5bd595f47c2c64f4ba02c849ca215ed56fcb5fd3335ae2720",
+ "kind": 0,
+ "pubkey": "1f79058c77a224e5be226c8f024cacdad4d741855d75ed9f11473ba8eb86e1cb",
+ "sig": "ee067f88344fa8380a16154b7d988087c41d6c87ae720dd52947a38e63232ab6998de37f28e8c7115a0604fc184035af543ad354ed7b616b8ba29974653042cc",
+ "tags": []
+}
diff --git a/tests/models/article_test.ts b/tests/models/article_test.ts
index 7d11689..554dcb3 100644
--- a/tests/models/article_test.ts
+++ b/tests/models/article_test.ts
@@ -1,19 +1,16 @@
-import {
- beforeAll,
- beforeEach,
- describe,
- it,
-} from "@std/testing/bdd";
+import { beforeAll, describe, it } from "@std/testing/bdd";
import { expect } from "@std/expect";
import { NEvent } from "../../nostr.ts";
import Article from "../../models/article.ts";
-describe("User", () => {
+describe("Article", () => {
let articleEvent: NEvent;
let article: Article;
beforeAll(() => {
- articleEvent = JSON.parse(Deno.readTextFileSync("tests/fixtures/article-1.json"));
+ articleEvent = JSON.parse(
+ Deno.readTextFileSync("tests/fixtures/article-1.json"),
+ );
article = new Article(articleEvent);
});
@@ -22,4 +19,44 @@ describe("User", () => {
expect(article.identifier).toEqual("1726396758485");
});
});
+
+ describe("#title", () => {
+ it("returns the content of the 'title' tag", () => {
+ expect(article.title).toMatch(
+ /How to confirm a stuck Lightning channel closing transaction/,
+ );
+ });
+ });
+
+ describe("#summary", () => {
+ it("returns the content of the 'summary' tag", () => {
+ expect(article.summary).toEqual("");
+ });
+ });
+
+ describe("#publishedAt", () => {
+ it("returns the value of the first 'published_at' tag", () => {
+ expect(article.publishedAt).toEqual(1726402055);
+ });
+ });
+
+ describe("#updatedAt", () => {
+ it("returns the value of the first 'published_at' tag", () => {
+ expect(article.updatedAt).toEqual(1729462158);
+ });
+ });
+
+ describe("#html", () => {
+ it("returns a rendered HTML version of the 'content'", () => {
+ expect(article.html).toMatch(/
/);
+ });
+ });
+
+ describe("#naddr", () => {
+ it("returns bech32 addressable event ID", () => {
+ expect(article.naddr).toEqual(
+ "naddr1qvzqqqr4gupzq8meqkx80g3yuklzymy0qfx2ekk56aqc2ht4ak03z3em4r4cdcwtqqxnzdejxcenjd3hx5urgwp4676hkz",
+ );
+ });
+ });
});
diff --git a/tests/models/profile_test.ts b/tests/models/profile_test.ts
new file mode 100644
index 0000000..a4cd09a
--- /dev/null
+++ b/tests/models/profile_test.ts
@@ -0,0 +1,43 @@
+import { beforeAll, describe, it } from "@std/testing/bdd";
+import { expect } from "@std/expect";
+import { NEvent } from "../../nostr.ts";
+import Profile from "../../models/profile.ts";
+
+describe("Profile", () => {
+ let profileEvent: NEvent;
+ let profile: Profile;
+
+ beforeAll(() => {
+ profileEvent = JSON.parse(
+ Deno.readTextFileSync("tests/fixtures/profile-1.json"),
+ );
+ profile = new Profile(profileEvent);
+ });
+
+ describe("constructor", () => {
+ it("instantiates the username when given", () => {
+ profile = new Profile(profileEvent, "raucao");
+ expect(profile.username).toEqual("raucao");
+ });
+ });
+
+ describe("#updatedAt", () => {
+ it("returns the value of the profile event's 'created_at'", () => {
+ expect(profile.updatedAt).toEqual(1728814592);
+ });
+ });
+
+ describe("#name", () => {
+ it("returns the profile's name when present", () => {
+ expect(profile.name).toEqual("Râu Cao ⚡");
+ });
+ });
+
+ describe("#npub", () => {
+ it("returns the bech32-encoded version of the pubkey", () => {
+ expect(profile.npub).toEqual(
+ "npub1raustrrh5gjwt03zdj8syn9vmt2dwsv9t467m8c3gua636uxu89svgdees",
+ );
+ });
+ });
+});