switch to docker based runners starting with nostr
This commit is contained in:
parent
3ffbfd9cd4
commit
a1c3231081
8 changed files with 367 additions and 573 deletions
33
src/nostr.ts
33
src/nostr.ts
|
|
@ -1,17 +1,22 @@
|
|||
import pino from "pino";
|
||||
import WebSocket from "ws";
|
||||
import { lambdaRequestTracker, pinoLambdaDestination } from "pino-lambda";
|
||||
import { MicroBlogBackend } from "./pocketbase";
|
||||
|
||||
// custom destination formatter
|
||||
const destination = pinoLambdaDestination();
|
||||
const logger = pino({}, destination);
|
||||
const withRequest = lambdaRequestTracker();
|
||||
|
||||
const logger = pino();
|
||||
const pb = new MicroBlogBackend(logger);
|
||||
|
||||
const fetcherNpub = process.env.NOSTR_FETCHER_NPUB!;
|
||||
const myNpub = process.env.NOSTR_ID!;
|
||||
const fetcherNpub = process.env.NOSTR_FETCHER_NPUB;
|
||||
const myNpub = process.env.NOSTR_ID;
|
||||
|
||||
if (!fetcherNpub) {
|
||||
logger.error("NOSTR_FETCHER_NPUB is not set");
|
||||
throw new Error("NOSTR_FETCHER_NPUB is not set");
|
||||
}
|
||||
|
||||
if (!myNpub) {
|
||||
logger.error("NOSTR_ID is not set");
|
||||
throw new Error("NOSTR_ID is not set");
|
||||
}
|
||||
|
||||
type NostrTag = ["t" | "r" | "imeta", string];
|
||||
type NostrEvent = [
|
||||
|
|
@ -28,8 +33,8 @@ type NostrEvent = [
|
|||
},
|
||||
];
|
||||
|
||||
exports.run = async (event: any, context: any) => {
|
||||
withRequest(event, context);
|
||||
(async () => {
|
||||
logger.info("Starting Nostr Fetcher");
|
||||
const events: NostrEvent[] = [];
|
||||
|
||||
// figure out when the last post of wasved
|
||||
|
|
@ -48,7 +53,7 @@ exports.run = async (event: any, context: any) => {
|
|||
ws.on("error", logger.error);
|
||||
ws.on("message", function message(data: Buffer) {
|
||||
const decodedData = JSON.parse(
|
||||
Buffer.from(data).toString("utf8")
|
||||
Buffer.from(data).toString("utf8"),
|
||||
) as NostrEvent;
|
||||
logger.info({ decodedData }, "recived a message from nostr relay");
|
||||
if (decodedData[0] === "EVENT") {
|
||||
|
|
@ -62,7 +67,7 @@ exports.run = async (event: any, context: any) => {
|
|||
"REQ",
|
||||
fetcherNpub,
|
||||
{ kinds: [1], authors: [myNpub], ...(since ? { since } : {}) },
|
||||
])
|
||||
]),
|
||||
);
|
||||
});
|
||||
await new Promise((resolve) => setTimeout(resolve, 30000));
|
||||
|
|
@ -89,9 +94,9 @@ exports.run = async (event: any, context: any) => {
|
|||
{
|
||||
remoteURL: url,
|
||||
},
|
||||
post.id
|
||||
post.id,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
})();
|
||||
|
|
|
|||
|
|
@ -44,8 +44,16 @@ export class MicroBlogBackend {
|
|||
}
|
||||
|
||||
private async login() {
|
||||
const pw = process.env.POCKET_BASE_PW!;
|
||||
const userName = process.env.POCKET_BASE_USER!;
|
||||
const pw = process.env.POCKET_BASE_PW;
|
||||
const userName = process.env.POCKET_BASE_USER;
|
||||
if (!pw) {
|
||||
this.logger.error("POCKET_BASE_PW env var not set");
|
||||
throw new Error("POCKET_BASE_PW env var not set");
|
||||
}
|
||||
if (!userName) {
|
||||
this.logger.error("POCKET_BASE_USER env var not set");
|
||||
throw new Error("POCKET_BASE_USER env var not set");
|
||||
}
|
||||
this.logger.info({ userName }, "Logging in to pocketbase");
|
||||
await this.pb.collection("users").authWithPassword(userName, pw);
|
||||
this.clientSetTime = new Date();
|
||||
|
|
@ -67,7 +75,7 @@ export class MicroBlogBackend {
|
|||
}
|
||||
|
||||
public async getLatestPostBySource(
|
||||
postSource: MicroBlogPostSource
|
||||
postSource: MicroBlogPostSource,
|
||||
): Promise<MicroBlogPost | undefined> {
|
||||
await this.checkLogin();
|
||||
try {
|
||||
|
|
@ -105,7 +113,7 @@ export class MicroBlogBackend {
|
|||
}
|
||||
|
||||
async getImageByRemoteURL(
|
||||
remoteURL: string
|
||||
remoteURL: string,
|
||||
): Promise<MicroBlogPostImage | undefined> {
|
||||
await this.checkLogin();
|
||||
try {
|
||||
|
|
@ -122,7 +130,7 @@ export class MicroBlogBackend {
|
|||
}
|
||||
|
||||
private async checkForPost(
|
||||
remoteId: string
|
||||
remoteId: string,
|
||||
): Promise<MicroBlogPost | undefined> {
|
||||
await this.checkLogin();
|
||||
try {
|
||||
|
|
@ -138,7 +146,7 @@ export class MicroBlogBackend {
|
|||
}
|
||||
|
||||
public async savePost(
|
||||
post: Omit<MicroBlogPost, "id" | "expand">
|
||||
post: Omit<MicroBlogPost, "id" | "expand">,
|
||||
): Promise<MicroBlogPost> {
|
||||
await this.checkLogin();
|
||||
const existingPost = await this.checkForPost(post.remoteId);
|
||||
|
|
@ -169,7 +177,7 @@ export class MicroBlogBackend {
|
|||
|
||||
public async saveAndSetImage(
|
||||
imageToSave: Omit<MicroBlogPostImage, "id" | "image" | "collectionId">,
|
||||
postId: string
|
||||
postId: string,
|
||||
) {
|
||||
await this.checkLogin();
|
||||
let image = await this.getImageByRemoteURL(imageToSave.remoteURL);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue