applesauce-actions 0.0.0-next-20250314151125 → 0.0.0-next-20250315140539

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,80 @@
1
+ import { beforeEach, describe, expect, it } from "vitest";
2
+ import { EventStore } from "applesauce-core";
3
+ import { EventFactory } from "applesauce-factory";
4
+ import { kinds } from "nostr-tools";
5
+ import { unlockHiddenTags } from "applesauce-core/helpers";
6
+ import { subscribeSpyTo } from "@hirez_io/observer-spy";
7
+ import { FakeUser } from "../../__tests__/fake-user.js";
8
+ import { ActionHub } from "../../action-hub.js";
9
+ import { AddUserToFollowSet, RemoveUserFromFollowSet } from "../follow-sets.js";
10
+ const user = new FakeUser();
11
+ const testPubkey = "test-pubkey";
12
+ const testIdentifier = "test-list";
13
+ let events;
14
+ let factory;
15
+ let hub;
16
+ beforeEach(() => {
17
+ events = new EventStore();
18
+ factory = new EventFactory({ signer: user });
19
+ hub = new ActionHub(events, factory);
20
+ // Add a follow set event to work with
21
+ const followSet = user.event({
22
+ kind: kinds.Followsets,
23
+ tags: [["d", testIdentifier]],
24
+ content: "",
25
+ created_at: Math.floor(Date.now() / 1000),
26
+ });
27
+ events.add(followSet);
28
+ });
29
+ describe("AddUserToList", () => {
30
+ it("should add a pubkey to public tags in a follow set", async () => {
31
+ const spy = subscribeSpyTo(hub.exec(AddUserToFollowSet, testPubkey, testIdentifier), { expectErrors: false });
32
+ await spy.onComplete();
33
+ const emittedEvent = spy.getLastValue();
34
+ expect(emittedEvent).toMatchObject({
35
+ kind: kinds.Followsets,
36
+ tags: expect.arrayContaining([
37
+ ["d", testIdentifier],
38
+ ["p", testPubkey],
39
+ ]),
40
+ });
41
+ });
42
+ it("should add a pubkey to hidden tags in a follow set", async () => {
43
+ const spy = subscribeSpyTo(hub.exec(AddUserToFollowSet, testPubkey, testIdentifier, true), { expectErrors: false });
44
+ await spy.onComplete();
45
+ const emittedEvent = spy.getLastValue();
46
+ expect(await unlockHiddenTags(emittedEvent, user)).toEqual(expect.arrayContaining([["p", testPubkey]]));
47
+ });
48
+ });
49
+ describe("RemoveUserFromList", () => {
50
+ beforeEach(async () => {
51
+ // Add a follow set with existing tags to remove
52
+ const followSetWithTags = user.event({
53
+ kind: kinds.Followsets,
54
+ tags: [
55
+ ["d", testIdentifier],
56
+ ["p", testPubkey],
57
+ ],
58
+ content: await user.nip04.encrypt(user.pubkey, JSON.stringify(["p", testPubkey])),
59
+ created_at: Math.floor(Date.now() / 1000),
60
+ });
61
+ events.add(followSetWithTags);
62
+ });
63
+ it("should remove a pubkey from public tags in a follow set", async () => {
64
+ const spy = subscribeSpyTo(hub.exec(RemoveUserFromFollowSet, testPubkey, testIdentifier), { expectErrors: false });
65
+ await spy.onComplete();
66
+ const emittedEvent = spy.getLastValue();
67
+ expect(emittedEvent).toMatchObject({
68
+ kind: kinds.Followsets,
69
+ tags: expect.not.arrayContaining([["p", testPubkey]]),
70
+ });
71
+ });
72
+ it("should remove a pubkey from hidden tags in a follow set", async () => {
73
+ const spy = subscribeSpyTo(hub.exec(RemoveUserFromFollowSet, testPubkey, testIdentifier, true), {
74
+ expectErrors: false,
75
+ });
76
+ await spy.onComplete();
77
+ const emittedEvent = spy.getLastValue();
78
+ expect(await unlockHiddenTags(emittedEvent, user)).toEqual(expect.not.arrayContaining([["hidden", "p", testPubkey]]));
79
+ });
80
+ });
@@ -0,0 +1,15 @@
1
+ import { Action } from "../action-hub.js";
2
+ /**
3
+ * An action that adds a pubkey to a follow set
4
+ * @param pubkey the pubkey to add to the set
5
+ * @param identifier the "d" tag of the follow set
6
+ * @param hidden set to true to add to hidden follows
7
+ */
8
+ export declare function AddUserToFollowSet(pubkey: string, identifier: string, hidden?: boolean): Action;
9
+ /**
10
+ * An action that removes a pubkey from a follow set
11
+ * @param pubkey the pubkey to remove from the set
12
+ * @param identifier the "d" tag of the follow set
13
+ * @param hidden set to true to remove from hidden follows
14
+ */
15
+ export declare function RemoveUserFromFollowSet(pubkey: string, identifier: string, hidden?: boolean): Action;
@@ -0,0 +1,37 @@
1
+ import { kinds } from "nostr-tools";
2
+ import { addPubkeyTag, removePubkeyTag } from "applesauce-factory/operations/tag";
3
+ function getFollowSetEvent(events, self, identifier) {
4
+ return events.getReplaceable(kinds.Followsets, self, identifier);
5
+ }
6
+ /**
7
+ * An action that adds a pubkey to a follow set
8
+ * @param pubkey the pubkey to add to the set
9
+ * @param identifier the "d" tag of the follow set
10
+ * @param hidden set to true to add to hidden follows
11
+ */
12
+ export function AddUserToFollowSet(pubkey, identifier, hidden = false) {
13
+ return async function* ({ events, factory, self }) {
14
+ const follows = getFollowSetEvent(events, self, identifier);
15
+ if (!follows)
16
+ throw new Error("Can't find follow set");
17
+ const operation = addPubkeyTag({ pubkey });
18
+ const draft = await factory.modifyTags(follows, hidden ? { hidden: operation } : operation);
19
+ yield await factory.sign(draft);
20
+ };
21
+ }
22
+ /**
23
+ * An action that removes a pubkey from a follow set
24
+ * @param pubkey the pubkey to remove from the set
25
+ * @param identifier the "d" tag of the follow set
26
+ * @param hidden set to true to remove from hidden follows
27
+ */
28
+ export function RemoveUserFromFollowSet(pubkey, identifier, hidden = false) {
29
+ return async function* ({ events, factory, self }) {
30
+ const follows = getFollowSetEvent(events, self, identifier);
31
+ if (!follows)
32
+ throw new Error("Can't find follow set");
33
+ const operation = removePubkeyTag(pubkey);
34
+ const draft = await factory.modifyTags(follows, hidden ? { hidden: operation } : operation);
35
+ yield await factory.sign(draft);
36
+ };
37
+ }
@@ -1,4 +1,5 @@
1
- export * from "./contacts.js";
2
1
  export * from "./bookmarks.js";
