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
bc31bee7
Commit
bc31bee7
authored
Apr 02, 2018
by
lain
Browse files
Generates contexts and ids on insertion time.
parent
a4db3a73
Changes
3
Hide whitespace changes
Inline
Side-by-side
lib/pleroma/web/activity_pub/utils.ex
View file @
bc31bee7
...
...
@@ -45,6 +45,18 @@ def generate_id(type) do
"
#{
Web
.
base_url
()
}
/
#{
type
}
/
#{
UUID
.
generate
()
}
"
end
def
create_context
(
context
)
do
context
=
context
||
generate_id
(
"contexts"
)
changeset
=
Object
.
context_mapping
(
context
)
case
Repo
.
insert
(
changeset
)
do
{
:ok
,
object
}
->
object
# This should be solved by an upsert, but it seems ecto
# has problems accessing the constraint inside the jsonb.
{
:error
,
_
}
->
Object
.
get_cached_by_ap_id
(
context
)
end
end
@doc
"""
Enqueues an activity for federation if it's local
"""
...
...
@@ -67,13 +79,17 @@ def maybe_federate(_), do: :ok
also adds it to an included object
"""
def
lazy_put_activity_defaults
(
map
)
do
%{
data:
%{
"id"
=>
context
},
id:
context_id
}
=
create_context
(
map
[
"context"
])
map
=
map
|>
Map
.
put_new_lazy
(
"id"
,
&
generate_activity_id
/
0
)
|>
Map
.
put_new_lazy
(
"published"
,
&
make_date
/
0
)
|>
Map
.
put_new
(
"context"
,
context
)
|>
Map
.
put_new
(
"context_id"
,
context_id
)
if
is_map
(
map
[
"object"
])
do
object
=
lazy_put_object_defaults
(
map
[
"object"
])
object
=
lazy_put_object_defaults
(
map
[
"object"
]
,
map
)
%{
map
|
"object"
=>
object
}
else
map
...
...
@@ -83,10 +99,12 @@ def lazy_put_activity_defaults(map) do
@doc
"""
Adds an id and published date if they aren't there.
"""
def
lazy_put_object_defaults
(
map
)
do
def
lazy_put_object_defaults
(
map
,
activity
\\
%{}
)
do
map
|>
Map
.
put_new_lazy
(
"id"
,
&
generate_object_id
/
0
)
|>
Map
.
put_new_lazy
(
"published"
,
&
make_date
/
0
)
|>
Map
.
put_new
(
"context"
,
activity
[
"context"
])
|>
Map
.
put_new
(
"context_id"
,
activity
[
"context_id"
])
end
@doc
"""
...
...
lib/pleroma/web/twitter_api/views/activity_view.ex
View file @
bc31bee7
...
...
@@ -32,13 +32,14 @@ defp collect_context_ids(activities) do
end
)
end
defp
get_context_id
(%{
data:
%{
"context"
=>
nil
}}),
do
:
nil
defp
get_context_id
(%{
data:
%{
"context"
=>
nil
}}
,
_
),
do
:
nil
defp
get_context_id
(%{
data:
%{
"context"
=>
context
}},
options
)
do
cond
do
id
=
options
[
:context_ids
][
context
]
->
id
true
->
TwitterAPI
.
context_to_conversation_id
(
context
)
end
end
defp
get_context_id
(
_
,
_
),
do
:
nil
def
render
(
"index.json"
,
opts
)
do
context_ids
=
collect_context_ids
(
opts
.
activities
)
...
...
test/web/activity_pub/activity_pub_test.exs
View file @
bc31bee7
...
...
@@ -40,12 +40,31 @@ test "inserts a given map into the activity database, giving it an id if it has
data
=
%{
"ok"
=>
true
,
"id"
=>
given_id
"id"
=>
given_id
,
"context"
=>
"blabla"
}
{
:ok
,
%
Activity
{}
=
activity
}
=
ActivityPub
.
insert
(
data
)
assert
activity
.
data
[
"ok"
]
==
data
[
"ok"
]
assert
activity
.
data
[
"id"
]
==
given_id
assert
activity
.
data
[
"context"
]
==
"blabla"
assert
activity
.
data
[
"context_id"
]
end
test
"adds a context when none is there"
do
data
=
%{
"id"
=>
"some_id"
,
"object"
=>
%{
"id"
=>
"object_id"
}
}
{
:ok
,
%
Activity
{}
=
activity
}
=
ActivityPub
.
insert
(
data
)
assert
is_binary
(
activity
.
data
[
"context"
])
assert
is_binary
(
activity
.
data
[
"object"
][
"context"
])
assert
activity
.
data
[
"context_id"
]
assert
activity
.
data
[
"object"
][
"context_id"
]
end
test
"adds an id to a given object if it lacks one and is a note and inserts it to the object database"
do
...
...
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