From 311a9a1b2e60a89999b6fa99a149cf31d8b4d071 Mon Sep 17 00:00:00 2001 From: Guy Van Sanden Date: Mon, 6 Mar 2023 12:34:26 +0100 Subject: [PATCH] Initial version --- .gitignore | 2 ++ Gruntfile.js | 33 ++++++++++++++++++ README.md | 25 ++++++++++++++ scripts/package.sh | 5 +++ src/app.py | 82 ++++++++++++++++++++++++++++++++++++++++++++ src/config.yml | 4 +++ src/requirements.txt | 4 +++ 7 files changed, 155 insertions(+) create mode 100644 Gruntfile.js create mode 100644 scripts/package.sh create mode 100644 src/app.py create mode 100644 src/config.yml create mode 100644 src/requirements.txt diff --git a/.gitignore b/.gitignore index 5d381cc..83d5163 100644 --- a/.gitignore +++ b/.gitignore @@ -160,3 +160,5 @@ cython_debug/ # option (not recommended) you can uncomment the following to ignore the entire idea folder. #.idea/ +venv + diff --git a/Gruntfile.js b/Gruntfile.js new file mode 100644 index 0000000..ff60108 --- /dev/null +++ b/Gruntfile.js @@ -0,0 +1,33 @@ +module.exports = function(grunt) { + + // Project configuration. + grunt.initConfig({ + pkg: grunt.file.readJSON( 'package.json' ), + + // Tasks here + version: { + control: { + options: { + prefix: 'Version: ' + }, + src: [ 'control' ], + }, + python: { + options: { + prefix: '__version__\\s*=\\s*\"' + }, + src: [ 'src/*.py' ], + } + }, + }); + + // Load all grunt plugins here + grunt.loadNpmTasks('grunt-version'); + + // Bump version task + grunt.registerTask( 'bump', [ 'version' ]); + + // Release task + grunt.registerTask( 'release', [ 'version', 'gitcommit', 'gittag', 'gitpush' ]); + +}; diff --git a/README.md b/README.md index bc758e9..308ef33 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,27 @@ # pleroma2nip05 +## Install + +Create a python venv +`python3 -m venv venv` + +Activate the venv +`python3 -m venv venv` + +Install requirements +`pip3 install -r requirements` + +## Create the config file + +Create /etc/pleroma2nip05/config.yml from the config.yml in this repo + +## Configure nginx + +Add this to your Pleroma vhost + +` + location /.well-known/nostr.json { + proxy_pass http://localhost:5000/json; + } + +` diff --git a/scripts/package.sh b/scripts/package.sh new file mode 100644 index 0000000..c0c352b --- /dev/null +++ b/scripts/package.sh @@ -0,0 +1,5 @@ +#!/usr/bin/env bash + +rm -rf dist +mkdir dist +cd src && tar -czf ../dist/pleroma2nip05.tgz * diff --git a/src/app.py b/src/app.py new file mode 100644 index 0000000..4338ae2 --- /dev/null +++ b/src/app.py @@ -0,0 +1,82 @@ +#!/bin/env python3 + +# Pleroma2nip05: Server nip05 users for Pleroma users +# AGPL v3.0 or later licensed +# Copyright (C) Taurix IT 2023 + +import json +from flask import Flask, jsonify +import yaml +import psycopg2 +import psycopg2.extras + +__VERSION__ = '0.5' + +app = Flask('app') +print("Pleroma2nip05 v%s" % (__VERSION__)) + +with open(r'/etc/pleroma2nip05/config.yml') as configfile: + config = yaml.load(configfile, Loader=yaml.FullLoader) + +# https:///.well-known/nostr.json +# { +# "names": { +# "bob": "b0635d6a9851d3aed0cd6c495b282167acf761729078d975fc341b22650b07b9" +# }, +# "relays": { +# "b0635d6a9851d3aed0cd6c495b282167acf761729078d975fc341b22650b07b9": [ "wss://relay.example.com", "wss://relay2.example.com" ] +# } +# } + +# {"names":{"gvs":"eb2881406ad19ba7cf01c210ce002f4fe53e8ce6d84e77df5a2319f9f00a8005"}} + +try: + # Connect to an existing database + connection = psycopg2.connect(user=config.get('pguser'), + password=config.get('pgpass'), + host=config.get('pghost'), + database=config.get('pleromadb')) + + cursor = connection.cursor(cursor_factory = psycopg2.extras.RealDictCursor) + print("PostgreSQL server information") + print(connection.get_dsn_parameters(), "\n") + # cursor.execute("SELECT version();") + # # Fetch result + # record = cursor.fetchone() + # print("You are connected to - ", record, "\n") + +except (Exception, Error) as error: + print("Error while connecting to PostgreSQL", error) +# finally: +# if (connection): +# cursor.close() +# connection.close() +# print("PostgreSQL connection is closed") + + +# Get local users only +userquery = "SELECT nickname, fields FROM users WHERE nickname NOT LIKE '%@%';" +relays = [ "wss://relay.rebelbase.site" , "wss://nostr-pub.wellorder.net" , "wss://relay.orangepill.dev", "wss://relay.shitforce.one" ] + +@app.route("/json") +def get_json(): + cursor.execute(userquery) + localusers = cursor.fetchall() + + nostr = {} + nostr['names'] = {} + if localusers is not None: + for user in localusers: + if user['nickname'] == 'gvs': + if user.get('fields') is not None: + for field in user.get('fields'): + if field.get('name').lower() == 'nostr': + nostr['names'][user['nickname']] = field.get('value') + + + # nostr['names']['gvs'] = 'eb2881406ad19ba7cf01c210ce002f4fe53e8ce6d84e77df5a2319f9f00a8005' + nostr['relays'] = {} + nostr['relays']['eb2881406ad19ba7cf01c210ce002f4fe53e8ce6d84e77df5a2319f9f00a8005'] = relays + + print(json.dumps(nostr)) + return json.dumps(nostr) diff --git a/src/config.yml b/src/config.yml new file mode 100644 index 0000000..8353285 --- /dev/null +++ b/src/config.yml @@ -0,0 +1,4 @@ +pguser: pleroma +pgpass: +pghost: localhost +pleromadb: pleroma diff --git a/src/requirements.txt b/src/requirements.txt new file mode 100644 index 0000000..4e0d9d0 --- /dev/null +++ b/src/requirements.txt @@ -0,0 +1,4 @@ +flask==2.2.3 +pyjson==1.3.0 +psycopg2==2.9.5 +pyaml==21.10.1