relay29

package module
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 5, 2024 License: MIT Imports: 13 Imported by: 3

README

= relay29 image:https://pkg.go.dev/badge/github.com/fiatjaf/relay29.svg[link=https://pkg.go.dev/github.com/fiatjaf/relay29]

a library for creating NIP-29 relays, based on https://github.com/fiatjaf/khatru[khatru].

CAUTION: This is probably broken so please don't trust it for anything serious and be prepared to delete your database.

[source,go]
---
package main

import (
	"fmt"
	"log"
	"net/http"

	"github.com/fiatjaf/eventstore/slicestore"
	"github.com/fiatjaf/khatru/policies"
	"github.com/fiatjaf/relay29"
	"github.com/nbd-wtf/go-nostr"
	"golang.org/x/exp/slices"
)

func main() {
	relayPrivateKey := nostr.GeneratePrivateKey()

	state := relay29.Init(relay29.Options{
		Domain:    "localhost:2929",
		DB:        &slicestore.SliceStore{},
		SecretKey: relayPrivateKey,
	})

	// init relay
	state.Relay.Info.Name = "very ephemeral chat relay"
	state.Relay.Info.PubKey, _ = nostr.GetPublicKey(relayPrivateKey)
	state.Relay.Info.Description = "everything will be deleted as soon as I turn off my computer"

	// extra policies
	state.Relay.RejectEvent = slices.Insert(state.Relay.RejectEvent, 0,
		policies.PreventLargeTags(64),
		policies.PreventTooManyIndexableTags(6, []int{9005}, nil),
		policies.RestrictToSpecifiedKinds(
			9, 10, 11, 12,
			30023, 31922, 31923, 9802,
			9000, 9001, 9002, 9003, 9004, 9005, 9006, 9007,
			9021,
		),
		policies.PreventTimestampsInThePast(60),
		policies.PreventTimestampsInTheFuture(30),
	)

	// http routes
	state.Relay.Router().HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprint(w, "nothing to see here, you must use a nip-29 powered client")
	})

	fmt.Println("running on http://0.0.0.0:2929")
	if err := http.ListenAndServe(":2929", state.Relay); err != nil {
		log.Fatal("failed to serve")
	}
}
---

== How to use

Basically you just call `relay29.Init()` and then you get back a `relay29.State` object that has a normal `khatru.Relay` inside and and also a map of `Group` objects that you can read but you should not modify manually. To modify these groups you must write moderation events with the `.AddEvent()` method of the `Relay`. This API may be improved later.

See link:examples/groups.fiatjaf.com/main.go[] for a (not very much) more complex example.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetGroupIDFromEvent

func GetGroupIDFromEvent(event *nostr.Event) string

Types

type Group

type Group struct {
	nip29.Group
	// contains filtered or unexported fields
}

type Options

type Options struct {
	Domain    string
	DB        eventstore.Store
	SecretKey string
}

type State

type State struct {
	Relay  *khatru.Relay
	Domain string
	Groups *xsync.MapOf[string, *Group]
	DB     eventstore.Store
	// contains filtered or unexported fields
}

func Init

func Init(opts Options) *State

func (*State) GetGroupFromEvent

func (s *State) GetGroupFromEvent(event *nostr.Event) *Group

func (*State) NewGroup

func (s *State) NewGroup(id string) *Group

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL