summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/db/gloda/modules/GlodaDataModel.jsm
blob: d9361c079c3097a9596a91d2668b94217fac34b0 (plain)
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

const EXPORTED_SYMBOLS = [
  "GlodaAttributeDBDef",
  "GlodaAccount",
  "GlodaConversation",
  "GlodaFolder",
  "GlodaMessage",
  "GlodaContact",
  "GlodaIdentity",
  "GlodaAttachment",
];

const { GlodaConstants } = ChromeUtils.import(
  "resource:///modules/gloda/GlodaConstants.jsm"
);
const { MailServices } = ChromeUtils.import(
  "resource:///modules/MailServices.jsm"
);
var LOG = console.createInstance({
  prefix: "gloda.datamodel",
  maxLogLevel: "Warn",
  maxLogLevelPref: "gloda.loglevel",
});

/**
 * @class Represents a gloda attribute definition's DB form.  This class
 *  stores the information in the database relating to this attribute
 *  definition.  Access its attrDef attribute to get at the really juicy data.
 *  This main interesting thing this class does is serve as the keeper of the
 *  mapping from parameters to attribute ids in the database if this is a
 *  parameterized attribute.
 */
function GlodaAttributeDBDef(
  aDatastore,
  aID,
  aCompoundName,
  aAttrType,
  aPluginName,
  aAttrName
) {
  // _datastore is now set on the prototype by GlodaDatastore
  this._id = aID;
  this._compoundName = aCompoundName;
  this._attrType = aAttrType;
  this._pluginName = aPluginName;
  this._attrName = aAttrName;

  this.attrDef = null;

  /** Map parameter values to the underlying database id. */
  this._parameterBindings = {};
}

GlodaAttributeDBDef.prototype = {
  // set by GlodaDatastore
  _datastore: null,
  get id() {
    return this._id;
  },
  get attributeName() {
    return this._attrName;
  },

  get parameterBindings() {
    return this._parameterBindings;
  },

  /**
   * Bind a parameter value to the attribute definition, allowing use of the
   *  attribute-parameter as an attribute.
   *
   * @returns
   */
  bindParameter(aValue) {
    // people probably shouldn't call us with null, but handle it
    if (aValue == null) {
      return this._id;
    }
    if (aValue in this._parameterBindings) {
      return this._parameterBindings[aValue];
    }
    // no database entry exists if we are here, so we must create it...
    let id = this._datastore._createAttributeDef(
      this._attrType,
      this._pluginName,
      this._attrName,
      aValue
    );
    this._parameterBindings[aValue] = id;
    this._datastore.reportBinding(id, this, aValue);
    return id;
  },

  /**
   * Given a list of values, return a list (regardless of plurality) of
   *  database-ready [attribute id,  value] tuples.  This is intended to be used
   *  to directly convert the value of a property on an object that corresponds
   *  to a bound attribute.
   *
   * @param {Array} aInstanceValues An array of instance values regardless of
   *     whether or not the attribute is singular.
   */
  convertValuesToDBAttributes(aInstanceValues) {
    let nounDef = this.attrDef.objectNounDef;
    let dbAttributes = [];
    if (nounDef.usesParameter) {
      for (let instanceValue of aInstanceValues) {
        let [param, dbValue] = nounDef.toParamAndValue(instanceValue);
        dbAttributes.push([this.bindParameter(param), dbValue]);
      }
    } else if ("toParamAndValue" in nounDef) {
      // Not generating any attributes is ok. This basically means the noun is
      // just an informative property on the Gloda Message and has no real
      // indexing purposes.
      for (let instanceValue of aInstanceValues) {
        dbAttributes.push([
          this._id,
          nounDef.toParamAndValue(instanceValue)[1],
        ]);
      }
    }
    return dbAttributes;
  },

  toString() {
    return this._compoundName;
  },
};

