database.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * vim: ts=4:sw=4:expandtab
  3. */
  4. (function () {
  5. 'use strict';
  6. window.Whisper = window.Whisper || {};
  7. window.Whisper.Database = window.Whisper.Database || {};
  8. window.Whisper.Database.id = window.Whisper.Database.id || 'signal';
  9. window.Whisper.Database.nolog = true;
  10. Whisper.Database.migrations = [
  11. {
  12. version: "1.0",
  13. migrate: function(transaction, next) {
  14. console.log('migration 1.0');
  15. console.log('creating object stores');
  16. var messages = transaction.db.createObjectStore("messages");
  17. messages.createIndex("conversation", ["conversationId", "received_at"], { unique: false });
  18. messages.createIndex("receipt", "sent_at", { unique: false });
  19. var conversations = transaction.db.createObjectStore("conversations");
  20. conversations.createIndex("inbox", "active_at", { unique: false });
  21. conversations.createIndex("group", "members", { unique: false, multiEntry: true });
  22. conversations.createIndex("type", "type", { unique: false });
  23. var groups = transaction.db.createObjectStore('groups');
  24. var sessions = transaction.db.createObjectStore('sessions');
  25. var identityKeys = transaction.db.createObjectStore('identityKeys');
  26. var preKeys = transaction.db.createObjectStore("preKeys");
  27. var signedPreKeys = transaction.db.createObjectStore("signedPreKeys");
  28. var items = transaction.db.createObjectStore("items");
  29. next();
  30. }
  31. },
  32. {
  33. version: "2.0",
  34. migrate: function(transaction, next) {
  35. console.log('migration 2.0');
  36. var conversations = transaction.objectStore("conversations");
  37. conversations.createIndex("search", "tokens", { unique: false, multiEntry: true });
  38. window.addEventListener('storage_ready', function() {
  39. console.log('migrating search tokens');
  40. var all = new Whisper.ConversationCollection();
  41. all.fetch().then(function() {
  42. all.each(function(model) {
  43. model.updateTokens();
  44. model.save();
  45. });
  46. });
  47. });
  48. next();
  49. }
  50. },
  51. {
  52. version: "3.0",
  53. migrate: function(transaction, next) {
  54. console.log('migration 3.0');
  55. var conversations = transaction.objectStore("items");
  56. window.addEventListener('storage_ready', function() {
  57. console.log('migrating unread count');
  58. var all = new Whisper.ConversationCollection();
  59. all.fetch().then(function() {
  60. var unreadCount = all.reduce(function(total, model) {
  61. var count = model.get('unreadCount');
  62. if (count === undefined) {
  63. count = 0;
  64. }
  65. return total + count;
  66. }, 0);
  67. storage.remove('unreadCount');
  68. storage.put('unreadCount', unreadCount);
  69. });
  70. });
  71. next();
  72. }
  73. },
  74. {
  75. version: "4.0",
  76. migrate: function(transaction, next) {
  77. console.log('migration 4.0');
  78. window.addEventListener('storage_ready', function() {
  79. console.log('migrating search tokens');
  80. var all = new Whisper.ConversationCollection();
  81. all.fetch().then(function() {
  82. all.each(function(c) {
  83. c.updateTokens();
  84. c.save();
  85. });
  86. });
  87. });
  88. next();
  89. }
  90. },
  91. {
  92. version: "5.0",
  93. migrate: function(transaction, next) {
  94. console.log('migration 5.0');
  95. window.addEventListener('storage_ready', function() {
  96. console.log('migrating registration flags');
  97. if (storage.get("chromiumRegistrationDone") === "") {
  98. storage.put("chromiumRegistrationDoneEver", "");
  99. }
  100. });
  101. next();
  102. }
  103. },
  104. {
  105. version: "6.0",
  106. migrate: function(transaction, next) {
  107. console.log('migration 6.0');
  108. window.addEventListener('storage_ready', function() {
  109. console.log('migrating registration flags');
  110. storage.onready(function() {
  111. if (storage.get("chromiumRegistrationDone") === "") {
  112. storage.put("chromiumRegistrationDoneEver", "");
  113. next();
  114. }
  115. });
  116. });
  117. next();
  118. }
  119. },
  120. {
  121. version: "7.0",
  122. migrate: function(transaction, next) {
  123. console.log('migration 7.0');
  124. console.log('creating debug log');
  125. transaction.db.createObjectStore("debug");
  126. next();
  127. }
  128. },
  129. {
  130. version: "8.0",
  131. migrate: function(transaction, next) {
  132. console.log('migration 8.0');
  133. console.log('creating unread message index');
  134. var conversations = transaction.objectStore('messages');
  135. conversations.createIndex('unread', ['conversationId', 'unread'], { unique: false });
  136. next();
  137. }
  138. },
  139. {
  140. version: "9.0",
  141. migrate: function(transaction, next) {
  142. console.log('migration 9.0');
  143. window.addEventListener('storage_ready', function() {
  144. console.log('marking contacts and groups active');
  145. var all = new Whisper.ConversationCollection();
  146. var myNumber = textsecure.storage.user.getNumber();
  147. all.fetch().then(function() {
  148. var inactive = all.filter(function(model) {
  149. return !model.get('active_at') && model.id !== myNumber;
  150. });
  151. inactive.sort(function(m1, m2) {
  152. var title1 = m1.getTitle().toLowerCase();
  153. var title2 = m2.getTitle().toLowerCase();
  154. if (title1 === title2) {
  155. return 0;
  156. }
  157. if (title1 < title2) {
  158. return -1;
  159. }
  160. if (title1 > title2) {
  161. return 1;
  162. }
  163. });
  164. inactive.forEach(function(model) {
  165. if (model.isPrivate() || !model.get('left')) {
  166. model.save({ active_at: 1 });
  167. }
  168. });
  169. });
  170. });
  171. next();
  172. }
  173. },
  174. {
  175. version: "10.0",
  176. migrate: function(transaction, next) {
  177. console.log('migration 10.0');
  178. console.log('creating expiring message index');
  179. var messages = transaction.objectStore('messages');
  180. messages.createIndex('expire', 'expireTimer', { unique: false });
  181. next();
  182. }
  183. }
  184. ];
  185. }());