Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
retribute.me
api
Commits
16d1f9ea
Verified
Commit
16d1f9ea
authored
May 25, 2019
by
Eliot Berriot
Browse files
Can now fetch links from ap username
parent
76e08567
Changes
7
Hide whitespace changes
Inline
Side-by-side
retribute_api/providers.py
View file @
16d1f9ea
...
...
@@ -9,6 +9,7 @@ class Registry(object):
if
not
obj
.
id
:
raise
ValueError
(
"{} has no id"
.
format
(
obj
.
id
))
self
.
_data
[
obj
.
id
]
=
obj
()
return
obj
def
__iter__
(
self
):
yield
from
self
.
_data
.
items
()
...
...
retribute_api/search/activitypub.py
View file @
16d1f9ea
...
...
@@ -37,5 +37,7 @@ class AttachmentSerializer(serializers.Serializer):
class
ActorSerializer
(
serializers
.
Serializer
):
id
=
serializers
.
URLField
()
url
=
serializers
.
URLField
(
required
=
False
)
attachment
=
serializers
.
ListField
(
child
=
AttachmentSerializer
(),
min_length
=
1
)
tag
=
serializers
.
ListField
(
child
=
TagSerializer
(),
min_length
=
0
)
retribute_api/search/sources.py
0 → 100644
View file @
16d1f9ea
from
..
import
providers
from
.
import
activitypub
from
.
import
webfinger
class
Registry
(
object
):
def
__init__
(
self
):
self
.
_data
=
{}
def
register
(
self
,
obj
):
if
not
obj
.
id
:
raise
ValueError
(
"{} has no id"
.
format
(
obj
.
id
))
self
.
_data
[
obj
.
id
]
=
obj
()
return
obj
registry
=
Registry
()
class
Source
(
object
):
id
=
None
async
def
get
(
self
,
lookup
):
raise
NotImplementedError
()
@
registry
.
register
class
Webfinger
(
Source
):
id
=
"webfinger"
async
def
get
(
self
,
lookup
,
session
):
webfinger_data
=
await
webfinger
.
lookup
(
lookup
,
session
)
links
=
webfinger
.
get_links
(
webfinger_data
)
found
=
None
if
"activitypub"
in
links
:
found
=
await
self
.
get_activitypub
(
links
[
"activitypub"
],
session
)
return
found
async
def
get_activitypub
(
self
,
url
,
session
):
response
=
await
session
.
get
(
url
,
headers
=
{
"Accept"
:
"application/activity+json"
}
)
response
.
raise_for_status
()
actor_data
=
await
response
.
json
()
serializer
=
activitypub
.
ActorSerializer
(
data
=
actor_data
)
serializer
.
is_valid
(
raise_exception
=
True
)
for
tag
in
serializer
.
validated_data
[
"tag"
]:
if
tag
[
"name"
]
in
[
"#nobot"
]:
return
data
=
{
"links"
:
activitypub
.
extract_urls_from_attachments
(
serializer
.
validated_data
[
"attachment"
]
)
}
return
data
retribute_api/search/webfinger.py
View file @
16d1f9ea
...
...
@@ -18,7 +18,18 @@ class AccountLinkSerializer(serializers.Serializer):
class
WebfingerSerializer
(
serializers
.
Serializer
):
links
=
serializers
.
ListField
(
child
=
AccountLinkSerializer
(),
min_length
=
1
)
links
=
serializers
.
ListField
(
child
=
serializers
.
DictField
(),
min_length
=
1
)
def
validate_links
(
self
,
value
):
final
=
[]
for
link
in
value
:
s
=
AccountLinkSerializer
(
data
=
link
)
if
s
.
is_valid
():
final
.
append
(
s
.
validated_data
)
if
not
final
:
raise
serializers
.
ValidationError
(
"No valid webfinger link found"
)
return
final
def
get_links
(
payload
):
...
...
tests/conftest.py
View file @
16d1f9ea
...
...
@@ -7,7 +7,7 @@ import asynctest
pytest_plugins
=
"aiohttp.pytest_plugin"
@
pytest
.
fixture
@
pytest
.
fixture
(
autouse
=
True
)
def
responses
():
with
aioresponses
()
as
m
:
yield
m
...
...
tests/search/test_activitypub.py
View file @
16d1f9ea
...
...
@@ -3,6 +3,8 @@ from retribute_api.search import activitypub
def
test_profile_serializer
():
payload
=
{
"id"
:
"https://test.domain"
,
"url"
:
"https://test.domain"
,
"tag"
:
[{
"type"
:
"Hashtag"
,
"name"
:
"#nobot"
}],
"attachment"
:
[
{
...
...
tests/search/test_sources.py
0 → 100644
View file @
16d1f9ea
from
retribute_api.search
import
sources
async
def
test_webfinger_source
(
mocker
,
session
,
responses
):
name
=
"username@domain.test"
webfinger_response
=
{
"subject"
:
"acct:user@domain.test"
,
"aliases"
:
[
"https://domain.test/@user"
,
"https://domain.test/users/user"
],
"links"
:
[
{
"rel"
:
"self"
,
"type"
:
"application/activity+json"
,
"href"
:
"https://domain.test/users/user"
,
}
],
}
responses
.
get
(
"https://domain.test/.well-known/webfinger?resource=acct:{}"
.
format
(
name
),
payload
=
webfinger_response
,
)
actor_response
=
{
"id"
:
"https://test.domain"
,
"tag"
:
[{
"type"
:
"Hashtag"
,
"name"
:
"#helo"
}],
"attachment"
:
[
{
"type"
:
"PropertyValue"
,
"name"
:
"Support me on Patreon"
,
"value"
:
'<a href="https://patreon.com/username" rel="me nofollow noopener">Test</a>'
,
},
{
"type"
:
"PropertyValue"
,
"name"
:
"Tip me on Ko-Fi"
,
"value"
:
'<a href="https://ko-fi.com/username" rel="me nofollow noopener">Test</a>'
,
},
],
}
responses
.
get
(
"https://domain.test/users/user"
,
payload
=
actor_response
)
expected
=
{
"links"
:
[
{
"summary"
:
"Support me on Patreon"
,
"url"
:
"https://patreon.com/username"
},
{
"summary"
:
"Tip me on Ko-Fi"
,
"url"
:
"https://ko-fi.com/username"
},
]
}
source
=
sources
.
Webfinger
()
result
=
await
source
.
get
(
name
,
session
)
assert
result
==
expected
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