var GlodaHasAttributesMixIn = {
  *enumerateAttributes() {
    let nounDef = this.NOUN_DEF;
    for (let key in this) {
      let value = this[key];
      let attrDef = nounDef.attribsByBoundName[key];
      // we expect to not have attributes for underscore prefixed values (those
      //  are managed by the instance's logic.  we also want to not explode
      //  should someone crap other values in there, we get both birds with this
      //  one stone.
      if (attrDef === undefined) {
        continue;
      }
      if (attrDef.singular) {
        // ignore attributes with null values
        if (value != null) {
          yield [attrDef, [value]];
        }
      } else if (value.length) {
        // ignore attributes with no values
        yield [attrDef, value];
      }
    }
  },

  domContribute(aDomNode) {
    let nounDef = this.NOUN_DEF;
    for (let attrName in nounDef.domExposeAttribsByBoundName) {
      let attr = nounDef.domExposeAttribsByBoundName[attrName];
      if (this[attrName]) {
        aDomNode.setAttribute(attr.domExpose, this[attrName]);
      }
    }
  },
};

function MixIn(aConstructor, aMixIn) {
  let proto = aConstructor.prototype;
  for (let [name, func] of Object.entries(aMixIn)) {
    if (name.startsWith("get_")) {
      proto.__defineGetter__(name.substring(4), func);
    } else {
      proto[name] = func;
    }
  }
}

/**
 * @class A gloda wrapper around nsIMsgIncomingServer.
 */
function GlodaAccount(aIncomingServer) {
  this._incomingServer = aIncomingServer;
}

GlodaAccount.prototype = {
  NOUN_ID: 106,
  get id() {
    return this._incomingServer.key;
  },
  get name() {
    return this._incomingServer.prettyName;
  },
  get incomingServer() {
    return this._incomingServer;
  },
  toString() {
    return "Account: " + this.id;
  },

  toLocaleString() {
    return this.name;
  },
};

/**
 * @class A gloda conversation (thread) exists so that messages can belong.
 */
function GlodaConversation(
  aDatastore,
  aID,
  aSubject,
  aOldestMessageDate,
  aNewestMessageDate
) {
  // _datastore is now set on the prototype by GlodaDatastore
  this._id = aID;
  this._subject = aSubject;
  this._oldestMessageDate = aOldestMessageDate;
  this._newestMessageDate = aNewestMessageDate;
}

GlodaConversation.prototype = {
  NOUN_ID: GlodaConstants.NOUN_CONVERSATION,
  // set by GlodaDatastore
  _datastore: null,
  get id() {
    return this._id;
  },
  get subject() {
    return this._subject;
  },
  get oldestMessageDate() {
    return this._oldestMessageDate;
  },
  get newestMessageDate() {
    return this._newestMessageDate;
  },

  getMessagesCollection(aListener, aData) {
    let query = new GlodaMessage.prototype.NOUN_DEF.queryClass();
    query.conversation(this._id).orderBy("date");
    return query.getCollection(aListener, aData);
  },

  toString() {
    return "Conversation:" + this._id;
  },

  toLocaleString() {
    return this._subject;
  },
};

function GlodaFolder(
  aDatastore,
  aID,
  aURI,
  aDirtyStatus,
  aPrettyName,
  aIndexingPriority
) {
  // _datastore is now set by GlodaDatastore
  this._id = aID;
  this._uri = aURI;
  this._dirtyStatus = aDirtyStatus;
  this._prettyName = aPrettyName;
  this._account = null;
  this._activeIndexing = false;
  this._indexingPriority = aIndexingPriority;
  this._deleted = false;
  this._compacting = false;
}

