From 2617c33fc3e4020f3d043f297e4c62c0fdef572f Mon Sep 17 00:00:00 2001 From: Claire Date: Wed, 14 Jun 2023 08:54:52 +0200 Subject: [PATCH] Fix ArgumentError when loading newer Private Mentions (#25399) --- .../api/v1/conversations_controller.rb | 2 +- app/models/account_conversation.rb | 19 +++++++++---------- .../api/v1/conversations_controller_spec.rb | 18 ++++++++++++++++++ 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/app/controllers/api/v1/conversations_controller.rb b/app/controllers/api/v1/conversations_controller.rb index afc115786e..818ba6ebba 100644 --- a/app/controllers/api/v1/conversations_controller.rb +++ b/app/controllers/api/v1/conversations_controller.rb @@ -45,7 +45,7 @@ class Api::V1::ConversationsController < Api::BaseController }, ] ) - .to_a_paginated_by_id(limit_param(LIMIT), **params_slice(:max_id, :since_id, :min_id)) + .to_a_paginated_by_id(limit_param(LIMIT), params_slice(:max_id, :since_id, :min_id)) end def insert_pagination_headers diff --git a/app/models/account_conversation.rb b/app/models/account_conversation.rb index 6b1d246042..78cc05614c 100644 --- a/app/models/account_conversation.rb +++ b/app/models/account_conversation.rb @@ -42,22 +42,21 @@ class AccountConversation < ApplicationRecord end class << self - def to_a_paginated_by_id(limit, min_id: nil, max_id: nil, since_id: nil, preload_participants: true) + def to_a_paginated_by_id(limit, options = {}) array = begin - if min_id - paginate_by_min_id(limit, min_id, max_id).reverse + if options[:min_id] + paginate_by_min_id(limit, options[:min_id], options[:max_id]).reverse else - paginate_by_max_id(limit, max_id, since_id).to_a + paginate_by_max_id(limit, options[:max_id], options[:since_id]).to_a end end - if preload_participants - participant_ids = array.flat_map(&:participant_account_ids) - accounts_by_id = Account.where(id: participant_ids).index_by(&:id) + # Preload participants + participant_ids = array.flat_map(&:participant_account_ids) + accounts_by_id = Account.where(id: participant_ids).index_by(&:id) - array.each do |conversation| - conversation.participant_accounts = conversation.participant_account_ids.filter_map { |id| accounts_by_id[id] } - end + array.each do |conversation| + conversation.participant_accounts = conversation.participant_account_ids.filter_map { |id| accounts_by_id[id] } end array diff --git a/spec/controllers/api/v1/conversations_controller_spec.rb b/spec/controllers/api/v1/conversations_controller_spec.rb index 5add7cf1d4..c5d604e053 100644 --- a/spec/controllers/api/v1/conversations_controller_spec.rb +++ b/spec/controllers/api/v1/conversations_controller_spec.rb @@ -33,5 +33,23 @@ RSpec.describe Api::V1::ConversationsController, type: :controller do json = body_as_json expect(json.size).to eq 1 end + + context 'with since_id' do + context 'when requesting old posts' do + it 'returns conversations' do + get :index, params: { since_id: Mastodon::Snowflake.id_at(1.hour.ago, with_random: false) } + json = body_as_json + expect(json.size).to eq 1 + end + end + + context 'when requesting posts in the future' do + it 'returns no conversation' do + get :index, params: { since_id: Mastodon::Snowflake.id_at(1.hour.from_now, with_random: false) } + json = body_as_json + expect(json.size).to eq 0 + end + end + end end end