diff --git a/app/services/discourse_rewind/rewind/action/activity_calendar.rb b/app/services/discourse_rewind/rewind/action/activity_calendar.rb new file mode 100644 index 0000000..8bdc7cb --- /dev/null +++ b/app/services/discourse_rewind/rewind/action/activity_calendar.rb @@ -0,0 +1,31 @@ +# 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 + def call + 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") + .map { |row| { date: row.date, post_count: row.post_count, visited: row.visited } } + + { data: calendar, identifier: "activity-calendar" } + end + end +end diff --git a/app/services/discourse_rewind/rewind/action/best_posts.rb b/app/services/discourse_rewind/rewind/action/best_posts.rb new file mode 100644 index 0000000..bd016b2 --- /dev/null +++ b/app/services/discourse_rewind/rewind/action/best_posts.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +module DiscourseRewind + class Rewind::Action::BestPosts < Rewind::Action::BaseReport + def call + best_posts = + Post + .where(user_id: user.id) + .where(created_at: date) + .where(deleted_at: nil) + .where("post_number > 1") + .order("like_count DESC NULLS LAST") + .limit(5) + .pluck(:id, :topic_id, :like_count, :reply_count, :raw, :cooked) + + { data: best_posts, identifier: "best-posts" } + end + end +end diff --git a/app/services/discourse_rewind/rewind/action/best_topics.rb b/app/services/discourse_rewind/rewind/action/best_topics.rb new file mode 100644 index 0000000..dcdfa12 --- /dev/null +++ b/app/services/discourse_rewind/rewind/action/best_topics.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +module DiscourseRewind + class Rewind::Action::BestTopics < Rewind::Action::BaseReport + def call + best_topics = + TopTopic + .includes(:topic) + .references(:topic) + .where(topic: { deleted_at: nil, created_at: date, user_id: user.id }) + .order("yearly_score DESC NULLS LAST") + .limit(5) + .pluck(:topic_id, :title, :excerpt, :yearly_score) + .map do |topic_id, title, excerpt, yearly_score| + { topic_id: topic_id, title: title, excerpt: excerpt, yearly_score: yearly_score } + end + + { data: best_topics, identifier: "best-topics" } + end + end +end diff --git a/app/services/discourse_rewind/rewind/action/favorite_categories.rb b/app/services/discourse_rewind/rewind/action/favorite_categories.rb new file mode 100644 index 0000000..c83747f --- /dev/null +++ b/app/services/discourse_rewind/rewind/action/favorite_categories.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +# Topics visited grouped by category +module DiscourseRewind + class Rewind::Action::FavoriteCategories < Rewind::Action::BaseReport + def call + favorite_categories = + TopicViewItem + .joins(:topic) + .joins("INNER JOIN categories ON categories.id = topics.category_id") + .where(user: user) + .where(viewed_at: date) + .group("categories.id, categories.name") + .order("COUNT(*) DESC") + .limit(5) + .pluck("categories.id, categories.name") + .map { |category_id, name| { category_id: category_id, name: name } } + + { data: favorite_categories, identifier: "favorite-categories" } + end + end +end diff --git a/app/services/discourse_rewind/rewind/action/favorite_tags.rb b/app/services/discourse_rewind/rewind/action/favorite_tags.rb new file mode 100644 index 0000000..30b5161 --- /dev/null +++ b/app/services/discourse_rewind/rewind/action/favorite_tags.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +# Topics visited grouped by tag +module DiscourseRewind + class Rewind::Action::FavoriteTags < Rewind::Action::BaseReport + def call + favorite_tags = + TopicViewItem + .joins(:topic) + .joins("INNER JOIN topic_tags ON topic_tags.topic_id = topics.id") + .joins("INNER JOIN tags ON tags.id = topic_tags.tag_id") + .where(user: user) + .where(viewed_at: date) + .group("tags.id, tags.name") + .order("COUNT(DISTINCT topic_views.topic_id) DESC") + .limit(5) + .pluck("tags.id, tags.name") + .map { |tag_id, name| { tag_id: tag_id, name: name } } + + { data: favorite_tags, identifier: "favorite-tags" } + end + end +end diff --git a/app/services/discourse_rewind/rewind/action/posting_calendar.rb b/app/services/discourse_rewind/rewind/action/posting_calendar.rb deleted file mode 100644 index 51921e7..0000000 --- a/app/services/discourse_rewind/rewind/action/posting_calendar.rb +++ /dev/null @@ -1,20 +0,0 @@ -# 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::PostingCalendar < Rewind::Action::BaseReport - def call - calendar = - Post - .where(user: user) - .where(created_at: date) - .select("DATE(created_at) as date, count(*) as count") - .group("DATE(created_at)") - .order("DATE(created_at)") - .map { |post| { date: post.date, count: post.count } } - - { data: calendar, identifier: "posting-calendar" } - end - end -end