-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathquantum.go
99 lines (88 loc) · 3.07 KB
/
quantum.go
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright 2021 The PDU Authors
// This file is part of the PDU library.
//
// The PDU library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The PDU library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the PDU library. If not, see <http://www.gnu.org/licenses/>.
package fb
import (
"context"
"encoding/json"
"cloud.google.com/go/firestore"
"github.com/pdupub/go-pdu/core"
)
type FBQuantum struct {
Received []byte `json:"recv,omitempty"`
Origin []byte `json:"origin,omitempty"`
Contents []*core.QContent `json:"cs,omitempty"`
Type int `json:"type"`
FBRef []*FBSig `json:"refs"`
Sequence int64 `json:"seq,omitempty"`
SelfSeq int64 `json:"sseq,omitempty"`
AddrHex string `json:"address,omitempty"`
SigHex string `json:"sig,omitempty"`
ReadableCS []*FBContent `json:"rcs,omitempty"`
CreateTimestamp int64 `json:"createTime"`
Trash []byte `json:"trash,omitempty"`
Ignore []byte `json:"ignore,omitempty"`
ManualIgnore []byte `json:"manualIgnore,omitempty"`
}
func NewFBQuantumFromSnap(docSnapshot *firestore.DocumentSnapshot) (*FBQuantum, error) {
if docSnapshot == nil {
return nil, errDocumentLoadDataFail
}
return Data2FBQuantum(docSnapshot.Data())
}
func NewFBQuantumFromDB(sig core.Sig, collect *firestore.CollectionRef, ctx context.Context) (*FBQuantum, error) {
docID := core.Sig2Hex(sig)
docRef := collect.Doc(docID)
docSnapshot, err := docRef.Get(ctx)
if err != nil {
return nil, err
}
return NewFBQuantumFromSnap(docSnapshot)
}
func NewFBQuantum(q *core.Quantum) (*FBQuantum, error) {
sigHex := core.Sig2Hex(q.Signature)
address, err := q.Ecrecover()
if err != nil {
return nil, err
}
fbq := &FBQuantum{Contents: q.Contents, Type: q.Type, AddrHex: address.Hex(), SigHex: sigHex}
for _, ref := range q.References {
fbq.FBRef = append(fbq.FBRef, &FBSig{SigHex: core.Sig2Hex(ref)})
}
return fbq, nil
}
func Data2FBQuantum(d map[string]interface{}) (*FBQuantum, error) {
dataBytes, err := json.Marshal(d)
if err != nil {
return nil, err
}
fbq := new(FBQuantum)
err = json.Unmarshal(dataBytes, fbq)
if err != nil {
return nil, err
}
return fbq, nil
}
func (fbq *FBQuantum) GetOriginQuantum() (*core.Quantum, error) {
resQuantum := core.Quantum{}
data := fbq.Origin
if len(fbq.Ignore) != 0 {
data = fbq.Ignore
} else if len(fbq.ManualIgnore) != 0 {
data = fbq.ManualIgnore
}
err := json.Unmarshal(data, &resQuantum)
return &resQuantum, err
}