Skip to content

Commit

Permalink
Revert "Unify ephemeral timer (#429)"
Browse files Browse the repository at this point in the history
This reverts commit b65bf77.
  • Loading branch information
rehez committed Nov 11, 2016
1 parent c2f54b2 commit 40fb65b
Show file tree
Hide file tree
Showing 12 changed files with 108 additions and 119 deletions.
19 changes: 5 additions & 14 deletions app/page/template/partials/template-message.htm
Original file line number Diff line number Diff line change
Expand Up @@ -120,17 +120,12 @@
<context-menu params="entries: function() {return $parent.get_context_menu_entries(message)}, tag: 'message', data: message.id"
class="context-menu icon-more font-size-xs"
tabindex="-1"></context-menu>
<!-- ko if: message.ephemeral_status() === z.message.EphemeralStatusType.ACTIVE -->
<time class="time" data-bind="text: message.display_timestamp_short(), attr: {'data-timestamp': message.timestamp, 'data-uie-uid': message.id, 'title': message.ephemeral_caption()}"></time>
<!-- /ko -->
<!-- ko ifnot: message.ephemeral_status() === z.message.EphemeralStatusType.ACTIVE -->
<time class="time" data-bind="text: message.display_timestamp_short(), attr: {'data-timestamp': message.timestamp, 'data-uie-uid': message.id}"></time>
<!-- /ko -->
<time class="time" data-bind="text: message.display_timestamp_short(), attr: {'data-timestamp': message.timestamp, 'data-uie-uid': message.id}" data-uie-name="item-message-timestamp"></time>
<!-- ko if: $parent.is_last_delivered_message(message) -->
<span class="message-status" data-bind="l10n_text: z.string.conversation_message_delivered"></span>
<!-- /ko -->
<!-- ko if: message.ephemeral_status() === z.message.EphemeralStatusType.ACTIVE -->
<ephemeral-timer class="message-ephemeral-timer" data-bind="attr: {'title': message.ephemeral_caption()}" params="message: message"></ephemeral-timer>
<ephemeral-timer class="message-ephemeral-timer" params="expires: message.expire_after_millis()"></ephemeral-timer>
<!-- /ko -->
</div>

Expand Down Expand Up @@ -236,13 +231,9 @@
<context-menu params="entries: function() {return $parent.get_context_menu_entries($data)}, tag: 'message', data: message.id"
class="context-menu icon-more font-size-xs"
tabindex="-1"></context-menu>
<!-- ko if: message.ephemeral_status() === z.message.EphemeralStatusType.ACTIVE -->
<time class="time" data-bind="text: message.display_timestamp_short(), attr: {'data-timestamp': message.timestamp, 'data-uie-uid': message.id, 'title': message.ephemeral_caption()}"></time>
<ephemeral-timer class="message-ephemeral-timer" data-bind="attr: {'title': message.ephemeral_caption()}" params="message: message"></ephemeral-timer>
<!-- /ko -->
<!-- ko ifnot: message.ephemeral_status() === z.message.EphemeralStatusType.ACTIVE -->
<time class="time" data-bind="text: message.display_timestamp_short(), attr: {'data-timestamp': message.timestamp}"></time>
<!-- /ko -->
<time class="time" data-bind="text: message.display_timestamp_short(), attr: {'data-timestamp': message.timestamp}"></time>
<!-- ko if: message.ephemeral_status() === z.message.EphemeralStatusType.ACTIVE -->
<ephemeral-timer class="message-ephemeral-timer" params="expires: message.expire_after_millis()"></ephemeral-timer>
<!-- /ko -->
</div>
</div>
Expand Down
42 changes: 31 additions & 11 deletions app/script/components/ephemeralTimer.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,40 @@ z.components ?= {}

class z.components.EphemeralTimer
constructor: (params) ->
message_et = params.message