GlodaFolder.prototype = {
  NOUN_ID: GlodaConstants.NOUN_FOLDER,
  // set by GlodaDatastore
  _datastore: null,

  /** The folder is believed to be up-to-date */
  kFolderClean: 0,
  /** The folder has some un-indexed or dirty messages */
  kFolderDirty: 1,
  /** The folder needs to be entirely re-indexed, regardless of the flags on
   * the messages in the folder. This state will be downgraded to dirty */
  kFolderFilthy: 2,

  _kFolderDirtyStatusMask: 0x7,
  /**
   * The (local) folder has been compacted and all of its message keys are
   *  potentially incorrect.  This is not a possible state for IMAP folders
   *  because their message keys are based on UIDs rather than offsets into
   *  the mbox file.
   */
  _kFolderCompactedFlag: 0x8,

  /** The folder should never be indexed. */
  kIndexingNeverPriority: -1,
  /** The lowest priority assigned to a folder. */
  kIndexingLowestPriority: 0,
  /** The highest priority assigned to a folder. */
  kIndexingHighestPriority: 100,

  /** The indexing priority for a folder if no other priority is assigned. */
  kIndexingDefaultPriority: 20,
  /** Folders marked check new are slightly more important I guess. */
  kIndexingCheckNewPriority: 30,
  /** Favorite folders are more interesting to the user, presumably. */
  kIndexingFavoritePriority: 40,
  /** The indexing priority for inboxes. */
  kIndexingInboxPriority: 50,
  /** The indexing priority for sent mail folders. */
  kIndexingSentMailPriority: 60,

  get id() {
    return this._id;
  },
  get uri() {
    return this._uri;
  },
  get dirtyStatus() {
    return this._dirtyStatus & this._kFolderDirtyStatusMask;
  },
  /**
   * Mark a folder as dirty if it was clean.  Do nothing if it was already dirty
   *  or filthy.  For use by GlodaMsgIndexer only.  And maybe rkent and his
   *  marvelous extensions.
   */
  _ensureFolderDirty() {
    if (this.dirtyStatus == this.kFolderClean) {
      this._dirtyStatus =
        (this.kFolderDirty & this._kFolderDirtyStatusMask) |
        (this._dirtyStatus & ~this._kFolderDirtyStatusMask);
      this._datastore.updateFolderDirtyStatus(this);
    }
  },
  /**
   * Definitely for use only by GlodaMsgIndexer to downgrade the dirty status of
   *  a folder.
   */
  _downgradeDirtyStatus(aNewStatus) {
    if (this.dirtyStatus != aNewStatus) {
      this._dirtyStatus =
        (aNewStatus & this._kFolderDirtyStatusMask) |
        (this._dirtyStatus & ~this._kFolderDirtyStatusMask);
      this._datastore.updateFolderDirtyStatus(this);
    }
  },
  /**
   * Indicate whether this folder is currently being compacted.  The
   *  |GlodaMsgIndexer| keeps this in-memory-only value up-to-date.
   */
  get compacting() {
    return this._compacting;
  },
  /**
   * Set whether this folder is currently being compacted.  This is really only
   *  for the |GlodaMsgIndexer| to set.
   */
  set compacting(aCompacting) {
    this._compacting = aCompacting;
  },
  /**
   * Indicate whether this folder was compacted and has not yet been
   *  compaction processed.
   */
  get compacted() {
    return Boolean(this._dirtyStatus & this._kFolderCompactedFlag);
  },
  /**
   * For use only by GlodaMsgIndexer to set/clear the compaction state of this
   *  folder.
   */
  _setCompactedState(aCompacted) {
    if (this.compacted != aCompacted) {
      if (aCompacted) {
        this._dirtyStatus |= this._kFolderCompactedFlag;
      } else {
        this._dirtyStatus &= ~this._kFolderCompactedFlag;
      }
      this._datastore.updateFolderDirtyStatus(this);
    }
  },

  get name() {
    return this._prettyName;
  },
  toString() {
    return "Folder:" + this._id;
  },

  toLocaleString() {
    let xpcomFolder = this.getXPCOMFolder(this.kActivityFolderOnlyNoData);
    if (!xpcomFolder) {
      return this._prettyName;
    }
    return (
      xpcomFolder.prettyName + " (" + xpcomFolder.rootFolder.prettyName + ")"
    );
  },

  get indexingPriority() {
    return this._indexingPriority;
  },

  /** We are going to index this folder. */
  kActivityIndexing: 0,
  /** Asking for the folder to perform header retrievals. */
  kActivityHeaderRetrieval: 1,
  /** We only want the folder for its metadata but are not going to open it. */
  kActivityFolderOnlyNoData: 2,

  /** Is this folder known to be actively used for indexing? */
  _activeIndexing: false,
  /** Get our indexing status. */
  get indexing() {
    return this._activeIndexing;
  },
  /**
   * Set our indexing status.  Normally, this will be enabled through passing
   *  an activity type of kActivityIndexing (which will set us), but we will
   *  still need to be explicitly disabled by the indexing code.
   * When disabling indexing, we will call forgetFolderIfUnused to take care of
   *  shutting things down.
   * We are not responsible for committing changes to the message database!
   *  That is on you!
   */
  set indexing(aIndexing) {
    this._activeIndexing = aIndexing;
  },

  /**
   * Retrieve the nsIMsgFolder instance corresponding to this folder, providing
   *  an explanation of why you are requesting it for tracking/cleanup purposes.
   *
   * @param aActivity One of the kActivity* constants.  If you pass
   *     kActivityIndexing, we will set indexing for you, but you will need to
   *     clear it when you are done.
   * @returns The nsIMsgFolder if available, null on failure.
   */
  getXPCOMFolder(aActivity) {
    switch (aActivity) {
      case this.kActivityIndexing:
        // mark us as indexing, but don't bother with live tracking.  we do
        //  that independently and only for header retrieval.
        this.indexing = true;
        break;
      case this.kActivityHeaderRetrieval:
      case this.kActivityFolderOnlyNoData:
        // we don't have to do anything here.
        break;
    }

    return MailServices.folderLookup.getFolderForURL(this.uri);
  },

  /**
   * Retrieve a GlodaAccount instance corresponding to this folder.
   *
   * @returns The GlodaAccount instance.
   */
  getAccount() {
    if (!this._account) {
      let msgFolder = this.getXPCOMFolder(this.kActivityFolderOnlyNoData);
      this._account = new GlodaAccount(msgFolder.server);
    }
    return this._account;
  },
};

/**
 * @class A message representation.
 */
function GlodaMessage(
  aDatastore,
  aID,
  aFolderID,
  aMessageKey,
  aConversationID,
  aConversation,
  aDate,
  aHeaderMessageID,
  aDeleted,
  aJsonText,
  aNotability,
  aSubject,
  aIndexedBodyText,
  aAttachmentNames
) {
  // _datastore is now set on the prototype by GlodaDatastore
  this._id = aID;
  this._folderID = aFolderID;
  this._messageKey = aMessageKey;
  this._conversationID = aConversationID;
  this._conversation = aConversation;
  this._date = aDate;
  this._headerMessageID = aHeaderMessageID;
  this._jsonText = aJsonText;
  this._notability = aNotability;
  this._subject = aSubject;
  this._indexedBodyText = aIndexedBodyText;
  this._attachmentNames = aAttachmentNames;

  // only set _deleted if we're deleted, otherwise the undefined does our
  //  speaking for us.
  if (aDeleted) {
    this._deleted = aDeleted;
  }
}

GlodaMessage.prototype = {
  NOUN_ID: GlodaConstants.NOUN_MESSAGE,
  // set by GlodaDatastore
  _datastore: null,
  get id() {
    return this._id;
  },
  get folderID() {
    return this._folderID;
  },
  get messageKey() {
    return this._messageKey;
  },
  get conversationID() {
    return this._conversationID;
  },
  // conversation is special
  get headerMessageID() {
    return this._headerMessageID;
  },
  get notability() {
    return this._notability;
  },
  set notability(aNotability) {
    this._notability = aNotability;
  },

  get subject() {
    return this._subject;
  },
  get indexedBodyText() {
    return this._indexedBodyText;
  },
  get attachmentNames() {
    return this._attachmentNames;
  },

  get date() {
    return this._date;
  },
  set date(aNewDate) {
    this._date = aNewDate;
  },

  get folder() {
    // XXX due to a deletion bug it is currently possible to get in a state
    //  where we have an illegal folderID value.  This will result in an
    //  exception.  As a workaround, let's just return null in that case.
    try {
      if (this._folderID != null) {
        return this._datastore._mapFolderID(this._folderID);
      }
    } catch (ex) {}
    return null;
  },
  get folderURI() {
    // XXX just like for folder, handle mapping failures and return null
    try {
      if (this._folderID != null) {
        return this._datastore._mapFolderID(this._folderID).uri;
      }
    } catch (ex) {}
    return null;
  },
  get account() {
    // XXX due to a deletion bug it is currently possible to get in a state
    //  where we have an illegal folderID value.  This will result in an
    //  exception.  As a workaround, let's just return null in that case.
    try {
      if (this._folderID == null) {
        return null;
      }
      let folder = this._datastore._mapFolderID(this._folderID);
      return folder.getAccount();
    } catch (ex) {}
    return null;
  },
  get conversation() {
    return this._conversation;
  },

  toString() {
    // uh, this is a tough one...
    return "Message:" + this._id;
  },

  _clone() {
    return new GlodaMessage(
      /* datastore */ null,
      this._id,
      this._folderID,
      this._messageKey,
      this._conversationID,
      this._conversation,
      this._date,
      this._headerMessageID,
      "_deleted" in this ? this._deleted : undefined,
      "_jsonText" in this ? this._jsonText : undefined,
      this._notability,
      this._subject,
      this._indexedBodyText,
      this._attachmentNames
    );
  },

  /**
   * Provide a means of propagating changed values on our clone back to
   *  ourselves.  This is required because of an object identity trick gloda
   *  does; when indexing an already existing object, all mutations happen on
   *  a clone of the existing object so that
   */
  _declone(aOther) {
    if ("_content" in aOther) {
      this._content = aOther._content;
    }

    // The _indexedAuthor/_indexedRecipients fields don't get updated on
    //  fulltext update so we don't need to propagate.
    this._indexedBodyText = aOther._indexedBodyText;
    this._attachmentNames = aOther._attachmentNames;
  },

  /**
   * Mark this message as a ghost.  Ghosts are characterized by having no folder
   *  id and no message key.  They also are not deleted or they would be of
   *  absolutely no use to us.
   *
   * These changes are suitable for persistence.
   */
  _ghost() {
    this._folderID = null;
    this._messageKey = null;
    if ("_deleted" in this) {
      delete this._deleted;
    }
  },

  /**
   * Are we a ghost (which implies not deleted)?  We are not a ghost if we have
   *  a definite folder location (we may not know our message key in the case
   *  of IMAP moves not fully completed) and are not deleted.
   */
  get _isGhost() {
    return this._folderID == null && !this._isDeleted;
  },

  /**
   * If we were dead, un-dead us.
   */
  _ensureNotDeleted() {
    if ("_deleted" in this) {
      delete this._deleted;
    }
  },

  /**
   * Are we deleted?  This is private because deleted gloda messages are not
   *  visible to non-core-gloda code.
   */
  get _isDeleted() {
    return "_deleted" in this && this._deleted;
  },

  /**
   * Trash this message's in-memory representation because it should no longer
   *  be reachable by any code.  The database record is gone, it's not coming
   *  back.
   */
  _objectPurgedMakeYourselfUnpleasant() {
    this._id = null;
    this._folderID = null;
    this._messageKey = null;
    this._conversationID = null;
    this._conversation = null;
    this.date = null;
    this._headerMessageID = null;
  },

  /**
   * Return the underlying nsIMsgDBHdr from the folder storage for this, or
   *  null if the message does not exist for one reason or another.  We may log
   *  to our logger in the failure cases.
   *
   * This method no longer caches the result, so if you need to hold onto it,
   *  hold onto it.
   *
   * In the process of retrieving the underlying message header, we may have to
   *  open the message header database associated with the folder.  This may
   *  result in blocking while the load happens, so you may want to try and find
   *  an alternate way to initiate the load before calling us.
   * We provide hinting to the GlodaDatastore via the GlodaFolder so that it
   *  knows when it's a good time for it to go and detach from the database.
   *
   * @returns The nsIMsgDBHdr associated with this message if available, null on
   *     failure.
   */
  get folderMessage() {
    if (this._folderID === null || this._messageKey === null) {
      return null;
    }

    // XXX like for folder and folderURI, return null if we can't map the folder
    let glodaFolder;
    try {
      glodaFolder = this._datastore._mapFolderID(this._folderID);
    } catch (ex) {
      return null;
    }
    let folder = glodaFolder.getXPCOMFolder(
      glodaFolder.kActivityHeaderRetrieval
    );
    if (folder) {
      let folderMessage;
      try {
        folderMessage = folder.GetMessageHeader(this._messageKey);
      } catch (ex) {
        folderMessage = null;
      }
      if (folderMessage !== null) {
        // verify the message-id header matches what we expect...
        if (folderMessage.messageId != this._headerMessageID) {
          LOG.info(
            "Message with message key " +
              this._messageKey +
              " in folder '" +
              folder.URI +
              "' does not match expected " +
              "header! (" +
              this._headerMessageID +
              " expected, got " +
              folderMessage.messageId +
              ")"
          );
          folderMessage = null;
        }
      }
      return folderMessage;
    }

    // this only gets logged if things have gone very wrong.  we used to throw
    //  here, but it's unlikely our caller can do anything more meaningful than
    //  treating this as a disappeared message.
    LOG.info(
      "Unable to locate folder message for: " +
        this._folderID +
        ":" +
        this._messageKey
    );
    return null;
  },
  get folderMessageURI() {
    let folderMessage = this.folderMessage;
    if (folderMessage) {
      return folderMessage.folder.getUriForMsg(folderMessage);
    }
    return null;
  },
};
MixIn(GlodaMessage, GlodaHasAttributesMixIn);

