add masto adapter, clean up some stuff

This commit is contained in:
Iris Lightshard 2024-04-27 16:39:53 -06:00
parent afef280a2b
commit a768261688
Signed by: Iris Lightshard
GPG key ID: 688407174966CAF3
6 changed files with 365 additions and 87 deletions

View file

@ -5,4 +5,4 @@ underBBS is a platform-agnostic messaging and social media client
## building ## building
1. `npm install` 1. `npm install`
2. `npx webpack` 2. `npx tsc && npx webpack` (or run `./build.sh`, it does this for you)

224
package-lock.json generated
View file

@ -10,12 +10,18 @@
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"@nostr-dev-kit/ndk": "^2.7.1", "@nostr-dev-kit/ndk": "^2.7.1",
"bech32": "^2.0.0", "change-case": "^5.4.4",
"events-to-async": "^2.0.1",
"isomorphic-ws": "^5.0.0",
"masto": "^6.7.4",
"nostr-tools": "^2.5.0", "nostr-tools": "^2.5.0",
"typescript": "^5.4.5" "ts-custom-error": "^3.3.1",
"ws": "^8.16.0"
}, },
"devDependencies": { "devDependencies": {
"@types/debug": "^4.1.12", "@types/debug": "^4.1.12",
"@types/node": "^20.12.7",
"typescript": "^5.4.5",
"webpack": "^5.91.0", "webpack": "^5.91.0",
"webpack-cli": "^5.1.4" "webpack-cli": "^5.1.4"
} }
@ -587,11 +593,6 @@
"ajv": "^6.9.1" "ajv": "^6.9.1"
} }
}, },
"node_modules/bech32": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/bech32/-/bech32-2.0.0.tgz",
"integrity": "sha512-LcknSilhIGatDAsY1ak2I8VtGaHNhgMSYVxFrGLXv+xLHytaKZKcaUJJUE7qmBr7h33o5YQwP55pMI0xmkpJwg=="
},
"node_modules/browserslist": { "node_modules/browserslist": {
"version": "4.23.0", "version": "4.23.0",
"resolved": "https://registry.npmjs.org/browserslist/-/browserslist-4.23.0.tgz", "resolved": "https://registry.npmjs.org/browserslist/-/browserslist-4.23.0.tgz",
@ -642,6 +643,15 @@
"node": ">=6.14.2" "node": ">=6.14.2"
} }
}, },
"node_modules/camel-case": {
"version": "4.1.2",
"resolved": "https://registry.npmjs.org/camel-case/-/camel-case-4.1.2.tgz",
"integrity": "sha512-gxGWBrTT1JuMx6R+o5PTXMmUnhnVzLQ9SNutD4YqKtI6ap897t3tKECYla6gCWEkplXnlNybEkZg9GEGxKFCgw==",
"dependencies": {
"pascal-case": "^3.1.2",
"tslib": "^2.0.3"
}
},
"node_modules/caniuse-lite": { "node_modules/caniuse-lite": {
"version": "1.0.30001612", "version": "1.0.30001612",
"resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001612.tgz", "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001612.tgz",
@ -662,6 +672,21 @@
} }
] ]
}, },
"node_modules/capital-case": {
"version": "1.0.4",
"resolved": "https://registry.npmjs.org/capital-case/-/capital-case-1.0.4.tgz",
"integrity": "sha512-ds37W8CytHgwnhGGTi88pcPyR15qoNkOpYwmMMfnWqqWgESapLqvDx6huFjQ5vqWSn2Z06173XNA7LtMOeUh1A==",
"dependencies": {
"no-case": "^3.0.4",
"tslib": "^2.0.3",
"upper-case-first": "^2.0.2"
}
},
"node_modules/change-case": {
"version": "5.4.4",
"resolved": "https://registry.npmjs.org/change-case/-/change-case-5.4.4.tgz",
"integrity": "sha512-HRQyTk2/YPEkt9TnUPbOpr64Uw3KOicFWPVBb+xiHvd6eBx/qPr9xqfBFDT8P2vWsvvz4jbEkfDe71W3VyNu2w=="
},
"node_modules/chrome-trace-event": { "node_modules/chrome-trace-event": {
"version": "1.0.3", "version": "1.0.3",
"resolved": "https://registry.npmjs.org/chrome-trace-event/-/chrome-trace-event-1.0.3.tgz", "resolved": "https://registry.npmjs.org/chrome-trace-event/-/chrome-trace-event-1.0.3.tgz",
@ -697,6 +722,16 @@
"integrity": "sha512-GpVkmM8vF2vQUkj2LvZmD35JxeJOLCwJ9cUkugyk2nuhbv3+mJvpLYYt+0+USMxE+oj+ey/lJEnhZw75x/OMcQ==", "integrity": "sha512-GpVkmM8vF2vQUkj2LvZmD35JxeJOLCwJ9cUkugyk2nuhbv3+mJvpLYYt+0+USMxE+oj+ey/lJEnhZw75x/OMcQ==",
"dev": true "dev": true
}, },
"node_modules/constant-case": {
"version": "3.0.4",
"resolved": "https://registry.npmjs.org/constant-case/-/constant-case-3.0.4.tgz",
"integrity": "sha512-I2hSBi7Vvs7BEuJDr5dDHfzb/Ruj3FyvFyh7KLilAjNQw3Be+xgqUBA2W6scVEcL0hL1dwPRtIqEPVUCKkSsyQ==",
"dependencies": {
"no-case": "^3.0.4",
"tslib": "^2.0.3",
"upper-case": "^2.0.2"
}
},
"node_modules/cross-spawn": { "node_modules/cross-spawn": {
"version": "7.0.3", "version": "7.0.3",
"resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.3.tgz", "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.3.tgz",
@ -747,6 +782,15 @@
} }
} }
}, },
"node_modules/dot-case": {
"version": "3.0.4",
"resolved": "https://registry.npmjs.org/dot-case/-/dot-case-3.0.4.tgz",
"integrity": "sha512-Kv5nKlh6yRrdrGvxeJ2e5y2eRUpkUosIW4A2AS38zwSz27zu7ufDwQPi5Jhs3XAlGNetl3bmnGhQsMtkKJnj3w==",
"dependencies": {
"no-case": "^3.0.4",
"tslib": "^2.0.3"
}
},
"node_modules/electron-to-chromium": { "node_modules/electron-to-chromium": {
"version": "1.4.745", "version": "1.4.745",
"resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.4.745.tgz", "resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.4.745.tgz",
@ -905,6 +949,11 @@
"node": ">=0.8.x" "node": ">=0.8.x"
} }
}, },
"node_modules/events-to-async": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/events-to-async/-/events-to-async-2.0.1.tgz",
"integrity": "sha512-RtnLYrMbXp4JkZIoZu+3VTqV21bNVBlJBZ4NmtwvMNqSE3qouhxv2gvLE4JJDaQc54ioPkrX74V6x+hp/hqjkQ=="
},
"node_modules/ext": { "node_modules/ext": {
"version": "1.7.0", "version": "1.7.0",
"resolved": "https://registry.npmjs.org/ext/-/ext-1.7.0.tgz", "resolved": "https://registry.npmjs.org/ext/-/ext-1.7.0.tgz",
@ -1031,6 +1080,15 @@
"node": ">= 0.4" "node": ">= 0.4"
} }
}, },
"node_modules/header-case": {
"version": "2.0.4",
"resolved": "https://registry.npmjs.org/header-case/-/header-case-2.0.4.tgz",
"integrity": "sha512-H/vuk5TEEVZwrR0lp2zed9OCo1uAILMlx0JEMgC26rzyJJ3N1v6XkwHHXJQdR2doSjcGPM6OKPYoJgf0plJ11Q==",
"dependencies": {
"capital-case": "^1.0.4",
"tslib": "^2.0.3"
}
},
"node_modules/import-local": { "node_modules/import-local": {
"version": "3.1.0", "version": "3.1.0",
"resolved": "https://registry.npmjs.org/import-local/-/import-local-3.1.0.tgz", "resolved": "https://registry.npmjs.org/import-local/-/import-local-3.1.0.tgz",
@ -1103,6 +1161,14 @@
"node": ">=0.10.0" "node": ">=0.10.0"
} }
}, },
"node_modules/isomorphic-ws": {
"version": "5.0.0",
"resolved": "https://registry.npmjs.org/isomorphic-ws/-/isomorphic-ws-5.0.0.tgz",
"integrity": "sha512-muId7Zzn9ywDsyXgTIafTry2sV3nySZeUDe6YedVd1Hvuuep5AsIlqK+XefWpYTyJG5e503F2xIuT2lcU6rCSw==",
"peerDependencies": {
"ws": "*"
}
},
"node_modules/jest-worker": { "node_modules/jest-worker": {
"version": "27.5.1", "version": "27.5.1",
"resolved": "https://registry.npmjs.org/jest-worker/-/jest-worker-27.5.1.tgz", "resolved": "https://registry.npmjs.org/jest-worker/-/jest-worker-27.5.1.tgz",
@ -1178,6 +1244,45 @@
"node": ">=8" "node": ">=8"
} }
}, },
"node_modules/lower-case": {
"version": "2.0.2",
"resolved": "https://registry.npmjs.org/lower-case/-/lower-case-2.0.2.tgz",
"integrity": "sha512-7fm3l3NAF9WfN6W3JOmf5drwpVqX78JtoGJ3A6W0a6ZnldM41w2fV5D490psKFTpMds8TJse/eHLFFsNHHjHgg==",
"dependencies": {
"tslib": "^2.0.3"
}
},
"node_modules/masto": {
"version": "6.7.4",
"resolved": "https://registry.npmjs.org/masto/-/masto-6.7.4.tgz",
"integrity": "sha512-PseCKPf9gtew/lkBPS5qcMfHp9jWejE6Wz/FwC3s5VfO6jPkh07GvziLN/AMyaAgr8PalB2F9B2LYJV4SwTd1A==",
"dependencies": {
"change-case": "^4.1.2",
"events-to-async": "^2.0.1",
"isomorphic-ws": "^5.0.0",
"ts-custom-error": "^3.3.1",
"ws": "^8.16.0"
}
},
"node_modules/masto/node_modules/change-case": {
"version": "4.1.2",
"resolved": "https://registry.npmjs.org/change-case/-/change-case-4.1.2.tgz",
"integrity": "sha512-bSxY2ws9OtviILG1EiY5K7NNxkqg/JnRnFxLtKQ96JaviiIxi7djMrSd0ECT9AC+lttClmYwKw53BWpOMblo7A==",
"dependencies": {
"camel-case": "^4.1.2",
"capital-case": "^1.0.4",
"constant-case": "^3.0.4",
"dot-case": "^3.0.4",
"header-case": "^2.0.4",
"no-case": "^3.0.4",
"param-case": "^3.0.4",
"pascal-case": "^3.1.2",
"path-case": "^3.0.4",
"sentence-case": "^3.0.4",
"snake-case": "^3.0.4",
"tslib": "^2.0.3"
}
},
"node_modules/merge-stream": { "node_modules/merge-stream": {
"version": "2.0.0", "version": "2.0.0",
"resolved": "https://registry.npmjs.org/merge-stream/-/merge-stream-2.0.0.tgz", "resolved": "https://registry.npmjs.org/merge-stream/-/merge-stream-2.0.0.tgz",
@ -1221,6 +1326,15 @@
"resolved": "https://registry.npmjs.org/next-tick/-/next-tick-1.1.0.tgz", "resolved": "https://registry.npmjs.org/next-tick/-/next-tick-1.1.0.tgz",
"integrity": "sha512-CXdUiJembsNjuToQvxayPZF9Vqht7hewsvy2sOWafLvi2awflj9mOC6bHIg50orX8IJvWKY9wYQ/zB2kogPslQ==" "integrity": "sha512-CXdUiJembsNjuToQvxayPZF9Vqht7hewsvy2sOWafLvi2awflj9mOC6bHIg50orX8IJvWKY9wYQ/zB2kogPslQ=="
}, },
"node_modules/no-case": {
"version": "3.0.4",
"resolved": "https://registry.npmjs.org/no-case/-/no-case-3.0.4.tgz",
"integrity": "sha512-fgAN3jGAh+RoxUGZHTSOLJIqUc2wmoBwGR4tbpNAKmmovFoWq0OdRkb0VkldReO2a2iBT/OEulG9XSUc10r3zg==",
"dependencies": {
"lower-case": "^2.0.2",
"tslib": "^2.0.3"
}
},
"node_modules/node-domexception": { "node_modules/node-domexception": {
"version": "1.0.0", "version": "1.0.0",
"resolved": "https://registry.npmjs.org/node-domexception/-/node-domexception-1.0.0.tgz", "resolved": "https://registry.npmjs.org/node-domexception/-/node-domexception-1.0.0.tgz",
@ -1382,6 +1496,33 @@
"node": ">=6" "node": ">=6"
} }
}, },
"node_modules/param-case": {
"version": "3.0.4",
"resolved": "https://registry.npmjs.org/param-case/-/param-case-3.0.4.tgz",
"integrity": "sha512-RXlj7zCYokReqWpOPH9oYivUzLYZ5vAPIfEmCTNViosC78F8F0H9y7T7gG2M39ymgutxF5gcFEsyZQSph9Bp3A==",
"dependencies": {
"dot-case": "^3.0.4",
"tslib": "^2.0.3"
}
},
"node_modules/pascal-case": {
"version": "3.1.2",
"resolved": "https://registry.npmjs.org/pascal-case/-/pascal-case-3.1.2.tgz",
"integrity": "sha512-uWlGT3YSnK9x3BQJaOdcZwrnV6hPpd8jFH1/ucpiLRPh/2zCVJKS19E4GvYHvaCcACn3foXZ0cLB9Wrx1KGe5g==",
"dependencies": {
"no-case": "^3.0.4",
"tslib": "^2.0.3"
}
},
"node_modules/path-case": {
"version": "3.0.4",
"resolved": "https://registry.npmjs.org/path-case/-/path-case-3.0.4.tgz",
"integrity": "sha512-qO4qCFjXqVTrcbPt/hQfhTQ+VhFsqNKOPtytgNKkKxSoEp3XPUQ8ObFuePylOIok5gjn69ry8XiULxCwot3Wfg==",
"dependencies": {
"dot-case": "^3.0.4",
"tslib": "^2.0.3"
}
},
"node_modules/path-exists": { "node_modules/path-exists": {
"version": "4.0.0", "version": "4.0.0",
"resolved": "https://registry.npmjs.org/path-exists/-/path-exists-4.0.0.tgz", "resolved": "https://registry.npmjs.org/path-exists/-/path-exists-4.0.0.tgz",
@ -1530,6 +1671,16 @@
"url": "https://opencollective.com/webpack" "url": "https://opencollective.com/webpack"
} }
}, },
"node_modules/sentence-case": {
"version": "3.0.4",
"resolved": "https://registry.npmjs.org/sentence-case/-/sentence-case-3.0.4.tgz",
"integrity": "sha512-8LS0JInaQMCRoQ7YUytAo/xUu5W2XnQxV2HI/6uM6U7CITS1RqPElr30V6uIqyMKM9lJGRVFy5/4CuzcixNYSg==",
"dependencies": {
"no-case": "^3.0.4",
"tslib": "^2.0.3",
"upper-case-first": "^2.0.2"
}
},
"node_modules/serialize-javascript": { "node_modules/serialize-javascript": {
"version": "6.0.2", "version": "6.0.2",
"resolved": "https://registry.npmjs.org/serialize-javascript/-/serialize-javascript-6.0.2.tgz", "resolved": "https://registry.npmjs.org/serialize-javascript/-/serialize-javascript-6.0.2.tgz",
@ -1572,6 +1723,15 @@
"node": ">=8" "node": ">=8"
} }
}, },
"node_modules/snake-case": {
"version": "3.0.4",
"resolved": "https://registry.npmjs.org/snake-case/-/snake-case-3.0.4.tgz",
"integrity": "sha512-LAOh4z89bGQvl9pFfNF8V146i7o7/CqFPbqzYgP+yYzDIDeS9HaNFtXABamRW+AQzEVODcvE79ljJ+8a9YSdMg==",
"dependencies": {
"dot-case": "^3.0.4",
"tslib": "^2.0.3"
}
},
"node_modules/source-map": { "node_modules/source-map": {
"version": "0.6.1", "version": "0.6.1",
"resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz", "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz",
@ -1679,11 +1839,24 @@
} }
} }
}, },
"node_modules/ts-custom-error": {
"version": "3.3.1",
"resolved": "https://registry.npmjs.org/ts-custom-error/-/ts-custom-error-3.3.1.tgz",
"integrity": "sha512-5OX1tzOjxWEgsr/YEUWSuPrQ00deKLh6D7OTWcvNHm12/7QPyRh8SYpyWvA4IZv8H/+GQWQEh/kwo95Q9OVW1A==",
"engines": {
"node": ">=14.0.0"
}
},
"node_modules/tseep": { "node_modules/tseep": {
"version": "1.2.1", "version": "1.2.1",
"resolved": "https://registry.npmjs.org/tseep/-/tseep-1.2.1.tgz", "resolved": "https://registry.npmjs.org/tseep/-/tseep-1.2.1.tgz",
"integrity": "sha512-VFnsNcPGC4qFJ1nxbIPSjTmtRZOhlqLmtwRqtLVos8mbRHki8HO9cy9Z1e89EiWyxFmq6LBviI9TQjijxw/mEw==" "integrity": "sha512-VFnsNcPGC4qFJ1nxbIPSjTmtRZOhlqLmtwRqtLVos8mbRHki8HO9cy9Z1e89EiWyxFmq6LBviI9TQjijxw/mEw=="
}, },
"node_modules/tslib": {
"version": "2.6.2",
"resolved": "https://registry.npmjs.org/tslib/-/tslib-2.6.2.tgz",
"integrity": "sha512-AEYxH93jGFPn/a2iVAwW87VuUIkR1FVUKB77NwMF7nBTDkDrrT/Hpt/IrCJ0QXhW27jTBDcf5ZY7w6RiqTMw2Q=="
},
"node_modules/tstl": { "node_modules/tstl": {
"version": "2.5.16", "version": "2.5.16",
"resolved": "https://registry.npmjs.org/tstl/-/tstl-2.5.16.tgz", "resolved": "https://registry.npmjs.org/tstl/-/tstl-2.5.16.tgz",
@ -1706,6 +1879,7 @@
"version": "5.4.5", "version": "5.4.5",
"resolved": "https://registry.npmjs.org/typescript/-/typescript-5.4.5.tgz", "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.4.5.tgz",
"integrity": "sha512-vcI4UpRgg81oIRUFwR0WSIHKt11nJ7SAVlYNIu+QpqeyXP+gpQJy/Z4+F0aGxSE4MqwjyXvW/TzgkLAx2AGHwQ==", "integrity": "sha512-vcI4UpRgg81oIRUFwR0WSIHKt11nJ7SAVlYNIu+QpqeyXP+gpQJy/Z4+F0aGxSE4MqwjyXvW/TzgkLAx2AGHwQ==",
"devOptional": true,
"bin": { "bin": {
"tsc": "bin/tsc", "tsc": "bin/tsc",
"tsserver": "bin/tsserver" "tsserver": "bin/tsserver"
@ -1755,6 +1929,22 @@
"browserslist": ">= 4.21.0" "browserslist": ">= 4.21.0"
} }
}, },
"node_modules/upper-case": {
"version": "2.0.2",
"resolved": "https://registry.npmjs.org/upper-case/-/upper-case-2.0.2.tgz",
"integrity": "sha512-KgdgDGJt2TpuwBUIjgG6lzw2GWFRCW9Qkfkiv0DxqHHLYJHmtmdUIKcZd8rHgFSjopVTlw6ggzCm1b8MFQwikg==",
"dependencies": {
"tslib": "^2.0.3"
}
},
"node_modules/upper-case-first": {
"version": "2.0.2",
"resolved": "https://registry.npmjs.org/upper-case-first/-/upper-case-first-2.0.2.tgz",
"integrity": "sha512-514ppYHBaKwfJRK/pNC6c/OxfGa0obSnAl106u97Ed0I625Nin96KAjttZF6ZL3e1XLtphxnqrOi9iWgm+u+bg==",
"dependencies": {
"tslib": "^2.0.3"
}
},
"node_modules/uri-js": { "node_modules/uri-js": {
"version": "4.4.1", "version": "4.4.1",
"resolved": "https://registry.npmjs.org/uri-js/-/uri-js-4.4.1.tgz", "resolved": "https://registry.npmjs.org/uri-js/-/uri-js-4.4.1.tgz",
@ -1988,6 +2178,26 @@
"integrity": "sha512-CC1bOL87PIWSBhDcTrdeLo6eGT7mCFtrg0uIJtqJUFyK+eJnzl8A1niH56uu7KMa5XFrtiV+AQuHO3n7DsHnLQ==", "integrity": "sha512-CC1bOL87PIWSBhDcTrdeLo6eGT7mCFtrg0uIJtqJUFyK+eJnzl8A1niH56uu7KMa5XFrtiV+AQuHO3n7DsHnLQ==",
"dev": true "dev": true
}, },
"node_modules/ws": {
"version": "8.16.0",
"resolved": "https://registry.npmjs.org/ws/-/ws-8.16.0.tgz",
"integrity": "sha512-HS0c//TP7Ina87TfiPUz1rQzMhHrl/SG2guqRcTOIUYD2q8uhUdNHZYJUaQ8aTGPzCh+c6oawMKW35nFl1dxyQ==",
"engines": {
"node": ">=10.0.0"
},
"peerDependencies": {
"bufferutil": "^4.0.1",
"utf-8-validate": ">=5.0.2"
},
"peerDependenciesMeta": {
"bufferutil": {
"optional": true
},
"utf-8-validate": {
"optional": true
}
}
},
"node_modules/yaeti": { "node_modules/yaeti": {
"version": "0.0.6", "version": "0.0.6",
"resolved": "https://registry.npmjs.org/yaeti/-/yaeti-0.0.6.tgz", "resolved": "https://registry.npmjs.org/yaeti/-/yaeti-0.0.6.tgz",

View file

@ -10,12 +10,18 @@
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"@nostr-dev-kit/ndk": "^2.7.1", "@nostr-dev-kit/ndk": "^2.7.1",
"bech32": "^2.0.0", "change-case": "^5.4.4",
"events-to-async": "^2.0.1",
"isomorphic-ws": "^5.0.0",
"masto": "^6.7.4",
"nostr-tools": "^2.5.0", "nostr-tools": "^2.5.0",
"typescript": "^5.4.5" "ts-custom-error": "^3.3.1",
"ws": "^8.16.0"
}, },
"devDependencies": { "devDependencies": {
"@types/debug": "^4.1.12", "@types/debug": "^4.1.12",
"@types/node": "^20.12.7",
"typescript": "^5.4.5",
"webpack": "^5.91.0", "webpack": "^5.91.0",
"webpack-cli": "^5.1.4" "webpack-cli": "^5.1.4"
} }

