mirror of
https://github.com/mastodon/mastodon.git
synced 2025-07-13 15:58:13 +00:00
Compare commits
2 Commits
d05b067eb5
...
8ef3f7e0c3
Author | SHA1 | Date | |
---|---|---|---|
![]() |
8ef3f7e0c3 | ||
![]() |
f9c8c00523 |
43
app/models/concerns/status/mappings.rb
Normal file
43
app/models/concerns/status/mappings.rb
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
module Status::Mappings
|
||||||
|
extend ActiveSupport::Concern
|
||||||
|
|
||||||
|
class_methods do
|
||||||
|
def bookmarks_map(status_ids, account_id)
|
||||||
|
Bookmark
|
||||||
|
.where(status_id: status_ids, account_id: account_id)
|
||||||
|
.pluck(:status_id)
|
||||||
|
.index_with(true)
|
||||||
|
end
|
||||||
|
|
||||||
|
def favourites_map(status_ids, account_id)
|
||||||
|
Favourite
|
||||||
|
.where(status_id: status_ids, account_id: account_id)
|
||||||
|
.pluck(:status_id)
|
||||||
|
.index_with(true)
|
||||||
|
end
|
||||||
|
|
||||||
|
def mutes_map(conversation_ids, account_id)
|
||||||
|
ConversationMute
|
||||||
|
.where(conversation_id: conversation_ids, account_id: account_id)
|
||||||
|
.pluck(:conversation_id)
|
||||||
|
.index_with(true)
|
||||||
|
end
|
||||||
|
|
||||||
|
def pins_map(status_ids, account_id)
|
||||||
|
StatusPin
|
||||||
|
.where(status_id: status_ids, account_id: account_id)
|
||||||
|
.pluck(:status_id)
|
||||||
|
.index_with(true)
|
||||||
|
end
|
||||||
|
|
||||||
|
def reblogs_map(status_ids, account_id)
|
||||||
|
Status
|
||||||
|
.unscoped
|
||||||
|
.where(reblog_of_id: status_ids, account_id: account_id)
|
||||||
|
.pluck(:reblog_of_id)
|
||||||
|
.index_with(true)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -38,6 +38,7 @@ class Status < ApplicationRecord
|
||||||
include RateLimitable
|
include RateLimitable
|
||||||
include Status::FaspConcern
|
include Status::FaspConcern
|
||||||
include Status::FetchRepliesConcern
|
include Status::FetchRepliesConcern
|
||||||
|
include Status::Mappings
|
||||||
include Status::SafeReblogInsert
|
include Status::SafeReblogInsert
|
||||||
include Status::SearchConcern
|
include Status::SearchConcern
|
||||||
include Status::SnapshotConcern
|
include Status::SnapshotConcern
|
||||||
|
@ -353,26 +354,6 @@ class Status < ApplicationRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
class << self
|
class << self
|
||||||
def favourites_map(status_ids, account_id)
|
|
||||||
Favourite.select(:status_id).where(status_id: status_ids).where(account_id: account_id).each_with_object({}) { |f, h| h[f.status_id] = true }
|
|
||||||
end
|
|
||||||
|
|
||||||
def bookmarks_map(status_ids, account_id)
|
|
||||||
Bookmark.select(:status_id).where(status_id: status_ids).where(account_id: account_id).to_h { |f| [f.status_id, true] }
|
|
||||||
end
|
|
||||||
|
|
||||||
def reblogs_map(status_ids, account_id)
|
|
||||||
unscoped.select(:reblog_of_id).where(reblog_of_id: status_ids).where(account_id: account_id).each_with_object({}) { |s, h| h[s.reblog_of_id] = true }
|
|
||||||
end
|
|
||||||
|
|
||||||
def mutes_map(conversation_ids, account_id)
|
|
||||||
ConversationMute.select(:conversation_id).where(conversation_id: conversation_ids).where(account_id: account_id).each_with_object({}) { |m, h| h[m.conversation_id] = true }
|
|
||||||
end
|
|
||||||
|
|
||||||
def pins_map(status_ids, account_id)
|
|
||||||
StatusPin.select(:status_id).where(status_id: status_ids).where(account_id: account_id).each_with_object({}) { |p, h| h[p.status_id] = true }
|
|
||||||
end
|
|
||||||
|
|
||||||
def from_text(text)
|
def from_text(text)
|
||||||
return [] if text.blank?
|
return [] if text.blank?
|
||||||
|
|
||||||
|
|
70
spec/models/concerns/status/mappings_spec.rb
Normal file
70
spec/models/concerns/status/mappings_spec.rb
Normal file
|
@ -0,0 +1,70 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require 'rails_helper'
|
||||||
|
|
||||||
|
RSpec.describe Status::Mappings do
|
||||||
|
describe '.bookmarks_map' do
|
||||||
|
subject { Status.bookmarks_map([status], account) }
|
||||||
|
|
||||||
|
let(:status) { Fabricate(:status) }
|
||||||
|
let(:account) { Fabricate(:account) }
|
||||||
|
|
||||||
|
context 'with a bookmarkeded status' do
|
||||||
|
before { Fabricate(:bookmark, account: account, status: status) }
|
||||||
|
|
||||||
|
it { is_expected.to be_a(Hash).and include(status.id => true) }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe '.favourites_map' do
|
||||||
|
subject { Status.favourites_map([status], account) }
|
||||||
|
|
||||||
|
let(:status) { Fabricate(:status) }
|
||||||
|
let(:account) { Fabricate(:account) }
|
||||||
|
|
||||||
|
context 'with a favourited status' do
|
||||||
|
before { Fabricate(:favourite, status: status, account: account) }
|
||||||
|
|
||||||
|
it { is_expected.to be_a(Hash).and include(status.id => true) }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe '.mutes_map' do
|
||||||
|
subject { Status.mutes_map([status.conversation.id], account) }
|
||||||
|
|
||||||
|
let(:status) { Fabricate(:status) }
|
||||||
|
let(:account) { Fabricate(:account) }
|
||||||
|
|
||||||
|
context 'with a muted conversation' do
|
||||||
|
before { account.mute_conversation!(status.conversation) }
|
||||||
|
|
||||||
|
it { is_expected.to be_a(Hash).and include(status.conversation_id => true) }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe '.pins_map' do
|
||||||
|
subject { Status.pins_map([status], account) }
|
||||||
|
|
||||||
|
let(:status) { Fabricate(:status, account: account) }
|
||||||
|
let(:account) { Fabricate(:account) }
|
||||||
|
|
||||||
|
context 'with a pinned status' do
|
||||||
|
before { Fabricate(:status_pin, account: account, status: status) }
|
||||||
|
|
||||||
|
it { is_expected.to be_a(Hash).and include(status.id => true) }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe '.reblogs_map' do
|
||||||
|
subject { Status.reblogs_map([status], account) }
|
||||||
|
|
||||||
|
let(:status) { Fabricate(:status) }
|
||||||
|
let(:account) { Fabricate(:account) }
|
||||||
|
|
||||||
|
context 'with a reblogged status' do
|
||||||
|
before { Fabricate(:status, account: account, reblog: status) }
|
||||||
|
|
||||||
|
it { is_expected.to be_a(Hash).and include(status.id => true) }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -317,54 +317,6 @@ RSpec.describe Status do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe '.mutes_map' do
|
|
||||||
subject { described_class.mutes_map([status.conversation.id], account) }
|
|
||||||
|
|
||||||
let(:status) { Fabricate(:status) }
|
|
||||||
let(:account) { Fabricate(:account) }
|
|
||||||
|
|
||||||
it 'returns a hash' do
|
|
||||||
expect(subject).to be_a Hash
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'contains true value' do
|
|
||||||
account.mute_conversation!(status.conversation)
|
|
||||||
expect(subject[status.conversation.id]).to be true
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
describe '.favourites_map' do
|
|
||||||
subject { described_class.favourites_map([status], account) }
|
|
||||||
|
|
||||||
let(:status) { Fabricate(:status) }
|
|
||||||
let(:account) { Fabricate(:account) }
|
|
||||||
|
|
||||||
it 'returns a hash' do
|
|
||||||
expect(subject).to be_a Hash
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'contains true value' do
|
|
||||||
Fabricate(:favourite, status: status, account: account)
|
|
||||||
expect(subject[status.id]).to be true
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
describe '.reblogs_map' do
|
|
||||||
subject { described_class.reblogs_map([status], account) }
|
|
||||||
|
|
||||||
let(:status) { Fabricate(:status) }
|
|
||||||
let(:account) { Fabricate(:account) }
|
|
||||||
|
|
||||||
it 'returns a hash' do
|
|
||||||
expect(subject).to be_a Hash
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'contains true value' do
|
|
||||||
Fabricate(:status, account: account, reblog: status)
|
|
||||||
expect(subject[status.id]).to be true
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
describe '.only_reblogs' do
|
describe '.only_reblogs' do
|
||||||
let!(:status) { Fabricate :status }
|
let!(:status) { Fabricate :status }
|
||||||
let!(:reblog) { Fabricate :status, reblog: Fabricate(:status) }
|
let!(:reblog) { Fabricate :status, reblog: Fabricate(:status) }
|
||||||
|
|
Loading…
Reference in New Issue
Block a user