-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmodel.rb
400 lines (322 loc) · 10.2 KB
/
model.rb
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
#!/usr/bin/python2.4
#
# Copyright 2009 Google Inc. All Rights Reserved.
"""Defines classes that represent parts of the common wave model.
Defines the core data structures for the common wave model. At this level,
models are read-only but can be modified through operations.
"""
__author__ = 'davidbyttow@google.com (David Byttow)'
require 'document'
require 'logger'
module Model
ROOT_WAVELET_ID_SUFFIX = '!conv+root'
class WaveData
"""Defines the data for a single wave."""
attr_accessor :id, :wavelet_ids
def initialize()
@id = nil
@wavelet_ids = []
end
end
class Wave
"""Models a single wave instance.
A single wave is composed of its id and any wavelet ids that belong to it.
"""
attr_reader :_data
def initialize(data)
"""Inits this wave with its data.
Args:
data: A WaveData instance.
"""
@_data = data
end
def GetId()
"""Returns this wave's id."""
return @_data.id
end
def GetWaveletIds()
"""Returns a set of wavelet ids."""
return @_data.wavelet_ids
end
end
class WaveletData
"""Defines the data for a single wavelet."""
JAVA_CLASS = 'com.google.wave.api.impl.WaveletData'
attr_accessor :creator, :creation_time, :data_documents
attr_accessor :last_modified_time, :participants, :root_blip_id
attr_accessor :title, :version, :wave_id, :wavelet_id
def initialize()
@creator = nil
@creation_time = 0
@data_documents = {}
@last_modified_time = 0
@participants = []
@root_blip_id = nil
@title = ''
@version = 0
@wave_id = nil
@wavelet_id = nil
end
end
class Wavelet
"""Models a single wavelet instance.
A single wavelet is composed of metadata, participants and the blips it
contains.
"""
attr_reader :_data
def initialize(data)
"""Inits this wavelet with its data.
Args:
data: A WaveletData instance.
"""
@_data = data
end
def GetCreator()
"""Returns the participant id of the creator of this wavelet."""
return @_data.creator
end
def GetCreationTime()
"""Returns the time that this wavelet was first created in milliseconds."""
return @_data.creation_time
end
def GetDataDocument(name, default=nil)
"""Returns a data document for this wavelet based on key name."""
return @_data.data_documents.fetch(name, default)
end
def GetId()
"""Returns this wavelet's id."""
return @_data.wavelet_id
end
def GetLastModifiedTime()
"""Returns the time that this wavelet was last modified in milliseconds."""
return @_data.last_modified_time
end
def GetParticipants()
"""Returns a set of participants on this wavelet."""
return @_data.participants
end
def GetRootBlipId()
"""Returns this wavelet's root blip id."""
return @_data.root_blip_id
end
def GetTitle()
"""Returns the title of this wavelet."""
return @_data.title
end
def GetWaveId()
"""Returns this wavelet's parent wave id."""
return @_data.wave_id
end
end
class BlipData
"""Data that describes a single blip."""
JAVA_CLASS = 'com.google.wave.api.impl.BlipData'
attr_accessor :annotations, :child_blip_ids, :content, :contributors
attr_accessor :creator, :elements, :last_modified_time, :parent_blip_id
attr_accessor :blip_id, :version, :wave_id, :wavelet_id
def initialize()
@annotations = []
@blip_id = nil
@child_blip_ids = []
@content = ''
@contributors = []
@creator = nil
@elements = {}
@last_modified_time = 0
@parent_blip_id = nil
@version = -1
@wave_id = nil
@wavelet_id = nil
end
end
class Blip
"""Models a single blip instance.
Blips are essentially elements of conversation. Blips can live in a
hierarchy of blips. A root blip has no parent blip id, but all blips
have the ids of the wave and wavelet that they are associated with.
Blips also contain annotations, content and elements, which are accessed via
the Document object.
"""
def initialize(data, doc)
"""Inits this blip with its data and document view.
Args:
data: A BlipData instance.
doc: A Document instance associated with this blip.
"""
@_data = data
@_document = doc
end
def GetChildBlipIds()
"""Returns a set of blip ids that are children of this blip."""
return @_data.child_blip_ids
end
def GetContributors()
"""Returns a set of participant ids that contributed to this blip."""
return @_data.contributors
end
def GetCreator()
"""Returns the id of the participant that created this blip."""
return @_data.creator
end
def GetDocument()
"""Returns the Document of this blip, which contains content data."""
return @_document
end
def GetId()
"""Returns the id of this blip."""
return @_data.blip_id
end
def GetLastModifiedTime()
"""Returns the time that this blip was last modified by the server."""
return @_data.last_modified_time
end
def GetParentBlipId()
"""Returns the id of this blips parent or nil if it is the root."""
return @_data.parent_blip_id
end
def GetWaveId()
"""Returns the id of the wave that this blip belongs to."""
return @_data.wave_id
end
def GetWaveletId()
"""Returns the id of the wavelet that this blip belongs to."""
return @_data.wavelet_id
end
def IsRoot()
"""Returns True if this is the root blip of a wavelet."""
return @_data.parent_blip_id == nil
end
end
class Document
"""Base representation of a document of a blip.
TODO(davidbyttow): Add support for annotations and elements.
"""
def initialize(blip_data)
"""Inits this document with the data of the blip it is representing.
Args:
blip_data: A BlipData instance.
"""
@_blip_data = blip_data
end
def GetText()
"""Returns the raw text content of this document."""
return @_blip_data.content
end
end
class Event
"""Data describing a single event."""
attr_accessor :type, :timestamp, :modified_by, :properties
def initialize()
@type = ''
@timestamp = 0
@modified_by = ''
@properties = {}
end
end
def self.CreateEvent(data)
"""Construct event data from the raw incoming wire protocol."""
event = Event.new()
event.type = data['type']
event.timestamp = data['timestamp']
event.modified_by = data['modifiedBy']
event.properties = data['properties'] or {}
return event
end
def self.CreateWaveletData(data)
"""Construct wavelet data from the raw incoming wire protocol.
TODO(davidbyttow): Automate this based on naming like the Serialize methods.
Args:
data: Serialized data from server.
Returns:
Instance of WaveletData based on the fields.
"""
wavelet_data = WaveletData.new()
wavelet_data.creator = data['creator']
wavelet_data.creation_time = data['creationTime']
wavelet_data.data_documents = data['dataDocuments'] or {}
wavelet_data.last_modified_time = data['lastModifiedTime']
wavelet_data.participants = data['participants']
wavelet_data.root_blip_id = data['rootBlipId']
wavelet_data.title = data['title']
wavelet_data.version = data['version']
wavelet_data.wave_id = data['waveId']
wavelet_data.wavelet_id = data['waveletId']
return wavelet_data
end
def self.CreateBlipData(data)
"""Construct blip data from the raw incoming wire protocol.
TODO(davidbyttow): Automate this based on naming like the Serialize methods.
Args:
data: Serialized data from server.
Returns:
Instance of BlipData based on the fields.
"""
blip_data = BlipData.new()
blip_data.annotations = []
for annotation in data['annotations']
r = Range.new(annotation['range']['start'], annotation['range']['end'])
blip_data.annotations.push(Annotation.new(annotation['name'],
annotation['value'],
:r=>r))
end
blip_data.child_blip_ids = data['childBlipIds']
blip_data.content = data['content']
blip_data.contributors = data['contributors']
blip_data.creator = data['creator']
blip_data.elements = data['elements']
blip_data.last_modified_time = data['lastModifiedTime']
blip_data.parent_blip_id = data['parentBlipId']
blip_data.blip_id = data['blipId']
blip_data.version = data['version']
blip_data.wave_id = data['waveId']
blip_data.wavelet_id = data['waveletId']
return blip_data
end
class Context
"""Contains information associated with a single request from the server.
This includes the current waves in this session
and any operations that have been enqueued during request processing.
"""
def initialize()
@_waves = {}
@_wavelets = {}
@_blips = {}
@_operations = []
end
def GetBlipById(blip_id)
"""Returns a blip by id or nil if it does not exist."""
return @_blips[blip_id]
end
def GetWaveById(wave_id)
"""Returns a wave by id or nil if it does not exist."""
return @_waves[wave_id]
end
def GetWaveletById(wavelet_id)
"""Returns a wavelet by id or nil if it does not exist."""
return @_wavelets[wavelet_id]
end
def GetRootWavelet()
"""Returns the root wavelet or nil if it is not in this context."""
for wavelet in @_wavelets.values()
wavelet_id = wavelet.GetId()
if wavelet_id[-ROOT_WAVELET_ID_SUFFIX.length .. -1] == ROOT_WAVELET_ID_SUFFIX
return wavelet
end
end
logging.warning("Could not retrieve root wavelet.")
return nil
end
def GetWaves()
"""Returns the list of waves associated with this session."""
return @_waves.values()
end
def GetWavelets()
"""Returns the list of wavelets associated with this session."""
return @_wavelets.values()
end
def GetBlips()
"""Returns the list of blips associated with this session."""
return @_blips.values()
end
end
end