@@ -9,6 +9,8 @@ require('dotenv').config()
9
9
let log = require ( 'loglevel' )
10
10
const { extraDataToMessage } = require ( 'hugin-crypto' )
11
11
const { performance } = require ( 'perf_hooks' )
12
+ const { WebSocket } = require ( 'ws' )
13
+ let ws = new WebSocket ( `ws://localhost:8080` )
12
14
13
15
const { getTimestamp } = require ( '../utils/time' )
14
16
const { messageCriteria } = require ( '../utils/messageCriteria' )
@@ -21,9 +23,6 @@ let models = require('../database/models')
21
23
22
24
let known_pool_txs = [ ] ;
23
25
24
- const { WebSocket } = require ( 'ws' ) ;
25
- let ws = new WebSocket ( `ws://localhost:8080` ) ;
26
-
27
26
/**
28
27
* Background sync to fetch data
29
28
*
@@ -39,14 +38,14 @@ module.exports.backgroundSyncMessages = async () => {
39
38
body : JSON . stringify ( { knownTxsIds : known_pool_txs } )
40
39
} )
41
40
42
- let json = await resp . json ( ) ;
41
+ let json = await resp . json ( )
43
42
json = JSON . stringify ( json )
44
43
. replaceAll ( '.txPrefix' , '' )
45
44
. replaceAll ( 'transactionPrefixInfo.txHash' , 'transactionPrefixInfotxHash' )
46
- json = JSON . parse ( json ) ;
45
+ json = JSON . parse ( json )
47
46
48
- let transactions = json . addedTxs ;
49
- let transaction ;
47
+ let transactions = json . addedTxs
48
+ let transaction
50
49
51
50
if ( transactions . length === 0 ) {
52
51
log . info ( getTimestamp ( ) + ' INFO: Got empty transaction array.' )
@@ -55,15 +54,24 @@ module.exports.backgroundSyncMessages = async () => {
55
54
56
55
for ( transaction in transactions ) {
57
56
try {
57
+ console . log ( transactions [ transaction ] )
58
+
58
59
let thisExtra = transactions [ transaction ] . transactionPrefixInfo . extra
59
- let thisHash = transactions [ transaction ] . transactionPrefixInfotxHash
60
+ let txHash = transactions [ transaction ] . transactionPrefixInfotxHash
60
61
61
- if ( known_pool_txs . indexOf ( thisHash ) === - 1 ) {
62
- known_pool_txs . push ( thisHash )
62
+ // checking if encrypted post with txHash already exists in db - if not create a new record
63
+ encryptedPostExists ( txHash ) . then ( result => {
64
+ if ( result === null ) {
65
+ saveEncryptedPost ( transactions [ transaction ] )
66
+ }
67
+ } )
68
+
69
+ if ( known_pool_txs . indexOf ( txHash ) === - 1 ) {
70
+ known_pool_txs . push ( txHash )
63
71
message_was_unknown = true
64
72
} else {
65
73
message_was_unknown = false
66
- console . log ( "This transaction is already known" , thisHash )
74
+ console . log ( "This transaction is already known" , txHash )
67
75
continue
68
76
}
69
77
@@ -79,8 +87,6 @@ module.exports.backgroundSyncMessages = async () => {
79
87
message = await extraDataToMessage ( thisExtra , knownk , keypair )
80
88
}
81
89
82
- // let msg = JSON.parse(message)
83
-
84
90
if ( ! message || message === undefined ) {
85
91
log . info ( getTimestamp ( ) + ' INFO: Caught undefined null message, continue.' )
86
92
continue
@@ -96,7 +102,7 @@ module.exports.backgroundSyncMessages = async () => {
96
102
board : message . brd || null ,
97
103
time : message . t || null ,
98
104
nickname : message . n || null ,
99
- tx_hash : thisHash || null ,
105
+ tx_hash : txHash || null ,
100
106
reply : message . r || null
101
107
}
102
108
@@ -108,99 +114,17 @@ module.exports.backgroundSyncMessages = async () => {
108
114
log . info ( getTimestamp ( ) + ' INFO: Message does not meet criteria based on configuration: ' + JSON . stringify ( message ) )
109
115
continue
110
116
}
111
-
112
117
log . info ( getTimestamp ( ) + ' INFO: Criteria fulfilled.' )
113
118
114
119
// broadcast message object to websocket server
115
120
ws . send ( JSON . stringify ( messageObj ) )
116
121
117
- let startTime = performance . now ( )
118
-
119
- try {
120
- // database lookup if row exists with a certain tx_hash
121
- const postTxHashLookup = models . Post . findOne ( {
122
- where : {
123
- tx_hash : thisHash
124
- } ,
125
- order : [ [ 'id' , 'DESC' ] ] ,
126
- raw : true ,
127
- } )
128
-
129
- // checking if post with tx_hash already exists in db - if not create a new record
130
- postTxHashLookup . then ( async result => {
131
- if ( result === null ) {
132
- await sequelize . transaction ( async ( t ) => {
133
- return models . Post . create ( messageObj ) . then ( postObj => {
134
- log . info ( getTimestamp ( ) + ` INFO: Post transaction was successful - Post with ID ${ postObj . id } was created.` )
135
-
136
- // extract hashtags from message and save it do db and add relationship in post_hashtag table
137
- let messageStr = message . m
138
- let hashtags
139
-
140
- try {
141
- hashtags = messageStr . match ( / # [ ^ \s # \. \; ! * € % & ( ) ? ^ $ @ ` ¨ ] * / gmi)
142
-
143
- if ( hashtags ) {
144
- // going through all hashtags and do separate lookups
145
- hashtags . forEach ( hashtag => {
146
- // removing the # and making it lowercase, so we have proper input for query
147
- let hashtagName = hashtag . replace ( '#' , '' ) . toLowerCase ( )
148
-
149
- const hashtagLookup = models . Hashtag . findOne ( {
150
- where : {
151
- name : hashtagName
152
- } ,
153
- order : [ [ 'id' , 'DESC' ] ] ,
154
- raw : true ,
155
- } )
156
-
157
- // create hashtag if it does not exist otherwise get the hashtag ID
158
- hashtagLookup . then ( async result => {
159
- if ( result === null ) {
160
- await sequelize . transaction ( async ( t1 ) => {
161
- return models . Hashtag . create ( {
162
- name : hashtagName
163
- } ) . then ( async hashtagObj => {
164
- await sequelize . transaction ( async ( t2 ) => {
165
- return models . PostHashtag . create ( {
166
- post_id : postObj . id ,
167
- hashtag_id : hashtagObj . id
168
- } )
169
- } )
170
- } )
171
- } )
172
- } else {
173
- // hashtag exists, so we add a new row in post_hashtag with its ID
174
- await sequelize . transaction ( async ( t3 ) => {
175
- return models . PostHashtag . create ( {
176
- post_id : postObj . id ,
177
- hashtag_id : result . id
178
- } )
179
- } )
180
- }
181
- } )
182
-
183
- } )
184
- }
185
- } catch ( TypeError ) {
186
- log . error ( getTimestamp ( ) + ' ERROR: Could not parse hashtags' )
187
- }
188
-
189
- } )
190
- } )
191
- } else {
192
- log . info ( getTimestamp ( ) + ' INFO: Found record in database - Skipping.' )
193
- }
194
- } )
195
-
196
- // calculating queries for debug reasons
197
- let endTime = performance . now ( )
198
- log . info ( getTimestamp ( ) + ` INFO: Queries to took ${ endTime - startTime } seconds` )
199
-
200
- } catch ( err ) {
201
- log . info ( getTimestamp ( ) + ' ERROR: An error adding a Post transaction - Rolling back. ' + err )
202
- }
203
-
122
+ // checking if post with txHash already exists in db - if not create a new record
123
+ postExists ( txHash ) . then ( result => {
124
+ if ( result === null ) {
125
+ savePost ( messageObj , txHash )
126
+ }
127
+ } )
204
128
} else {
205
129
log . info ( getTimestamp ( ) + ' INFO: No message.' )
206
130
}
@@ -212,4 +136,149 @@ module.exports.backgroundSyncMessages = async () => {
212
136
} catch ( err ) {
213
137
log . error ( getTimestamp ( ) + ' ERROR: Sync error. ' + err )
214
138
}
139
+ }
140
+
141
+ /**
142
+ * Check if encrypted post exists in database.
143
+ *
144
+ * @param {String } txHash - Hash value.
145
+ * @returns {Boolean } Resolves to true if found.
146
+ */
147
+ async function encryptedPostExists ( txHash ) {
148
+ try {
149
+ const postEncryptedTxHashLookup = models . PostEncrypted . findOne ( {
150
+ where : {
151
+ tx_hash : txHash
152
+ } ,
153
+ order : [ [ 'id' , 'DESC' ] ] ,
154
+ raw : true ,
155
+ } )
156
+
157
+ return postEncryptedTxHashLookup
158
+ } catch ( err ) {
159
+ log . error ( getTimestamp ( ) + ' ERROR: Sync error. ' + err )
160
+ }
161
+ }
162
+
163
+ /**
164
+ * Check if post exists in database.
165
+ *
166
+ * @param {String } txHash - Hash value.
167
+ * @returns {Boolean } Resolves to true if found.
168
+ */
169
+ async function postExists ( txHash ) {
170
+ try {
171
+ const postTxHashLookup = models . Post . findOne ( {
172
+ where : {
173
+ tx_hash : txHash
174
+ } ,
175
+ order : [ [ 'id' , 'DESC' ] ] ,
176
+ raw : true ,
177
+ } )
178
+
179
+ return postTxHashLookup
180
+ } catch ( err ) {
181
+ log . error ( getTimestamp ( ) + ' ERROR: Sync error. ' + err )
182
+ }
183
+ }
184
+
185
+ /**
186
+ * Save an encrypted post to database.
187
+ *
188
+ * @param {Object } transaction - Transaction object.
189
+ * @returns {Promise } Resolves to this if transaction to database succeeded.
190
+ */
191
+ async function saveEncryptedPost ( transaction ) {
192
+ try {
193
+ await sequelize . transaction ( async ( t ) => {
194
+ return models . PostEncrypted . create ( {
195
+ tx_hash : transaction . transactionPrefixInfotxHash ,
196
+ tx_extra : transaction . transactionPrefixInfo . extra ,
197
+ tx_unlock_time : transaction . transactionPrefixInfo . unlock_time ,
198
+ tx_version : transaction . transactionPrefixInfo . version
199
+ } )
200
+ } )
201
+ } catch ( err ) {
202
+ log . error ( getTimestamp ( ) + ' ERROR: ' + err )
203
+ }
204
+ }
205
+
206
+ /**
207
+ * Save a post to database.
208
+ *
209
+ * @param {Object } messageObj - Message object.
210
+ * @param {String } txHash - Hash value.
211
+ * @returns {Promise } Resolves to this if transaction to database succeeded.
212
+ */
213
+ async function savePost ( messageObj , txHash ) {
214
+ let startTime = performance . now ( )
215
+
216
+ try {
217
+ await sequelize . transaction ( async ( t ) => {
218
+ return models . Post . create ( messageObj ) . then ( postObj => {
219
+ log . info ( getTimestamp ( ) + ` INFO: Post transaction was successful - Post with ID ${ postObj . id } was created.` )
220
+
221
+ // extract hashtags from message and save it do db and add relationship in post_hashtag table
222
+ let messageStr = messageObj . message
223
+
224
+ try {
225
+ let hashtags = messageStr . match ( / # [ ^ \s # \. \; ! * € % & ( ) ? ^ $ @ ` ¨ ] * / gmi)
226
+
227
+
228
+ if ( hashtags ) {
229
+ // going through all hashtags and do separate lookups
230
+ hashtags . forEach ( hashtag => {
231
+ // removing the # and making it lowercase, so we have proper input for query
232
+ let hashtagName = hashtag . replace ( '#' , '' ) . toLowerCase ( )
233
+
234
+ const hashtagLookup = models . Hashtag . findOne ( {
235
+ where : {
236
+ name : hashtagName
237
+ } ,
238
+ order : [ [ 'id' , 'DESC' ] ] ,
239
+ raw : true ,
240
+ } )
241
+
242
+ // create hashtag if it does not exist otherwise get the hashtag ID
243
+ hashtagLookup . then ( async result => {
244
+ if ( result === null ) {
245
+ await sequelize . transaction ( async ( t1 ) => {
246
+ return models . Hashtag . create ( {
247
+ name : hashtagName
248
+ } ) . then ( async hashtagObj => {
249
+ await sequelize . transaction ( async ( t2 ) => {
250
+ return models . PostHashtag . create ( {
251
+ post_id : postObj . id ,
252
+ hashtag_id : hashtagObj . id
253
+ } )
254
+ } )
255
+ } )
256
+ } )
257
+ } else {
258
+ // hashtag exists, so we add a new row in post_hashtag with its ID
259
+ await sequelize . transaction ( async ( t3 ) => {
260
+ return models . PostHashtag . create ( {
261
+ post_id : postObj . id ,
262
+ hashtag_id : result . id
263
+ } )
264
+ } )
265
+ }
266
+ } )
267
+
268
+ } )
269
+ }
270
+ } catch ( TypeError ) {
271
+ log . error ( getTimestamp ( ) + ' ERROR: Could not parse hashtags' )
272
+ }
273
+
274
+ } )
275
+ } )
276
+
277
+ // calculating queries for debug reasons
278
+ let endTime = performance . now ( )
279
+ log . info ( getTimestamp ( ) + ` INFO: Queries to took ${ endTime - startTime } seconds` )
280
+
281
+ } catch ( err ) {
282
+ log . info ( getTimestamp ( ) + ' ERROR: An error adding a Post transaction - Rolling back. ' + err )
283
+ }
215
284
}
0 commit comments