-
-
Notifications
You must be signed in to change notification settings - Fork 376
/
commento.go
133 lines (111 loc) · 3.34 KB
/
commento.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package migrator
import (
"encoding/json"
"fmt"
"io"
"time"
"github.com/umputun/remark42/backend/app/store"
log "github.com/go-pkgz/lgr"
)
// Commento implements Importer from commento export json
type Commento struct {
DataStore Store
}
// Credit: https://gitlab.com/commento/commento/-/blob/master/api/domain_import_commento.go#L11-L15
type commentoExport struct {
Version int `json:"version"`
Comments []commentoComment `json:"comments"`
Commenters []commentoCommenter `json:"commenters"`
}
// Credit: https://gitlab.com/commento/commento/-/blob/master/api/comment.go#L7-L20
type commentoComment struct {
CommentHex string `json:"commentHex"`
Domain string `json:"domain,omitempty"`
Path string `json:"url,omitempty"`
CommenterHex string `json:"commenterHex"`
Markdown string `json:"markdown"`
HTML string `json:"html"`
ParentHex string `json:"parentHex"`
Score int `json:"score"`
State string `json:"state,omitempty"`
CreationDate time.Time `json:"creationDate"`
Direction int `json:"direction"`
Deleted bool `json:"deleted"`
}
// Credit: https://gitlab.com/commento/commento/-/blob/master/api/commenter.go#L7-L16
type commentoCommenter struct {
CommenterHex string `json:"commenterHex,omitempty"`
Email string `json:"email,omitempty"`
Name string `json:"name"`
Link string `json:"link"`
Photo string `json:"photo"`
Provider string `json:"provider,omitempty"`
JoinDate time.Time `json:"joinDate,omitempty"`
IsModerator bool `json:"isModerator"`
}
// Import comments from Commento and save to store
func (d *Commento) Import(r io.Reader, siteID string) (size int, err error) {
if e := d.DataStore.DeleteAll(siteID); e != nil {
return 0, e
}
commentsCh := d.convert(r, siteID)
failed, passed := 0, 0
for c := range commentsCh {
if _, err = d.DataStore.Create(c); err != nil {
failed++
continue
}
passed++
}
if failed > 0 {
err = fmt.Errorf("failed to save %d comments", failed)
if passed == 0 {
err = fmt.Errorf("import failed")
}
}
log.Printf("[DEBUG] imported %d comments to site %s", passed, siteID)
return passed, err
}
func (d *Commento) convert(r io.Reader, siteID string) (ch chan store.Comment) {
commentsCh := make(chan store.Comment)
decoder := json.NewDecoder(r)
go func() {
var exportedData commentoExport
err := decoder.Decode(&exportedData)
if err != nil {
log.Printf("[WARN] can't decode commento export json, %s", err.Error())
}
usersMap := map[string]store.User{}
for _, commenter := range exportedData.Commenters {
usersMap[commenter.CommenterHex] = store.User{
Name: commenter.Name,
ID: "commento_" + store.EncodeID(commenter.CommenterHex),
Picture: commenter.Photo,
}
}
for _, comment := range exportedData.Comments {
u, ok := usersMap[comment.CommenterHex]
if !ok {
continue
}
if comment.Deleted {
continue
}
c := store.Comment{
ID: comment.CommentHex,
Locator: store.Locator{
URL: comment.Path,
SiteID: siteID,
},
User: u,
Text: comment.Markdown,
Timestamp: comment.CreationDate,
ParentID: comment.ParentHex,
Imported: true,
}
commentsCh <- c
}
close(commentsCh)
}()
return commentsCh
}