mirror of
https://github.com/fiatjaf/nak.git
synced 2025-12-09 09:08:50 +00:00
Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d899a92f15 | ||
|
|
1c058f2846 | ||
|
|
4b4d9ec155 | ||
|
|
3031568266 |
8
.github/workflows/release-cli.yml
vendored
8
.github/workflows/release-cli.yml
vendored
@@ -25,7 +25,7 @@ jobs:
|
|||||||
strategy:
|
strategy:
|
||||||
matrix:
|
matrix:
|
||||||
goos: [linux, freebsd, darwin, windows]
|
goos: [linux, freebsd, darwin, windows]
|
||||||
goarch: [arm, amd64, arm64, riscv64]
|
goarch: [amd64, arm64, riscv64]
|
||||||
exclude:
|
exclude:
|
||||||
- goarch: arm64
|
- goarch: arm64
|
||||||
goos: windows
|
goos: windows
|
||||||
@@ -33,11 +33,7 @@ jobs:
|
|||||||
goos: windows
|
goos: windows
|
||||||
- goarch: riscv64
|
- goarch: riscv64
|
||||||
goos: darwin
|
goos: darwin
|
||||||
- goarch: arm
|
- goarch: arm64
|
||||||
goos: windows
|
|
||||||
- goarch: arm
|
|
||||||
goos: darwin
|
|
||||||
- goarch: arm
|
|
||||||
goos: freebsd
|
goos: freebsd
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v3
|
- uses: actions/checkout@v3
|
||||||
|
|||||||
7
go.mod
7
go.mod
@@ -1,8 +1,6 @@
|
|||||||
module github.com/fiatjaf/nak
|
module github.com/fiatjaf/nak
|
||||||
|
|
||||||
go 1.23.3
|
go 1.24.1
|
||||||
|
|
||||||
toolchain go1.23.4
|
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/bep/debounce v1.2.1
|
github.com/bep/debounce v1.2.1
|
||||||
@@ -14,10 +12,11 @@ require (
|
|||||||
github.com/fiatjaf/khatru v0.16.0
|
github.com/fiatjaf/khatru v0.16.0
|
||||||
github.com/hanwen/go-fuse/v2 v2.7.2
|
github.com/hanwen/go-fuse/v2 v2.7.2
|
||||||
github.com/json-iterator/go v1.1.12
|
github.com/json-iterator/go v1.1.12
|
||||||
|
github.com/liamg/magic v0.0.1
|
||||||
github.com/mailru/easyjson v0.9.0
|
github.com/mailru/easyjson v0.9.0
|
||||||
github.com/mark3labs/mcp-go v0.8.3
|
github.com/mark3labs/mcp-go v0.8.3
|
||||||
github.com/markusmobius/go-dateparser v1.2.3
|
github.com/markusmobius/go-dateparser v1.2.3
|
||||||
github.com/nbd-wtf/go-nostr v0.51.0
|
github.com/nbd-wtf/go-nostr v0.51.2
|
||||||
github.com/urfave/cli/v3 v3.0.0-beta1
|
github.com/urfave/cli/v3 v3.0.0-beta1
|
||||||
golang.org/x/exp v0.0.0-20250210185358-939b2ce775ac
|
golang.org/x/exp v0.0.0-20250210185358-939b2ce775ac
|
||||||
)
|
)
|
||||||
|
|||||||
10
go.sum
10
go.sum
@@ -96,6 +96,8 @@ github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEW
|
|||||||
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
|
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
|
||||||
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
|
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
|
||||||
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||||
|
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
||||||
|
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||||
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||||
@@ -127,6 +129,8 @@ github.com/klauspost/cpuid/v2 v2.2.10/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQe
|
|||||||
github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M=
|
github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M=
|
||||||
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
|
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
|
||||||
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
|
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
|
||||||
|
github.com/liamg/magic v0.0.1 h1:Ru22ElY+sCh6RvRTWjQzKKCxsEco8hE0co8n1qe7TBM=
|
||||||
|
github.com/liamg/magic v0.0.1/go.mod h1:yQkOmZZI52EA+SQ2xyHpVw8fNvTBruF873Y+Vt6S+fk=
|
||||||
github.com/magefile/mage v1.14.0 h1:6QDX3g6z1YvJ4olPhT1wksUcSa/V0a1B+pJb73fBjyo=
|
github.com/magefile/mage v1.14.0 h1:6QDX3g6z1YvJ4olPhT1wksUcSa/V0a1B+pJb73fBjyo=
|
||||||
github.com/magefile/mage v1.14.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A=
|
github.com/magefile/mage v1.14.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A=
|
||||||
github.com/mailru/easyjson v0.9.0 h1:PrnmzHw7262yW8sTBwxi1PdJA3Iw/EKBa8psRf7d9a4=
|
github.com/mailru/easyjson v0.9.0 h1:PrnmzHw7262yW8sTBwxi1PdJA3Iw/EKBa8psRf7d9a4=
|
||||||
@@ -147,8 +151,8 @@ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w
|
|||||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||||
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
|
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
|
||||||
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
|
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
|
||||||
github.com/nbd-wtf/go-nostr v0.51.0 h1:Z6gir3lQmlbQGYkccEPbvHlfCydMWXD6bIqukR4DZqU=
|
github.com/nbd-wtf/go-nostr v0.51.2 h1:wQysG8omkF4LO7kcU6yoeCBBxD92SwUNab4TMeSuZZM=
|
||||||
github.com/nbd-wtf/go-nostr v0.51.0/go.mod h1:9PcGOZ+e1VOaLvcK0peT4dbip+/eS+eTWXR3HuexQrA=
|
github.com/nbd-wtf/go-nostr v0.51.2/go.mod h1:9PcGOZ+e1VOaLvcK0peT4dbip+/eS+eTWXR3HuexQrA=
|
||||||
github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=
|
github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=
|
||||||
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
|
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
|
||||||
github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
|
github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
|
||||||
@@ -258,4 +262,6 @@ gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
|||||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo=
|
||||||
|
gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=
|
||||||
nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50=
|
nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50=
|
||||||
|
|||||||
214
nostrfs/entitydir.go
Normal file
214
nostrfs/entitydir.go
Normal file
@@ -0,0 +1,214 @@
|
|||||||
|
package nostrfs
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"path/filepath"
|
||||||
|
"strconv"
|
||||||
|
"syscall"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"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/nip27"
|
||||||
|
"github.com/nbd-wtf/go-nostr/nip92"
|
||||||
|
sdk "github.com/nbd-wtf/go-nostr/sdk"
|
||||||
|
)
|
||||||
|
|
||||||
|
type EntityDir struct {
|
||||||
|
fs.Inode
|
||||||
|
ctx context.Context
|
||||||
|
wd string
|
||||||
|
evt *nostr.Event
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ = (fs.NodeGetattrer)((*EntityDir)(nil))
|
||||||
|
|
||||||
|
func (e *EntityDir) Getattr(ctx context.Context, f fs.FileHandle, out *fuse.AttrOut) syscall.Errno {
|
||||||
|
publishedAt := uint64(e.evt.CreatedAt)
|
||||||
|
out.Ctime = publishedAt
|
||||||
|
|
||||||
|
if tag := e.evt.Tags.Find("published_at"); tag != nil {
|
||||||
|
publishedAt, _ = strconv.ParseUint(tag[1], 10, 64)
|
||||||
|
}
|
||||||
|
out.Mtime = publishedAt
|
||||||
|
|
||||||
|
return fs.OK
|
||||||
|
}
|
||||||
|
|
||||||
|
func FetchAndCreateEntityDir(
|
||||||
|
ctx context.Context,
|
||||||
|
parent fs.InodeEmbedder,
|
||||||
|
wd string,
|
||||||
|
extension string,
|
||||||
|
sys *sdk.System,
|
||||||
|
pointer nostr.EntityPointer,
|
||||||
|
) (*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 CreateEntityDir(ctx, parent, wd, extension, event), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func CreateEntityDir(
|
||||||
|
ctx context.Context,
|
||||||
|
parent fs.InodeEmbedder,
|
||||||
|
wd string,
|
||||||
|
extension string,
|
||||||
|
event *nostr.Event,
|
||||||
|
) *fs.Inode {
|
||||||
|
h := parent.EmbeddedInode().NewPersistentInode(
|
||||||
|
ctx,
|
||||||
|
&EntityDir{ctx: ctx, wd: wd, evt: event},
|
||||||
|
fs.StableAttr{Mode: syscall.S_IFDIR, Ino: hexToUint64(event.ID)},
|
||||||
|
)
|
||||||
|
|
||||||
|
var publishedAt uint64
|
||||||
|
if tag := event.Tags.Find("published_at"); tag != nil {
|
||||||
|
publishedAt, _ = strconv.ParseUint(tag[1], 10, 64)
|
||||||
|
}
|
||||||
|
|
||||||
|
npub, _ := nip19.EncodePublicKey(event.PubKey)
|
||||||
|
h.AddChild("@author", h.NewPersistentInode(
|
||||||
|
ctx,
|
||||||
|
&fs.MemSymlink{
|
||||||
|
Data: []byte(wd + "/" + npub),
|
||||||
|
},
|
||||||
|
fs.StableAttr{Mode: syscall.S_IFLNK},
|
||||||
|
), true)
|
||||||
|
|
||||||
|
eventj, _ := json.MarshalIndent(event, "", " ")
|
||||||
|
h.AddChild("event.json", h.NewPersistentInode(
|
||||||
|
ctx,
|
||||||
|
&fs.MemRegularFile{
|
||||||
|
Data: eventj,
|
||||||
|
Attr: fuse.Attr{
|
||||||
|
Mode: 0444,
|
||||||
|
Ctime: uint64(event.CreatedAt),
|
||||||
|
Mtime: uint64(publishedAt),
|
||||||
|
Size: uint64(len(event.Content)),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
fs.StableAttr{},
|
||||||
|
), true)
|
||||||
|
|
||||||
|
h.AddChild("identifier", h.NewPersistentInode(
|
||||||
|
ctx,
|
||||||
|
&fs.MemRegularFile{
|
||||||
|
Data: []byte(event.Tags.GetD()),
|
||||||
|
Attr: fuse.Attr{
|
||||||
|
Mode: 0444,
|
||||||
|
Ctime: uint64(event.CreatedAt),
|
||||||
|
Mtime: uint64(publishedAt),
|
||||||
|
Size: uint64(len(event.Tags.GetD())),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
fs.StableAttr{},
|
||||||
|
), true)
|
||||||
|
|
||||||
|
if tag := event.Tags.Find("title"); tag != nil {
|
||||||
|
h.AddChild("title", h.NewPersistentInode(
|
||||||
|
ctx,
|
||||||
|
&fs.MemRegularFile{
|
||||||
|
Data: []byte(tag[1]),
|
||||||
|
Attr: fuse.Attr{
|
||||||
|
Mode: 0444,
|
||||||
|
Ctime: uint64(event.CreatedAt),
|
||||||
|
Mtime: uint64(publishedAt),
|
||||||
|
Size: uint64(len(tag[1])),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
fs.StableAttr{},
|
||||||
|
), true)
|
||||||
|
}
|
||||||
|
|
||||||
|
h.AddChild("content"+extension, h.NewPersistentInode(
|
||||||
|
ctx,
|
||||||
|
&fs.MemRegularFile{
|
||||||
|
Data: []byte(event.Content),
|
||||||
|
Attr: fuse.Attr{
|
||||||
|
Mode: 0444,
|
||||||
|
Ctime: uint64(event.CreatedAt),
|
||||||
|
Mtime: uint64(publishedAt),
|
||||||
|
Size: uint64(len(event.Content)),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
fs.StableAttr{},
|
||||||
|
), true)
|
||||||
|
|
||||||
|
var refsdir *fs.Inode
|
||||||
|
i := 0
|
||||||
|
for ref := range nip27.ParseReferences(*event) {
|
||||||
|
i++
|
||||||
|
if refsdir == nil {
|
||||||
|
refsdir = h.NewPersistentInode(ctx, &fs.Inode{}, fs.StableAttr{Mode: syscall.S_IFDIR})
|
||||||
|
h.AddChild("references", refsdir, true)
|
||||||
|
}
|
||||||
|
refsdir.AddChild(fmt.Sprintf("ref_%02d", i), refsdir.NewPersistentInode(
|
||||||
|
ctx,
|
||||||
|
&fs.MemSymlink{
|
||||||
|
Data: []byte(wd + "/" + nip19.EncodePointer(ref.Pointer)),
|
||||||
|
},
|
||||||
|
fs.StableAttr{Mode: syscall.S_IFLNK},
|
||||||
|
), true)
|
||||||
|
}
|
||||||
|
|
||||||
|
var imagesdir *fs.Inode
|
||||||
|
addImage := func(url string) {
|
||||||
|
if imagesdir == nil {
|
||||||
|
in := &fs.Inode{}
|
||||||
|
imagesdir = h.NewPersistentInode(ctx, in, fs.StableAttr{Mode: syscall.S_IFDIR})
|
||||||
|
h.AddChild("images", imagesdir, true)
|
||||||
|
}
|
||||||
|
imagesdir.AddChild(filepath.Base(url), imagesdir.NewPersistentInode(
|
||||||
|
ctx,
|
||||||
|
&AsyncFile{
|
||||||
|
ctx: ctx,
|
||||||
|
load: func() ([]byte, nostr.Timestamp) {
|
||||||
|
ctx, cancel := context.WithTimeout(ctx, time.Second*20)
|
||||||
|
defer cancel()
|
||||||
|
r, err := http.NewRequestWithContext(ctx, "GET", url, nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, 0
|
||||||
|
}
|
||||||
|
resp, err := http.DefaultClient.Do(r)
|
||||||
|
if err != nil {
|
||||||
|
return nil, 0
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
if resp.StatusCode >= 300 {
|
||||||
|
return nil, 0
|
||||||
|
}
|
||||||
|
w := &bytes.Buffer{}
|
||||||
|
io.Copy(w, resp.Body)
|
||||||
|
return w.Bytes(), 0
|
||||||
|
},
|
||||||
|
},
|
||||||
|
fs.StableAttr{},
|
||||||
|
), true)
|
||||||
|
}
|
||||||
|
|
||||||
|
images := nip92.ParseTags(event.Tags)
|
||||||
|
for _, imeta := range images {
|
||||||
|
if imeta.URL == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
addImage(imeta.URL)
|
||||||
|
}
|
||||||
|
|
||||||
|
if tag := event.Tags.Find("image"); tag != nil {
|
||||||
|
addImage(tag[1])
|
||||||
|
}
|
||||||
|
|
||||||
|
return h
|
||||||
|
}
|
||||||
@@ -3,6 +3,7 @@ package nostrfs
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -12,7 +13,6 @@ import (
|
|||||||
|
|
||||||
"github.com/hanwen/go-fuse/v2/fs"
|
"github.com/hanwen/go-fuse/v2/fs"
|
||||||
"github.com/hanwen/go-fuse/v2/fuse"
|
"github.com/hanwen/go-fuse/v2/fuse"
|
||||||
"github.com/mailru/easyjson"
|
|
||||||
"github.com/nbd-wtf/go-nostr"
|
"github.com/nbd-wtf/go-nostr"
|
||||||
"github.com/nbd-wtf/go-nostr/nip10"
|
"github.com/nbd-wtf/go-nostr/nip10"
|
||||||
"github.com/nbd-wtf/go-nostr/nip19"
|
"github.com/nbd-wtf/go-nostr/nip19"
|
||||||
@@ -74,7 +74,7 @@ func CreateEventDir(
|
|||||||
fs.StableAttr{Mode: syscall.S_IFLNK},
|
fs.StableAttr{Mode: syscall.S_IFLNK},
|
||||||
), true)
|
), true)
|
||||||
|
|
||||||
eventj, _ := easyjson.Marshal(event)
|
eventj, _ := json.MarshalIndent(event, "", " ")
|
||||||
h.AddChild("event.json", h.NewPersistentInode(
|
h.AddChild("event.json", h.NewPersistentInode(
|
||||||
ctx,
|
ctx,
|
||||||
&fs.MemRegularFile{
|
&fs.MemRegularFile{
|
||||||
@@ -97,7 +97,7 @@ func CreateEventDir(
|
|||||||
Mode: 0444,
|
Mode: 0444,
|
||||||
Ctime: uint64(event.CreatedAt),
|
Ctime: uint64(event.CreatedAt),
|
||||||
Mtime: uint64(event.CreatedAt),
|
Mtime: uint64(event.CreatedAt),
|
||||||
Size: uint64(len(event.Content)),
|
Size: uint64(64),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
fs.StableAttr{},
|
fs.StableAttr{},
|
||||||
|
|||||||
@@ -1,13 +1,18 @@
|
|||||||
package nostrfs
|
package nostrfs
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/hanwen/go-fuse/v2/fs"
|
"github.com/hanwen/go-fuse/v2/fs"
|
||||||
"github.com/hanwen/go-fuse/v2/fuse"
|
"github.com/hanwen/go-fuse/v2/fuse"
|
||||||
|
"github.com/liamg/magic"
|
||||||
"github.com/nbd-wtf/go-nostr"
|
"github.com/nbd-wtf/go-nostr"
|
||||||
sdk "github.com/nbd-wtf/go-nostr/sdk"
|
sdk "github.com/nbd-wtf/go-nostr/sdk"
|
||||||
)
|
)
|
||||||
@@ -42,6 +47,61 @@ func CreateNpubDir(
|
|||||||
fs.StableAttr{},
|
fs.StableAttr{},
|
||||||
), true)
|
), true)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
pm := sys.FetchProfileMetadata(ctx, pointer.PublicKey)
|
||||||
|
if pm.Event == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
metadataj, _ := json.MarshalIndent(pm, "", " ")
|
||||||
|
h.AddChild(
|
||||||
|
"metadata.json",
|
||||||
|
h.NewPersistentInode(
|
||||||
|
ctx,
|
||||||
|
&fs.MemRegularFile{
|
||||||
|
Data: metadataj,
|
||||||
|
Attr: fuse.Attr{
|
||||||
|
Mtime: uint64(pm.Event.CreatedAt),
|
||||||
|
Mode: 0444,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
fs.StableAttr{},
|
||||||
|
),
|
||||||
|
true,
|
||||||
|
)
|
||||||
|
|
||||||
|
ctx, cancel := context.WithTimeout(ctx, time.Second*20)
|
||||||
|
defer cancel()
|
||||||
|
r, err := http.NewRequestWithContext(ctx, "GET", pm.Picture, nil)
|
||||||
|
if err == nil {
|
||||||
|
resp, err := http.DefaultClient.Do(r)
|
||||||
|
if err == nil {
|
||||||
|
defer resp.Body.Close()
|
||||||
|
if resp.StatusCode < 300 {
|
||||||
|
b := &bytes.Buffer{}
|
||||||
|
io.Copy(b, resp.Body)
|
||||||
|
|
||||||
|
ext := "png"
|
||||||
|
if ft, err := magic.Lookup(b.Bytes()); err == nil {
|
||||||
|
ext = ft.Extension
|
||||||
|
}
|
||||||
|
|
||||||
|
h.AddChild("picture."+ext, h.NewPersistentInode(
|
||||||
|
ctx,
|
||||||
|
&fs.MemRegularFile{
|
||||||
|
Data: b.Bytes(),
|
||||||
|
Attr: fuse.Attr{
|
||||||
|
Mtime: uint64(pm.Event.CreatedAt),
|
||||||
|
Mode: 0444,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
fs.StableAttr{},
|
||||||
|
), true)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
h.AddChild(
|
h.AddChild(
|
||||||
"notes",
|
"notes",
|
||||||
h.NewPersistentInode(
|
h.NewPersistentInode(
|
||||||
@@ -54,7 +114,11 @@ func CreateNpubDir(
|
|||||||
Kinds: []int{1},
|
Kinds: []int{1},
|
||||||
Authors: []string{pointer.PublicKey},
|
Authors: []string{pointer.PublicKey},
|
||||||
},
|
},
|
||||||
relays: relays,
|
paginate: true,
|
||||||
|
relays: relays,
|
||||||
|
create: func(ctx context.Context, n *ViewDir, event *nostr.Event) (string, *fs.Inode) {
|
||||||
|
return event.ID, CreateEventDir(ctx, n, n.wd, event)
|
||||||
|
},
|
||||||
},
|
},
|
||||||
fs.StableAttr{Mode: syscall.S_IFDIR},
|
fs.StableAttr{Mode: syscall.S_IFDIR},
|
||||||
),
|
),
|
||||||
@@ -73,7 +137,11 @@ func CreateNpubDir(
|
|||||||
Kinds: []int{1111},
|
Kinds: []int{1111},
|
||||||
Authors: []string{pointer.PublicKey},
|
Authors: []string{pointer.PublicKey},
|
||||||
},
|
},
|
||||||
relays: relays,
|
paginate: true,
|
||||||
|
relays: relays,
|
||||||
|
create: func(ctx context.Context, n *ViewDir, event *nostr.Event) (string, *fs.Inode) {
|
||||||
|
return event.ID, CreateEventDir(ctx, n, n.wd, event)
|
||||||
|
},
|
||||||
},
|
},
|
||||||
fs.StableAttr{Mode: syscall.S_IFDIR},
|
fs.StableAttr{Mode: syscall.S_IFDIR},
|
||||||
),
|
),
|
||||||
@@ -92,7 +160,11 @@ func CreateNpubDir(
|
|||||||
Kinds: []int{20},
|
Kinds: []int{20},
|
||||||
Authors: []string{pointer.PublicKey},
|
Authors: []string{pointer.PublicKey},
|
||||||
},
|
},
|
||||||
relays: relays,
|
paginate: true,
|
||||||
|
relays: relays,
|
||||||
|
create: func(ctx context.Context, n *ViewDir, event *nostr.Event) (string, *fs.Inode) {
|
||||||
|
return event.ID, CreateEventDir(ctx, n, n.wd, event)
|
||||||
|
},
|
||||||
},
|
},
|
||||||
fs.StableAttr{Mode: syscall.S_IFDIR},
|
fs.StableAttr{Mode: syscall.S_IFDIR},
|
||||||
),
|
),
|
||||||
@@ -111,7 +183,11 @@ func CreateNpubDir(
|
|||||||
Kinds: []int{21, 22},
|
Kinds: []int{21, 22},
|
||||||
Authors: []string{pointer.PublicKey},
|
Authors: []string{pointer.PublicKey},
|
||||||
},
|
},
|
||||||
relays: relays,
|
paginate: true,
|
||||||
|
relays: relays,
|
||||||
|
create: func(ctx context.Context, n *ViewDir, event *nostr.Event) (string, *fs.Inode) {
|
||||||
|
return event.ID, CreateEventDir(ctx, n, n.wd, event)
|
||||||
|
},
|
||||||
},
|
},
|
||||||
fs.StableAttr{Mode: syscall.S_IFDIR},
|
fs.StableAttr{Mode: syscall.S_IFDIR},
|
||||||
),
|
),
|
||||||
@@ -130,7 +206,11 @@ func CreateNpubDir(
|
|||||||
Kinds: []int{9802},
|
Kinds: []int{9802},
|
||||||
Authors: []string{pointer.PublicKey},
|
Authors: []string{pointer.PublicKey},
|
||||||
},
|
},
|
||||||
relays: relays,
|
paginate: true,
|
||||||
|
relays: relays,
|
||||||
|
create: func(ctx context.Context, n *ViewDir, event *nostr.Event) (string, *fs.Inode) {
|
||||||
|
return event.ID, CreateEventDir(ctx, n, n.wd, event)
|
||||||
|
},
|
||||||
},
|
},
|
||||||
fs.StableAttr{Mode: syscall.S_IFDIR},
|
fs.StableAttr{Mode: syscall.S_IFDIR},
|
||||||
),
|
),
|
||||||
@@ -138,22 +218,55 @@ func CreateNpubDir(
|
|||||||
)
|
)
|
||||||
|
|
||||||
h.AddChild(
|
h.AddChild(
|
||||||
"metadata.json",
|
"articles",
|
||||||
h.NewPersistentInode(
|
h.NewPersistentInode(
|
||||||
ctx,
|
ctx,
|
||||||
&AsyncFile{
|
&ViewDir{
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
load: func() ([]byte, nostr.Timestamp) {
|
sys: sys,
|
||||||
pm := sys.FetchProfileMetadata(ctx, pointer.PublicKey)
|
wd: wd,
|
||||||
jsonb, _ := json.MarshalIndent(pm.Event, "", " ")
|
filter: nostr.Filter{
|
||||||
var ts nostr.Timestamp
|
Kinds: []int{30023},
|
||||||
if pm.Event != nil {
|
Authors: []string{pointer.PublicKey},
|
||||||
ts = pm.Event.CreatedAt
|
},
|
||||||
|
paginate: false,
|
||||||
|
relays: relays,
|
||||||
|
create: func(ctx context.Context, n *ViewDir, event *nostr.Event) (string, *fs.Inode) {
|
||||||
|
d := event.Tags.GetD()
|
||||||
|
if d == "" {
|
||||||
|
d = "_"
|
||||||
}
|
}
|
||||||
return jsonb, ts
|
return d, CreateEntityDir(ctx, n, n.wd, ".md", event)
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
fs.StableAttr{},
|
fs.StableAttr{Mode: syscall.S_IFDIR},
|
||||||
|
),
|
||||||
|
true,
|
||||||
|
)
|
||||||
|
|
||||||
|
h.AddChild(
|
||||||
|
"wiki",
|
||||||
|
h.NewPersistentInode(
|
||||||
|
ctx,
|
||||||
|
&ViewDir{
|
||||||
|
ctx: ctx,
|
||||||
|
sys: sys,
|
||||||
|
wd: wd,
|
||||||
|
filter: nostr.Filter{
|
||||||
|
Kinds: []int{30818},
|
||||||
|
Authors: []string{pointer.PublicKey},
|
||||||
|
},
|
||||||
|
paginate: false,
|
||||||
|
relays: relays,
|
||||||
|
create: func(ctx context.Context, n *ViewDir, event *nostr.Event) (string, *fs.Inode) {
|
||||||
|
d := event.Tags.GetD()
|
||||||
|
if d == "" {
|
||||||
|
d = "_"
|
||||||
|
}
|
||||||
|
return d, CreateEntityDir(ctx, n, n.wd, ".adoc", event)
|
||||||
|
},
|
||||||
|
},
|
||||||
|
fs.StableAttr{Mode: syscall.S_IFDIR},
|
||||||
),
|
),
|
||||||
true,
|
true,
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -13,12 +13,14 @@ import (
|
|||||||
|
|
||||||
type ViewDir struct {
|
type ViewDir struct {
|
||||||
fs.Inode
|
fs.Inode
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
sys *sdk.System
|
sys *sdk.System
|
||||||
wd string
|
wd string
|
||||||
fetched atomic.Bool
|
fetched atomic.Bool
|
||||||
filter nostr.Filter
|
filter nostr.Filter
|
||||||
relays []string
|
paginate bool
|
||||||
|
relays []string
|
||||||
|
create func(context.Context, *ViewDir, *nostr.Event) (string, *fs.Inode)
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -33,6 +35,7 @@ func (n *ViewDir) Getattr(ctx context.Context, f fs.FileHandle, out *fuse.AttrOu
|
|||||||
}
|
}
|
||||||
aMonthAgo := now - 30*24*60*60
|
aMonthAgo := now - 30*24*60*60
|
||||||
out.Mtime = uint64(aMonthAgo)
|
out.Mtime = uint64(aMonthAgo)
|
||||||
|
|
||||||
return fs.OK
|
return fs.OK
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -41,32 +44,39 @@ func (n *ViewDir) Opendir(ctx context.Context) syscall.Errno {
|
|||||||
return fs.OK
|
return fs.OK
|
||||||
}
|
}
|
||||||
|
|
||||||
now := nostr.Now()
|
if n.paginate {
|
||||||
if n.filter.Until != nil {
|
now := nostr.Now()
|
||||||
now = *n.filter.Until
|
if n.filter.Until != nil {
|
||||||
|
now = *n.filter.Until
|
||||||
|
}
|
||||||
|
aMonthAgo := now - 30*24*60*60
|
||||||
|
n.filter.Since = &aMonthAgo
|
||||||
|
|
||||||
|
for ie := range n.sys.Pool.FetchMany(ctx, n.relays, n.filter, nostr.WithLabel("nakfs")) {
|
||||||
|
basename, inode := n.create(ctx, n, ie.Event)
|
||||||
|
n.AddChild(basename, inode, true)
|
||||||
|
}
|
||||||
|
|
||||||
|
filter := n.filter
|
||||||
|
filter.Until = &aMonthAgo
|
||||||
|
|
||||||
|
n.AddChild("@previous", n.NewPersistentInode(
|
||||||
|
ctx,
|
||||||
|
&ViewDir{
|
||||||
|
ctx: n.ctx,
|
||||||
|
sys: n.sys,
|
||||||
|
filter: filter,
|
||||||
|
wd: n.wd,
|
||||||
|
relays: n.relays,
|
||||||
|
},
|
||||||
|
fs.StableAttr{Mode: syscall.S_IFDIR},
|
||||||
|
), true)
|
||||||
|
} else {
|
||||||
|
for ie := range n.sys.Pool.FetchMany(ctx, n.relays, n.filter, nostr.WithLabel("nakfs")) {
|
||||||
|
basename, inode := n.create(ctx, n, ie.Event)
|
||||||
|
n.AddChild(basename, inode, true)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
aMonthAgo := now - 30*24*60*60
|
|
||||||
n.filter.Since = &aMonthAgo
|
|
||||||
|
|
||||||
for ie := range n.sys.Pool.FetchMany(ctx, n.relays, n.filter, nostr.WithLabel("nakfs")) {
|
|
||||||
e := CreateEventDir(ctx, n, n.wd, ie.Event)
|
|
||||||
n.AddChild(ie.Event.ID, e, true)
|
|
||||||
}
|
|
||||||
|
|
||||||
filter := n.filter
|
|
||||||
filter.Until = &aMonthAgo
|
|
||||||
|
|
||||||
n.AddChild("@previous", n.NewPersistentInode(
|
|
||||||
ctx,
|
|
||||||
&ViewDir{
|
|
||||||
ctx: n.ctx,
|
|
||||||
sys: n.sys,
|
|
||||||
filter: filter,
|
|
||||||
wd: n.wd,
|
|
||||||
relays: n.relays,
|
|
||||||
},
|
|
||||||
fs.StableAttr{Mode: syscall.S_IFDIR},
|
|
||||||
), true)
|
|
||||||
|
|
||||||
return fs.OK
|
return fs.OK
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user