Skip to content

Commit 65e13cf

Browse files
ClearlyClaireGargron
authored andcommitted
Add abilityto add oneself to lists (mastodon#12271)
* Add ability to add oneself to lists * Change search results to include oneself when searching through followers * Mark follow relation as optional in ListAccount
1 parent 1a12943 commit 65e13cf

5 files changed

Lines changed: 14 additions & 7 deletions

File tree

app/models/account.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,8 @@ def advanced_search_for(terms, account, limit = 10, following = false, offset =
429429
SELECT target_account_id
430430
FROM follows
431431
WHERE account_id = ?
432+
UNION ALL
433+
SELECT ?
432434
)
433435
SELECT
434436
accounts.*,
@@ -444,7 +446,7 @@ def advanced_search_for(terms, account, limit = 10, following = false, offset =
444446
LIMIT ? OFFSET ?
445447
SQL
446448

447-
records = find_by_sql([sql, account.id, account.id, account.id, limit, offset])
449+
records = find_by_sql([sql, account.id, account.id, account.id, account.id, limit, offset])
448450
else
449451
sql = <<-SQL.squish
450452
SELECT

app/models/list_account.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
# id :bigint(8) not null, primary key
77
# list_id :bigint(8) not null
88
# account_id :bigint(8) not null
9-
# follow_id :bigint(8) not null
9+
# follow_id :bigint(8)
1010
#
1111

1212
class ListAccount < ApplicationRecord
1313
belongs_to :list
1414
belongs_to :account
15-
belongs_to :follow
15+
belongs_to :follow, optional: true
1616

1717
validates :account_id, uniqueness: { scope: :list_id }
1818

@@ -21,6 +21,6 @@ class ListAccount < ApplicationRecord
2121
private
2222

2323
def set_follow
24-
self.follow = Follow.find_by(account_id: list.account_id, target_account_id: account.id)
24+
self.follow = Follow.find_by!(account_id: list.account_id, target_account_id: account.id) unless list.account_id == account.id
2525
end
2626
end

app/services/account_search_service.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ def time_distance_function
127127
end
128128

129129
def following_ids
130-
@following_ids ||= account.active_relationships.pluck(:target_account_id)
130+
@following_ids ||= account.active_relationships.pluck(:target_account_id) + [account.id]
131131
end
132132

133133
def limit_for_non_exact_results
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class ChangeListAccountFollowNullable < ActiveRecord::Migration[5.1]
2+
def change
3+
change_column_null :list_accounts, :follow_id, true
4+
end
5+
end

db/schema.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema.define(version: 2019_10_07_013357) do
13+
ActiveRecord::Schema.define(version: 2019_10_31_163205) do
1414

1515
# These are extensions that must be enabled in order to support this database
1616
enable_extension "plpgsql"
@@ -373,7 +373,7 @@
373373
create_table "list_accounts", force: :cascade do |t|
374374
t.bigint "list_id", null: false
375375
t.bigint "account_id", null: false
376-
t.bigint "follow_id", null: false
376+
t.bigint "follow_id"
377377
t.index ["account_id", "list_id"], name: "index_list_accounts_on_account_id_and_list_id", unique: true
378378
t.index ["follow_id"], name: "index_list_accounts_on_follow_id"
379379
t.index ["list_id", "account_id"], name: "index_list_accounts_on_list_id_and_account_id"

0 commit comments

Comments
 (0)