Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
pleroma
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
Container Registry
Model registry
Operate
Environments
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
Alex Gleason
pleroma
Commits
84fe1620
Commit
84fe1620
authored
5 years ago
by
rinpatch
Browse files
Options
Downloads
Patches
Plain Diff
Search: join on activities instead of objects
parent
6d33c89c
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
lib/mix/tasks/pleroma/benchmark.ex
+4
-1
4 additions, 1 deletion
lib/mix/tasks/pleroma/benchmark.ex
lib/pleroma/activity/search.ex
+77
-12
77 additions, 12 deletions
lib/pleroma/activity/search.ex
lib/pleroma/object.ex
+1
-0
1 addition, 0 deletions
lib/pleroma/object.ex
with
82 additions
and
13 deletions
lib/mix/tasks/pleroma/benchmark.ex
+
4
−
1
View file @
84fe1620
...
...
@@ -10,8 +10,11 @@ def run(["search"]) do
start_pleroma
()
Benchee
.
run
(%{
"
search
"
=>
fn
->
"
With joined objects
"
=>
fn
->
Pleroma
.
Activity
.
search
(
nil
,
"cofe"
)
end
,
"With joined activities"
=>
fn
->
Pleroma
.
Activity
.
search
(
nil
,
"cofe"
,
join_on:
:activities
)
end
})
end
...
...
This diff is collapsed.
Click to expand it.
lib/pleroma/activity/search.ex
+
77
−
12
View file @
84fe1620
...
...
@@ -4,6 +4,7 @@
defmodule
Pleroma
.
Activity
.
Search
do
alias
Pleroma
.
Activity
alias
Pleroma
.
Object
alias
Pleroma
.
Object
.
Fetcher
alias
Pleroma
.
Pagination
alias
Pleroma
.
User
...
...
@@ -15,38 +16,94 @@ defmodule Pleroma.Activity.Search do
def
search
(
user
,
search_query
,
options
\\
[])
do
index_type
=
if
Pleroma
.
Config
.
get
([
:database
,
:rum_enabled
]),
do
:
:rum
,
else
:
:gin
join_on
=
options
[
:join_on
]
||
:activities
limit
=
Enum
.
min
([
Keyword
.
get
(
options
,
:limit
),
40
])
offset
=
Keyword
.
get
(
options
,
:offset
,
0
)
author
=
Keyword
.
get
(
options
,
:author
)
Activity
|>
Activity
.
with_preloaded_object
()
|>
Activity
.
restrict_deactivated_users
()
|>
restrict_public
()
base_query
(
join_on
)
|>
restrict_deactivated
(
join_on
)
|>
restrict_public
(
join_on
)
|>
query_with
(
index_type
,
search_query
)
|>
maybe_restrict_local
(
user
)
|>
maybe_restrict_author
(
author
)
|>
maybe_restrict_author
(
author
,
join_on
)
|>
Pagination
.
fetch_paginated
(%{
"offset"
=>
offset
,
"limit"
=>
limit
},
:offset
)
|>
maybe_transform
(
join_on
)
|>
maybe_fetch
(
user
,
search_query
)
end
def
maybe_restrict_author
(
query
,
%
User
{}
=
author
)
do
def
maybe_restrict_author
(
query
,
%
User
{}
=
author
,
:objects
)
do
from
([
a
,
o
]
in
query
,
where:
a
.
actor
==
^
author
.
ap_id
)
end
def
maybe_restrict_author
(
query
,
_
),
do
:
query
def
maybe_restrict_author
(
query
,
%
User
{}
=
author
,
:activities
)
do
from
([
o
]
in
query
,
where:
fragment
(
"?->>'actor' = ?"
,
o
.
data
,
^
author
.
ap_id
)
)
end
defp
restrict_public
(
q
)
do
def
maybe_restrict_author
(
query
,
_
,
_
),
do
:
query
defp
restrict_public
(
q
,
:objects
)
do
from
([
a
,
o
]
in
q
,
where:
fragment
(
"?->>'type' = 'Create'"
,
a
.
data
),
where:
^
Pleroma
.
Constants
.
as_public
()
in
a
.
recipients
)
end
defp
restrict_public
(
q
,
:activities
)
do
from
([
o
,
a
]
in
q
,
where:
fragment
(
"?->'to'
\\
? ? OR ?->'cc'
\\
? ?"
,
o
.
data
,
^
Pleroma
.
Constants
.
as_public
(),
o
.
data
,
^
Pleroma
.
Constants
.
as_public
()
)
)
end
defp
base_query
(
:activities
)
do
from
(
o
in
Object
,
as:
:object
,
join:
a
in
Activity
,
as:
:activity
,
on:
fragment
(
"(?->>'id') = COALESCE(?->'object'->>'id', ?->>'object')"
,
o
.
data
,
a
.
data
,
a
.
data
)
and
fragment
(
"?->>'type' = 'Create'"
,
a
.
data
),
preload:
[
create_activity:
a
]
)
end
defp
base_query
(
:objects
)
do
from
(
a
in
Activity
,
as:
:activity
)
|>
Activity
.
with_preloaded_object
()
|>
where
([
activity:
a
],
fragment
(
"?->>'type' = 'Create'"
,
a
.
data
))
end
defp
restrict_deactivated
(
query
,
:activities
)
do
from
(
[
object:
o
]
in
query
,
where:
fragment
(
"?->>'actor' not in (SELECT ap_id FROM users WHERE info->'deactivated' @> 'true')"
,
o
.
data
)
)
end
defp
restrict_deactivated
(
query
,
:objects
)
do
Activity
.
restrict_deactivated_users
(
query
)
end
defp
query_with
(
q
,
:gin
,
search_query
)
do
from
([
a
,
o
]
in
q
,
from
([
object:
o
]
in
q
,
where:
fragment
(
"to_tsvector('english', ?->>'content') @@ plainto_tsquery('english', ?)"
,
...
...
@@ -57,7 +114,7 @@ defp query_with(q, :gin, search_query) do
end
defp
query_with
(
q
,
:rum
,
search_query
)
do
from
([
a
,
o
]
in
q
,
from
([
object:
o
]
in
q
,
where:
fragment
(
"? @@ plainto_tsquery('english', ?)"
,
...
...
@@ -79,7 +136,15 @@ defp maybe_restrict_local(q, user) do
end
end
defp
restrict_local
(
q
),
do
:
where
(
q
,
local:
true
)
defp
restrict_local
(
q
),
do
:
where
(
q
,
[
activity:
a
],
a
.
local
==
true
)
defp
maybe_transform
(
activities
,
:objects
),
do
:
activities
defp
maybe_transform
(
objects
,
:activities
)
do
Enum
.
map
(
objects
,
fn
object
->
Map
.
put
(
object
.
create_activity
,
:object
,
object
)
end
)
end
defp
maybe_fetch
(
activities
,
user
,
search_query
)
do
with
true
<-
Regex
.
match?
(
~r/https?:/
,
search_query
),
...
...
This diff is collapsed.
Click to expand it.
lib/pleroma/object.ex
+
1
−
0
View file @
84fe1620
...
...
@@ -19,6 +19,7 @@ defmodule Pleroma.Object do
schema
"objects"
do
field
(
:data
,
:map
)
has_one
(
:create_activity
,
Activity
,
on_delete:
:nothing
,
foreign_key:
:id
)
timestamps
()
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