Skip to content

Commit

Permalink
Merge pull request #18 from planetary-social/slack-notifications
Browse files Browse the repository at this point in the history
Add slack notifications
  • Loading branch information
dcadenas authored Feb 22, 2024
2 parents 2fdaf37 + ea2ad2c commit 604296b
Show file tree
Hide file tree
Showing 9 changed files with 159 additions and 80 deletions.
3 changes: 3 additions & 0 deletions config/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ if (process.env.NODE_ENV === "production") {
}

export default {
latestEntriesCount: 10,
slackWebhookUrl: process.env.SLACK_WEBHOOK_URL,
slackCron: process.env.SLACK_CRON || "*/10 * * * *",
redis: {
host: process.env.REDIS_HOST || "localhost",
},
Expand Down
8 changes: 4 additions & 4 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ export default {
modulePathIgnorePatterns: ["./config/"],
coverageThreshold: {
global: {
branches: 80,
functions: 80,
lines: 80,
statements: 80,
branches: 75,
functions: 75,
lines: 75,
statements: 75,
},
},
};
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
"express-prom-bundle": "^6.6.0",
"ioredis": "^5.3.2",
"ioredis-mock": "^8.9.0",
"node-cron": "^3.0.3",
"node-fetch": "^3.3.2",
"nostr-tools": "^1.17.0",
"pino": "^8.17.1",
"pino-http": "^8.6.0",
Expand Down
122 changes: 59 additions & 63 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 11 additions & 3 deletions src/app.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import express, { json } from "express";
import getRedisClient from "./getRedisClient.js";
import routes from "./routes.js";
import logger from "./logger.js";
import cron from "node-cron";
import pinoHTTP from "pino-http";
import promClient from "prom-client";
import promBundle from "express-prom-bundle";
import cors from "cors";
import getRedisClient from "./getRedisClient.js";
import routes from "./routes.js";
import logger from "./logger.js";
import NameRecordRepository from "./nameRecordRepository.js";
import fetchAndSendLatestEntries from "./slackNotifier.js";
import config from "../config/index.js";

const redisClient = await getRedisClient();
const nameRecordRepository = new NameRecordRepository(redisClient);
Expand Down Expand Up @@ -48,4 +51,9 @@ app.use((err, req, res, next) => {
res.status(status).json({ error: message });
});

cron.schedule(config.slackCron, async () => {
logger.info("Checking for new entries to send to Slack...");
await fetchAndSendLatestEntries(nameRecordRepository);
});

export default app;
4 changes: 2 additions & 2 deletions src/nameRecord.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default class NameRecord {
relays = [],
clientIp = "",
userAgent = "",
updated_at
updatedAt
) {
validateName(name);

Expand All @@ -15,7 +15,7 @@ export default class NameRecord {
this.relays = relays;
this.clientIp = clientIp;
this.userAgent = userAgent;
this.updated_at = updated_at;
this.updatedAt = updatedAt;
}
}

Expand Down
17 changes: 15 additions & 2 deletions src/nameRecordRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,24 @@ export default class NameRecordRepository {
}

async findLatest(limit = 10) {
const names = await this.redis.zrevrange("nameRecordUpdates", 0, limit - 1);
const names = await this.redis.zrevrange(
"name_record_updates",
0,
limit - 1
);
const records = await Promise.all(
names.map((name) => this.findByName(name))
);

return records; // These are sorted by updated_at due to the sorted set's ordering
return records;
}

async setLastSentEntryTimestamp(timestamp) {
await this.redis.set("lastSentEntryTimestamp", timestamp);
}

async getLastSentEntryTimestamp() {
const timestamp = await this.redis.get("lastSentEntryTimestamp");
return timestamp ? parseInt(timestamp, 10) : null;
}
}
Loading

0 comments on commit 604296b

Please sign in to comment.