Skip to content

Commit

Permalink
Move NIP-28 channel helpers to applesauce-channel package
Browse files Browse the repository at this point in the history
create applesauce-user-status package
  • Loading branch information
hzrd149 committed Sep 30, 2024
1 parent 76f07e7 commit b39a005
Show file tree
Hide file tree
Showing 22 changed files with 1,808 additions and 1,837 deletions.
2 changes: 1 addition & 1 deletion .changeset/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"changelog": "@changesets/cli/changelog",
"commit": false,
"fixed": [],
"linked": [],
"linked": [["applesauce-core", "applesauce-user-status", "applesauce-channel"]],
"access": "public",
"baseBranch": "master",
"updateInternalDependencies": "patch",
Expand Down
5 changes: 5 additions & 0 deletions .changeset/late-masks-move.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"applesauce-core": minor
---

Move NIP-28 channel helpers to applesauce-channel package
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# applesauce
# AppleSauce

AppleSauce is a collection typescript libraries built on top of [nostr-tools](https://github.com/nbd-wtf/nostr-tools) and is used in [noStrudel](https://github.com/hzrd149/nostrudel)
21 changes: 21 additions & 0 deletions packages/channel/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2024 hzrd149

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
9 changes: 9 additions & 0 deletions packages/channel/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# applesauce-channel

Helper methods an queries for [NIP-28](https://github.com/nostr-protocol/nips/blob/master/28.md) Public Channels

## Install

```bash
npm install applesauce-core applesauce-channel
```
54 changes: 54 additions & 0 deletions packages/channel/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"name": "applesauce-channel",
"version": "0.4.0",
"description": "",
"type": "module",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"build": "tsc",
"watch:build": "tsc --watch > /dev/null",
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js",
"watch:test": "(trap 'kill 0' SIGINT; pnpm run build -w > /dev/null & pnpm run test --watch)"
},
"keywords": [
"nostr"
],
"author": "hzrd149",
"license": "MIT",
"files": [
"dist"
],
"exports": {
".": {
"import": "./dist/index.js",
"types": "./dist/index.d.ts"
},
"./helpers": {
"import": "./dist/helpers/index.js",
"types": "./dist/helpers/index.d.ts"
},
"./queries": {
"import": "./dist/queries/index.js",
"types": "./dist/queries/index.d.ts"
}
},
"dependencies": {
"applesauce-core": "^0.4.0",
"nostr-tools": "^2.7.2"
},
"devDependencies": {
"@jest/globals": "^29.7.0",
"@types/jest": "^29.5.13",
"jest": "^29.7.0",
"jest-extended": "^4.0.2"
},
"jest": {
"roots": [
"dist"
],
"setupFilesAfterEnv": [
"jest-extended/all"
]
}
}
File renamed without changes.
2 changes: 2 additions & 0 deletions packages/channel/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./helpers/channel.js";
export * from "./queries/channel.js";
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { Filter, kinds, NostrEvent } from "nostr-tools";

import { Query } from "../query-store/index.js";
import { ChannelMetadataContent, getChannelMetadataContent } from "../helpers/channel.js";
import { safeParse } from "../helpers/json.js";
import { Query } from "applesauce-core";

function safeParse<T extends unknown = any>(str: string) {
try {
return JSON.parse(str) as T;
} catch (error) {
return undefined;
}
}

/** Creates a query that returns the latest parsed metadata */
export function ChannelMetadataQuery(channel: NostrEvent): Query<ChannelMetadataContent | undefined> {
Expand Down
27 changes: 27 additions & 0 deletions packages/channel/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"lib": ["ES2022", "DOM"],

"rootDir": "src",
"outDir": "dist",

"noImplicitAny": true,
"noImplicitReturns": true,
"noImplicitThis": true,
"noFallthroughCasesInSwitch": true,
"noUnusedLocals": true,
"noUnusedParameters": true,

"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"downlevelIteration": true,
"declaration": true,
"strict": true,

"pretty": true
},
"include": ["src/**/*"]
}
16 changes: 8 additions & 8 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
"import": "./dist/helpers/index.js",
"types": "./dist/helpers/index.d.ts"
},
"./queries": {
"import": "./dist/queries/index.js",
"types": "./dist/queries/index.d.ts"
},
"./observable": {
"import": "./dist/observable/index.js",
"types": "./dist/observable/index.d.ts"
Expand All @@ -39,28 +43,24 @@
"./event-store": {
"import": "./dist/event-store/index.js",
"types": "./dist/event-store/index.d.ts"
},
"./queries": {
"import": "./dist/queries/index.js",
"types": "./dist/queries/index.d.ts"
}
},
"dependencies": {
"@types/zen-push": "^0.1.4",
"debug": "^4.3.7",
"json-stringify-deterministic": "^1.0.12",
"nanoid": "^5.0.7",
"nostr-tools": "^2.7.2",
"zen-push": "^0.3.1"
"zen-push": "^0.3.1",
"zen-observable": "^0.10.0"
},
"devDependencies": {
"@types/zen-push": "^0.1.4",
"@jest/globals": "^29.7.0",
"@types/debug": "^4.1.12",
"@types/jest": "^29.5.13",
"@types/zen-observable": "^0.8.7",
"jest": "^29.7.0",
"jest-extended": "^4.0.2",
"zen-observable": "^0.10.0"
"jest-extended": "^4.0.2"
},
"jest": {
"roots": [
Expand Down
1 change: 0 additions & 1 deletion packages/core/src/queries/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@ export * from "./simple.js";
export * from "./profile.js";
export * from "./mailboxes.js";
export * from "./reactions.js";
export * from "./channel.js";
export * from "./mute.js";
export * from "./thread.js";
17 changes: 16 additions & 1 deletion packages/core/src/queries/simple.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,40 @@
import { Filter, NostrEvent } from "nostr-tools";
import stringify from "json-stringify-deterministic";

import { getReplaceableUID } from "../helpers/event.js";
import { getEventUID, getReplaceableUID } from "../helpers/event.js";
import { Query } from "../query-store/index.js";

/** Creates a Query that returns a single event or undefined */
export function SingleEventQuery(uid: string): Query<NostrEvent | undefined> {
return {
key: uid,
run: (events) => events.event(uid),
};
}

/** Creates a Query returning the latest version of a replaceable event */
export function ReplaceableQuery(kind: number, pubkey: string, d?: string): Query<NostrEvent | undefined> {
return {
key: getReplaceableUID(kind, pubkey, d),
run: (events) => events.replaceable(kind, pubkey, d),
};
}

/** Creates a Query that returns an array of sorted events matching the filters */
export function TimelineQuery(filters: Filter | Filter[]): Query<NostrEvent[]> {
return {
key: stringify(filters),
run: (events) => events.timeline(Array.isArray(filters) ? filters : [filters]),
};
}

/** Creates a Query that returns a directory of events by their UID */
export function ReplaceableSetQuery(filters: Filter | Filter[]): Query<Record<string, NostrEvent>> {
return {
key: stringify(filters),
run: (events) =>
events
.timeline(Array.isArray(filters) ? filters : [filters])
.map((events) => events.reduce((dir, event) => ({ ...dir, [getEventUID(event)]: event }), {})),
};
}
5 changes: 5 additions & 0 deletions packages/core/src/query-store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ export class QueryStore {
return this.runQuery(Queries.ReplaceableQuery)(kind, pubkey, d);
}

/** Returns a directory of events by their UID */
replaceableSet(filters: Filter | Filter[]) {
return this.runQuery(Queries.ReplaceableSetQuery)(filters);
}

/** Returns an array of events that match the filter */
timeline(filters: Filter | Filter[]) {
return this.runQuery(Queries.TimelineQuery)(filters);
Expand Down
21 changes: 21 additions & 0 deletions packages/user-status/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2024 hzrd149

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
9 changes: 9 additions & 0 deletions packages/user-status/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# applesauce-user-status

Helper methods an queries for [NIP-38](https://github.com/nostr-protocol/nips/blob/master/38.md) User Statuses

## Install

```bash
npm install applesauce-core applesauce-user-status
```
54 changes: 54 additions & 0 deletions packages/user-status/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"name": "applesauce-user-status",
"version": "0.4.0",
"description": "",
"type": "module",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"build": "tsc",
"watch:build": "tsc --watch > /dev/null",
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js",
"watch:test": "(trap 'kill 0' SIGINT; pnpm run build -w > /dev/null & pnpm run test --watch)"
},
"keywords": [
"nostr"
],
"author": "hzrd149",
"license": "MIT",
"files": [
"dist"
],
"exports": {
".": {
"import": "./dist/index.js",
"types": "./dist/index.d.ts"
},
"./helpers": {
"import": "./dist/helpers/index.js",
"types": "./dist/helpers/index.d.ts"
},
"./queries": {
"import": "./dist/queries/index.js",
"types": "./dist/queries/index.d.ts"
}
},
"dependencies": {
"applesauce-core": "^0.4.0",
"nostr-tools": "^2.7.2"
},
"devDependencies": {
"@jest/globals": "^29.7.0",
"@types/jest": "^29.5.13",
"jest": "^29.7.0",
"jest-extended": "^4.0.2"
},
"jest": {
"roots": [
"dist"
],
"setupFilesAfterEnv": [
"jest-extended/all"
]
}
}
42 changes: 42 additions & 0 deletions packages/user-status/src/helpers/user-status.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { NostrEvent } from "nostr-tools";
import { AddressPointer, EventPointer, ProfilePointer } from "nostr-tools/nip19";
import { getAddressPointerFromTag, getEventPointerFromTag, getProfilePointerFromTag } from "applesauce-core/helpers";

export const UserStatusPointerSymbol = Symbol.for("user-status-pointer");
declare module "nostr-tools" {
export interface Event {
[UserStatusPointerSymbol]?: UserStatusPointer | null;
}
}

export type UserStatusPointer =
| { type: "nevent"; data: EventPointer }
| { type: "nprofile"; data: ProfilePointer }
| { type: "naddr"; data: AddressPointer }
| { type: "url"; data: string };

function getStatusPointer(status: NostrEvent): UserStatusPointer | null {
const pTag = status.tags.find((t) => t[0] === "p" && t[1]);
if (pTag) return { type: "nprofile", data: getProfilePointerFromTag(pTag) };

const eTag = status.tags.find((t) => t[0] === "e" && t[1]);
if (eTag) return { type: "nevent", data: getEventPointerFromTag(eTag) };

const aTag = status.tags.find((t) => t[0] === "a" && t[1]);
if (aTag) return { type: "naddr", data: getAddressPointerFromTag(aTag) };

const rTag = status.tags.find((t) => t[0] === "r" && t[1]);
if (rTag) return { type: "url", data: rTag[1] };

return null;
}

export function getUserStatusPointer(status: NostrEvent) {
let pointer = status[UserStatusPointerSymbol];

if (pointer === undefined) {
pointer = status[UserStatusPointerSymbol] = getStatusPointer(status);
}

return pointer;
}
2 changes: 2 additions & 0 deletions packages/user-status/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./helpers/user-status.js";
export * from "./queries/user-status.js";
Loading

0 comments on commit b39a005

Please sign in to comment.