Skip to content
GitLab
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
2db28df4
Commit
2db28df4
authored
Mar 21, 2017
by
lain
Browse files
Add public timeline TwAPI.
parent
b9d0e345
Changes
6
Hide whitespace changes
Inline
Side-by-side
lib/pleroma/web/activity_pub/activity_pub.ex
View file @
2db28df4
...
...
@@ -8,7 +8,7 @@ def insert(map) when is_map(map) do
end
def
fetch_public_activities
(
opts
\\
%{})
do
since_id
=
opts
[
:
since_id
]
||
0
since_id
=
opts
[
"
since_id
"
]
||
0
query
=
from
activity
in
Activity
,
where:
fragment
(
~s(? @> '{"to": ["https://www.w3.org/ns/activitystreams#Public"]}')
,
activity
.
data
),
...
...
lib/pleroma/web/router.ex
View file @
2db28df4
...
...
@@ -9,12 +9,19 @@ def user_fetcher(username) do
pipeline
:api
do
plug
:accepts
,
[
"json"
]
plug
:fetch_session
plug
Pleroma
.
Plugs
.
AuthenticationPlug
,
%{
fetcher:
&
Pleroma
.
Web
.
Router
.
user_fetcher
/
1
,
optional:
true
}
end
pipeline
:authenticated_api
do
plug
:accepts
,
[
"json"
]
plug
:fetch_session
plug
Pleroma
.
Plugs
.
AuthenticationPlug
,
fetcher:
&
Pleroma
.
Web
.
Router
.
user_fetcher
/
1
plug
Pleroma
.
Plugs
.
AuthenticationPlug
,
%{
fetcher:
&
Pleroma
.
Web
.
Router
.
user_fetcher
/
1
}
end
scope
"/api"
,
Pleroma
.
Web
do
pipe_through
:api
get
"/statuses/public_timeline.json"
,
TwitterAPI
.
Controller
,
:public_timeline
end
scope
"/api"
,
Pleroma
.
Web
do
...
...
lib/pleroma/web/twitter_api/twitter_api.ex
View file @
2db28df4
...
...
@@ -21,8 +21,8 @@ def create_status(user = %User{}, data = %{}) do
ActivityPub
.
insert
(
activity
)
end
def
fetch_public_statuses
do
activities
=
ActivityPub
.
fetch_public_activities
def
fetch_public_statuses
(
opts
\\
%{})
do
activities
=
ActivityPub
.
fetch_public_activities
(
opts
)
Enum
.
map
(
activities
,
fn
(
activity
)
->
actor
=
get_in
(
activity
.
data
,
[
"actor"
])
...
...
lib/pleroma/web/twitter_api/twitter_api_controller.ex
View file @
2db28df4
...
...
@@ -16,6 +16,14 @@ def status_update(%{assigns: %{user: user}} = conn, status_data) do
|>
json_reply
(
200
,
ActivityRepresenter
.
to_json
(
activity
,
%{
user:
user
}))
end
def
public_timeline
(
conn
,
params
)
do
statuses
=
TwitterAPI
.
fetch_public_statuses
(
params
)
{
:ok
,
json
}
=
Poison
.
encode
(
statuses
)
conn
|>
json_reply
(
200
,
json
)
end
defp
json_reply
(
conn
,
status
,
json
)
do
conn
|>
put_resp_content_type
(
"application/json"
)
...
...
test/web/activity_pub/activity_pub_test.exs
View file @
2db28df4
...
...
@@ -41,7 +41,7 @@ test "retrieves ids starting from a since_id" do
since_id
=
List
.
last
(
activities
)
.
id
last_expected
=
List
.
last
(
later_activities
)
activities
=
ActivityPub
.
fetch_public_activities
(%{
since_id
:
since_id
})
activities
=
ActivityPub
.
fetch_public_activities
(%{
"
since_id
"
=>
since_id
})
last
=
List
.
last
(
activities
)
assert
length
(
activities
)
==
10
...
...
test/web/twitter_api/twitter_api_controller_test.exs
View file @
2db28df4
defmodule
Pleroma
.
Web
.
TwitterAPI
.
ControllerTest
do
use
Pleroma
.
Web
.
ConnCase
alias
Pleroma
.
Web
.
TwitterAPI
.
Representers
.
{
UserRepresenter
,
ActivityRepresenter
}
alias
Pleroma
.
Builders
.
UserBuilder
alias
Pleroma
.
Builders
.
{
ActivityBuilder
,
UserBuilder
}
alias
Pleroma
.
{
Repo
,
Activity
}
describe
"POST /api/account/verify_credentials"
do
...
...
@@ -36,6 +36,22 @@ test "with credentials", %{conn: conn, user: user} do
end
end
describe
"GET /statuses/public_timeline.json"
do
test
"returns statuses"
,
%{
conn:
conn
}
do
{
:ok
,
user
}
=
UserBuilder
.
insert
activities
=
ActivityBuilder
.
insert_list
(
30
,
%{},
%{
user:
user
})
ActivityBuilder
.
insert_list
(
10
,
%{},
%{
user:
user
})
since_id
=
List
.
last
(
activities
)
.
id
conn
=
conn
|>
get
(
"/api/statuses/public_timeline.json"
,
%{
since_id:
since_id
})
response
=
json_response
(
conn
,
200
)
assert
length
(
response
)
==
10
end
end
defp
valid_user
(
_context
)
do
{
:ok
,
user
}
=
UserBuilder
.
insert
[
user:
user
]
...
...
Write
Preview
Supports
Markdown
0%
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!
Cancel
Please
register
or
sign in
to comment