fs: something that makes more sense.

This commit is contained in:
fiatjaf
2025-03-08 12:52:05 -03:00
parent d6a23bd00c
commit 3d961d4bec
8 changed files with 208 additions and 151 deletions

68
nostrfs/eventdir.go Normal file
View File

@@ -0,0 +1,68 @@
package nostrfs
import (
"context"
"fmt"
"syscall"
"github.com/hanwen/go-fuse/v2/fs"
"github.com/hanwen/go-fuse/v2/fuse"
"github.com/mailru/easyjson"
"github.com/nbd-wtf/go-nostr"
sdk "github.com/nbd-wtf/go-nostr/sdk"
)
type EventDir struct {
fs.Inode
ctx context.Context
evt *nostr.Event
}
func FetchAndCreateEventDir(
ctx context.Context,
parent fs.InodeEmbedder,
sys *sdk.System,
pointer nostr.EventPointer,
) (*fs.Inode, error) {
event, _, err := sys.FetchSpecificEvent(ctx, pointer, sdk.FetchSpecificEventParameters{
WithRelays: false,
})
if err != nil {
return nil, fmt.Errorf("failed to fetch: %w", err)
}
return CreateEventDir(ctx, parent, event), nil
}
func CreateEventDir(
ctx context.Context,
parent fs.InodeEmbedder,
event *nostr.Event,
) *fs.Inode {
h := parent.EmbeddedInode().NewPersistentInode(
ctx,
&EventDir{ctx: ctx, evt: event},
fs.StableAttr{Mode: syscall.S_IFDIR},
)
eventj, _ := easyjson.Marshal(event)
h.AddChild("event.json", h.NewPersistentInode(
ctx,
&fs.MemRegularFile{
Data: eventj,
Attr: fuse.Attr{Mode: 0444},
},
fs.StableAttr{},
), true)
h.AddChild("content.txt", h.NewPersistentInode(
ctx,
&fs.MemRegularFile{
Data: []byte(event.Content),
Attr: fuse.Attr{Mode: 0444},
},
fs.StableAttr{},
), true)
return h
}

8
nostrfs/helpers.go Normal file
View File

@@ -0,0 +1,8 @@
package nostrfs
import "strconv"
func hexToUint64(hexStr string) uint64 {
v, _ := strconv.ParseUint(hexStr[0:16], 16, 64)
return v
}

46
nostrfs/npubdir.go Normal file
View File

@@ -0,0 +1,46 @@
package nostrfs
import (
"context"
"sync/atomic"
"syscall"
"github.com/hanwen/go-fuse/v2/fs"
"github.com/nbd-wtf/go-nostr"
sdk "github.com/nbd-wtf/go-nostr/sdk"
)
type NpubDir struct {
sys *sdk.System
fs.Inode
pointer nostr.ProfilePointer
ctx context.Context
fetched atomic.Bool
}
func CreateNpubDir(ctx context.Context, parent fs.InodeEmbedder, pointer nostr.ProfilePointer) *fs.Inode {
npubdir := &NpubDir{pointer: pointer}
return parent.EmbeddedInode().NewPersistentInode(
ctx,
npubdir,
fs.StableAttr{Mode: syscall.S_IFDIR},
)
}
var _ = (fs.NodeOpendirer)((*NpubDir)(nil))
func (n *NpubDir) Opendir(ctx context.Context) syscall.Errno {
if n.fetched.CompareAndSwap(true, true) {
return fs.OK
}
for ie := range n.sys.Pool.FetchMany(ctx, n.sys.FetchOutboxRelays(ctx, n.pointer.PublicKey, 2), nostr.Filter{
Kinds: []int{1},
Authors: []string{n.pointer.PublicKey},
}, nostr.WithLabel("nak-fs-feed")) {
e := CreateEventDir(ctx, n, ie.Event)
n.AddChild(ie.Event.ID, e, true)
}
return fs.OK
}

80
nostrfs/root.go Normal file
View File

@@ -0,0 +1,80 @@
package nostrfs
import (
"context"
"syscall"
"github.com/hanwen/go-fuse/v2/fs"
"github.com/hanwen/go-fuse/v2/fuse"
"github.com/nbd-wtf/go-nostr"
"github.com/nbd-wtf/go-nostr/nip19"
"github.com/nbd-wtf/go-nostr/sdk"
)
type NostrRoot struct {
sys *sdk.System
fs.Inode
rootPubKey string
signer nostr.Signer
ctx context.Context
}
var _ = (fs.NodeOnAdder)((*NostrRoot)(nil))
func NewNostrRoot(ctx context.Context, sys *sdk.System, user nostr.User) *NostrRoot {
pubkey, _ := user.GetPublicKey(ctx)
signer, _ := user.(nostr.Signer)
return &NostrRoot{
sys: sys,
ctx: ctx,
rootPubKey: pubkey,
signer: signer,
}
}
func (r *NostrRoot) OnAdd(context.Context) {
if r.rootPubKey == "" {
return
}
fl := r.sys.FetchFollowList(r.ctx, r.rootPubKey)
for _, f := range fl.Items {
h := r.NewPersistentInode(
r.ctx,
&NpubDir{sys: r.sys, pointer: nostr.ProfilePointer{PublicKey: f.Pubkey, Relays: []string{f.Relay}}},
fs.StableAttr{Mode: syscall.S_IFDIR},
)
npub, _ := nip19.EncodePublicKey(f.Pubkey)
r.AddChild(npub, h, true)
}
}
func (r *NostrRoot) Lookup(ctx context.Context, name string, out *fuse.EntryOut) (*fs.Inode, syscall.Errno) {
// check if we already have this npub
child := r.GetChild(name)
if child != nil {
return child, fs.OK
}
pointer, err := nip19.ToPointer(name)
if err != nil {
return nil, syscall.ENOENT
}
switch p := pointer.(type) {
case nostr.ProfilePointer:
npubdir := CreateNpubDir(ctx, r, p)
return npubdir, fs.OK
case nostr.EventPointer:
eventdir, err := FetchAndCreateEventDir(ctx, r, r.sys, p)
if err != nil {
return nil, syscall.ENOENT
}
return eventdir, fs.OK
default:
return nil, syscall.ENOENT
}
}