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
7382adf4
Commit
7382adf4
authored
Jan 07, 2019
by
lain
Browse files
Make TwAPI UserView more resilient to issues.
Will work for missing users and badly migrated users.
parent
b0e8e521
Pipeline
#6053
passed with stages
in 6 minutes and 8 seconds
Changes
3
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
lib/pleroma/user.ex
View file @
7382adf4
...
...
@@ -367,6 +367,15 @@ def get_by_ap_id(ap_id) do
Repo
.
get_by
(
User
,
ap_id:
ap_id
)
end
# This is mostly an SPC migration fix. This guesses the user nickname (by taking the last part of the ap_id and the domain) and tries to get that user
def
get_by_guessed_nickname
(
ap_id
)
do
domain
=
URI
.
parse
(
ap_id
)
.
host
name
=
List
.
last
(
String
.
split
(
ap_id
,
"/"
))
nickname
=
"
#{
name
}
@
#{
domain
}
"
get_by_nickname
(
nickname
)
end
def
update_and_set_cache
(
changeset
)
do
with
{
:ok
,
user
}
<-
Repo
.
update
(
changeset
)
do
Cachex
.
put
(
:user_cache
,
"ap_id:
#{
user
.
ap_id
}
"
,
user
)
...
...
lib/pleroma/web/twitter_api/views/activity_view.ex
View file @
7382adf4
...
...
@@ -94,11 +94,25 @@ defp get_user(ap_id, opts) do
ap_id
==
"https://www.w3.org/ns/activitystreams#Public"
->
nil
user
=
User
.
get_cached_by_ap_id
(
ap_id
)
->
user
user
=
User
.
get_by_guessed_nickname
(
ap_id
)
->
user
true
->
User
.
get_cached_by_ap_id
(
ap_id
)
error_user
(
)
end
end
defp
error_user
do
%
User
{
info:
%
User
.
Info
{},
nickname:
"erroruser@example.com"
,
inserted_at:
NaiveDateTime
.
utc_now
()
}
end
def
render
(
"index.json"
,
opts
)
do
context_ids
=
collect_context_ids
(
opts
.
activities
)
users
=
collect_users
(
opts
.
activities
)
...
...
test/web/twitter_api/views/activity_view_test.exs
View file @
7382adf4
...
...
@@ -25,6 +25,34 @@ defmodule Pleroma.Web.TwitterAPI.ActivityViewTest do
import
Mock
test
"returns an error user for activities missing users"
do
user
=
insert
(
:user
)
{
:ok
,
activity
}
=
CommonAPI
.
post
(
user
,
%{
"status"
=>
"Hey @shp!"
,
"visibility"
=>
"direct"
})
Repo
.
delete
(
user
)
Cachex
.
clear
(
:user_cache
)
result
=
ActivityView
.
render
(
"activity.json"
,
activity:
activity
)
assert
result
end
test
"tries to get a user by nickname if fetching by ap_id doesn't work"
do
user
=
insert
(
:user
)
{
:ok
,
activity
}
=
CommonAPI
.
post
(
user
,
%{
"status"
=>
"Hey @shp!"
,
"visibility"
=>
"direct"
})
{
:ok
,
user
}
=
user
|>
Ecto
.
Changeset
.
change
(%{
ap_id:
"
#{
user
.
ap_id
}
/extension/
#{
user
.
nickname
}
"
})
|>
Repo
.
update
()
Cachex
.
clear
(
:user_cache
)
result
=
ActivityView
.
render
(
"activity.json"
,
activity:
activity
)
assert
result
[
"user"
][
"id"
]
==
user
.
id
end
test
"a create activity with a html status"
do
text
=
"""
#Bike log - Commute Tuesday\nhttps://pla.bike/posts/20181211/\n#cycling #CHScycling #commute\nMVIMG_20181211_054020.jpg
...
...
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