forked from ssbc/go-ssb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
drop_content_req.go
70 lines (57 loc) · 1.5 KB
/
drop_content_req.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
// SPDX-FileCopyrightText: 2021 The Go-SSB Authors
//
// SPDX-License-Identifier: MIT
package ssb
import (
"encoding/json"
refs "github.com/ssbc/go-ssb-refs"
"github.com/ssbc/margaret"
)
// DropContentRequest has special meaning on a gabby-grove feed.
// It's signature verification allows ommiting the content.
// A feed author can ask other peers to drop a previous message of theirs with this.
// Sequence must be smaller then current, also the targeted message can't be a drop-content-request
type DropContentRequest struct {
Type string `json:"type"`
Sequence uint `json:"sequence"`
Hash refs.MessageRef `json:"hash"`
}
const DropContentRequestType = "drop-content-request"
func NewDropContentRequest(seq uint, h refs.MessageRef) *DropContentRequest {
return &DropContentRequest{
Type: DropContentRequestType,
Sequence: seq,
Hash: h,
}
}
func (dcr DropContentRequest) Valid(log margaret.Log) bool {
if dcr.Sequence < 1 {
return false
}
msgv, err := log.Get(int64(dcr.Sequence - 1))
if err != nil {
return false
}
msg, ok := msgv.(refs.Message)
if !ok {
return false
}
if msg.Author().Algo() != refs.RefAlgoFeedGabby {
return false
}
match := msg.Key().Equal(dcr.Hash)
if !match {
return false
}
// check we can't delete deletes
var msgType struct {
Type string `json:"type"`
}
if err := json.Unmarshal(msg.ContentBytes(), &msgType); err != nil {
return false
}
if msgType.Type == DropContentRequestType {
return false
}
return true
}