diff --git a/nip34/repository.go b/nip34/repository.go index 2046366..a01ff80 100644 --- a/nip34/repository.go +++ b/nip34/repository.go @@ -52,7 +52,7 @@ func ParseRepository(event nostr.Event) Repository { return repo } -func (r Repository) ToEvent() nostr.Event { +func (r Repository) ToEvent() *nostr.Event { tags := make(nostr.Tags, 0, 10) tags = append(tags, nostr.Tag{"d", r.ID}) @@ -91,12 +91,29 @@ func (r Repository) ToEvent() nostr.Event { tags = append(tags, tag) } - return nostr.Event{ - Kind: 30617, - Tags: tags, + return &nostr.Event{ + Kind: nostr.KindRepositoryAnnouncement, + Tags: tags, + CreatedAt: nostr.Now(), } } +func (repo Repository) FetchState(ctx context.Context, s nostr.RelayStore) *RepositoryState { + res, _ := s.QuerySync(ctx, nostr.Filter{ + Kinds: []int{nostr.KindRepositoryState}, + Tags: nostr.TagMap{ + "d": []string{repo.Tags.GetD()}, + }, + }) + + if len(res) == 0 { + return nil + } + + rs := ParseRepositoryState(*res[0]) + return &rs +} + func (repo Repository) GetPatchesSync(ctx context.Context, s nostr.RelayStore) []Patch { res, _ := s.QuerySync(ctx, nostr.Filter{ Kinds: []int{nostr.KindPatch}, diff --git a/nip34/state.go b/nip34/state.go index 6e96892..6865ead 100644 --- a/nip34/state.go +++ b/nip34/state.go @@ -45,20 +45,23 @@ func ParseRepositoryState(event nostr.Event) RepositoryState { return st } -func (rs RepositoryState) ToEvent() nostr.Event { - tags := make(nostr.Tags, 0, 2+len(rs.Branches)+len(rs.Tags)) +func (rs RepositoryState) ToEvent() *nostr.Event { + tags := make(nostr.Tags, 1, 2+len(rs.Branches)+len(rs.Tags)) - tags = append(tags, nostr.Tag{"d", rs.ID}) - tags = append(tags, nostr.Tag{"ref: refs/heads/" + rs.HEAD}) + tags[0] = nostr.Tag{"d", rs.ID} for branchName, commitId := range rs.Branches { tags = append(tags, nostr.Tag{"refs/heads/" + branchName, commitId}) } for tagName, commitId := range rs.Tags { tags = append(tags, nostr.Tag{"refs/tags/" + tagName, commitId}) } + if rs.HEAD != "" { + tags = append(tags, nostr.Tag{"HEAD", "ref: refs/heads/" + rs.HEAD}) + } - return nostr.Event{ - Kind: 30618, - Tags: tags, + return &nostr.Event{ + Kind: 30618, + Tags: tags, + CreatedAt: nostr.Now(), } }