diff --git a/app/models/period_highlights.rb b/app/models/period_highlights.rb index 357e5380d..a45e5213a 100644 --- a/app/models/period_highlights.rb +++ b/app/models/period_highlights.rb @@ -1,14 +1,23 @@ class PeriodHighlights < ApplicationRecord class << self + def create_or_find_for(collections, starts_at:, duration: 1.week) + starts_at = normalize_anchor_date(starts_at) + + self.for(collections, starts_at:, duration:) || create_for(collections, starts_at:, duration:) + end + def create_for(collections, starts_at:, duration: 1.week) starts_at = normalize_anchor_date(starts_at) key = key_for(collections) events = Event.where(collection: collections).where(created_at: starts_at..starts_at + duration) - create_or_find_by!(key:, starts_at:, duration:) do |record| + if events.any? summarizer = Event::Summarizer.new(events) - record.content = summarizer.summarized_content - record.cost_in_microcents = summarizer.cost.in_microcents + summarized_content = summarizer.summarized_content # outside of transaction as this can be slow + create_or_find_by!(key:, starts_at:, duration:) do |record| + record.content = summarized_content + record.cost_in_microcents = summarizer.cost.in_microcents + end end end @@ -28,6 +37,10 @@ class PeriodHighlights < ApplicationRecord end end + def ends_at + starts_at + duration + end + def to_html renderer = Redcarpet::Render::HTML.new markdowner = Redcarpet::Markdown.new(renderer, autolink: true, tables: true, fenced_code_blocks: true, strikethrough: true, superscript: true,) diff --git a/app/models/user/day_timeline.rb b/app/models/user/day_timeline.rb index 71b520c6d..26a15a05b 100644 --- a/app/models/user/day_timeline.rb +++ b/app/models/user/day_timeline.rb @@ -29,7 +29,19 @@ class User::DayTimeline day.yesterday.beginning_of_day end + def has_weekly_highlights? + first_day_with_activity_this_week? && weekly_highlights.present? + end + + def weekly_highlights + @weekly_highlights ||= user.weekly_highlights_for(day - 1.week) + end + private + def first_day_with_activity_this_week? + day.monday? || (earliest_time.present? && earliest_time < day.beginning_of_week(:monday)) + end + def filtered_events @filtered_events ||= begin events = Event.where(collection: collections) diff --git a/app/models/user/highlights.rb b/app/models/user/highlights.rb index d40abafdd..d65e10bda 100644 --- a/app/models/user/highlights.rb +++ b/app/models/user/highlights.rb @@ -13,16 +13,16 @@ module User::Highlights end end - def generate_weekly_highlights - PeriodHighlights.create_for collections, starts_at: current_highlights_starts_at, duration: 1.week + def generate_weekly_highlights(date = Time.current) + PeriodHighlights.create_or_find_for collections, starts_at: highlights_starts_at(date), duration: 1.week end - def current_weekly_highlights - PeriodHighlights.for collections, starts_at: current_highlights_starts_at, duration: 1.week + def weekly_highlights_for(date) + PeriodHighlights.for collections, starts_at: highlights_starts_at(date), duration: 1.week end private - def current_highlights_starts_at - Time.current.utc.beginning_of_week(:sunday) + def highlights_starts_at(date = Time.current) + date.utc.beginning_of_week(:sunday) end end diff --git a/app/views/events/_day.html.erb b/app/views/events/_day.html.erb index 6fbcc8b50..bb84ff713 100644 --- a/app/views/events/_day.html.erb +++ b/app/views/events/_day.html.erb @@ -1,16 +1,18 @@
<% if day_timeline.has_activity? %> -

- - - Highlights for - <%= local_datetime_tag day_timeline.day, style: :agoorweekday %> - - -

- <%= render "events/weekly_summary" %> + <% if day_timeline.has_weekly_highlights? %> +

+ + + Highlights for this week + + +

+ <% end %> + + <%= render "events/weekly_highlights", day_timeline: day_timeline if day_timeline.has_weekly_highlights? %>
<%= render "events/day_timeline_column", event_type: "added", day_timeline: day_timeline %> diff --git a/app/views/events/_weekly_highlights.html.erb b/app/views/events/_weekly_highlights.html.erb new file mode 100644 index 000000000..51666e4b7 --- /dev/null +++ b/app/views/events/_weekly_highlights.html.erb @@ -0,0 +1,3 @@ +
+ <%= day_timeline.weekly_highlights.to_html %> +
diff --git a/app/views/events/_weekly_summary.html.erb b/app/views/events/_weekly_summary.html.erb deleted file mode 100644 index 646669e27..000000000 --- a/app/views/events/_weekly_summary.html.erb +++ /dev/null @@ -1,5 +0,0 @@ -<% if weekly_highlights = Current.user.current_weekly_highlights %> -
- <%= weekly_highlights.to_html %> -
-<% end %> diff --git a/script/migrations/backfill_weekly_highlights.rb b/script/migrations/backfill_weekly_highlights.rb new file mode 100644 index 000000000..40668f586 --- /dev/null +++ b/script/migrations/backfill_weekly_highlights.rb @@ -0,0 +1,13 @@ +#!/usr/bin/env ruby + +require_relative "../config/environment" + +WEEKS_TO_BACKFILL = 20 + +ApplicationRecord.with_each_tenant do |tenant| + WEEKS_TO_BACKFILL.times do |index| + User.find_each do |user| + user.generate_weekly_highlights(Time.current - index.weeks) + end + end +end diff --git a/test/models/period_highlights_test.rb b/test/models/period_highlights_test.rb index b1c942a91..5bcfca99a 100644 --- a/test/models/period_highlights_test.rb +++ b/test/models/period_highlights_test.rb @@ -9,17 +9,17 @@ class PeriodHighlightTest < ActiveSupport::TestCase test "generate period highlights" do period_highlights = assert_difference -> { PeriodHighlights.count }, 1 do - PeriodHighlights.create_for(@user.collections, starts_at: 1.month.ago, duration: 2.months) + PeriodHighlights.create_or_find_for(@user.collections, starts_at: 1.month.ago, duration: 2.months) end assert_match /logo/i, period_highlights.to_html end test "don't generate highlights for existing periods" do - new_period_highlights = PeriodHighlights.create_for(@user.collections, starts_at: 1.month.ago, duration: 2.months) + new_period_highlights = PeriodHighlights.create_or_find_for(@user.collections, starts_at: 1.month.ago, duration: 2.months) existing_period_highlights = assert_no_difference -> { PeriodHighlights.count } do - PeriodHighlights.create_for(@user.collections, starts_at: 1.month.ago, duration: 2.months) + PeriodHighlights.create_or_find_for(@user.collections, starts_at: 1.month.ago, duration: 2.months) end assert_equal new_period_highlights, existing_period_highlights