Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Pleroma
pleroma
Commits
422d0f32
Commit
422d0f32
authored
Sep 17, 2017
by
lain
Browse files
MastodonAPI: Add user favorites endpoint.
parent
6d5bd4dc
Changes
4
Hide whitespace changes
Inline
Side-by-side
lib/pleroma/web/activity_pub/activity_pub.ex
View file @
422d0f32
...
...
@@ -147,6 +147,12 @@ defp restrict_type(query, %{"type" => type}) do
end
defp
restrict_type
(
query
,
_
),
do
:
query
defp
restrict_favorited_by
(
query
,
%{
"favorited_by"
=>
ap_id
})
do
from
activity
in
query
,
where:
fragment
(
"? <@ (? #> '{
\"
object
\"
,
\"
likes
\"
}')"
,
^
ap_id
,
activity
.
data
)
end
defp
restrict_favorited_by
(
query
,
_
),
do
:
query
def
fetch_activities
(
recipients
,
opts
\\
%{})
do
base_query
=
from
activity
in
Activity
,
limit:
20
,
...
...
@@ -160,6 +166,7 @@ def fetch_activities(recipients, opts \\ %{}) do
|>
restrict_max
(
opts
)
|>
restrict_actor
(
opts
)
|>
restrict_type
(
opts
)
|>
restrict_favorited_by
(
opts
)
|>
Repo
.
all
|>
Enum
.
reverse
end
...
...
lib/pleroma/web/mastodon_api/mastodon_api_controller.ex
View file @
422d0f32
...
...
@@ -312,6 +312,18 @@ def search(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do
json
(
conn
,
res
)
end
def
favourites
(%{
assigns:
%{
user:
user
}}
=
conn
,
params
)
do
params
=
conn
|>
Map
.
put
(
"type"
,
"Create"
)
|>
Map
.
put
(
"favorited_by"
,
user
.
ap_id
)
activities
=
ActivityPub
.
fetch_activities
([],
params
)
|>
Enum
.
reverse
conn
|>
render
(
StatusView
,
"index.json"
,
%{
activities:
activities
,
for:
user
,
as:
:activity
})
end
def
relationship_noop
(%{
assigns:
%{
user:
user
}}
=
conn
,
%{
"id"
=>
id
})
do
Logger
.
debug
(
"Unimplemented, returning unmodified relationship"
)
with
%
User
{}
=
target
<-
Repo
.
get
(
User
,
id
)
do
...
...
lib/pleroma/web/router.ex
View file @
422d0f32
...
...
@@ -58,6 +58,8 @@ def user_fetcher(username) do
get
"/timelines/home"
,
MastodonAPIController
,
:home_timeline
get
"/favourites"
,
MastodonAPIController
,
:favourites
post
"/statuses"
,
MastodonAPIController
,
:post_status
delete
"/statuses/:id"
,
MastodonAPIController
,
:delete_status
...
...
test/web/mastodon_api/mastodon_api_controller_test.exs
View file @
422d0f32
...
...
@@ -341,4 +341,21 @@ test "search fetches remote accounts", %{conn: conn} do
[
account
]
=
results
[
"accounts"
]
assert
account
[
"acct"
]
==
"shp@social.heldscal.la"
end
test
"returns the favorites of a user"
,
%{
conn:
conn
}
do
user
=
insert
(
:user
)
other_user
=
insert
(
:user
)
{
:ok
,
_
}
=
CommonAPI
.
post
(
other_user
,
%{
"status"
=>
"bla"
})
{
:ok
,
activity
}
=
CommonAPI
.
post
(
other_user
,
%{
"status"
=>
"traps are happy"
})
{
:ok
,
_
,
_
}
=
CommonAPI
.
favorite
(
activity
.
id
,
user
)
conn
=
conn
|>
assign
(
:user
,
user
)
|>
get
(
"/api/v1/favourites"
)
assert
[
status
]
=
json_response
(
conn
,
200
)
assert
status
[
"id"
]
==
activity
.
id
end
end
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment