summaryrefslogtreecommitdiffstats
path: root/internal/handler/websocket/convert.go
blob: 5a5692abf79a4511090c739c08bd7898b98f2320 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package websocket

import (
	pb "northwest.io/muxstr/api/nostr/v1"
	"northwest.io/nostr"
)

func NostrToPB(n *nostr.Event) *pb.Event {
	tags := make([]*pb.Tag, len(n.Tags))
	for i, tag := range n.Tags {
		tags[i] = &pb.Tag{Values: tag}
	}

	return &pb.Event{
		Id:        n.ID,
		Pubkey:    n.PubKey,
		CreatedAt: n.CreatedAt,
		Kind:      int32(n.Kind),
		Tags:      tags,
		Content:   n.Content,
		Sig:       n.Sig,
	}
}

func PBToNostr(e *pb.Event) *nostr.Event {
	tags := make(nostr.Tags, len(e.Tags))
	for i, tag := range e.Tags {
		tags[i] = tag.Values
	}

	return &nostr.Event{
		ID:        e.Id,
		PubKey:    e.Pubkey,
		CreatedAt: e.CreatedAt,
		Kind:      int(e.Kind),
		Tags:      tags,
		Content:   e.Content,
		Sig:       e.Sig,
	}
}

func NostrFilterToPB(f *nostr.Filter) *pb.Filter {
	pbFilter := &pb.Filter{
		Ids:     f.IDs,
		Authors: f.Authors,
		Kinds:   make([]int32, len(f.Kinds)),
	}

	for i, kind := range f.Kinds {
		pbFilter.Kinds[i] = int32(kind)
	}

	if f.Since != nil {
		since := int64(*f.Since)
		pbFilter.Since = &since
	}

	if f.Until != nil {
		until := int64(*f.Until)
		pbFilter.Until = &until
	}

	if f.Limit > 0 {
		limit := int32(f.Limit)
		pbFilter.Limit = &limit
	}

	if len(f.Tags) > 0 {
		pbFilter.TagFilters = make(map[string]*pb.TagFilter)
		for tagName, values := range f.Tags {
			pbFilter.TagFilters[tagName] = &pb.TagFilter{Values: values}
		}
	}

	return pbFilter
}