From 8ae260fa8387219957b93d42a15b79b5966b9353 Mon Sep 17 00:00:00 2001 From: KarishmaVanwari Date: Sat, 6 Aug 2022 00:59:37 +0530 Subject: [PATCH 1/3] Fixes #11331: Created random generator to fetch nodes randomly as cards --- app/controllers/wiki_controller.rb | 5 ++++ app/models/tag.rb | 30 ++++++++++++++++++++++++ app/views/dashboard_v2/_sidebar.html.erb | 15 +++++++++++- 3 files changed, 49 insertions(+), 1 deletion(-) diff --git a/app/controllers/wiki_controller.rb b/app/controllers/wiki_controller.rb index e9b1d3a89e..89b81efa15 100644 --- a/app/controllers/wiki_controller.rb +++ b/app/controllers/wiki_controller.rb @@ -60,6 +60,11 @@ def show if !@node.nil? # it's a place page! @tags = @node.tags @tags += [Tag.find_by(name: params[:id])] if Tag.find_by(name: params[:id]) + + tag1, tag2 = @node.normal_tags(:followers).includes(:tag).pluck(:name).first(2) + + # get recommendations + @recommendations = Tag.get_recommendations(tag1, tag2) else # it's a new wiki page! @title = I18n.t('wiki_controller.new_wiki_page') if current_user diff --git a/app/models/tag.rb b/app/models/tag.rb index 147190bdfb..e37ced71d9 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -478,4 +478,34 @@ def span(start, fin) def range(fin, week) (fin.to_i - week.weeks.to_i).to_s..(fin.to_i - (week - 1).weeks.to_i).to_s end + + def self.get_recommendations(tag1, tag2) + + tag1_content_nids = find_recommended_nodes(tag1) + tag2_content_nids = find_recommended_nodes(tag2) + + random_content_nids = tag1_content_nids.sample(3) + tag2_content_nids.sample(3) + + Node.where("nid IN (?)", random_content_nids) + end + + def self.find_recommended_nodes(tagnames, limit = 10) + + date_ranges = [1.years.ago..3.years.ago, 4.years.ago..6.years.ago, 7.years.ago..9.years.ago] + + selected_date_range = date_ranges.sample(1) + + nodes = Node.where("cached_likes > 20 AND views > 100", status: 1) + .where(created: selected_date_range) + .includes(:tag) + .references(:term_data) + .where('term_data.name IN (?)', tagnames) + + Node.where('node.nid IN (?)', nodes.collect(&:nid)) + .includes(:revision, :tag) + .references(:node_revisions) + .where(status: 1) + .limit(limit) + .pluck(:nid) + end end diff --git a/app/views/dashboard_v2/_sidebar.html.erb b/app/views/dashboard_v2/_sidebar.html.erb index c6cd6ad7cf..ac2a133d91 100644 --- a/app/views/dashboard_v2/_sidebar.html.erb +++ b/app/views/dashboard_v2/_sidebar.html.erb @@ -55,7 +55,20 @@ - +
+<% if !@recommendations.nil? %> + <% @recommendations.each do |node| %> +
+ Card image cap +
+
<%= node.nid %>
+

Post content here

+ Read more button here +
+
+ <% end %> +<% end %> +