Skip to content

Commit

Permalink
Merge pull request #98 from lowercasename/rk/new-config
Browse files Browse the repository at this point in the history
TOML config library and migration from CommonJS to ES modules
  • Loading branch information
lowercasename authored May 13, 2023
2 parents f1a5a9b + 3653983 commit 3ec47e9
Show file tree
Hide file tree
Showing 19 changed files with 271 additions and 261 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@ jobs:
- name: Install dependencies
run: pnpm install
- name: Set up config files
run: |
cp src/config/api-example.js src/config/api.js
cp src/config/database-example.js src/config/database.js
cp src/config/domain-example.js src/config/domain.js
run: cp config/config.example.toml config/config.toml
- name: Build
run: pnpm build
continue-on-error: true
Expand Down
4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

# gathio custom

config/config.toml
dist
src/config/api.js
src/config/database.js
src/config/domain.js
public/events/*
!public/events/.gitkeep

Expand Down
33 changes: 33 additions & 0 deletions config/config.example.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
[general]
# Your domain goes here. If there is a port it should be 'domain.com:port', but
# otherwise just 'domain.com'.
domain = "localhost:3000"
port = "3000"
email = "[email protected]"
site_name = "gathio"
is_federated = true
# If left blank, this defaults to
# https://yourdomain.com/images/gathio-email-logo.gif. Set a full URL here to
# change it to your own logo (or just change the file itself).
email_logo_url = ""
# Show a Ko-Fi box to donate money to Raphael (Gathio's creator) on the front
# page.
show_kofi = false
# Which mail service to use to send emails to hosts and attendees. Options are
# 'nodemailer' or 'sendgrid'. Configure settings for this mail
# service below.
mail_service = "nodemailer"

[database]
# Set up for a locally running MongoDB connection. Change this to
# 'mongodb://mongo:27017/gathio' for a Dockerised connection.
mongodb_url = "mongodb://localhost:27017/gathio"

[nodemailer]
smtp_server = ""
smtp_port = ""
smtp_username = ""
smtp_password = ""

[sendgrid]
api_key = ""
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "1.3.0",
"description": "",
"main": "index.js",
"type": "module",
"scripts": {
"build": "tsc",
"start": "node dist/start.js",
Expand All @@ -19,7 +20,6 @@
"@sendgrid/mail": "^6.5.5",
"body-parser": "^1.20.2",
"cors": "^2.8.5",
"dotenv": "^6.2.0",
"express": "^4.18.2",
"express-fileupload": "^1.4.0",
"express-handlebars": "^6.0.7",
Expand All @@ -38,7 +38,8 @@
"nodemailer": "^6.9.2",
"randomstring": "^1.2.3",
"request": "^2.88.2",
"sanitize-html": "^2.10.0"
"sanitize-html": "^2.10.0",
"toml": "^3.0.0"
},
"devDependencies": {
"eslint": "^8.40.0",
Expand Down
15 changes: 7 additions & 8 deletions pnpm-lock.yaml

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

71 changes: 27 additions & 44 deletions src/activitypub.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
const domain = require("./config/domain.js").domain;
const contactEmail = require("./config/domain.js").email;
const siteName = require("./config/domain.js").sitename;
const isFederated = require("./config/domain.js").isFederated;
const request = require("request");
const addToLog = require("./helpers.js").addToLog;
const crypto = require("crypto");
import request from "request";
import { addToLog } from "./helpers.js";
import crypto from "crypto";
import { customAlphabet } from "nanoid";
import moment from "moment-timezone";
import sanitizeHtml from "sanitize-html";
import { getConfig } from "./lib/config.js";
const config = getConfig();
const domain = config.general.domain;
const siteName = config.general.site_name;
const isFederated = config.general.is_federated;
import Event from "./models/Event.js";

// This alphabet (used to generate all event, group, etc. IDs) is missing '-'
// because ActivityPub doesn't like it in IDs
const { customAlphabet } = require("nanoid");
const nanoid = customAlphabet(
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_",
21
);
var moment = require("moment-timezone");
const mongoose = require("mongoose");
const Event = mongoose.model("Event");
const EventGroup = mongoose.model("EventGroup");
var sanitizeHtml = require("sanitize-html");

function createActivityPubActor(
export function createActivityPubActor(
eventID,
domain,
pubkey,
Expand Down Expand Up @@ -70,7 +70,7 @@ function createActivityPubActor(
return JSON.stringify(actor);
}

function createActivityPubEvent(
export function createActivityPubEvent(
name,
startUTC,
endUTC,
Expand All @@ -92,7 +92,7 @@ function createActivityPubEvent(
return JSON.stringify(eventObject);
}

function createFeaturedPost(
export function createFeaturedPost(
eventID,
name,
startUTC,
Expand All @@ -113,7 +113,7 @@ function createFeaturedPost(
return featured;
}

function updateActivityPubEvent(
export function updateActivityPubEvent(
oldEvent,
name,
startUTC,
Expand All @@ -137,7 +137,7 @@ function updateActivityPubEvent(
return JSON.stringify(eventObject);
}

function updateActivityPubActor(
export function updateActivityPubActor(
actor,
description,
name,
Expand Down Expand Up @@ -168,7 +168,7 @@ function updateActivityPubActor(
return JSON.stringify(actor);
}

function signAndSend(message, eventID, targetDomain, inbox, callback) {
export function signAndSend(message, eventID, targetDomain, inbox, callback) {
if (!isFederated) return;
let inboxFragment = inbox.replace("https://" + targetDomain, "");
// get the private key
Expand Down Expand Up @@ -264,7 +264,7 @@ function signAndSend(message, eventID, targetDomain, inbox, callback) {
// this function sends something to the timeline of every follower in the followers array
// it's also an unlisted public message, meaning non-followers can see the message if they look at
// the profile but it doesn't spam federated timelines
function broadcastCreateMessage(apObject, followers, eventID) {
export function broadcastCreateMessage(apObject, followers, eventID) {
if (!isFederated) return;
let guidCreate = crypto.randomBytes(16).toString("hex");
Event.findOne(
Expand Down Expand Up @@ -325,7 +325,7 @@ function broadcastCreateMessage(apObject, followers, eventID) {
}

// sends an Announce for the apObject
function broadcastAnnounceMessage(apObject, followers, eventID) {
export function broadcastAnnounceMessage(apObject, followers, eventID) {
if (!isFederated) return;
let guidUpdate = crypto.randomBytes(16).toString("hex");
Event.findOne(
Expand Down Expand Up @@ -386,7 +386,7 @@ function broadcastAnnounceMessage(apObject, followers, eventID) {
}

// sends an Update for the apObject
function broadcastUpdateMessage(apObject, followers, eventID) {
export function broadcastUpdateMessage(apObject, followers, eventID) {
if (!isFederated) return;
let guidUpdate = crypto.randomBytes(16).toString("hex");
// iterate over followers
Expand Down Expand Up @@ -440,7 +440,7 @@ function broadcastUpdateMessage(apObject, followers, eventID) {
);
}

function broadcastDeleteMessage(apObject, followers, eventID, callback) {
export function broadcastDeleteMessage(apObject, followers, eventID, callback) {
callback = callback || function () {};
if (!isFederated) {
callback([]);
Expand Down Expand Up @@ -521,7 +521,7 @@ function broadcastDeleteMessage(apObject, followers, eventID, callback) {
}

// this sends a message "to:" an individual fediverse user
function sendDirectMessage(apObject, actorId, eventID, callback) {
export function sendDirectMessage(apObject, actorId, eventID, callback) {
if (!isFederated) return;
callback = callback || function () {};
const guidCreate = crypto.randomBytes(16).toString("hex");
Expand Down Expand Up @@ -567,7 +567,7 @@ function sendDirectMessage(apObject, actorId, eventID, callback) {
);
}

function sendAcceptMessage(thebody, eventID, targetDomain, callback) {
export function sendAcceptMessage(thebody, eventID, targetDomain, callback) {
if (!isFederated) return;
callback = callback || function () {};
const guid = crypto.randomBytes(16).toString("hex");
Expand Down Expand Up @@ -1185,7 +1185,7 @@ function _handleCreateNoteComment(req, res) {
} // end public message
}

function processInbox(req, res) {
export function processInbox(req, res) {
if (!isFederated) return res.sendStatus(404);
try {
// if a Follow activity hits the inbox
Expand Down Expand Up @@ -1251,7 +1251,7 @@ function processInbox(req, res) {
}
}

function createWebfinger(eventID, domain) {
export function createWebfinger(eventID, domain) {
return {
subject: `acct:${eventID}@${domain}`,

Expand All @@ -1264,20 +1264,3 @@ function createWebfinger(eventID, domain) {
],
};
}

module.exports = {
processInbox,
sendAcceptMessage,
sendDirectMessage,
broadcastAnnounceMessage,
broadcastUpdateMessage,
broadcastDeleteMessage,
broadcastCreateMessage,
signAndSend,
createActivityPubActor,
updateActivityPubActor,
createActivityPubEvent,
updateActivityPubEvent,
createFeaturedPost,
createWebfinger,
};
13 changes: 5 additions & 8 deletions src/app.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
const express = require("express");
const path = require("path");
const session = require("express-session");
const cors = require("cors");
const routes = require("./routes");
const hbs = require("express-handlebars");
const bodyParser = require("body-parser");
import express from "express";
import routes from "./routes.js";
import hbs from "express-handlebars";
import bodyParser from "body-parser";

const app = express();

Expand Down Expand Up @@ -50,4 +47,4 @@ app.use(bodyParser.json({ type: "application/activity+json" })); // support json
app.use(bodyParser.urlencoded({ extended: true }));
app.use("/", routes);

module.exports = app;
export default app;
8 changes: 0 additions & 8 deletions src/config/api-example.js

This file was deleted.

3 changes: 0 additions & 3 deletions src/config/database-docker.js

This file was deleted.

3 changes: 0 additions & 3 deletions src/config/database-example.js

This file was deleted.

13 changes: 0 additions & 13 deletions src/config/domain-example.js

This file was deleted.

13 changes: 0 additions & 13 deletions src/config/gathio.service

This file was deleted.

Loading

0 comments on commit 3ec47e9

Please sign in to comment.