]> git.openstreetmap.org Git - rails.git/blob - app/helpers/user_blocks_helper.rb
Merge pull request #5406 from AntonKhorev/offline-message-oauth
[rails.git] / app / helpers / user_blocks_helper.rb
1 module UserBlocksHelper
2   include ActionView::Helpers::TranslationHelper
3
4   ##
5   # returns a translated string representing the status of the
6   # user block (i.e: whether it's active, what the expiry time is)
7   def block_status(block)
8     if block.active?
9       # if the block hasn't expired yet show the date, if the user just needs to login show that
10       if block.needs_view?
11         if block.ends_at > Time.now.utc
12           t("user_blocks.helper.time_future_and_until_login_html", :time => friendly_date(block.ends_at))
13         else
14           t("user_blocks.helper.until_login")
15         end
16       else
17         t("user_blocks.helper.time_future_html", :time => friendly_date(block.ends_at))
18       end
19     else
20       # the max of the last update time or the ends_at time is when this block finished
21       # either because the user viewed the block (updated_at) or it expired or was
22       # revoked (ends_at)
23       last_time = block.deactivates_at || [block.ends_at, block.updated_at].max
24       t("user_blocks.helper.time_past_html", :time => friendly_date_ago(last_time))
25     end
26   end
27
28   def block_short_status(block)
29     if block.active?
30       if block.needs_view?
31         if block.ends_at > Time.now.utc
32           t("user_blocks.helper.short.active_unread")
33         else
34           t("user_blocks.helper.short.expired_unread")
35         end
36       else
37         t("user_blocks.helper.short.active")
38       end
39     else
40       if block.revoker_id.nil?
41         if block.updated_at > block.ends_at
42           t("user_blocks.helper.short.read_html", :time => block_short_time_in_past(block.updated_at))
43         else
44           t("user_blocks.helper.short.ended")
45         end
46       else
47         t("user_blocks.helper.short.revoked_html", :name => link_to(block.revoker.display_name, block.revoker,
48                                                                     :class => "username d-inline-block text-truncate text-wrap align-bottom",
49                                                                     :dir => "auto"))
50       end
51     end
52   end
53
54   def block_short_time_in_future(time)
55     tag.time l(time.to_date),
56              :datetime => time.xmlschema,
57              :title => t("user_blocks.helper.short.time_in_future_title",
58                          :time_absolute => l(time, :format => :friendly),
59                          :time_relative => time_ago_in_words(time))
60   end
61
62   def block_short_time_in_past(time)
63     tag.time l(time.to_date),
64              :datetime => time.xmlschema,
65              :title => t("user_blocks.helper.short.time_in_past_title",
66                          :time_absolute => l(time, :format => :friendly),
67                          :time_relative => time_ago_in_words(time, :scope => :"datetime.distance_in_words_ago"))
68   end
69
70   def block_duration_in_words(duration)
71     # Ensure the requested duration isn't negative, even by a millisecond
72     duration = 0 if duration.negative?
73     parts = ActiveSupport::Duration.build(duration).parts
74     if duration < 1.day
75       t("user_blocks.helper.block_duration.hours", :count => parts.fetch(:hours, 0))
76     elsif duration < 1.week
77       t("user_blocks.helper.block_duration.days", :count => parts[:days])
78     elsif duration < 1.month
79       t("user_blocks.helper.block_duration.weeks", :count => parts[:weeks])
80     elsif duration < 1.year
81       t("user_blocks.helper.block_duration.months", :count => parts[:months])
82     else
83       t("user_blocks.helper.block_duration.years", :count => parts[:years])
84     end
85   end
86 end