/**
 * @class Contacts correspond to people (one per person), and may own multiple
 *  identities (e-mail address, IM account, etc.)
 */
function GlodaContact(
  aDatastore,
  aID,
  aDirectoryUUID,
  aContactUUID,
  aName,
  aPopularity,
  aFrecency,
  aJsonText
) {
  // _datastore set on the prototype by GlodaDatastore
  this._id = aID;
  this._directoryUUID = aDirectoryUUID;
  this._contactUUID = aContactUUID;
  this._name = aName;
  this._popularity = aPopularity;
  this._frecency = aFrecency;
  if (aJsonText) {
    this._jsonText = aJsonText;
  }

  this._identities = null;
}

GlodaContact.prototype = {
  NOUN_ID: GlodaConstants.NOUN_CONTACT,
  // set by GlodaDatastore
  _datastore: null,

  get id() {
    return this._id;
  },
  get directoryUUID() {
    return this._directoryUUID;
  },
  get contactUUID() {
    return this._contactUUID;
  },
  get name() {
    return this._name;
  },
  set name(aName) {
    this._name = aName;
  },

  get popularity() {
    return this._popularity;
  },
  set popularity(aPopularity) {
    this._popularity = aPopularity;
    this.dirty = true;
  },

  get frecency() {
    return this._frecency;
  },
  set frecency(aFrecency) {
    this._frecency = aFrecency;
    this.dirty = true;
  },

  get identities() {
    return this._identities;
  },

  toString() {
    return "Contact:" + this._id;
  },

  get accessibleLabel() {
    return "Contact: " + this._name;
  },

  _clone() {
    return new GlodaContact(
      /* datastore */ null,
      this._id,
      this._directoryUUID,
      this._contactUUID,
      this._name,
      this._popularity,
      this._frecency
    );
  },
};
MixIn(GlodaContact, GlodaHasAttributesMixIn);

