Skip to content

Commit

Permalink
draft nip-34 helpers.
Browse files Browse the repository at this point in the history
  • Loading branch information
fiatjaf committed Feb 8, 2024
1 parent 6035911 commit 385aa9c
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 0 deletions.
2 changes: 2 additions & 0 deletions event.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const (
KindChannelMessage int = 42
KindChannelHideMessage int = 43
KindChannelMuteUser int = 44
KindPatch int = 1617
KindFileMetadata int = 1063
KindSimpleGroupAddUser int = 9000
KindSimpleGroupRemoveUser int = 9001
Expand Down Expand Up @@ -67,6 +68,7 @@ const (
KindProductDefinition int = 30018
KindArticle int = 30023
KindApplicationSpecificData int = 30078
KindRepositoryAnnouncement int = 30617
KindSimpleGroupMetadata int = 39000
KindSimpleGroupAdmins int = 39001
KindSimpleGroupMembers int = 39002
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ require (
require (
github.com/FactomProject/basen v0.0.0-20150613233007-fe3947df716e // indirect
github.com/FactomProject/btcutilecc v0.0.0-20130527213604-d3a63a5752ec // indirect
github.com/bluekeyes/go-gitdiff v0.7.1 // indirect
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.2 // indirect
github.com/decred/dcrd/crypto/blake256 v1.0.1 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ github.com/FactomProject/basen v0.0.0-20150613233007-fe3947df716e/go.mod h1:kGUq
github.com/FactomProject/btcutilecc v0.0.0-20130527213604-d3a63a5752ec h1:1Qb69mGp/UtRPn422BH4/Y4Q3SLUrD9KHuDkm8iodFc=
github.com/FactomProject/btcutilecc v0.0.0-20130527213604-d3a63a5752ec/go.mod h1:CD8UlnlLDiqb36L110uqiP2iSflVjx9g/3U9hCI4q2U=
github.com/aead/siphash v1.0.1/go.mod h1:Nywa3cDsYNNK3gaciGTWPwHt0wlpNV15vwmswBAUSII=
github.com/bluekeyes/go-gitdiff v0.7.1 h1:graP4ElLRshr8ecu0UtqfNTCHrtSyZd3DABQm/DWesQ=
github.com/bluekeyes/go-gitdiff v0.7.1/go.mod h1:QpfYYO1E0fTVHVZAZKiRjtSGY9823iCdvGXBcEzHGbM=
github.com/btcsuite/btcd v0.20.1-beta/go.mod h1:wVuoA8VJLEcwgqHBwHmzLRazpKxTv13Px/pDuV7OomQ=
github.com/btcsuite/btcd v0.22.0-beta.0.20220111032746-97732e52810c/go.mod h1:tjmYdS6MLJ5/s0Fj4DbLgSbDHbEqLJrtnHecBFkdz5M=
github.com/btcsuite/btcd v0.23.0/go.mod h1:0QJIIN1wwIXF/3G/m87gIwGniDMDQqjVn4SZgnFpsYY=
Expand Down
1 change: 1 addition & 0 deletions nip34/nip34.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package nip34
60 changes: 60 additions & 0 deletions nip34/patch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package nip34

import (
"strings"

"github.com/bluekeyes/go-gitdiff/gitdiff"
"github.com/nbd-wtf/go-nostr"
)

type Patch struct {
nostr.Event

Repository nostr.EntityPointer
Subject string

Files []*gitdiff.File
Header *gitdiff.PatchHeader
}

func ParsePatch(event nostr.Event) Patch {
patch := Patch{
Event: event,
}

for _, tag := range event.Tags {
if len(tag) < 2 {
continue
}
switch tag[0] {
case "a":
spl := strings.Split(tag[1], ":")
if len(spl) != 3 {
continue
}
if !nostr.IsValid32ByteHex(spl[1]) {
continue
}
patch.Repository.Kind = nostr.KindRepositoryAnnouncement
patch.Repository.PublicKey = spl[1]
patch.Repository.Identifier = spl[2]
if len(tag) >= 3 {
patch.Repository.Relays = []string{tag[2]}
}
}
}

files, preamble, err := gitdiff.Parse(strings.NewReader(event.Content))
if err != nil {
return patch
}
patch.Files = files

header, err := gitdiff.ParsePatchHeader(preamble)
if err != nil {
return patch
}
patch.Header = header

return patch
}
42 changes: 42 additions & 0 deletions nip34/repository.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package nip34

import "github.com/nbd-wtf/go-nostr"

type Repository struct {
nostr.Event

ID string
Name string
Description string
Web []string
Clone []string
Relays []string
}

func ParseRepository(event nostr.Event) Repository {
repo := Repository{
Event: event,
}

for _, tag := range event.Tags {
if len(tag) < 2 {
continue
}
switch tag[0] {
case "d":
repo.ID = tag[1]
case "name":
repo.Name = tag[1]
case "description":
repo.Description = tag[1]
case "web":
repo.Web = append(repo.Web, tag[1])
case "clone":
repo.Clone = append(repo.Clone, tag[1])
case "relays":
repo.Relays = append(repo.Relays, tag[1])
}
}

return repo
}

0 comments on commit 385aa9c

Please sign in to comment.