View file

@ -1,10 +1,16 @@
import NDK, {NDKPrivateKeySigner} from "@nostr-dev-kit/ndk"; import NDK, {NDKPrivateKeySigner} from "@nostr-dev-kit/ndk";
import * as nip19 from 'nostr-tools/nip19' import * as nip19 from 'nostr-tools/nip19'
import { createRestAPIClient } from "masto";
import * as masto from "masto";
type MastodonClient = masto.mastodon.rest.Client;
export class Adapter { export class Adapter {
public nickname: string = ""; public nickname: string = "";
public protocol: string = ""; public protocol: string = "";
public identity: any | null; public identity: any | null;
private _self: NDK | MastodonClient | null = null ;
public init(): void {}; public init(): void {};
public getInbox(): void {}; public getInbox(): void {};
@ -13,55 +19,84 @@ export class Adapter {
public getFollowing(): any[] { return [] }; public getFollowing(): any[] { return [] };
public updateMetadata(): void {}; public updateMetadata(): void {};
public getMetadata(): any { return {} }; public getMetadata(): any { return {} };
}
let ndk: NDK | null = null;
function createAdapter(): Adapter {
let adapter = new Adapter();
adapter.init = ()=>{}; private static ndk: NDK | null = null;
adapter.getInbox = ()=>{};
adapter.getFollowers = ()=>[];
adapter.getFollowing = ()=>[];
adapter.publish = ()=>{};
adapter.updateMetadata = ()=>{};
adapter.getMetadata = ()=>{return {}};
return adapter; public static create(): Adapter {
} let adapter = new Adapter();
function toNostrAdapter(adapter: Adapter, settings: any): Adapter { adapter.init = ()=>{};
adapter.identity = { privkey: settings.privkey }; adapter.getInbox = async ()=>{};
adapter.nickname = settings.nickname; adapter.getFollowers = ()=>[];
adapter.init = ()=> { adapter.getFollowing = ()=>[];
if (!ndk) { adapter.publish = ()=>{};
let privkey_raw = nip19.decode(settings.privkey); adapter.updateMetadata = ()=>{};
ndk = new NDK({ adapter.getMetadata = ()=>{return {}};
signer: new NDKPrivateKeySigner(<string>privkey_raw.data),
explicitRelayUrls: [ settings.relays ] return adapter;
}
public static toNostr(adapter: Adapter, settings: any): Adapter {
adapter.identity = { privkey: settings.privkey };
adapter.nickname = settings.nickname;
adapter.init = ()=> {
if (!Adapter.ndk) {
let privkey_raw = nip19.decode(settings.privkey);
Adapter.ndk = new NDK({
signer: new NDKPrivateKeySigner(<string>privkey_raw.data),
explicitRelayUrls: [ settings.relays ]
});
adapter._self = Adapter.ndk;
Adapter.ndk.connect();
} else {
Adapter.ndk.signer = new NDKPrivateKeySigner(settings.privatekey);
for (let i of settings.relays) {
Adapter.ndk.addExplicitRelay(i);
}
}
};
adapter.getInbox = async () => {
if (Adapter.ndk) {
const sub = Adapter.ndk.subscribe({ kinds: [1] }); // Get all kind:1s
sub.on("event", (event) => console.log(event.content)); // Show the content
sub.on("eose", () => console.log("All relays have reached the end of the event stream"));
sub.on("close", () => console.log("Subscription closed"));
setTimeout(() => sub.stop(), 10000); // Stop the subscription after 10 seconds
}
};
return adapter;
}
public static toMasto(adapter: Adapter, settings: any): Adapter {
adapter.identity = { server: settings.server, apiKey: settings.apiKey };
adapter.nickname = settings.nickname;
adapter.init = () => {
adapter._self = createRestAPIClient({
url: adapter.identity.server,
accessToken: adapter.identity.apiKey
}); });
ndk.connect(); }
} else {
ndk.signer = new NDKPrivateKeySigner(settings.privatekey); adapter.getInbox = async () => {
for (let i of settings.relays) { let i = 0;
ndk.addExplicitRelay(i); for await (const statuses of (adapter._self as MastodonClient).v1.timelines.public.list()) {
for (const status of statuses) {
console.log(status);
i++;
}
if (i >= 10) break;
} }
} }
};
adapter.getInbox = () => { return adapter;
if (ndk) { }
const sub = ndk.subscribe({ kinds: [1] }); // Get all kind:1s
sub.on("event", (event) => console.log(event.content)); // Show the content
sub.on("eose", () => console.log("All relays have reached the end of the event stream"));
sub.on("close", () => console.log("Subscription closed"));
setTimeout(() => sub.stop(), 10000); // Stop the subscription after 10 seconds
}
};
return adapter;
} }
export default { createAdapter, toNostrAdapter }
export default { Adapter }

View file

@ -1,12 +1,10 @@
import adapter, {Adapter} from "./adapter"; import {Adapter} from "./adapter";
function _s(key: string, value: any | null) { function _(key: string, value: any | null | undefined = undefined): any | null {
const x = <any>window;
x[key] = value;
}
function _g(key: string): any | null {
const x = <any>window; const x = <any>window;
if (value !== undefined) {
x[key] = value;
}
return x[key]; return x[key];
} }
@ -15,23 +13,23 @@ function $(id: string): HTMLElement | null {
} }
function main():void { function main():void {
_s("settings", JSON.parse(localStorage.getItem("settings") ?? "{}")); const settings = _("settings", JSON.parse(localStorage.getItem("settings") ?? "{}"));
_s("adapters", []); const adapters = _("adapters", []);
const settings = _g("settings");
const adapters = _g("adapters");
if (settings != null) { if (settings != null) {
for (let s of settings.adapters) { for (let s of settings.adapters) {
switch (s.protocol) { let a: Adapter = Adapter.create()
switch (s.protocol) {
case "nostr": case "nostr":
let a = adapter.toNostrAdapter(adapter.createAdapter(), s); adapters.push(Adapter.toNostr(a, s));
adapters.push(a);
break; break;
case "mastodon":
adapters.push(Adapter.toMasto(a, s));
} }
} }
} else { } else {
console.log("no settings exist for this client"); console.log("no settings exist for this client");
_s("settings", { adapters: [] }); _("settings", { adapters: [] });
} }
}; };
@ -44,11 +42,12 @@ function showSettings():void {
// tabcontent to show settings ui // tabcontent to show settings ui
const tabcontent = $("tabcontent"); const tabcontent = $("tabcontent");
const adapters = _("adapters") as Adapter[];
if (tabcontent) { if (tabcontent) {
let html = "<p>this is our settings dialogue</p>"; let html = "<p>this is our settings dialogue</p>";
html += "<button onclick='addAdapter()'>New</button>"; html += "<button onclick='addAdapter()'>New</button>";
html += _g("adapters").reduce((self: string, a: Adapter) => { html += adapters.reduce((self: string, a: Adapter) => {
self += `<li><a href='#' onclick='editAdapter(${a.nickname})'>${a.nickname}</a></li>` self += `<li><a href='#' onclick='editAdapter(${a.nickname})'>${a.nickname}</a></li>`
return self; return self;
}, "<ul id='settings_adapterlist'>"); }, "<ul id='settings_adapterlist'>");
@ -63,7 +62,7 @@ function addAdapter(): void {
if (tabcontent) { if (tabcontent) {
// dropdown for protocol // dropdown for protocol
let html = "<select id='settings_newadapter_protocolselect' onchange='fillAdapterProtocolOptions()'>"; let html = "<select id='settings_newadapter_protocolselect' onchange='fillAdapterProtocolOptions()'>";
html += [ "nostr" ].reduce((self, p)=>{ html += [ "nostr", "mastodon" ].reduce((self, p)=>{
self += `<option value='${p}'>${p}</option>`; self += `<option value='${p}'>${p}</option>`;
return self; return self;
}, ""); }, "");
@ -71,9 +70,9 @@ function addAdapter(): void {
// depending on protocol, different fields // depending on protocol, different fields
// nostr: privkey, initial relays // nostr: privkey, initial relays
html += "<div id='settings_newadapter_protocoloptions'>"; html += "<div id='settings_newadapter_protocoloptions'>";
html += " <input id='settings_newadapter_nickname'/>"; html += " <label>nickname<input id='settings_newadapter_nickname'/></label>";
html += " <input id='settings_newadapter_nostr_privkey'/>"; html += " <label>privkey<input id='settings_newadapter_nostr_privkey'/></label>";
html += " <input id='settings_newadapter_nostr_default_relays'/>"; html += " <label>default relays<input id='settings_newadapter_nostr_default_relays'/></label>";
html += "</div>"; html += "</div>";
// masto/AP: server, username, pw/apikey // masto/AP: server, username, pw/apikey
// save button, back button // save button, back button
@ -87,13 +86,30 @@ function addAdapter(): void {
function fillAdapterProtocolOptions(): void { function fillAdapterProtocolOptions(): void {
const proto = $("settings_newadapter_protocolselect") as HTMLSelectElement; const proto = $("settings_newadapter_protocolselect") as HTMLSelectElement;
let html = "";
switch(proto?.options[proto.selectedIndex].value) {
case "nostr":
html += " <label>nickname<input id='settings_newadapter_nickname'/></label>";
html += " <label>privkey<input id='settings_newadapter_nostr_privkey'/></label>";
html += " <label>default relays<input id='settings_newadapter_nostr_default_relays'/></label>";
break;
case "mastodon":
html += " <label>nickname<input id='settings_newadapter_nickname'/></label>";
html += " <label>server<input id='settings_newadapter_masto_server'/></label>";
html += " <label>API key<input id='settings_newadapter_masto_apikey'/></label>";
break;
}
console.log(proto?.options[proto.selectedIndex] ?? "no proto"); const div = $("settings_newadapter_protocoloptions");
if (div) {
div.innerHTML = html;
}
} }
function saveSettings(): void { function saveSettings(): void {
const settings = _g("settings"); const settings = _("settings");
if (settings) { if (settings) {
localStorage.setItem("settings", JSON.stringify(settings)); localStorage.setItem("settings", JSON.stringify(settings));
} }
@ -116,34 +132,44 @@ function saveAdapter(): void {
const proto = $("settings_newadapter_protocolselect") as HTMLSelectElement; const proto = $("settings_newadapter_protocolselect") as HTMLSelectElement;
console.log(proto.options[proto.selectedIndex]); console.log(proto.options[proto.selectedIndex]);
const nickname = ($("settings_newadapter_nickname") as HTMLInputElement)?.value ?? "" ;
// switch protocol // switch protocol
switch (proto.options[proto.selectedIndex].value) { switch (proto.options[proto.selectedIndex].value) {
// nostr: save privkey
// save relays
case "nostr": case "nostr":
const privkey = ($("settings_newadapter_nostr_privkey") as HTMLInputElement)?.value ?? ""; const privkey = ($("settings_newadapter_nostr_privkey") as HTMLInputElement)?.value ?? "";
const relays = ($("settings_newadapter_nostr_default_relays") as HTMLInputElement)?.value ?? ""; const relays = ($("settings_newadapter_nostr_default_relays") as HTMLInputElement)?.value ?? "";
const nickname = ($("settings_newadapter_nickname") as HTMLInputElement)?.value ?? "" ;
self = { nickname: nickname, protocol: "nostr", privkey: privkey, relays: relays.split(",").map(r=>r.trim()) }; self = { nickname: nickname, protocol: "nostr", privkey: privkey, relays: relays.split(",").map(r=>r.trim()) };
break; break;
// AP/masto: whatever case "mastodon":
case "ap": const server = ($("settings_newadapter_masto_server") as HTMLInputElement)?.value ?? "";
const apiKey = ($("settings_newadapter_masto_apikey") as HTMLInputElement)?.value ?? "";
self = { nickname: nickname, protocol: "mastodon", server: server, apiKey: apiKey };
break; break;
} }
const settings = _g("settings"); const settings = _("settings");
const adapters = _g("adapters"); const adapters = _("adapters");
if (settings && adapters) { if (settings && adapters) {
settings.adapters.push(self); settings.adapters.push(self);
adapters.push(adapter.toNostrAdapter(adapter.createAdapter(), self)); let a: Adapter = Adapter.create();
switch (self.protocol) {
case "nostr":
adapters.push(Adapter.toNostr(a, self));
break;
case "mastodon":
adapters.push(Adapter.toMasto(a, self));
break;
}
localStorage.setItem("settings", JSON.stringify(settings)); localStorage.setItem("settings", JSON.stringify(settings));
showSettings(); showSettings();
} }
} }
_s("addAdapter", addAdapter); _("addAdapter", addAdapter);
_s("saveAdapter", saveAdapter); _("saveAdapter", saveAdapter);
_s("fillAdapterProtocolOptions", fillAdapterProtocolOptions); _("fillAdapterProtocolOptions", fillAdapterProtocolOptions);
_s("showSettings", showSettings); _("showSettings", showSettings);
_s("saveSettings", saveSettings); _("saveSettings", saveSettings);
main(); main();

View file

@ -1,5 +1,6 @@
{ {
"compilerOptions": { "compilerOptions": {
"target": "ES2022",
"module": "preserve", "module": "preserve",
"moduleResolution": "bundler", "moduleResolution": "bundler",
"noImplicitAny": true, "noImplicitAny": true,