2024-12-19 15:31:12 -03:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# For a GitHub like calendar
|
|
|
|
# https://docs.github.com/assets/cb-35216/mw-1440/images/help/profile/contributions-graph.webp
|
|
|
|
module DiscourseRewind
|
|
|
|
class Rewind::Action::ActivityCalendar < Rewind::Action::BaseReport
|
2025-01-09 17:55:58 +01:00
|
|
|
FakeData = {
|
|
|
|
data:
|
|
|
|
(Date.new(2024, 1, 1)..Date.new(2024, 12, 31)).map do |date|
|
2025-01-15 12:06:06 -03:00
|
|
|
{ date: date.strftime("%Y-%m-%d"), post_count: rand(0..20), visited: false }
|
2025-01-09 17:55:58 +01:00
|
|
|
end,
|
|
|
|
identifier: "activity-calendar",
|
|
|
|
}
|
|
|
|
|
2024-12-19 15:31:12 -03:00
|
|
|
def call
|
2025-01-09 17:55:58 +01:00
|
|
|
return FakeData if Rails.env.development?
|
|
|
|
|
2024-12-19 15:31:12 -03:00
|
|
|
calendar =
|
|
|
|
Post
|
|
|
|
.unscoped
|
|
|
|
.joins(<<~SQL)
|
|
|
|
RIGHT JOIN
|
|
|
|
generate_series('#{date.first}', '#{date.last}', '1 day'::interval) ON
|
|
|
|
posts.created_at::date = generate_series::date AND
|
|
|
|
posts.user_id = #{user.id} AND
|
|
|
|
posts.deleted_at IS NULL
|
|
|
|
SQL
|
|
|
|
.joins(
|
|
|
|
"LEFT JOIN user_visits ON generate_series::date = visited_at AND user_visits.user_id = #{user.id}",
|
|
|
|
)
|
|
|
|
.select(
|
|
|
|
"generate_series::date as date, count(posts.id) as post_count, COUNT(visited_at) > 0 as visited",
|
|
|
|
)
|
|
|
|
.group("generate_series, user_visits.id")
|
|
|
|
.order("generate_series")
|
2025-01-15 12:06:06 -03:00
|
|
|
.map do |row|
|
|
|
|
{
|
|
|
|
date: row.date.strftime("%Y-%m-%d"),
|
|
|
|
post_count: row.post_count,
|
|
|
|
visited: row.visited,
|
|
|
|
}
|
|
|
|
end
|
2024-12-19 15:31:12 -03:00
|
|
|
|
|
|
|
{ data: calendar, identifier: "activity-calendar" }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|