From 6afa8b1f351b21e2b5ca7a9783f6398396f16358 Mon Sep 17 00:00:00 2001 From: mgabdev <> Date: Fri, 6 Nov 2020 23:24:16 -0600 Subject: [PATCH] Added PopularLinks controller MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit • Added: - PopularLinks controller to fetch popular links, most recent 2 gabs from `@gab` • Todo: - Clean up code, store results in redis for time - Add top video/links --- .../api/v1/popular_links_controller.rb | 59 +++++++++++++++++++ config/routes.rb | 1 + 2 files changed, 60 insertions(+) create mode 100644 app/controllers/api/v1/popular_links_controller.rb diff --git a/app/controllers/api/v1/popular_links_controller.rb b/app/controllers/api/v1/popular_links_controller.rb new file mode 100644 index 00000000..e23de5a4 --- /dev/null +++ b/app/controllers/api/v1/popular_links_controller.rb @@ -0,0 +1,59 @@ +# frozen_string_literal: true + +class Api::V1::PopularLinksController < Api::BaseController + + def show + type = params[:type] + + if type == 'links' + render json: get_cards, each_serializer: REST::PreviewCardSerializer + elsif type == 'videos' + render json: get_videos, each_serializer: REST::PreviewCardSerializer + elsif type == 'gab' + render json: get_top_gab, each_serializer: REST::StatusSerializer + else + raise GabSocial::NotPermittedError + end + end + + private + + def get_top_gab + # prod gab.com @gab account = 251 + Status.where(account_id: '1').limit(2) + end + + def get_videos + end + + def get_cards + statusIds = Status.where('statuses.created_at > ?', 24.hours.ago) + .joins(:status_stat) + .order('status_stats.favourites_count DESC') + .where('status_stats.favourites_count > 1') + .joins("LEFT JOIN preview_cards_statuses ON statuses.id = preview_cards_statuses.status_id") + .where('preview_cards_statuses.status_id IS NOT NULL') + .limit(100) + .pluck('statuses.id') + + cards = PreviewCard.joins("LEFT JOIN preview_cards_statuses ON preview_cards.id = preview_cards_statuses.preview_card_id") + .where('preview_cards_statuses.preview_card_id IS NOT NULL') + .where('preview_cards_statuses.status_id IN (?)', statusIds) + .having('COUNT(preview_cards_statuses.preview_card_id) > 1') + .where('preview_cards.updated_at > ?', 24.hours.ago) + .order('COUNT(preview_cards_statuses.preview_card_id) DESC') + .group('preview_cards.id') + .limit(10) + + cards + + # if body.nil? || body.empty? + # Redis.current.set("gabtrends:feed", body) + # Redis.current.expire("gabtrends:feed", 1.hour.seconds) + # return cards + # else + # return cardIds + # end + end + +end diff --git a/config/routes.rb b/config/routes.rb index 95cd6dc3..94bf6cc9 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -337,6 +337,7 @@ Rails.application.routes.draw do resources :gab_trends, only: [:index] resources :links, only: :show + resource :popular_links, only: :show resources :shop, only: [:index] resources :streaming, only: [:index] resources :custom_emojis, only: [:index]