Skip to content

Commit

Permalink
ES6: migrated "storage"
Browse files Browse the repository at this point in the history
  • Loading branch information
Gregor Herdmann committed Apr 26, 2017
1 parent f05c47d commit 3aed7b1
Show file tree
Hide file tree
Showing 21 changed files with 740 additions and 592 deletions.
5 changes: 3 additions & 2 deletions .eslintrc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ globals:
cryptobox: true
CryptoJS: true
dcodeIO: true
hljs: true,
Dexie: true
hljs: true
ko: true
libsodium: true
LRUCache: true
marked: true,
marked: true
moment: true
pako: true
platform: true
Expand Down
6 changes: 4 additions & 2 deletions app/script/audio/AudioRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,10 @@ window.z.audio.AudioRepository = class AudioRepository {
*/
_init_sounds() {
for (const type in z.audio.AudioType) {
const audio_id = z.audio.AudioType[type];
this.audio_elements[audio_id] = this._create_audio_element(`/audio/${audio_id}.mp3`);
if (z.audio.AudioType.hasOwnProperty(type)) {
const audio_id = z.audio.AudioType[type];
this.audio_elements[audio_id] = this._create_audio_element(`/audio/${audio_id}.mp3`);
}
}
this.logger.info('Initialized sounds');
}
Expand Down
10 changes: 5 additions & 5 deletions app/script/client/ClientService.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,15 +140,15 @@ z.client.ClientService = class ClientService {
* @returns {Promise} Resolves once the client is deleted
*/
delete_client_from_db(primary_key) {
return this.storage_service.delete(this.storage_service.OBJECT_STORE_CLIENTS, primary_key);
return this.storage_service.delete(z.storage.StorageService.OBJECT_STORE.CLIENTS, primary_key);
}

/**
* Load all clients we have stored in the database.
* @returns {Promise} Resolves with all the clients payloads
*/
load_all_clients_from_db() {
return this.storage_service.get_all(this.storage_service.OBJECT_STORE_CLIENTS);
return this.storage_service.get_all(z.storage.StorageService.OBJECT_STORE.CLIENTS);
}

/**
Expand All @@ -157,7 +157,7 @@ z.client.ClientService = class ClientService {
* @returns {Promise<JSON|string>} Resolves with the client's payload or the primary key if not found
*/
load_client_from_db(primary_key) {
return this.storage_service.db[this.storage_service.OBJECT_STORE_CLIENTS]
return this.storage_service.db[z.storage.StorageService.OBJECT_STORE.CLIENTS]
.where('meta.primary_key')
.equals(primary_key)
.first()
Expand Down Expand Up @@ -185,7 +185,7 @@ z.client.ClientService = class ClientService {

client_payload.meta.primary_key = primary_key;

return this.storage_service.save(this.storage_service.OBJECT_STORE_CLIENTS, primary_key, client_payload)
return this.storage_service.save(z.storage.StorageService.OBJECT_STORE.CLIENTS, primary_key, client_payload)
.then(() => {
this.logger.info(`Client '${client_payload.id}' stored with primary key '${primary_key}'`, client_payload);
return client_payload;
Expand All @@ -200,6 +200,6 @@ z.client.ClientService = class ClientService {
* @returns {Promise<Integer>} Number of updated records (1 if an object was updated, otherwise 0)
*/
update_client_in_db(primary_key, changes) {
return this.storage_service.update(this.storage_service.OBJECT_STORE_CLIENTS, primary_key, changes);
return this.storage_service.update(z.storage.StorageService.OBJECT_STORE.CLIENTS, primary_key, changes);
}
};
2 changes: 1 addition & 1 deletion app/script/conversation/ConversationRepository.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -2100,7 +2100,7 @@ class z.conversation.ConversationRepository
@logger.debug "Updating reactions to message '#{event_json.data.message_id}' in conversation '#{conversation_et.id}'", event_json
return @conversation_service.update_message_in_db message_et, changes, conversation_et.id
.catch (error) =>
if error.type is z.storage.StorageError::TYPE.NON_SEQUENTIAL_UPDATE
if error.type is z.storage.StorageError.TYPE.NON_SEQUENTIAL_UPDATE
Raygun.send 'Failed sequential database update'
if error.type isnt z.conversation.ConversationError::TYPE.MESSAGE_NOT_FOUND
@logger.error "Failed to handle reaction to message '#{event_json.data.message_id}' in conversation '#{conversation_et.id}'", {error: error, event: event_json}
Expand Down
46 changes: 23 additions & 23 deletions app/script/conversation/ConversationService.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ class z.conversation.ConversationService
@return [Promise] Resolves with the number of deleted records
###
delete_message_with_key_from_db: (conversation_id, primary_key) ->
@storage_service.db[@storage_service.OBJECT_STORE_EVENTS].delete primary_key
@storage_service.db[z.storage.StorageService.OBJECT_STORE.EVENTS].delete primary_key

###
Delete a message from a conversation. Duplicates are delete as well.
Expand All @@ -146,7 +146,7 @@ class z.conversation.ConversationService
@return [Promise] Resolves with the number of deleted records
###
delete_message_from_db: (conversation_id, message_id) ->
@storage_service.db[@storage_service.OBJECT_STORE_EVENTS]
@storage_service.db[z.storage.StorageService.OBJECT_STORE.EVENTS]
.where 'conversation'
.equals conversation_id
.and (record) -> record.id is message_id
Expand All @@ -157,7 +157,7 @@ class z.conversation.ConversationService
@param conversation_id [String] Delete messages for this conversation
###
delete_messages_from_db: (conversation_id) ->
@storage_service.db[@storage_service.OBJECT_STORE_EVENTS]
@storage_service.db[z.storage.StorageService.OBJECT_STORE.EVENTS]
.where 'conversation'
.equals conversation_id
.delete()
Expand All @@ -172,14 +172,14 @@ class z.conversation.ConversationService
.then (primary_key) =>
if Object.keys(changes).length
if changes.version
return @storage_service.db.transaction 'rw', @storage_service.OBJECT_STORE_EVENTS, =>
return @storage_service.db.transaction 'rw', z.storage.StorageService.OBJECT_STORE.EVENTS, =>
@load_event_from_db conversation_id, message_et.id
.then (record) =>
if record and changes.version is (record.version or 1) + 1
return @storage_service.update @storage_service.OBJECT_STORE_EVENTS, primary_key, changes
throw new z.storage.StorageError z.storage.StorageError::TYPE.NON_SEQUENTIAL_UPDATE
return @storage_service.update z.storage.StorageService.OBJECT_STORE.EVENTS, primary_key, changes
throw new z.storage.StorageError z.storage.StorageError.TYPE.NON_SEQUENTIAL_UPDATE

return @storage_service.update @storage_service.OBJECT_STORE_EVENTS, primary_key, changes
return @storage_service.update z.storage.StorageService.OBJECT_STORE.EVENTS, primary_key, changes

throw new z.conversation.ConversationError z.conversation.ConversationError::TYPE.NO_CHANGES

Expand All @@ -188,15 +188,15 @@ class z.conversation.ConversationService
@param primary_key [String] Primary key used to find an event in the database
###
update_asset_as_uploaded_in_db: (primary_key, asset_data) ->
@storage_service.load @storage_service.OBJECT_STORE_EVENTS, primary_key
@storage_service.load z.storage.StorageService.OBJECT_STORE.EVENTS, primary_key
.then (record) =>
record.data.id = asset_data.id
record.data.otr_key = asset_data.otr_key
record.data.sha256 = asset_data.sha256
record.data.key = asset_data.key
record.data.token = asset_data.token
record.data.status = z.assets.AssetTransferState.UPLOADED
@storage_service.update @storage_service.OBJECT_STORE_EVENTS, primary_key, record
@storage_service.update z.storage.StorageService.OBJECT_STORE.EVENTS, primary_key, record
.then =>
@logger.info 'Updated asset message_et (uploaded)', primary_key

Expand All @@ -205,14 +205,14 @@ class z.conversation.ConversationService
@param primary_key [String] Primary key used to find an event in the database
###
update_asset_preview_in_db: (primary_key, asset_data) ->
@storage_service.load @storage_service.OBJECT_STORE_EVENTS, primary_key
@storage_service.load z.storage.StorageService.OBJECT_STORE.EVENTS, primary_key
.then (record) =>
record.data.preview_id = asset_data.id
record.data.preview_otr_key = asset_data.otr_key
record.data.preview_sha256 = asset_data.sha256
record.data.preview_key = asset_data.key
record.data.preview_token = asset_data.token
@storage_service.update @storage_service.OBJECT_STORE_EVENTS, primary_key, record
@storage_service.update z.storage.StorageService.OBJECT_STORE.EVENTS, primary_key, record
.then =>
@logger.info 'Updated asset message_et (preview)', primary_key

Expand All @@ -221,11 +221,11 @@ class z.conversation.ConversationService
@param primary_key [String] Primary key used to find an event in the database
###
update_asset_as_failed_in_db: (primary_key, reason) ->
@storage_service.load @storage_service.OBJECT_STORE_EVENTS, primary_key
@storage_service.load z.storage.StorageService.OBJECT_STORE.EVENTS, primary_key
.then (record) =>
record.data.status = z.assets.AssetTransferState.UPLOAD_FAILED
record.data.reason = reason
@storage_service.update @storage_service.OBJECT_STORE_EVENTS, primary_key, record
@storage_service.update z.storage.StorageService.OBJECT_STORE.EVENTS, primary_key, record
.then =>
@logger.info 'Updated asset message_et (failed)', primary_key

Expand All @@ -234,15 +234,15 @@ class z.conversation.ConversationService
@return [Promise] Promise that resolves with all the stored conversation states
###
load_conversation_states_from_db: =>
@storage_service.get_all @storage_service.OBJECT_STORE_CONVERSATIONS
@storage_service.get_all z.storage.StorageService.OBJECT_STORE.CONVERSATIONS

###
Load conversation event.
@param conversation_id [String] ID of conversation
@param message_id [String]
###
load_event_from_db: (conversation_id, message_id) ->
@storage_service.db[@storage_service.OBJECT_STORE_EVENTS]
@storage_service.db[z.storage.StorageService.OBJECT_STORE.EVENTS]
.where 'conversation'
.equals conversation_id
.filter (record) -> record.id is message_id
Expand All @@ -255,7 +255,7 @@ class z.conversation.ConversationService
min_date = new Date()
min_date.setDate min_date.getDate() - 30

@storage_service.db[@storage_service.OBJECT_STORE_EVENTS]
@storage_service.db[z.storage.StorageService.OBJECT_STORE.EVENTS]
.where 'time'
.between min_date.toISOString(), new Date().toISOString()
.toArray()
Expand Down Expand Up @@ -286,7 +286,7 @@ class z.conversation.ConversationService
else if lower_bound.getTime() > upper_bound.getTime()
throw new Error "Lower bound (#{lower_bound.getTime()}) cannot be greater than upper bound (#{upper_bound.getTime()})."

@storage_service.db[@storage_service.OBJECT_STORE_EVENTS]
@storage_service.db[z.storage.StorageService.OBJECT_STORE.EVENTS]
.where '[conversation+time]'
.between [conversation_id, lower_bound.toISOString()], [conversation_id, upper_bound.toISOString()], true, false
.reverse()
Expand All @@ -309,7 +309,7 @@ class z.conversation.ConversationService
if not _.isDate upper_bound
throw new Error "Upper bound (#{typeof upper_bound}) must be of type 'Date'."

@storage_service.db[@storage_service.OBJECT_STORE_EVENTS]
@storage_service.db[z.storage.StorageService.OBJECT_STORE.EVENTS]
.where '[conversation+time]'
.between [conversation_id, upper_bound.toISOString()], [conversation_id, new Date().toISOString()], include_upper_bound, true
.limit limit
Expand All @@ -323,7 +323,7 @@ class z.conversation.ConversationService
@return [Promise]
###
load_events_with_category_from_db: (conversation_id, category_min, category_max = z.message.MessageCategory.LIKED) ->
@storage_service.db[@storage_service.OBJECT_STORE_EVENTS]
@storage_service.db[z.storage.StorageService.OBJECT_STORE.EVENTS]
.where '[conversation+category]'
.between [conversation_id, category_min], [conversation_id, category_max], true, true
.sortBy 'time'
Expand All @@ -335,7 +335,7 @@ class z.conversation.ConversationService
###
save_event: (event) ->
event.category = z.message.MessageCategorization.category_from_event event
@storage_service.save(@storage_service.OBJECT_STORE_EVENTS, undefined, event).then -> event
@storage_service.save(z.storage.StorageService.OBJECT_STORE.EVENTS, undefined, event).then -> event

###
Load conversation events by event type.
Expand All @@ -344,7 +344,7 @@ class z.conversation.ConversationService
@return [Promise] Promise that resolves with the retrieved records
###
load_events_with_types: (event_types) ->
return @storage_service.db[@storage_service.OBJECT_STORE_EVENTS]
return @storage_service.db[z.storage.StorageService.OBJECT_STORE.EVENTS]
.where 'type'
.anyOf event_types
.sortBy 'time'
Expand Down Expand Up @@ -432,15 +432,15 @@ class z.conversation.ConversationService
###
save_conversations_in_db: (conversations) =>
keys = conversations.map (conversation) -> conversation.id
@storage_service.db[@storage_service.OBJECT_STORE_CONVERSATIONS].bulkPut(conversations, keys).then -> conversations
@storage_service.db[z.storage.StorageService.OBJECT_STORE.CONVERSATIONS].bulkPut(conversations, keys).then -> conversations

###
Saves a conversation entity in the local database.
@param conversation_et [z.entity.Conversation] Conversation entity
@return [Promise<String|z.entity.Conversation>] Promise which resolves with the conversation entity
###
save_conversation_state_in_db: (conversation_et) =>
@storage_service.save @storage_service.OBJECT_STORE_CONVERSATIONS, conversation_et.id, conversation_et.serialize()
@storage_service.save z.storage.StorageService.OBJECT_STORE.CONVERSATIONS, conversation_et.id, conversation_et.serialize()
.then (primary_key) =>
@logger.log @logger.levels.INFO, "State of conversation '#{primary_key}' was stored"
return conversation_et
Expand Down
4 changes: 2 additions & 2 deletions app/script/conversation/ConversationServiceNoCompound.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class z.conversation.ConversationServiceNoCompound extends z.conversation.Conver
lower_bound = lower_bound.getTime()
upper_bound = upper_bound.getTime()

@storage_service.db[@storage_service.OBJECT_STORE_EVENTS]
@storage_service.db[z.storage.StorageService.OBJECT_STORE.EVENTS]
.where 'conversation'
.equals conversation_id
.reverse()
Expand All @@ -63,7 +63,7 @@ class z.conversation.ConversationServiceNoCompound extends z.conversation.Conver
@return [Promise]
###
load_events_with_category_from_db: (conversation_id, category) ->
@storage_service.db[@storage_service.OBJECT_STORE_EVENTS]
@storage_service.db[z.storage.StorageService.OBJECT_STORE.EVENTS]
.where 'conversation'
.equals conversation_id
.sortBy 'time'
Expand Down
4 changes: 2 additions & 2 deletions app/script/event/NotificationService.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ z.event.NotificationService = class NotificationService {
* @returns {Promise} Resolves with the stored last notification ID.
*/
get_last_notification_id_from_db() {
return this.storage_service.load(this.storage_service.OBJECT_STORE_AMPLIFY, NotificationService.CONFIG.PRIMARY_KEY_LAST_NOTIFICATION)
return this.storage_service.load(z.storage.StorageService.OBJECT_STORE.AMPLIFY, NotificationService.CONFIG.PRIMARY_KEY_LAST_NOTIFICATION)
.then(function(record) {
if (record && record.value) {
return record.value;
Expand All @@ -104,6 +104,6 @@ z.event.NotificationService = class NotificationService {
* @returns {Promise} Resolves with the stored record
*/
save_last_notification_id_to_db(notification_id) {
return this.storage_service.save(this.storage_service.OBJECT_STORE_AMPLIFY, NotificationService.CONFIG.PRIMARY_KEY_LAST_NOTIFICATION, {value: notification_id});
return this.storage_service.save(z.storage.StorageService.OBJECT_STORE.AMPLIFY, NotificationService.CONFIG.PRIMARY_KEY_LAST_NOTIFICATION, {value: notification_id});
}
};
62 changes: 0 additions & 62 deletions app/script/storage/StorageError.coffee

This file was deleted.

Loading

0 comments on commit 3aed7b1

Please sign in to comment.