/**
 * @class A specific means of communication for a contact.
 */
function GlodaIdentity(
  aDatastore,
  aID,
  aContactID,
  aContact,
  aKind,
  aValue,
  aDescription,
  aIsRelay
) {
  // _datastore set on the prototype by GlodaDatastore
  this._id = aID;
  this._contactID = aContactID;
  this._contact = aContact;
  this._kind = aKind;
  this._value = aValue;
  this._description = aDescription;
  this._isRelay = aIsRelay;
  // Cached indication of whether there is an address book card for this
  //  identity.  We keep this up-to-date via address book listener
  //  notifications in |GlodaABIndexer|.
  this._hasAddressBookCard = undefined;
}

GlodaIdentity.prototype = {
  NOUN_ID: GlodaConstants.NOUN_IDENTITY,
  // set by GlodaDatastore
  _datastore: null,
  get id() {
    return this._id;
  },
  get contactID() {
    return this._contactID;
  },
  get contact() {
    return this._contact;
  },
  get kind() {
    return this._kind;
  },
  get value() {
    return this._value;
  },
  get description() {
    return this._description;
  },
  get isRelay() {
    return this._isRelay;
  },

  get uniqueValue() {
    return this._kind + "@" + this._value;
  },

  toString() {
    return "Identity:" + this._kind + ":" + this._value;
  },

  toLocaleString() {
    if (this.contact.name == this.value) {
      return this.value;
    }
    return this.contact.name + " : " + this.value;
  },

  get abCard() {
    // for our purposes, the address book only speaks email
    if (this._kind != "email") {
      return false;
    }
    let card = MailServices.ab.cardForEmailAddress(this._value);
    this._hasAddressBookCard = card != null;
    return card;
  },

  /**
   * Indicates whether we have an address book card for this identity.  This
   *  value is cached once looked-up and kept up-to-date by |GlodaABIndexer|
   *  and its notifications.
   */
  get inAddressBook() {
    if (this._hasAddressBookCard !== undefined) {
      return this._hasAddressBookCard;
    }
    return (this.abCard && true) || false;
  },
};

