Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
mastodon
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Pierre Boudes
mastodon
Commits
682b6843
Commit
682b6843
authored
7 years ago
by
alpaca-tc
Committed by
Eugen Rochko
7 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Improve Account#triadic_closures (#3079)
parent
09ec6e50
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
app/models/account.rb
+13
-7
13 additions, 7 deletions
app/models/account.rb
spec/models/account_spec.rb
+31
-7
31 additions, 7 deletions
spec/models/account_spec.rb
with
44 additions
and
14 deletions
app/models/account.rb
+
13
−
7
View file @
682b6843
...
...
@@ -259,24 +259,30 @@ class Account < ApplicationRecord
nil
end
def
triadic_closures
(
account
,
limit
=
5
)
def
triadic_closures
(
account
,
limit
:
5
,
offset:
0
)
sql
=
<<-
SQL
.
squish
WITH first_degree AS (
SELECT target_account_id
FROM follows
WHERE account_id = :account_id
)
SELECT target_account_id
FROM follows
WHERE account_id = :account_id
)
SELECT accounts.*
FROM follows
INNER JOIN accounts ON follows.target_account_id = accounts.id
WHERE account_id IN (SELECT * FROM first_degree) AND target_account_id NOT IN (SELECT * FROM first_degree) AND target_account_id <> :account_id
WHERE
account_id IN (SELECT * FROM first_degree)
AND target_account_id NOT IN (SELECT * FROM first_degree)
AND target_account_id NOT IN (:excluded_account_ids)
GROUP BY target_account_id, accounts.id
ORDER BY count(account_id) DESC
OFFSET :offset
LIMIT :limit
SQL
excluded_account_ids
=
account
.
excluded_from_timeline_account_ids
+
[
account
.
id
]
find_by_sql
(
[
sql
,
{
account_id:
account
.
id
,
limit:
limi
t
}]
[
sql
,
{
account_id:
account
.
id
,
excluded_account_ids:
excluded_account_ids
,
limit:
limit
,
offset:
offse
t
}]
)
end
...
...
This diff is collapsed.
Click to expand it.
spec/models/account_spec.rb
+
31
−
7
View file @
682b6843
...
...
@@ -261,19 +261,43 @@ RSpec.describe Account, type: :model do
end
describe
'.triadic_closures'
do
it
'finds accounts you dont follow which are followed by accounts you do follow'
do
me
=
Fabricate
(
:account
)
friend
=
Fabricate
(
:account
)
friends_friend
=
Fabricate
(
:account
)
subject
{
described_class
.
triadic_closures
(
me
)
}
let!
(
:me
)
{
Fabricate
(
:account
)
}
let!
(
:friend
)
{
Fabricate
(
:account
)
}
let!
(
:friends_friend
)
{
Fabricate
(
:account
)
}
let!
(
:both_follow
)
{
Fabricate
(
:account
)
}
before
do
me
.
follow!
(
friend
)
friend
.
follow!
(
friends_friend
)
both_follow
=
Fabricate
(
:account
)
me
.
follow!
(
both_follow
)
friend
.
follow!
(
both_follow
)
end
it
'finds accounts you dont follow which are followed by accounts you do follow'
do
is_expected
.
to
eq
[
friends_friend
]
end
context
'when you block account'
do
before
do
me
.
block!
(
friends_friend
)
end
it
'rejects blocked accounts'
do
is_expected
.
to
be_empty
end
end
results
=
Account
.
triadic_closures
(
me
)
expect
(
results
).
to
eq
[
friends_friend
]
context
'when you mute account'
do
before
do
me
.
mute!
(
friends_friend
)
end
it
'rejects muted accounts'
do
is_expected
.
to
be_empty
end
end
end
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment