-
Notifications
You must be signed in to change notification settings - Fork 0
/
event_handlers.go
266 lines (211 loc) · 6.54 KB
/
event_handlers.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package events
import (
"bytes"
"encoding/hex"
"encoding/json"
"fmt"
"net/http"
"strings"
"time"
"github.com/Bnei-Baruch/sqlboiler/queries/qm"
log "github.com/Sirupsen/logrus"
"github.com/spf13/viper"
"github.com/Bnei-Baruch/archive-backend/consts"
"github.com/Bnei-Baruch/archive-backend/mdb"
"github.com/Bnei-Baruch/archive-backend/mdb/models"
)
var httpClient = &http.Client{
Timeout: 5 * time.Second,
}
func CollectionCreate(d Data) {
putToIndexer(indexer.CollectionUpdate, d.Payload["uid"].(string))
}
func CollectionDelete(d Data) {
putToIndexer(indexer.CollectionUpdate, d.Payload["uid"].(string))
}
func CollectionUpdate(d Data) {
putToIndexer(indexer.CollectionUpdate, d.Payload["uid"].(string))
}
func CollectionPublishedChange(d Data) {
putToIndexer(indexer.CollectionUpdate, d.Payload["uid"].(string))
}
func CollectionContentUnitsChange(d Data) {
putToIndexer(indexer.CollectionUpdate, d.Payload["uid"].(string))
}
func ContentUnitCreate(d Data) {
putToIndexer(indexer.ContentUnitUpdate, d.Payload["uid"].(string))
}
func ContentUnitDelete(d Data) {
putToIndexer(indexer.ContentUnitUpdate, d.Payload["uid"].(string))
}
func ContentUnitUpdate(d Data) {
putToIndexer(indexer.ContentUnitUpdate, d.Payload["uid"].(string))
}
func ContentUnitPublishedChange(d Data) {
uid := d.Payload["uid"].(string)
putToIndexer(indexer.ContentUnitUpdate, uid)
// Prepare unit thumbnail
unit, err := mdbmodels.ContentUnitsG(qm.Where("uid=?", uid)).One()
if err != nil {
log.Errorf("Error loading unit from mdb %s: %s", uid, err.Error())
return
}
ct := mdb.CONTENT_TYPE_REGISTRY.ByID[unit.TypeID].Name
createThumbnail := unit.Published &&
unit.Secure == 0 &&
ct != consts.CT_KITEI_MAKOR &&
ct != consts.CT_LELO_MIKUD &&
ct != consts.CT_PUBLICATION &&
ct != consts.CT_RESEARCH_MATERIAL &&
ct != consts.CT_ARTICLE
if createThumbnail {
log.Infof("thumbnail %s [%s]", unit.UID, ct)
AssetsAPI("thumbnail", unit.UID)
}
}
func ContentUnitDerivativesChange(d Data) {
putToIndexer(indexer.ContentUnitUpdate, d.Payload["uid"].(string))
}
func ContentUnitSourcesChange(d Data) {
putToIndexer(indexer.ContentUnitUpdate, d.Payload["uid"].(string))
}
func ContentUnitTagsChange(d Data) {
putToIndexer(indexer.ContentUnitUpdate, d.Payload["uid"].(string))
}
func ContentUnitPersonsChange(d Data) {
putToIndexer(indexer.ContentUnitUpdate, d.Payload["uid"].(string))
}
func ContentUnitPublishersChange(d Data) {
putToIndexer(indexer.ContentUnitUpdate, d.Payload["uid"].(string))
}
func FilePublished(d Data) {
putToIndexer(indexer.FileUpdate, d.Payload["uid"].(string))
uid := d.Payload["uid"].(string)
file, err := mdbmodels.FilesG(qm.Where("uid=?", uid)).One()
if err != nil {
log.Errorf("Error loading file from mdb %s: %s", uid, err.Error())
return
}
if file.Secure == 0 {
switch file.Type {
case "image":
if strings.HasSuffix(file.Name, ".zip") {
log.Infof("unzip %s [%s]", file.Name, file.UID)
AssetsAPI("unzip", file.UID)
}
case "text":
if file.MimeType.String == "application/msword" {
log.Infof("doc2html %s [%s]", file.Name, file.UID)
AssetsAPI("doc2html", file.UID)
}
}
}
}
func FileReplace(d Data) {
oFile := d.Payload["old"].(map[string]interface{})
nFile := d.Payload["new"].(map[string]interface{})
putToIndexer(indexer.FileUpdate, oFile["uid"].(string))
putToIndexer(indexer.FileUpdate, nFile["uid"].(string))
}
func FileInsert(d Data) {
putToIndexer(indexer.FileUpdate, d.Payload["uid"].(string))
}
func FileUpdate(d Data) {
putToIndexer(indexer.FileUpdate, d.Payload["uid"].(string))
RemoveFile(d.Payload["uid"].(string))
}
func SourceCreate(d Data) {
putToIndexer(indexer.SourceUpdate, d.Payload["uid"].(string))
}
func SourceUpdate(d Data) {
putToIndexer(indexer.SourceUpdate, d.Payload["uid"].(string))
}
func TagCreate(d Data) {
putToIndexer(indexer.TagUpdate, d.Payload["uid"].(string))
}
func TagUpdate(d Data) {
putToIndexer(indexer.TagUpdate, d.Payload["uid"].(string))
}
func PersonCreate(d Data) {
putToIndexer(indexer.PersonUpdate, d.Payload["uid"].(string))
}
func PersonDelete(d Data) {
putToIndexer(indexer.PersonUpdate, d.Payload["uid"].(string))
}
func PersonUpdate(d Data) {
putToIndexer(indexer.PersonUpdate, d.Payload["uid"].(string))
}
func PublisherCreate(d Data) {
putToIndexer(indexer.PublisherUpdate, d.Payload["uid"].(string))
}
func PublisherUpdate(d Data) {
putToIndexer(indexer.PublisherUpdate, d.Payload["uid"].(string))
}
func BlogPostUpdate(d Data) {
id := fmt.Sprintf("%d-%d", int64(d.Payload["blogId"].(float64)), int64(d.Payload["wpId"].(float64)))
putToIndexer(indexer.BlogPostUpdate, id)
}
func BlogPostCreate(d Data) {
BlogPostUpdate(d)
}
func BlogPostDelete(d Data) {
BlogPostUpdate(d)
}
func TweetCreate(d Data) {
putToIndexer(indexer.TweetUpdate, d.Payload["tid"].(string))
}
func TweetUpdate(d Data) {
putToIndexer(indexer.TweetUpdate, d.Payload["tid"].(string))
}
func TweetDelete(d Data) {
putToIndexer(indexer.TweetUpdate, d.Payload["tid"].(string))
}
func putToIndexer(f func(string) error, s string) {
indexerQueue.Enqueue(IndexerTask{F: f, S: s})
}
// AssetsAPI sends request to unzip api by file UID
func AssetsAPI(path string, uid string) {
apiURL := viper.GetString("assets_service.url")
resp, err := httpClient.Get(fmt.Sprintf("%s%s/%s", apiURL, path, uid))
if err != nil {
log.Errorf("file_service http.POST path= %s uid = %s: %s", path, uid, err.Error())
return
}
if resp.StatusCode != 200 {
log.Warnf("assets_service returned status code %d path= %s uid = %s", resp.StatusCode, path, uid)
}
}
type FileBackendRequest struct {
SHA1 string `json:"sha1"`
Name string `json:"name"`
}
// RemoveFile to send post req to file-api and remove file from search?
func RemoveFile(uid string) {
file, err := mdbmodels.FilesG(qm.Where("uid=?", uid)).One()
if err != nil {
log.Errorf("Error loading file from mdb %s: %s", uid, err.Error())
return
}
if file.Secure > 0 && file.Published == true {
log.Infof("file_service disable file %s", uid)
data := FileBackendRequest{
SHA1: hex.EncodeToString(file.Sha1.Bytes),
Name: file.Name,
}
b := new(bytes.Buffer)
err := json.NewEncoder(b).Encode(data)
if err != nil {
log.Errorf("json.Encode uid = %s: %s", uid, err.Error())
return
}
apiURL := viper.GetString("file_service.url1") + "/api/v1/getremove"
resp, err := httpClient.Post(apiURL, "application/json; charset=utf-8", b)
if err != nil {
log.Errorf("file_service http.POST uid = %s: %s", uid, err.Error())
return
}
if resp.StatusCode != 200 {
log.Warnf("file_service returned status code %d", resp.StatusCode)
}
}
}