-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFulltext.js
158 lines (143 loc) · 6.39 KB
/
Fulltext.js
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
var Promise = require('bluebird'),
r = require('rethinkdb'),
elasticsearch = require('elasticsearch'),
config = require('./config'),
bunyan = require('bunyan'),
stream = require('gelf-stream'),
discover = require('./lib/discover'),
helper = require('./lib/helper'),
log;
if (!!config.graylog) {
log = bunyan.createLogger({
name: 'API-Fulltext',
streams: [{
type: 'raw',
stream: stream.forBunyan(config.graylog.host, config.graylog.port)
}]
});
}else{
log = bunyan.createLogger({
name: 'API-Fulltext'
});
}
if (!config.elasticsearch) {
log.info({ message: 'Elasticsearch not configured.' });
process.exit(0);
}
var ready = false;
var accountToUserMapping = {};
discover().then(function(ip) {
if (ip !== null) config.rethinkdb.host = ip;
r.connect(config.rethinkdb).then(function(conn) {
r.conn = conn;
log.info('Process ' + process.pid + ' is running as an API-Fulltext.');
var client = new elasticsearch.Client({
host: config.elasticsearch + ':9200',
requestTimeout: 1000 * 60 * 5
});
// Elasticsearch does not allow multiple types in one indices...
client.count(function (error, response, status) {
var count = response.count;
log.info({ message: count + ' messages on Elastic' });
r.table('messages')
.pluck('messageId', 'from', 'to', 'cc', 'bcc', 'attachments', 'accountId', 'subject', 'text', 'html')
.changes({
includeInitial: true,
includeStates: true
})
.run(r.conn)
.then(function(cursor) {
var getUserId = function(accountId) {
if (typeof accountToUserMapping[accountId] !== 'undefined') {
return Promise.resolve(accountToUserMapping[accountId])
}else{
return helper.auth.accountIdToUserId(r, accountId)
.then(function(userId) {
accountToUserMapping[accountId] = userId.toLowerCase();
return accountToUserMapping[accountId];
})
}
}
var fetchNext = function(err, result) {
if (err) throw err;
if (result.state === 'initializing') {
log.info({ message: 'Initializing feeds.' });
return cursor.next(fetchNext);
}
if (result.state === 'ready') {
ready = true;
log.info({ message: 'Feeds ready.' });
return cursor.next(fetchNext);
}
if (!ready) {
return getUserId(result.new_val.accountId).then(function(userId) {
return client.get({
index: [userId, result.new_val.accountId].join('_').toLowerCase(),
type: 'messages',
id: result.new_val.messageId,
_source: false
}, function(err, res) {
if (err) {
if (err.message.indexOf('index_not_found_exception') !== -1) {
// safely ignore
}else if (res && res.found === false) {
// safely ignore
}else {
throw err;
}
}
if (res.found === true) return cursor.next(fetchNext);
return client.create({
index: [userId, result.new_val.accountId].join('_').toLowerCase(),
type: 'messages',
id: result.new_val.messageId,
body: result.new_val
}, function(error, response) {
if (error) throw error;
cursor.next(fetchNext);
})
})
})
}
if (result.new_val === null && result.old_val !== null) {
// delete
return getUserId(result.old_val.accountId).then(function(userId) {
return client.delete({
index: [userId, result.old_val.accountId].join('_').toLowerCase(),
type: 'messages',
id: result.old_val.messageId
}, function(error, response) {
if (error) throw error;
cursor.next(fetchNext);
})
})
}
if (result.new_val !== null && result.old_val !== null) {
// update
// `messages` doesn't really update (subject, text, html)
return cursor.next(fetchNext);
}
if (result.new_val !== null && result.old_val === null) {
// create
return getUserId(result.new_val.accountId).then(function(userId) {
return client.create({
index: [userId, result.new_val.accountId].join('_').toLowerCase(),
type: 'messages',
id: result.new_val.messageId,
body: result.new_val
}, function(error, response) {
if (error) throw error;
cursor.next(fetchNext);
})
})
}
}
cursor.next(fetchNext);
})
.catch(function(e) {
log.error({ message: 'Error thrown from Fulltext', error: '[' + e.name + '] ' + e.message, stack: e.stack })
process.exit(1)
})
})
})
})