/**
 * An attachment, with as much information as we can gather on it
 */
function GlodaAttachment(
  aGlodaMessage,
  aName,
  aContentType,
  aSize,
  aPart,
  aExternalUrl,
  aIsExternal
) {
  // _datastore set on the prototype by GlodaDatastore
  this._glodaMessage = aGlodaMessage;
  this._name = aName;
  this._contentType = aContentType;
  this._size = aSize;
  this._part = aPart;
  this._externalUrl = aExternalUrl;
  this._isExternal = aIsExternal;
}

GlodaAttachment.prototype = {
  NOUN_ID: GlodaConstants.NOUN_ATTACHMENT,
  // set by GlodaDatastore
  get name() {
    return this._name;
  },
  get contentType() {
    return this._contentType;
  },
  get size() {
    return this._size;
  },
  get url() {
    if (this.isExternal) {
      return this._externalUrl;
    }

    let uri = this._glodaMessage.folderMessageURI;
    if (!uri) {
      throw new Error(
        "The message doesn't exist anymore, unable to rebuild attachment URL"
      );
    }
    let msgService = MailServices.messageServiceFromURI(uri);
    let neckoURL = msgService.getUrlForUri(uri);
    let url = neckoURL.spec;
    let hasParamAlready = url.match(/\?[a-z]+=[^\/]+$/);
    let sep = hasParamAlready ? "&" : "?";
    return (
      url +
      sep +
      "part=" +
      this._part +
      "&filename=" +
      encodeURIComponent(this._name)
    );
  },
  get isExternal() {
    return this._isExternal;
  },

  toString() {
    return "attachment: " + this._name + ":" + this._contentType;
  },
};