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
Changes from 1 commit
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
Next Next commit
working
github-tijlxyz committed Nov 16, 2024

Verified

This commit was signed with the committer’s verified signature.
inkydragon Chengyu Han
commit 4f2c738a05061ce6834b3f00bf35dc79f4ff9692
25 changes: 24 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -10,6 +10,7 @@ require (
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
@@ -29,20 +30,42 @@ 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.1

toolchain go1.23.3
132 changes: 132 additions & 0 deletions go.sum

Large diffs are not rendered by default.

177 changes: 177 additions & 0 deletions internal/nostr/nostr.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
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("nostr:%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)
}
}

nevent, err := nip19.EncodeEvent(event.ID, []string{event.Relay.String()}, event.PubKey)
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("nostr:%s", nevent),
Hash: fmt.Sprintf("nostr:%s", event.ID),
}

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(url string) (bool, *sdk.ProfileMetadata) {
ctx := context.Background()
if NostrSdk == nil {
Initialize()
}

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

// 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
@@ -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"
@@ -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())
@@ -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)

13 changes: 13 additions & 0 deletions internal/reader/icon/checker.go
Original file line number Diff line number Diff line change
@@ -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"
)
@@ -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),
@@ -80,5 +92,6 @@ func (c *IconChecker) CreateFeedIconIfMissing() {
}

func (c *IconChecker) UpdateOrCreateFeedIcon() {
slog.Info("hleeeeeeeeeello")
c.fetchAndStoreIcon()
}
1 change: 1 addition & 0 deletions internal/reader/processor/processor.go
Original file line number Diff line number Diff line change
@@ -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
21 changes: 21 additions & 0 deletions internal/reader/subscription/finder.go
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@ package subscription // import "miniflux.app/v2/internal/reader/subscription"

import (
"bytes"
"context"
"fmt"
"io"
"log/slog"
@@ -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"
@@ -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.FormatNostr)}
}

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()

Binary file added miniflux
Binary file not shown.