@ephemeral_duration = ko.computed => message_et.ephemeral_expires() - message_et.ephemeral_started()
scheduled_time = new Date(params.expires()).getTime()
starting_time = Date.now()
timer = scheduled_time - starting_time
update_interval = 1000 / 10

@progress = ko.observable 0
@remaining_time = ko.observable 0

@remaining_subscription = message_et.ephemeral_remaining.subscribe (remaining_time) =>
if Date.now() >= message_et.ephemeral_expires()
@progress 1
@title_caption = ko.pureComputed =>
title = ''
moment_duration = moment.duration(@remaining_time())
if moment_duration.asHours() is 1
title += "#{moment_duration.hours()} #{z.localization.Localizer.get_text z.string.ephememal_units_hour}, "
else if moment_duration.asHours() > 1
title += "#{moment_duration.hours()} #{z.localization.Localizer.get_text z.string.ephememal_units_hours}, "

if moment_duration.asMinutes() is 1
title += "#{moment_duration.minutes()} #{z.localization.Localizer.get_text z.string.ephememal_units_minute} #{z.localization.Localizer.get_text z.string.and} "
else if moment_duration.asMinutes() > 1
title += "#{moment_duration.minutes()} #{z.localization.Localizer.get_text z.string.ephememal_units_minutes} #{z.localization.Localizer.get_text z.string.and} "

if moment_duration.asSeconds() is 1
title += "#{moment_duration.seconds()} #{z.localization.Localizer.get_text z.string.ephememal_units_second}"
else if moment_duration.asSeconds() > 1
title += "#{moment_duration.seconds()} #{z.localization.Localizer.get_text z.string.ephememal_units_seconds}"

@interval_id = window.setInterval =>
if Date.now() >= scheduled_time
window.clearInterval @interval_id
else
elapsed_time = @ephemeral_duration() - remaining_time
@progress elapsed_time / @ephemeral_duration()
@remaining_time scheduled_time - Date.now()
elapsed_time = timer - @remaining_time()
@progress elapsed_time / timer
, update_interval

@bullet_count = [0..4]

Expand All @@ -42,13 +63,12 @@ class z.components.EphemeralTimer
return 'ephemeral-timer-bullet-inactive' if passed_index

destroy: =>
@remaining_subscription.dispose()
window.clearInterval message_et.ephemeral_interval_id
window.clearInterval @interval_id

