Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement the nostr protocol #2946

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ require (
github.com/abadojack/whatlanggo v1.0.1
github.com/andybalholm/brotli v1.1.1
github.com/coreos/go-oidc/v3 v3.11.0
github.com/github-tijlxyz/goldmark-nostr v0.2.0
github.com/go-webauthn/webauthn v0.11.2
github.com/gorilla/mux v1.8.1
github.com/lib/pq v1.10.9
github.com/nbd-wtf/go-nostr v0.42.2
github.com/prometheus/client_golang v1.20.5
github.com/tdewolff/minify/v2 v2.21.1
github.com/yuin/goldmark v1.7.8
Expand All @@ -29,20 +31,40 @@ require (
require (
github.com/andybalholm/cascadia v1.3.2 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/btcsuite/btcd/btcec/v2 v2.3.4 // indirect
github.com/btcsuite/btcd/btcutil v1.1.3 // indirect
github.com/btcsuite/btcd/chaincfg/chainhash v1.1.0 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/decred/dcrd/crypto/blake256 v1.1.0 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0 // indirect
github.com/dgraph-io/ristretto v1.0.0 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/fiatjaf/eventstore v0.9.0 // indirect
github.com/fxamacker/cbor/v2 v2.7.0 // indirect
github.com/go-jose/go-jose/v4 v4.0.2 // indirect
github.com/gobwas/httphead v0.1.0 // indirect
github.com/gobwas/pool v0.2.1 // indirect
github.com/gobwas/ws v1.4.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/graph-gophers/dataloader/v7 v7.1.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/klauspost/compress v1.17.9 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.55.0 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/puzpuzpuz/xsync/v3 v3.4.0 // indirect
github.com/tdewolff/parse/v2 v2.7.18 // indirect
github.com/tidwall/gjson v1.17.3 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.1 // indirect
github.com/x448/float16 v0.8.4 // indirect
golang.org/x/exp v0.0.0-20240909161429-701f63a606c0 // indirect
golang.org/x/sys v0.27.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
)

go 1.23
go 1.23.3
134 changes: 134 additions & 0 deletions go.sum

Large diffs are not rendered by default.

178 changes: 178 additions & 0 deletions internal/nostr/nostr.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
package nostr

import (
"context"
"fmt"
"strconv"
"strings"
"time"

"github.com/nbd-wtf/go-nostr"
"github.com/nbd-wtf/go-nostr/nip05"
"github.com/nbd-wtf/go-nostr/nip19"
"github.com/nbd-wtf/go-nostr/sdk"
"miniflux.app/v2/internal/model"
"miniflux.app/v2/internal/reader/processor"
"miniflux.app/v2/internal/reader/rewrite"
"miniflux.app/v2/internal/storage"
)

var (
NostrSdk *sdk.System
)

func GetIcon(feed *model.Feed) (bool, string) {
yes, profile := IsItNostr(feed.FeedURL)

if yes {
return true, profile.Picture
}

return false, ""
}

func CreateFeed(store *storage.Storage, user *model.User, feedCreationRequest *model.FeedCreationRequest) (bool, *model.Feed) {
ctx := context.Background()
yes, profile := IsItNostr(feedCreationRequest.FeedURL)

if yes {
subscription := &model.Feed{}
nprofile := profile.Nprofile(ctx, NostrSdk, 3)
subscription.Title = profile.Name
subscription.UserID = user.ID
subscription.UserAgent = feedCreationRequest.UserAgent
subscription.Cookie = feedCreationRequest.Cookie
subscription.Username = feedCreationRequest.Username
subscription.Password = feedCreationRequest.Password
subscription.Crawler = feedCreationRequest.Crawler
subscription.FetchViaProxy = feedCreationRequest.FetchViaProxy
subscription.HideGlobally = feedCreationRequest.HideGlobally
subscription.FeedURL = fmt.Sprintf("nostr:%s", nprofile)
subscription.SiteURL = fmt.Sprintf("https://njump.me/%s", nprofile)
subscription.WithCategoryID(feedCreationRequest.CategoryID)
subscription.CheckedNow()

if storeErr := store.CreateFeed(subscription); storeErr != nil {
return false, nil
}

if err := RefreshFeed(store, user, subscription); !err {
// TODO: error handling
return false, nil
}

return true, subscription
}

return false, nil
}

func Initialize() {
NostrSdk = sdk.NewSystem(
sdk.WithRelayListRelays([]string{
"wss://nos.lol", "wss://nostr.mom", "wss://nostr.bitcoiner.social", "wss://relay.damus.io", "wss://nostr-pub.wellorder.net"}, // some standard relays
),
)
}

func RefreshFeed(store *storage.Storage, user *model.User, originalFeed *model.Feed) bool {
ctx := context.Background()
if yes, profile := IsItNostr(originalFeed.FeedURL); yes {
relays := NostrSdk.FetchOutboxRelays(ctx, profile.PubKey, 3)
evchan := NostrSdk.Pool.SubManyEose(ctx, relays, nostr.Filters{
{
Authors: []string{profile.PubKey},
Kinds: []int{nostr.KindArticle},
Limit: 32,
},
})
updatedFeed := originalFeed
for event := range evchan {

publishedAt := event.CreatedAt.Time()
if publishedAtTag := event.Tags.GetFirst([]string{"published_at"}); publishedAtTag != nil && len(*publishedAtTag) >= 2 {
i, err := strconv.ParseInt((*publishedAtTag)[1], 10, 64)
if err != nil {
publishedAt = time.Unix(i, 0)
}
}

naddr, err := nip19.EncodeEntity(event.PubKey, event.Kind, event.Tags.GetD(), relays)
if err != nil {
continue
}

title := ""
titleTag := event.Tags.GetFirst([]string{"title"})
if titleTag != nil && len(*titleTag) >= 2 {
title = (*titleTag)[1]
}

// format content from markdown to html
entry := &model.Entry{
Date: publishedAt,
Title: title,
Content: event.Content,
URL: fmt.Sprintf("https://njump.me/%s", naddr),
Hash: fmt.Sprintf("nostr:%s:%s", event.PubKey, event.Tags.GetD()),
}

rewrite.Rewriter(entry.URL, entry, "parse_markdown")

updatedFeed.Entries = append(updatedFeed.Entries, entry)

}

processor.ProcessFeedEntries(store, updatedFeed, user, true)

_, storeErr := store.RefreshFeedEntries(originalFeed.UserID, originalFeed.ID, updatedFeed.Entries, false)
if storeErr != nil {
// TODO: Error handling
return false
}

return true
}
return false
}

func IsItNostr(candidateUrl string) (bool, *sdk.ProfileMetadata) {
url := candidateUrl
ctx := context.Background()
if NostrSdk == nil {
Initialize()
}

// check for nostr url prefixes
hasNostrPrefix := false
if strings.HasPrefix(url, "nostr://") {
hasNostrPrefix = true
url = url[8:]
} else if strings.HasPrefix(url, "nostr:") {
hasNostrPrefix = true
url = url[6:]
}

// check for npub or nprofile
if prefix, _, err := nip19.Decode(url); err == nil {
if prefix == "nprofile" || prefix == "npub" {
profile, err := NostrSdk.FetchProfileFromInput(ctx, url)
if err != nil {
return false, nil
}
return true, &profile
}
}

// only do nip05 check when nostr prefix
if hasNostrPrefix && nip05.IsValidIdentifier(url) {
profile, err := NostrSdk.FetchProfileFromInput(ctx, url)
if err != nil {
return false, nil
}
return true, &profile
}

return false, nil

}
15 changes: 15 additions & 0 deletions internal/reader/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"miniflux.app/v2/internal/integration"
"miniflux.app/v2/internal/locale"
"miniflux.app/v2/internal/model"
"miniflux.app/v2/internal/nostr"
"miniflux.app/v2/internal/reader/fetcher"
"miniflux.app/v2/internal/reader/icon"
"miniflux.app/v2/internal/reader/parser"
Expand Down Expand Up @@ -114,6 +115,15 @@ func CreateFeed(store *storage.Storage, userID int64, feedCreationRequest *model
return nil, locale.NewLocalizedErrorWrapper(ErrCategoryNotFound, "error.category_not_found")
}

if nostr, subscription := nostr.CreateFeed(store, user, feedCreationRequest); nostr {

// Icon refresh here for now
iconChecker := icon.NewIconChecker(store, subscription)
iconChecker.UpdateOrCreateFeedIcon()

return subscription, nil
}

requestBuilder := fetcher.NewRequestBuilder()
requestBuilder.WithUsernameAndPassword(feedCreationRequest.Username, feedCreationRequest.Password)
requestBuilder.WithUserAgent(feedCreationRequest.UserAgent, config.Opts.HTTPClientUserAgent())
Expand Down Expand Up @@ -219,6 +229,11 @@ func RefreshFeed(store *storage.Storage, userID, feedID int64, forceRefresh bool
}
}

// TODO: this is probably not the best place to implement this
if nostr := nostr.RefreshFeed(store, user, originalFeed); nostr {
return nil
}

originalFeed.CheckedNow()
originalFeed.ScheduleNextCheck(weeklyEntryCount, refreshDelayInMinutes)

Expand Down
12 changes: 12 additions & 0 deletions internal/reader/icon/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"miniflux.app/v2/internal/config"
"miniflux.app/v2/internal/model"
"miniflux.app/v2/internal/nostr"
"miniflux.app/v2/internal/reader/fetcher"
"miniflux.app/v2/internal/storage"
)
Expand Down Expand Up @@ -35,6 +36,17 @@ func (c *IconChecker) fetchAndStoreIcon() {
requestBuilder.DisableHTTP2(c.feed.DisableHTTP2)

iconFinder := NewIconFinder(requestBuilder, c.feed.FeedURL, c.feed.IconURL)

// TODO: look at this
if nostr, iconUrl := nostr.GetIcon(c.feed); nostr {
icon, err := iconFinder.DownloadIcon(iconUrl)
if err == nil {
if err := c.store.StoreFeedIcon(c.feed.ID, icon); err == nil {
return
}
}
}

if icon, err := iconFinder.FindIcon(); err != nil {
slog.Debug("Unable to find feed icon",
slog.Int64("feed_id", c.feed.ID),
Expand Down
1 change: 1 addition & 0 deletions internal/reader/processor/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ var customReplaceRuleRegex = regexp.MustCompile(`rewrite\("(.*)"\|"(.*)"\)`)

// ProcessFeedEntries downloads original web page for entries and apply filters.
func ProcessFeedEntries(store *storage.Storage, feed *model.Feed, user *model.User, forceRefresh bool) {

var filteredEntries model.Entries

// Process older entries first
Expand Down
4 changes: 4 additions & 0 deletions internal/reader/rewrite/rewrite_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
nethtml "golang.org/x/net/html"

"github.com/PuerkitoBio/goquery"
nostrGoldmarkExtension "github.com/github-tijlxyz/goldmark-nostr"
"github.com/yuin/goldmark"
goldmarkhtml "github.com/yuin/goldmark/renderer/html"
)
Expand Down Expand Up @@ -413,6 +414,9 @@ func addHackerNewsLinksUsing(entryContent, app string) string {
func parseMarkdown(entryContent string) string {
var sb strings.Builder
md := goldmark.New(
goldmark.WithExtensions(
nostrGoldmarkExtension.New(nostrGoldmarkExtension.WithStrict(), nostrGoldmarkExtension.WithNostrLink("https://njump.me/%s")),
),
goldmark.WithRendererOptions(
goldmarkhtml.WithUnsafe(),
),
Expand Down
21 changes: 21 additions & 0 deletions internal/reader/subscription/finder.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package subscription // import "miniflux.app/v2/internal/reader/subscription"

import (
"bytes"
"context"
"fmt"
"io"
"log/slog"
Expand All @@ -16,6 +17,7 @@ import (
"miniflux.app/v2/internal/integration/rssbridge"
"miniflux.app/v2/internal/locale"
"miniflux.app/v2/internal/model"
"miniflux.app/v2/internal/nostr"
"miniflux.app/v2/internal/reader/fetcher"
"miniflux.app/v2/internal/reader/parser"
"miniflux.app/v2/internal/urllib"
Expand Down Expand Up @@ -49,7 +51,26 @@ func (f *SubscriptionFinder) FeedResponseInfo() *model.FeedCreationRequestFromSu
return f.feedResponseInfo
}

func nostrFindSubscription(url string) (bool, Subscriptions) {
ctx := context.Background()

isNostr, profile := nostr.IsItNostr(url)
if !isNostr {
return false, nil
}

nprofile := profile.Nprofile(ctx, nostr.NostrSdk, 3)

return true, Subscriptions{NewSubscription(profile.Name, nprofile, parser.FormatUnknown)}
}

func (f *SubscriptionFinder) FindSubscriptions(websiteURL, rssBridgeURL string) (Subscriptions, *locale.LocalizedErrorWrapper) {

// Find a nostr subscription
if nostr, subscriptions := nostrFindSubscription(websiteURL); nostr {
return subscriptions, nil
}

responseHandler := fetcher.NewResponseHandler(f.requestBuilder.ExecuteRequest(websiteURL))
defer responseHandler.Close()

Expand Down