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

database: count: handle masterkey #31

Merged
merged 2 commits into from
Nov 29, 2024
Merged
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
2 changes: 1 addition & 1 deletion database/query/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ func (db *dbClient) eventTransform(event *databaseEvent) *databaseEvent {

case model.KindDVMCountResponse:
var ev databaseEvent
ev.Kind = model.KindDVMCountRequest
ev.Kind = model.KindJobNostrEventCount
ev.CreatedAt = event.CreatedAt
ev.Content = event.Dtag
ev.Tags = append(event.Tags, model.Tag{"param", "relay", db.relayURL})
Expand Down
6 changes: 4 additions & 2 deletions database/query/query_where_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ select
6400,
unixepoch(),
0,
f.reference_id,
case when f.kind = 3 then '' else f.reference_id end as id,
coalesce(evr.pubkey, ''),
coalesce(evr.master_pubkey, ''),
'',
Expand All @@ -544,7 +544,7 @@ select
end as jtags
from
event_counters f
inner join events evr on f.reference_id in (evr.id, evr.pubkey)
inner join ` + cteName + ` evr on evr.kind = :` + (filterID + "kind") + ` and f.reference_id in (evr.id, evr.pubkey, evr.master_pubkey)
where
`)
} else {
Expand Down Expand Up @@ -690,6 +690,8 @@ group by e.pubkey, e.master_pubkey`)
w.WriteString(" AND f.reference_id IN (")
if filter.Reduce.Kinds[1] == nostr.KindFollowList {
w.WriteString(w.createWhereForDepFilter(filterID, cteName, "pubkey", &filter.Start))
w.WriteString(" UNION ALL ")
w.WriteString(w.createWhereForDepFilter(filterID, cteName, "master_pubkey", &filter.Start))
} else {
w.WriteString(w.createWhereForDepFilter(filterID, cteName, "id", &filter.Start))
}
Expand Down
78 changes: 78 additions & 0 deletions database/query/query_where_dependencies_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -598,5 +598,83 @@ func TestSelectWithDependencies(t *testing.T) {
}
}
}
t.Run("Delegated", func(t *testing.T) {
const (
masterPrivate = `612be8342c593ba8a592f34c462e65a63c8233bad13aea022c5dcb3656a975d560f97174e3fc1c8decee03bfed97157a1a8db0d1140b8792958cd57f6de252e0`
userPrivate = `f66568ed325fac494593d5a191a591ca0e3cd4b04141350aa4703776f603e5769c1a22718581dc75961acc4b49f43935356bbecfc4cdb4229b12c31017a8a70c`
)
masterPublic, err := model.GetPublicKey(masterPrivate)
require.NoError(t, err)
userPublic, err := model.GetPublicKey(userPrivate)
require.NoError(t, err)

t.Logf("master public key: %s", masterPublic)
t.Logf("user public key: %s", userPublic)

ev := &model.Event{
Event: nostr.Event{
Kind: model.CustomIONKindAttestation,
CreatedAt: 1,
Tags: model.Tags{{model.TagAttestationName, userPublic, "", model.CustomIONAttestationKindActive + ":1"}},
},
}
require.NoError(t, ev.SignWithAlg(masterPrivate, model.SignAlgEDDSA, model.KeyAlgCurve25519))
require.NoError(t, db.AcceptEvents(context.TODO(), ev))

meta := &model.Event{
Event: nostr.Event{
Kind: nostr.KindProfileMetadata,
CreatedAt: 2,
Tags: model.Tags{{model.CustomIONTagOnBehalfOf, masterPublic}},
},
}
require.NoError(t, meta.SignWithAlg(userPrivate, model.SignAlgEDDSA, model.KeyAlgCurve25519))
require.NoError(t, db.AcceptEvents(context.TODO(), meta))

require.NoError(t, db.AcceptEvents(context.Background(),
&model.Event{
Event: nostr.Event{
ID: "t10id1",
Kind: nostr.KindFollowList,
PubKey: "t10pk1",
CreatedAt: 3,
Tags: model.Tags{
{"p", masterPublic},
},
},
},
&model.Event{
Event: nostr.Event{
ID: "t10id2",
Kind: nostr.KindFollowList,
PubKey: "t10pk2",
CreatedAt: 4,
Tags: model.Tags{
{"p", masterPublic},
},
},
},
&model.Event{
Event: nostr.Event{
ID: "t10id3",
Kind: nostr.KindFollowList,
PubKey: "t10pk3",
CreatedAt: 5,
Tags: model.Tags{
{"p", masterPublic},
},
},
},
))
events := helperSelectEvents(t, db, model.Filter{
Authors: []string{masterPublic},
Search: "include:dependencies:kind0>kind6400+kind3+group+p",
})
require.Len(t, events, 3) // Attestation, Profile metadata, follower count.
for i, kind := range []int{nostr.KindProfileMetadata, model.CustomIONKindAttestation, model.KindDVMCountResponse} {
require.Equalf(t, kind, events[i].Kind, "event %d: %v", i, events[i])
}
require.Equal(t, "3", events[2].Content)
})
})
}
1 change: 0 additions & 1 deletion model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ const (
CustomIONKindAttestation = 10_100
CustomIONKindRelayListMetadata = 20_002

KindDVMCountRequest = 5400
KindDVMCountResponse = 6400
)

Expand Down
Loading