2
+ export * from "./contacts.js";
3
+ export * from "./follow-sets.js";
3
4
  export * from "./pins.js";
4
5
  export * from "./profile.js";
@@ -1,4 +1,5 @@
1
- export * from "./contacts.js";
2
1
  export * from "./bookmarks.js";
2
+ export * from "./contacts.js";
3
+ export * from "./follow-sets.js";
3
4
  export * from "./pins.js";
4
5
  export * from "./profile.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "applesauce-actions",
3
- "version": "0.0.0-next-20250314151125",
3
+ "version": "0.0.0-next-20250315140539",
4
4
  "description": "A package for performing common nostr actions",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -32,15 +32,15 @@
32
32
  }
33
33
  },
34
34
  "dependencies": {
35
- "applesauce-core": "0.0.0-next-20250314151125",
36
- "applesauce-factory": "0.0.0-next-20250314151125",
35
+ "applesauce-core": "0.0.0-next-20250315140539",
36
+ "applesauce-factory": "0.0.0-next-20250315140539",
37
37
  "nostr-tools": "^2.10.4",
38
38
  "rxjs": "^7.8.1"
39
39
  },
40
40
  "devDependencies": {
41
41
  "@hirez_io/observer-spy": "^2.2.0",
42
42
  "@types/debug": "^4.1.12",
43
- "applesauce-signers": "0.0.0-next-20250314151125",
43
+ "applesauce-signers": "0.0.0-next-20250315140539",
44
44
  "nanoid": "^5.0.9",
45
45
  "typescript": "^5.7.3",
46
46
  "vitest": "^3.0.5"