ko.components.register 'ephemeral-timer',
viewModel: z.components.EphemeralTimer
template: """
<ul class="ephemeral-timer">
<ul class="ephemeral-timer" data-bind="attr: {'title': title_caption()}">
<!-- ko foreach: bullet_count -->
<li class="ephemeral-timer-bullet" data-bind="css: $parent.is_bullet_active($data)"></li>
<!-- /ko -->
Expand Down
78 changes: 40 additions & 38 deletions app/script/conversation/ConversationRepository.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ class z.conversation.ConversationRepository
_init_subscriptions: ->
amplify.subscribe z.event.WebApp.CONVERSATION.ASSET.CANCEL, @cancel_asset_upload
amplify.subscribe z.event.WebApp.CONVERSATION.EVENT_FROM_BACKEND, @on_conversation_event
amplify.subscribe z.event.WebApp.CONVERSATION.EPHEMERAL_MESSAGE_TIMEOUT, @timeout_ephemeral_message
amplify.subscribe z.event.WebApp.CONVERSATION.MAP_CONNECTION, @map_connection
amplify.subscribe z.event.WebApp.CONVERSATION.STORE, @save_conversation_in_db
amplify.subscribe z.event.WebApp.CLIENT.ADD, @on_self_client_add
Expand Down Expand Up @@ -1451,7 +1450,7 @@ class z.conversation.ConversationRepository
delete_message_everyone: (conversation_et, message_et, user_ids) =>
Promise.resolve()
.then ->
if not message_et.user().is_me and not message_et.ephemeral_expires()
if not message_et.user().is_me and not message_et.expire_after_millis()
throw new z.conversation.ConversationError z.conversation.ConversationError::TYPE.WRONG_USER
generic_message = new z.proto.GenericMessage z.util.create_random_uuid()
generic_message.set 'deleted', new z.proto.MessageDelete message_et.id
Expand Down Expand Up @@ -1490,43 +1489,46 @@ class z.conversation.ConversationRepository

###
Get remaining lifetime for give message.
@param message_et [z.entity.Message]
@return [Promise] Promise that resolves with milliseconds when the message expires
###
get_ephemeral_timer: (message_et) =>
millis = message_et.ephemeral_expires()
millis = message_et.expire_after_millis()

switch message_et.ephemeral_status()
when z.message.EphemeralStatusType.TIMED_OUT
@timeout_ephemeral_message message_et
return Promise.resolve 0
when z.message.EphemeralStatusType.ACTIVE
message_et.start_ephemeral_timer()
expiration_timestamp = new Date(millis).getTime()
expires_in = expiration_timestamp - Date.now()
return Promise.resolve expires_in
when z.message.EphemeralStatusType.INACTIVE
message_et.ephemeral_expires new Date(Date.now() + millis).getTime.toString()
message_et.ephemeral_started new Date(Date.now()).getTime().toString()
return @conversation_service.update_message_in_db message_et, {ephemeral_expires: message_et.ephemeral_expires(), ephemeral_started: message_et.ephemeral_started()}
.then -> message_et.start_ephemeral_timer()

timeout_ephemeral_message: (message_et) =>
@get_conversation_by_id message_et.conversation_id, (conversation_et) =>
if message_et.user().is_me
switch
when message_et.has_asset_text()
@_obfuscate_text_message conversation_et, message_et.id
when message_et.is_ping()
@_obfuscate_ping_message conversation_et, message_et.id
when message_et.has_asset()
@_obfuscate_asset_message conversation_et, message_et.id
when message_et.has_asset_image()
@_obfuscate_image_message conversation_et, message_et.id
else
@logger.log @logger.log.levels.WARN, "Unsupported ephemeral type: #{message_et.type}"
expiration_date_iso = new Date(Date.now() + millis).toISOString()
message_et.expire_after_millis expiration_date_iso
return @conversation_service.update_message_in_db message_et, {expire_after_millis: expiration_date_iso}
.then -> return millis
else
if conversation_et.is_group()
user_ids = _.union [@user_repository.self().id], [message_et.from]
@delete_message_everyone conversation_et, message_et, user_ids
Promise.resolve()

timeout_ephemeral_message: (conversation_et, message_et) =>
if message_et.user().is_me
switch
when message_et.has_asset_text()
@_obfuscate_text_message conversation_et, message_et.id
when message_et.is_ping()
@_obfuscate_ping_message conversation_et, message_et.id
when message_et.has_asset()
@_obfuscate_asset_message conversation_et, message_et.id
when message_et.has_asset_image()
@_obfuscate_image_message conversation_et, message_et.id
else
@delete_message_everyone conversation_et, message_et
@logger.log 'Unsupported ephemeral type', message_et.type
else
if conversation_et.is_group()
user_ids = _.union [@user_repository.self().id], [message_et.from]
@delete_message_everyone conversation_et, message_et, user_ids
else
@delete_message_everyone conversation_et, message_et

_obfuscate_text_message: (conversation_et, message_id) =>
@get_message_in_conversation_by_id conversation_et, message_id
Expand All @@ -1536,10 +1538,10 @@ class z.conversation.ConversationRepository
obfuscated.previews asset.previews()
obfuscated.text = z.util.StringUtil.obfuscate asset.text if obfuscated.previews().length is 0
message_et.assets [obfuscated]
message_et.ephemeral_expires true
message_et.expire_after_millis true

@conversation_service.update_message_in_db message_et,
ephemeral_expires: true
expire_after_millis: true
data:
content: obfuscated.text
nonce: obfuscated.id
Expand All @@ -1549,39 +1551,39 @@ class z.conversation.ConversationRepository
_obfuscate_ping_message: (conversation_et, message_id) =>
@get_message_in_conversation_by_id conversation_et, message_id
.then (message_et) =>
message_et.ephemeral_expires true
@conversation_service.update_message_in_db message_et, {ephemeral_expires: true}
message_et.expire_after_millis true
@conversation_service.update_message_in_db message_et, {expire_after_millis: true}
.then =>
@logger.log 'Obfuscated ping message'

_obfuscate_asset_message: (conversation_et, message_id) =>
@get_message_in_conversation_by_id conversation_et, message_id
.then (message_et) =>
asset = message_et.get_first_asset()
message_et.ephemeral_expires true
message_et.expire_after_millis true
@conversation_service.update_message_in_db message_et,
data:
content_type: asset.file_type
info:
nonce: message_et.nonce
meta: {}
ephemeral_expires: true
expire_after_millis: true
.then =>
@logger.log 'Obfuscated asset message'

_obfuscate_image_message: (conversation_et, message_id) =>
@get_message_in_conversation_by_id conversation_et, message_id
.then (message_et) =>
asset = message_et.get_first_asset()
message_et.ephemeral_expires true
message_et.expire_after_millis true
@conversation_service.update_message_in_db message_et,
data:
info:
nonce: message_et.nonce
height: asset.height
width: asset.width
tag: 'medium'
ephemeral_expires: true
expire_after_millis: true
.then =>
@logger.log 'Obfuscated image message'

Expand Down Expand Up @@ -1844,7 +1846,7 @@ class z.conversation.ConversationRepository
_on_message_deleted: (conversation_et, event_json) =>
@get_message_in_conversation_by_id conversation_et, event_json.data.message_id
.then (message_to_delete_et) =>
if message_to_delete_et.ephemeral_expires()
if message_to_delete_et.expire_after_millis()
return
if event_json.from isnt message_to_delete_et.from
throw new z.conversation.ConversationError z.conversation.ConversationError::TYPE.WRONG_USER
Expand Down
8 changes: 2 additions & 6 deletions app/script/conversation/EventMapper.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -94,16 +94,12 @@ class z.conversation.EventMapper
message_et.type = event.type
message_et.version = event.version or 1

message_et.conversation_id = conversation_et.id

if message_et.is_reactable()
message_et.reactions event.reactions or {}
message_et.status event.status if event.status

# todo Remove deprecated expire_after_millis with subsequent release
if event.ephemeral_expires or event.expire_after_millis
message_et.ephemeral_expires event.ephemeral_expires or event.expire_after_millis
message_et.ephemeral_started event.ephemeral_started or '0'
if event.expire_after_millis
message_et.expire_after_millis event.expire_after_millis

if window.isNaN message_et.timestamp
@logger.log @logger.levels.WARN, "Could not get timestamp for message '#{message_et.id}'. Skipping it.", event
Expand Down
2 changes: 1 addition & 1 deletion app/script/cryptography/CryptographyMapper.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ class z.cryptography.CryptographyMapper
millis_as_number = generic_message.ephemeral.expire_after_millis.toNumber()
generic_message.ephemeral.message_id = generic_message.message_id
embedded_message = @_map_generic_message generic_message.ephemeral, event
embedded_message.ephemeral_expires = z.ephemeral.timings.map_to_closest_timing millis_as_number
embedded_message.expire_after_millis = z.ephemeral.timings.map_to_closest_timing millis_as_number
return embedded_message

###
Expand Down
27 changes: 4 additions & 23 deletions app/script/entity/message/Message.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,9 @@ class z.entity.Message

# Construct a new base message entity.
constructor: (@id = '0', @super_type = '') ->
@ephemeral_caption = ko.observable ''
@ephemeral_duration = ko.observable 0
@ephemeral_remaining = ko.observable 0
@ephemeral_expires = ko.observable false
@ephemeral_started = ko.observable '0'
@expire_after_millis = ko.observable false
@ephemeral_status = ko.computed =>
expiration = @ephemeral_expires()
expiration = @expire_after_millis()

if expiration is true
return z.message.EphemeralStatusType.TIMED_OUT
Expand All @@ -53,7 +49,6 @@ class z.entity.Message
else
return z.message.EphemeralStatusType.NONE

@conversation_id = ''
@from = ''
@is_editing = ko.observable false
@primary_key = undefined
Expand Down Expand Up @@ -204,14 +199,14 @@ class z.entity.Message
@return [Boolean]
###
is_ephemeral: ->
return @ephemeral_expires() isnt false
return @expire_after_millis() isnt false

###
Check if ephemeral message is expired.
@return [Boolean]
###
is_expired: ->
return @ephemeral_expires() is true
return @expire_after_millis() is true

###
Check if message can be reacted to.
Expand All @@ -231,17 +226,3 @@ class z.entity.Message
else if @status() isnt updated_status
return @status updated_status
return false

start_ephemeral_timer: =>
return if @ephemeral_timer
@ephemeral_remaining new Date(@ephemeral_expires()) - Date.now()

@ephemeral_interval_id = window.setInterval =>
@ephemeral_remaining new Date(@ephemeral_expires()) - Date.now()
@ephemeral_caption z.util.format_time_remaining @ephemeral_remaining()
, 250

window.setTimeout =>
amplify.publish z.event.WebApp.CONVERSATION.EPHEMERAL_MESSAGE_TIMEOUT, @
window.clearInterval @ephemeral_interval_id
, @ephemeral_remaining()
1 change: 0 additions & 1 deletion app/script/event/WebApp.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ z.event.WebApp =
CONVERSATION:
DEBUG: 'wire.webapp.conversation.debug'
EVENT_FROM_BACKEND: 'wire.webapp.conversation.event-from-backend'
EPHEMERAL_MESSAGE_TIMEOUT: 'wire.webapp.conversation.ephemeral-message-timeout'
LOADED_STATES: 'wire.webapp.conversation.loaded-states'
MAP_CONNECTION: 'wire.webapp.conversation.map-connection'
PEOPLE:
Expand Down
19 changes: 0 additions & 19 deletions app/script/util/util.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -659,22 +659,3 @@ z.util.bucket_values = (value, bucket_limits) ->

last_limit = bucket_limits[bucket_limits.length - 1]
return "#{last_limit+1}-"

z.util.format_time_remaining = (time_remaining) ->
moment_duration = moment.duration time_remaining
if moment_duration.asHours() is 1
title = "#{moment_duration.hours()} #{z.localization.Localizer.get_text z.string.ephememal_units_hour}, "
else if moment_duration.asHours() > 1
title = "#{moment_duration.hours()} #{z.localization.Localizer.get_text z.string.ephememal_units_hours}, "

if moment_duration.asMinutes() is 1
title = "#{moment_duration.minutes()} #{z.localization.Localizer.get_text z.string.ephememal_units_minute} #{z.localization.Localizer.get_text z.string.and} "
else if moment_duration.asMinutes() > 1
title = "#{moment_duration.minutes()} #{z.localization.Localizer.get_text z.string.ephememal_units_minutes} #{z.localization.Localizer.get_text z.string.and} "

if moment_duration.asSeconds() is 1
title = "#{moment_duration.seconds()} #{z.localization.Localizer.get_text z.string.ephememal_units_second}"
else if moment_duration.asSeconds() > 1
title = "#{moment_duration.seconds()} #{z.localization.Localizer.get_text z.string.ephememal_units_seconds}"

return title or ''
Loading

0 comments on commit 40fb65b

Please sign in to comment.