2016-07-21 19:11:15 +08:00
|
|
|
module ActivityHelper
|
2017-10-06 23:12:15 +08:00
|
|
|
# constants for correct truncation length
|
2017-08-02 21:51:32 +08:00
|
|
|
TAGS_LENGTH = 4
|
|
|
|
TRUNCATE_OFFSET = 3
|
2016-10-05 23:45:20 +08:00
|
|
|
def activity_truncate(message, len = Constants::NAME_TRUNCATION_LENGTH)
|
2017-06-01 21:09:01 +08:00
|
|
|
activity_titles = message.scan(/<strong>(.*?)<\/strong>/)
|
|
|
|
activity_titles.each do |activity_title|
|
|
|
|
activity_title = activity_title[0]
|
2017-10-06 23:12:15 +08:00
|
|
|
# find first closing tag of smart annotation
|
|
|
|
closing_tag_sa = activity_title.index('</a>')
|
|
|
|
unless closing_tag_sa.nil?
|
|
|
|
opening_tag_sa = activity_title.index('<img')
|
|
|
|
opening_temp = activity_title.index('<span')
|
|
|
|
# depending on user/experiment set the first opening tag
|
|
|
|
opening_tag_sa = opening_temp if opening_tag_sa.nil? ||
|
|
|
|
opening_tag_sa > opening_temp
|
2017-08-02 21:51:32 +08:00
|
|
|
end
|
2017-10-06 23:12:15 +08:00
|
|
|
len_temp = len
|
|
|
|
# check until we run out of smart annotations in message
|
|
|
|
while !opening_tag_sa.nil? && !closing_tag_sa.nil? &&
|
|
|
|
opening_tag_sa < len_temp
|
|
|
|
stripped = strip_tags(activity_title[opening_tag_sa...closing_tag_sa])
|
|
|
|
.length
|
|
|
|
len_temp += (activity_title[opening_tag_sa...closing_tag_sa +
|
|
|
|
TAGS_LENGTH]).length - stripped
|
|
|
|
len = len_temp + TRUNCATE_OFFSET + TAGS_LENGTH if len <= closing_tag_sa
|
|
|
|
closing_temp = closing_tag_sa + 1
|
|
|
|
closing_tag_sa = activity_title.index('</a>', closing_temp)
|
|
|
|
unless closing_tag_sa.nil?
|
|
|
|
# find next smart annotation
|
|
|
|
opening_tag_sa = activity_title.index('<img', closing_temp)
|
|
|
|
opening_temp = activity_title.index('<span', closing_temp)
|
|
|
|
# depending on user/experiment set the next opening tag
|
|
|
|
opening_tag_sa = opening_temp if opening_tag_sa.nil? ||
|
|
|
|
opening_tag_sa > opening_temp
|
2017-07-07 21:56:16 +08:00
|
|
|
end
|
|
|
|
end
|
2017-10-06 23:12:15 +08:00
|
|
|
# adjust truncation length according to smart annotations length
|
2017-08-02 21:51:32 +08:00
|
|
|
len = activity_title.length if len > activity_title.length &&
|
|
|
|
len != Constants::NAME_TRUNCATION_LENGTH
|
2017-07-07 21:56:16 +08:00
|
|
|
if activity_title.length > len
|
2017-06-05 22:07:21 +08:00
|
|
|
title = "<div class='modal-tooltip'>
|
2017-08-02 21:51:32 +08:00
|
|
|
#{truncate(activity_title, length: len, escape: false)}
|
2017-06-05 22:07:21 +08:00
|
|
|
<span class='modal-tooltiptext'>
|
|
|
|
#{activity_title}
|
|
|
|
</span>
|
|
|
|
</div>"
|
2017-06-01 21:09:01 +08:00
|
|
|
else
|
2017-08-02 21:51:32 +08:00
|
|
|
title = truncate(activity_title, length: len, escape: false)
|
2017-06-01 21:09:01 +08:00
|
|
|
end
|
2017-08-02 21:51:32 +08:00
|
|
|
message = message.gsub(/#{Regexp.escape(activity_title)}/, title)
|
2016-07-21 19:11:15 +08:00
|
|
|
end
|
2017-01-04 00:27:22 +08:00
|
|
|
sanitize_input(message) if message
|
2016-07-21 19:11:15 +08:00
|
|
|
end
|
2017-01-12 21:47:15 +08:00
|
|
|
|
|
|
|
def days_since_1970(dt)
|
|
|
|
dt.to_i / 86400
|
|
|
|
end
|
2018-01-06 00:08:00 +08:00
|
|
|
|
|
|
|
def calculate_previous_date(activities,
|
|
|
|
index,
|
|
|
|
previus_batch_last_activitiy_date)
|
|
|
|
if index == 1 && !activities.first_page?
|
|
|
|
return previus_batch_last_activitiy_date
|
|
|
|
end
|
|
|
|
activity = activities[index - 1]
|
|
|
|
return activity.created_at.to_date if activity
|
|
|
|
Date.new(1901, 1, 1)
|
|
|
|
end
|
2016-07-21 19:11:15 +08:00
|
|
|
end
|