From 389e3f6094bfcc4d0338947d29434113320c8142 Mon Sep 17 00:00:00 2001 From: ThibG Date: Sat, 25 Jan 2020 22:59:00 +0100 Subject: [PATCH 01/18] Fix status overflowing in report dialog (#12959) Fixes #12942 --- app/javascript/styles/mastodon/components.scss | 1 + 1 file changed, 1 insertion(+) diff --git a/app/javascript/styles/mastodon/components.scss b/app/javascript/styles/mastodon/components.scss index 7edd0ba1497..fe111db96e5 100644 --- a/app/javascript/styles/mastodon/components.scss +++ b/app/javascript/styles/mastodon/components.scss @@ -1086,6 +1086,7 @@ .status-check-box__status { margin: 10px 0 10px 10px; flex: 1; + overflow: hidden; .media-gallery { max-width: 250px; From 408b3e2b9328f54d471deba346a182f7c8856676 Mon Sep 17 00:00:00 2001 From: ButterflyOfFire <42316180+BoFFire@users.noreply.github.com> Date: Sun, 26 Jan 2020 14:42:35 +0100 Subject: [PATCH 02/18] Add kabyle language to Mastodon (#12962) * Add kabyle language to settings_helper * Adding kabyle language to application.rb --- app/helpers/settings_helper.rb | 1 + config/application.rb | 1 + 2 files changed, 2 insertions(+) diff --git a/app/helpers/settings_helper.rb b/app/helpers/settings_helper.rb index 10aaf3acaa5..825aa974d88 100644 --- a/app/helpers/settings_helper.rb +++ b/app/helpers/settings_helper.rb @@ -36,6 +36,7 @@ module SettingsHelper it: 'Italiano', ja: '日本語', ka: 'ქართული', + kab: 'Taqbaylit', kk: 'Қазақша', kn: 'ಕನ್ನಡ', ko: '한국어', diff --git a/config/application.rb b/config/application.rb index bf25fa0d9e9..1baa166ce64 100644 --- a/config/application.rb +++ b/config/application.rb @@ -73,6 +73,7 @@ module Mastodon :it, :ja, :ka, + :kab, :kk, :kn, :ko, From b9d74d407673a6dbdc87c3310618b22c85358c85 Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Sun, 26 Jan 2020 20:07:26 +0100 Subject: [PATCH 03/18] Add streaming API updates for announcements being modified or deleted (#12963) Change `all_day` to be a visual client-side cue only Publish immediately if `scheduled_at` is in the past Add `published_at` and `updated_at` to announcements JSON --- .../admin/announcements_controller.rb | 3 ++ .../mastodon/actions/announcements.js | 14 ++++-- app/javascript/mastodon/actions/streaming.js | 10 +++- .../mastodon/components/animated_number.js | 7 +-- .../components/announcements.js | 12 +++-- .../mastodon/reducers/announcements.js | 49 +++++++++++++++---- app/lib/feed_manager.rb | 4 ++ app/models/announcement.rb | 21 +++----- .../rest/announcement_serializer.rb | 3 +- .../publish_announcement_reaction_worker.rb | 2 +- .../publish_scheduled_announcement_worker.rb | 2 +- app/workers/unpublish_announcement_worker.rb | 14 ++++++ 12 files changed, 100 insertions(+), 41 deletions(-) create mode 100644 app/workers/unpublish_announcement_worker.rb diff --git a/app/controllers/admin/announcements_controller.rb b/app/controllers/admin/announcements_controller.rb index 02198f0b527..212a9f69334 100644 --- a/app/controllers/admin/announcements_controller.rb +++ b/app/controllers/admin/announcements_controller.rb @@ -20,6 +20,7 @@ class Admin::AnnouncementsController < Admin::BaseController @announcement = Announcement.new(resource_params) if @announcement.save + PublishScheduledAnnouncementWorker.perform_async(@announcement.id) if @announcement.published? log_action :create, @announcement redirect_to admin_announcements_path else @@ -35,6 +36,7 @@ class Admin::AnnouncementsController < Admin::BaseController authorize :announcement, :update? if @announcement.update(resource_params) + PublishScheduledAnnouncementWorker.perform_async(@announcement.id) if @announcement.published? log_action :update, @announcement redirect_to admin_announcements_path else @@ -45,6 +47,7 @@ class Admin::AnnouncementsController < Admin::BaseController def destroy authorize :announcement, :destroy? @announcement.destroy! + UnpublishAnnouncementWorker.perform_async(@announcement.id) if @announcement.published? log_action :destroy, @announcement redirect_to admin_announcements_path end diff --git a/app/javascript/mastodon/actions/announcements.js b/app/javascript/mastodon/actions/announcements.js index 53b19a6fda0..f072a407f98 100644 --- a/app/javascript/mastodon/actions/announcements.js +++ b/app/javascript/mastodon/actions/announcements.js @@ -5,6 +5,7 @@ export const ANNOUNCEMENTS_FETCH_REQUEST = 'ANNOUNCEMENTS_FETCH_REQUEST'; export const ANNOUNCEMENTS_FETCH_SUCCESS = 'ANNOUNCEMENTS_FETCH_SUCCESS'; export const ANNOUNCEMENTS_FETCH_FAIL = 'ANNOUNCEMENTS_FETCH_FAIL'; export const ANNOUNCEMENTS_UPDATE = 'ANNOUNCEMENTS_UPDATE'; +export const ANNOUNCEMENTS_DELETE = 'ANNOUNCEMENTS_DELETE'; export const ANNOUNCEMENTS_REACTION_ADD_REQUEST = 'ANNOUNCEMENTS_REACTION_ADD_REQUEST'; export const ANNOUNCEMENTS_REACTION_ADD_SUCCESS = 'ANNOUNCEMENTS_REACTION_ADD_SUCCESS'; @@ -139,8 +140,11 @@ export const updateReaction = reaction => ({ reaction, }); -export function toggleShowAnnouncements() { - return dispatch => { - dispatch({ type: ANNOUNCEMENTS_TOGGLE_SHOW }); - }; -} +export const toggleShowAnnouncements = () => ({ + type: ANNOUNCEMENTS_TOGGLE_SHOW, +}); + +export const deleteAnnouncement = id => ({ + type: ANNOUNCEMENTS_DELETE, + id, +}); diff --git a/app/javascript/mastodon/actions/streaming.js b/app/javascript/mastodon/actions/streaming.js index ac325f74cdc..79b08bddacb 100644 --- a/app/javascript/mastodon/actions/streaming.js +++ b/app/javascript/mastodon/actions/streaming.js @@ -8,7 +8,12 @@ import { } from './timelines'; import { updateNotifications, expandNotifications } from './notifications'; import { updateConversations } from './conversations'; -import { fetchAnnouncements, updateAnnouncements, updateReaction as updateAnnouncementsReaction } from './announcements'; +import { + fetchAnnouncements, + updateAnnouncements, + updateReaction as updateAnnouncementsReaction, + deleteAnnouncement, +} from './announcements'; import { fetchFilters } from './filters'; import { getLocale } from '../locales'; @@ -51,6 +56,9 @@ export function connectTimelineStream (timelineId, path, pollingRefresh = null, case 'announcement.reaction': dispatch(updateAnnouncementsReaction(JSON.parse(data.payload))); break; + case 'announcement.delete': + dispatch(deleteAnnouncement(data.payload)); + break; } }, }; diff --git a/app/javascript/mastodon/components/animated_number.js b/app/javascript/mastodon/components/animated_number.js index 2d359fbc60f..3bfe86aa6ac 100644 --- a/app/javascript/mastodon/components/animated_number.js +++ b/app/javascript/mastodon/components/animated_number.js @@ -27,7 +27,8 @@ export default class AnimatedNumber extends React.PureComponent { } const styles = [{ - key: value, + key: `${value}`, + data: value, style: { y: spring(0, { damping: 35, stiffness: 400 }) }, }]; @@ -35,8 +36,8 @@ export default class AnimatedNumber extends React.PureComponent { {items => ( - {items.map(({ key, style }) => ( - 0 ? 'absolute' : 'static', transform: `translateY(${style.y * 100}%)` }}> + {items.map(({ key, data, style }) => ( + 0 ? 'absolute' : 'static', transform: `translateY(${style.y * 100}%)` }}> ))} )} diff --git a/app/javascript/mastodon/features/getting_started/components/announcements.js b/app/javascript/mastodon/features/getting_started/components/announcements.js index 8ff1b0b4e80..acaa552aa06 100644 --- a/app/javascript/mastodon/features/getting_started/components/announcements.js +++ b/app/javascript/mastodon/features/getting_started/components/announcements.js @@ -367,11 +367,13 @@ class Announcements extends ImmutablePureComponent { ))} -
- - {index + 1} / {announcements.size} - -
+ {announcements.size > 1 && ( +
+ + {index + 1} / {announcements.size} + +
+ )} ); diff --git a/app/javascript/mastodon/reducers/announcements.js b/app/javascript/mastodon/reducers/announcements.js index 1cfb598fb98..3215c1c2dcc 100644 --- a/app/javascript/mastodon/reducers/announcements.js +++ b/app/javascript/mastodon/reducers/announcements.js @@ -9,6 +9,7 @@ import { ANNOUNCEMENTS_REACTION_REMOVE_REQUEST, ANNOUNCEMENTS_REACTION_REMOVE_FAIL, ANNOUNCEMENTS_TOGGLE_SHOW, + ANNOUNCEMENTS_DELETE, } from '../actions/announcements'; import { Map as ImmutableMap, List as ImmutableList, Set as ImmutableSet, fromJS } from 'immutable'; @@ -22,14 +23,10 @@ const initialState = ImmutableMap({ const updateReaction = (state, id, name, updater) => state.update('items', list => list.map(announcement => { if (announcement.get('id') === id) { return announcement.update('reactions', reactions => { - if (reactions.find(reaction => reaction.get('name') === name)) { - return reactions.map(reaction => { - if (reaction.get('name') === name) { - return updater(reaction); - } + const idx = reactions.findIndex(reaction => reaction.get('name') === name); - return reaction; - }); + if (idx > -1) { + return reactions.update(idx, reaction => updater(reaction)); } return reactions.push(updater(fromJS({ name, count: 0 }))); @@ -46,13 +43,33 @@ const addReaction = (state, id, name) => updateReaction(state, id, name, x => x. const removeReaction = (state, id, name) => updateReaction(state, id, name, x => x.set('me', false).update('count', y => y - 1)); const addUnread = (state, items) => { - if (state.get('show')) return state; + if (state.get('show')) { + return state; + } const newIds = ImmutableSet(items.map(x => x.get('id'))); const oldIds = ImmutableSet(state.get('items').map(x => x.get('id'))); + return state.update('unread', unread => unread.union(newIds.subtract(oldIds))); }; +const sortAnnouncements = list => list.sortBy(x => x.get('starts_at') || x.get('published_at')); + +const updateAnnouncement = (state, announcement) => { + const idx = state.get('items').findIndex(x => x.get('id') === announcement.get('id')); + + state = addUnread(state, [announcement]); + + if (idx > -1) { + // Deep merge is used because announcements from the streaming API do not contain + // personalized data about which reactions have been selected by the given user, + // and that is information we want to preserve + return state.update('items', list => sortAnnouncements(list.update(idx, x => x.mergeDeep(announcement)))); + } + + return state.update('items', list => sortAnnouncements(list.unshift(announcement))); +}; + export default function announcementsReducer(state = initialState, action) { switch(action.type) { case ANNOUNCEMENTS_TOGGLE_SHOW: @@ -65,15 +82,17 @@ export default function announcementsReducer(state = initialState, action) { case ANNOUNCEMENTS_FETCH_SUCCESS: return state.withMutations(map => { const items = fromJS(action.announcements); + map.set('unread', ImmutableSet()); - addUnread(map, items); map.set('items', items); map.set('isLoading', false); + + addUnread(map, items); }); case ANNOUNCEMENTS_FETCH_FAIL: return state.set('isLoading', false); case ANNOUNCEMENTS_UPDATE: - return addUnread(state, [fromJS(action.announcement)]).update('items', list => list.unshift(fromJS(action.announcement)).sortBy(announcement => announcement.get('starts_at'))); + return updateAnnouncement(state, fromJS(action.announcement)); case ANNOUNCEMENTS_REACTION_UPDATE: return updateReactionCount(state, action.reaction); case ANNOUNCEMENTS_REACTION_ADD_REQUEST: @@ -82,6 +101,16 @@ export default function announcementsReducer(state = initialState, action) { case ANNOUNCEMENTS_REACTION_REMOVE_REQUEST: case ANNOUNCEMENTS_REACTION_ADD_FAIL: return removeReaction(state, action.id, action.name); + case ANNOUNCEMENTS_DELETE: + return state.update('unread', set => set.delete(action.id)).update('items', list => { + const idx = list.findIndex(x => x.get('id') === action.id); + + if (idx > -1) { + return list.delete(idx); + } + + return list; + }); default: return state; } diff --git a/app/lib/feed_manager.rb b/app/lib/feed_manager.rb index 3f167f0d888..13b7aafdffa 100644 --- a/app/lib/feed_manager.rb +++ b/app/lib/feed_manager.rb @@ -11,6 +11,10 @@ class FeedManager # Must be <= MAX_ITEMS or the tracking sets will grow forever REBLOG_FALLOFF = 40 + def with_active_accounts(&block) + Account.joins(:user).where('users.current_sign_in_at > ?', User::ACTIVE_DURATION.ago).find_each(&block) + end + def key(type, id, subtype = nil) return "feed:#{type}:#{id}" unless subtype diff --git a/app/models/announcement.rb b/app/models/announcement.rb index 4da9f94d6de..c5cf08f7c72 100644 --- a/app/models/announcement.rb +++ b/app/models/announcement.rb @@ -16,8 +16,6 @@ # class Announcement < ApplicationRecord - after_commit :queue_publish, on: :create - scope :unpublished, -> { where(published: false) } scope :published, -> { where(published: true) } scope :without_muted, ->(account) { joins("LEFT OUTER JOIN announcement_mutes ON announcement_mutes.announcement_id = announcements.id AND announcement_mutes.account_id = #{account.id}").where('announcement_mutes.id IS NULL') } @@ -31,8 +29,11 @@ class Announcement < ApplicationRecord validates :ends_at, presence: true, if: -> { starts_at.present? } before_validation :set_all_day - before_validation :set_starts_at, on: :create - before_validation :set_ends_at, on: :create + before_validation :set_published, on: :create + + def published_at + scheduled_at || created_at + end def time_range? starts_at.present? && ends_at.present? @@ -71,15 +72,7 @@ class Announcement < ApplicationRecord self.all_day = false if starts_at.blank? || ends_at.blank? end - def set_starts_at - self.starts_at = starts_at.change(hour: 0, min: 0, sec: 0) if all_day? && starts_at.present? - end - - def set_ends_at - self.ends_at = ends_at.change(hour: 23, min: 59, sec: 59) if all_day? && ends_at.present? - end - - def queue_publish - PublishScheduledAnnouncementWorker.perform_async(id) if scheduled_at.blank? + def set_published + self.published = true if scheduled_at.blank? || scheduled_at.past? end end diff --git a/app/serializers/rest/announcement_serializer.rb b/app/serializers/rest/announcement_serializer.rb index 924d87b3417..ae962aa1de7 100644 --- a/app/serializers/rest/announcement_serializer.rb +++ b/app/serializers/rest/announcement_serializer.rb @@ -1,7 +1,8 @@ # frozen_string_literal: true class REST::AnnouncementSerializer < ActiveModel::Serializer - attributes :id, :content, :starts_at, :ends_at, :all_day + attributes :id, :content, :starts_at, :ends_at, :all_day, + :published_at, :updated_at has_many :mentions has_many :tags, serializer: REST::StatusSerializer::TagSerializer diff --git a/app/workers/publish_announcement_reaction_worker.rb b/app/workers/publish_announcement_reaction_worker.rb index e01deb64dd8..418dc71275c 100644 --- a/app/workers/publish_announcement_reaction_worker.rb +++ b/app/workers/publish_announcement_reaction_worker.rb @@ -13,7 +13,7 @@ class PublishAnnouncementReactionWorker payload = InlineRenderer.render(reaction, nil, :reaction).tap { |h| h[:announcement_id] = announcement_id.to_s } payload = Oj.dump(event: :'announcement.reaction', payload: payload) - Account.joins(:user).where('users.current_sign_in_at > ?', User::ACTIVE_DURATION.ago).find_each do |account| + FeedManager.instance.with_active_accounts do |account| redis.publish("timeline:#{account.id}", payload) if redis.exists("subscribed:timeline:#{account.id}") end rescue ActiveRecord::RecordNotFound diff --git a/app/workers/publish_scheduled_announcement_worker.rb b/app/workers/publish_scheduled_announcement_worker.rb index 4b2014e34ad..6b5898bf574 100644 --- a/app/workers/publish_scheduled_announcement_worker.rb +++ b/app/workers/publish_scheduled_announcement_worker.rb @@ -11,7 +11,7 @@ class PublishScheduledAnnouncementWorker payload = InlineRenderer.render(announcement, nil, :announcement) payload = Oj.dump(event: :announcement, payload: payload) - Account.joins(:user).where('users.current_sign_in_at > ?', User::ACTIVE_DURATION.ago).find_each do |account| + FeedManager.instance.with_active_accounts do |account| redis.publish("timeline:#{account.id}", payload) if redis.exists("subscribed:timeline:#{account.id}") end end diff --git a/app/workers/unpublish_announcement_worker.rb b/app/workers/unpublish_announcement_worker.rb new file mode 100644 index 00000000000..e99d70cf87a --- /dev/null +++ b/app/workers/unpublish_announcement_worker.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +class UnpublishAnnouncementWorker + include Sidekiq::Worker + include Redisable + + def perform(announcement_id) + payload = Oj.dump(event: :'announcement.delete', payload: announcement_id.to_s) + + FeedManager.instance.with_active_accounts do |account| + redis.publish("timeline:#{account.id}", payload) if redis.exists("subscribed:timeline:#{account.id}") + end + end +end From 2f8c3c17ee26dc21ce94959b0ea18984e7bc5560 Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Sun, 26 Jan 2020 22:26:50 +0100 Subject: [PATCH 04/18] Fix link colors in announcements (#12965) --- app/javascript/styles/mastodon/components.scss | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/app/javascript/styles/mastodon/components.scss b/app/javascript/styles/mastodon/components.scss index fe111db96e5..563ac7e5ed4 100644 --- a/app/javascript/styles/mastodon/components.scss +++ b/app/javascript/styles/mastodon/components.scss @@ -886,7 +886,7 @@ } a { - color: $highlight-text-color; + color: $secondary-text-color; text-decoration: none; &:hover { @@ -902,6 +902,10 @@ } } } + + &.unhandled-link { + color: lighten($ui-highlight-color, 8%); + } } } From 401f32f9eedf9f41c097ee01c1f6203eae568663 Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Sun, 26 Jan 2020 22:43:18 +0100 Subject: [PATCH 05/18] Fix expired announcements being re-published (#12964) --- app/models/announcement.rb | 12 ++++++------ app/workers/publish_scheduled_announcement_worker.rb | 3 ++- .../scheduler/scheduled_statuses_scheduler.rb | 2 +- ...200126203551_add_published_at_to_announcements.rb | 5 +++++ db/schema.rb | 3 ++- 5 files changed, 16 insertions(+), 9 deletions(-) create mode 100644 db/migrate/20200126203551_add_published_at_to_announcements.rb diff --git a/app/models/announcement.rb b/app/models/announcement.rb index c5cf08f7c72..670b24f01de 100644 --- a/app/models/announcement.rb +++ b/app/models/announcement.rb @@ -13,13 +13,14 @@ # ends_at :datetime # created_at :datetime not null # updated_at :datetime not null +# published_at :datetime # class Announcement < ApplicationRecord scope :unpublished, -> { where(published: false) } scope :published, -> { where(published: true) } scope :without_muted, ->(account) { joins("LEFT OUTER JOIN announcement_mutes ON announcement_mutes.announcement_id = announcements.id AND announcement_mutes.account_id = #{account.id}").where('announcement_mutes.id IS NULL') } - scope :chronological, -> { order(Arel.sql('COALESCE(announcements.starts_at, announcements.scheduled_at, announcements.created_at) ASC')) } + scope :chronological, -> { order(Arel.sql('COALESCE(announcements.starts_at, announcements.scheduled_at, announcements.published_at, announcements.created_at) ASC')) } has_many :announcement_mutes, dependent: :destroy has_many :announcement_reactions, dependent: :destroy @@ -31,10 +32,6 @@ class Announcement < ApplicationRecord before_validation :set_all_day before_validation :set_published, on: :create - def published_at - scheduled_at || created_at - end - def time_range? starts_at.present? && ends_at.present? end @@ -73,6 +70,9 @@ class Announcement < ApplicationRecord end def set_published - self.published = true if scheduled_at.blank? || scheduled_at.past? + return unless scheduled_at.blank? || scheduled_at.past? + + self.published = true + self.published_at = Time.now.utc end end diff --git a/app/workers/publish_scheduled_announcement_worker.rb b/app/workers/publish_scheduled_announcement_worker.rb index 6b5898bf574..4fc6bef2fdc 100644 --- a/app/workers/publish_scheduled_announcement_worker.rb +++ b/app/workers/publish_scheduled_announcement_worker.rb @@ -6,7 +6,8 @@ class PublishScheduledAnnouncementWorker def perform(announcement_id) announcement = Announcement.find(announcement_id) - announcement.update(published: true) + + announcement.update(published: true, published_at: Time.now.utc, scheduled_at: nil) unless announcement.published? payload = InlineRenderer.render(announcement, nil, :announcement) payload = Oj.dump(event: :announcement, payload: payload) diff --git a/app/workers/scheduler/scheduled_statuses_scheduler.rb b/app/workers/scheduler/scheduled_statuses_scheduler.rb index 4262f1d01b6..9cfe949dec5 100644 --- a/app/workers/scheduler/scheduled_statuses_scheduler.rb +++ b/app/workers/scheduler/scheduled_statuses_scheduler.rb @@ -34,7 +34,7 @@ class Scheduler::ScheduledStatusesScheduler end def unpublish_expired_announcements! - expired_announcements.in_batches.update_all(published: false) + expired_announcements.in_batches.update_all(published: false, scheduled_at: nil) end def expired_announcements diff --git a/db/migrate/20200126203551_add_published_at_to_announcements.rb b/db/migrate/20200126203551_add_published_at_to_announcements.rb new file mode 100644 index 00000000000..d99f9569428 --- /dev/null +++ b/db/migrate/20200126203551_add_published_at_to_announcements.rb @@ -0,0 +1,5 @@ +class AddPublishedAtToAnnouncements < ActiveRecord::Migration[5.2] + def change + add_column :announcements, :published_at, :datetime + end +end diff --git a/db/schema.rb b/db/schema.rb index d3a2c05b313..b09ee0e7636 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2020_01_19_112504) do +ActiveRecord::Schema.define(version: 2020_01_26_203551) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -228,6 +228,7 @@ ActiveRecord::Schema.define(version: 2020_01_19_112504) do t.datetime "ends_at" t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.datetime "published_at" end create_table "backups", force: :cascade do |t| From 42d2a915e4aa31533032e37aaa46354cee2386da Mon Sep 17 00:00:00 2001 From: ThibG Date: Sun, 26 Jan 2020 23:13:48 +0100 Subject: [PATCH 06/18] Change last_status_at to be a date, not datetime (#12966) * Return last_status_at as date, not datetime * Fix relative timestamp for dates when delay is inferior to 1 day * Also fix public directory * Fix error when last_status_at isn't set --- .../mastodon/components/relative_timestamp.js | 16 +++++++++++----- app/serializers/rest/account_serializer.rb | 4 ++++ app/views/directories/index.html.haml | 2 +- app/views/relationships/_account.html.haml | 2 +- 4 files changed, 17 insertions(+), 7 deletions(-) diff --git a/app/javascript/mastodon/components/relative_timestamp.js b/app/javascript/mastodon/components/relative_timestamp.js index aa4b73cfe6a..711181dcdbe 100644 --- a/app/javascript/mastodon/components/relative_timestamp.js +++ b/app/javascript/mastodon/components/relative_timestamp.js @@ -3,6 +3,7 @@ import { injectIntl, defineMessages } from 'react-intl'; import PropTypes from 'prop-types'; const messages = defineMessages({ + today: { id: 'relative_time.today', defaultMessage: 'today' }, just_now: { id: 'relative_time.just_now', defaultMessage: 'now' }, seconds: { id: 'relative_time.seconds', defaultMessage: '{number}s' }, minutes: { id: 'relative_time.minutes', defaultMessage: '{number}m' }, @@ -65,12 +66,14 @@ const getUnitDelay = units => { } }; -export const timeAgoString = (intl, date, now, year) => { +export const timeAgoString = (intl, date, now, year, timeGiven = true) => { const delta = now - date.getTime(); let relativeTime; - if (delta < 10 * SECOND) { + if (delta < DAY && !timeGiven) { + relativeTime = intl.formatMessage(messages.today); + } else if (delta < 10 * SECOND) { relativeTime = intl.formatMessage(messages.just_now); } else if (delta < 7 * DAY) { if (delta < MINUTE) { @@ -91,12 +94,14 @@ export const timeAgoString = (intl, date, now, year) => { return relativeTime; }; -const timeRemainingString = (intl, date, now) => { +const timeRemainingString = (intl, date, now, timeGiven = true) => { const delta = date.getTime() - now; let relativeTime; - if (delta < 10 * SECOND) { + if (delta < DAY && !timeGiven) { + relativeTime = intl.formatMessage(messages.today); + } else if (delta < 10 * SECOND) { relativeTime = intl.formatMessage(messages.moments_remaining); } else if (delta < MINUTE) { relativeTime = intl.formatMessage(messages.seconds_remaining, { number: Math.floor(delta / SECOND) }); @@ -173,8 +178,9 @@ class RelativeTimestamp extends React.Component { render () { const { timestamp, intl, year, futureDate } = this.props; + const timeGiven = timestamp.includes('T'); const date = new Date(timestamp); - const relativeTime = futureDate ? timeRemainingString(intl, date, this.state.now) : timeAgoString(intl, date, this.state.now, year); + const relativeTime = futureDate ? timeRemainingString(intl, date, this.state.now, timeGiven) : timeAgoString(intl, date, this.state.now, year, timeGiven); return (