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
|
require 'elasticsearch'
require 'date'
module Ag::Storage
module_function
def create_index(list)
begin
$es.indices.delete index: 'ml-' + list
rescue Elasticsearch::Transport::Transport::Errors::NotFound => e
$stderr.puts "Index did not exist yet. Creating."
end
$es.indices.create(
index: 'ml-' + list,
body: {
mappings: {
message: {
properties: {
attachments: {
properties: {
filename: {
type: 'string',
index: 'not_analyzed'
},
mime: {
type: 'string',
index: 'not_analyzed'
}
}
},
cc: {
type: 'string'
},
content: {
type: 'string'
},
date: {
type: 'date',
format: 'dateOptionalTime'
},
from: {
type: 'string'
},
from_realname: {
type: 'string'
},
month: {
type: 'integer'
},
parent: {
type: 'string',
index: 'not_analyzed'
},
raw_message_id: {
type: 'string',
index: 'not_analyzed'
},
raw_filename: {
type: 'string',
index: 'not_analyzed'
},
raw_parent: {
type: 'string'
},
subject: {
type: 'string'
},
to: {
type: 'string'
}
}
}
}
})
# Give elasticsearch some time to process the new index
sleep 1
end
def get_content(message, filename)
content = 'Cannot parse MIME/contents.'
begin
raw_content = Ag::Rendering::HTMLizer.HTMLize(message)
content = Ag::Utils.fix_encoding(raw_content || '', true).strip
if content == ''
$stderr.puts "#{message.message_id}: Content empty?"
end
rescue => e
$stderr.puts "Cannot render message #{message.message_id} (file: #{filename}): #{e}"
end
content
end
def get_parent_message(list, parent_message_id = nil)
return nil if parent_message_id == nil
result = $es.search(
index: 'ml-' + list,
body: {
query: {
filtered: {
filter: {
term: { raw_message_id: parent_message_id }
}
}
},
fields: ['_id']
}
)
return nil if result['hits']['total'] == 0
result['hits']['hits'].first['_id']
end
def store(list, message, filename)
content = get_content(message)
identifier = message['X-Archives-Hash'].value
raw_parent = Ag::Threading.get_parent_message_id(message)
from = Ag::Utils.fix_encoding(message[:from].formatted.first)
from_realname = from.gsub(/<(.*)>/, '').strip
to = ''
if message[:to]
to = Ag::Utils.fix_encoding(message[:to].formatted.join(','))
end
cc = ''
if message[:cc]
cc = Ag::Utils.fix_encoding(message[:cc].formatted.join(','))
end
subject = Ag::Utils.fix_encoding(message.subject)
attachments = []
if message.has_attachments?
message.attachments.each do |attachment|
attachments << {
filename: attachment.filename,
mime: attachment.mime_type
}
end
end
$es.index(
index: 'ml-' + list,
type: 'message',
id: identifier,
body: {
raw_message_id: message.message_id,
subject: subject,
to: to,
cc: cc,
from: from,
from_realname: from_realname,
date: message.date,
month: ("%i%02i" % [message.date.year, message.date.month]).to_i, # this is a sortable number!
content: content,
attachments: attachments,
raw_parent: raw_parent,
raw_filename: filename
}
)
end
def fix_threading(list)
result = $es.search(
index: 'ml-' + list,
size: 100000,
body: {
size: 100000,
query: {
filtered: {
filter: {
and: [
{
missing: {
field: 'parent'
}
},
{
exists: {
field: 'raw_parent'
}
}
]
}
}
}
}
)
result['hits']['hits'].each do |hit|
msg = get_parent_message(list, hit['_source']['raw_parent'])
unless msg == nil
$es.update(
index: 'ml-' + list,
type: 'message',
id: hit['_id'],
body: {
doc: {
parent: msg
}
}
)
end
end
result['hits']['total']
end
end
|