Merge branch 'develop' of git.pleroma.social:pleroma/pleroma into feature/jobs

This commit is contained in:
Egor Kislitsyn 2019-02-06 11:33:00 +07:00
commit 6f05f448f8
22 changed files with 319 additions and 62 deletions

1
.gitignore vendored
View File

@ -25,6 +25,7 @@ erl_crash.dump
# secrets files as long as you replace their contents by environment
# variables.
/config/*.secret.exs
/config/generated_config.exs
# Database setup file, some may forget to delete it
/config/setup_db.psql

View File

@ -52,6 +52,14 @@ defmodule Mix.Tasks.Pleroma.User do
- `--locked`/`--no-locked` - whether the user's account is locked
- `--moderator`/`--no-moderator` - whether the user is a moderator
- `--admin`/`--no-admin` - whether the user is an admin
## Add tags to a user.
mix pleroma.user tag NICKNAME TAGS
## Delete tags from a user.
mix pleroma.user untag NICKNAME TAGS
"""
def run(["new", nickname, email | rest]) do
{options, [], []} =
@ -249,6 +257,32 @@ def run(["set", nickname | rest]) do
end
end
def run(["tag", nickname | tags]) do
Common.start_pleroma()
with %User{} = user <- User.get_by_nickname(nickname) do
user = user |> User.tag(tags)
Mix.shell().info("Tags of #{user.nickname}: #{inspect(tags)}")
else
_ ->
Mix.shell().error("Could not change user tags for #{nickname}")
end
end
def run(["untag", nickname | tags]) do
Common.start_pleroma()
with %User{} = user <- User.get_by_nickname(nickname) do
user = user |> User.untag(tags)
Mix.shell().info("Tags of #{user.nickname}: #{inspect(tags)}")
else
_ ->
Mix.shell().error("Could not change user tags for #{nickname}")
end
end
def run(["invite"]) do
Common.start_pleroma()

View File

@ -59,6 +59,8 @@ defp generate_scrubber_signature(scrubbers) do
end)
end
def extract_first_external_url(_, nil), do: {:error, "No content"}
def extract_first_external_url(object, content) do
key = "URL|#{object.id}"

View File

@ -30,7 +30,8 @@ defmodule Pleroma.User.Info do
field(:topic, :string, default: nil)
field(:hub, :string, default: nil)
field(:salmon, :string, default: nil)
field(:hide_network, :boolean, default: false)
field(:hide_followers, :boolean, default: false)
field(:hide_followings, :boolean, default: false)
field(:pinned_activities, {:array, :string}, default: [])
# Found in the wild
@ -143,7 +144,8 @@ def profile_update(info, params) do
:no_rich_text,
:default_scope,
:banner,
:hide_network,
:hide_followings,
:hide_followers,
:background
])
end

View File

@ -521,7 +521,7 @@ defp restrict_actor(query, %{"actor_id" => actor_id}) do
defp restrict_actor(query, _), do: query
defp restrict_type(query, %{"type" => type}) when is_binary(type) do
restrict_type(query, %{"type" => [type]})
from(activity in query, where: fragment("?->>'type' = ?", activity.data, ^type))
end
defp restrict_type(query, %{"type" => type}) do

View File

@ -198,6 +198,14 @@ def relay(conn, _params) do
end
end
def whoami(%{assigns: %{user: %User{} = user}} = conn, _params) do
conn
|> put_resp_header("content-type", "application/activity+json")
|> json(UserView.render("user.json", %{user: user}))
end
def whoami(_conn, _params), do: {:error, :not_found}
def read_inbox(%{assigns: %{user: user}} = conn, %{"nickname" => nickname} = params) do
if nickname == user.nickname do
conn

View File

@ -0,0 +1,139 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.ActivityPub.MRF.TagPolicy do
alias Pleroma.User
@behaviour Pleroma.Web.ActivityPub.MRF
defp get_tags(%User{tags: tags}) when is_list(tags), do: tags
defp get_tags(_), do: []
defp process_tag(
"mrf_tag:media-force-nsfw",
%{"type" => "Create", "object" => %{"attachment" => child_attachment} = object} = message
)
when length(child_attachment) > 0 do
tags = (object["tag"] || []) ++ ["nsfw"]
object =
object
|> Map.put("tags", tags)
|> Map.put("sensitive", true)
message = Map.put(message, "object", object)
{:ok, message}
end
defp process_tag(
"mrf_tag:media-strip",
%{"type" => "Create", "object" => %{"attachment" => child_attachment} = object} = message
)
when length(child_attachment) > 0 do
object = Map.delete(object, "attachment")
message = Map.put(message, "object", object)
{:ok, message}
end
defp process_tag(
"mrf_tag:force-unlisted",
%{"type" => "Create", "to" => to, "cc" => cc, "actor" => actor} = message
) do
user = User.get_cached_by_ap_id(actor)
if Enum.member?(to, "https://www.w3.org/ns/activitystreams#Public") do
to =
List.delete(to, "https://www.w3.org/ns/activitystreams#Public") ++ [user.follower_address]
cc =
List.delete(cc, user.follower_address) ++ ["https://www.w3.org/ns/activitystreams#Public"]
object =
message["object"]
|> Map.put("to", to)
|> Map.put("cc", cc)
message =
message
|> Map.put("to", to)
|> Map.put("cc", cc)
|> Map.put("object", object)
{:ok, message}
else
{:ok, message}
end
end
defp process_tag(
"mrf_tag:sandbox",
%{"type" => "Create", "to" => to, "cc" => cc, "actor" => actor} = message
) do
user = User.get_cached_by_ap_id(actor)
if Enum.member?(to, "https://www.w3.org/ns/activitystreams#Public") or
Enum.member?(cc, "https://www.w3.org/ns/activitystreams#Public") do
to =
List.delete(to, "https://www.w3.org/ns/activitystreams#Public") ++ [user.follower_address]
cc = List.delete(cc, "https://www.w3.org/ns/activitystreams#Public")
object =
message["object"]
|> Map.put("to", to)
|> Map.put("cc", cc)
message =
message
|> Map.put("to", to)
|> Map.put("cc", cc)
|> Map.put("object", object)
{:ok, message}
else
{:ok, message}
end
end
defp process_tag(
"mrf_tag:disable-remote-subscription",
%{"type" => "Follow", "actor" => actor} = message
) do
user = User.get_cached_by_ap_id(actor)
if user.local == true do
{:ok, message}
else
{:reject, nil}
end
end
defp process_tag("mrf_tag:disable-any-subscription", %{"type" => "Follow"}), do: {:reject, nil}
defp process_tag(_, message), do: {:ok, message}
def filter_message(actor, message) do
User.get_cached_by_ap_id(actor)
|> get_tags()
|> Enum.reduce({:ok, message}, fn
tag, {:ok, message} ->
process_tag(tag, message)
_, error ->
error
end)
end
@impl true
def filter(%{"object" => target_actor, "type" => "Follow"} = message),
do: filter_message(target_actor, message)
@impl true
def filter(%{"actor" => actor, "type" => "Create"} = message),
do: filter_message(actor, message)
@impl true
def filter(message), do: {:ok, message}
end

View File

@ -313,6 +313,8 @@ def fix_tag(%{"tag" => %{"type" => "Hashtag", "name" => hashtag} = tag} = object
|> Map.put("tag", combined)
end
def fix_tag(%{"tag" => %{} = tag} = object), do: Map.put(object, "tag", [tag])
def fix_tag(object), do: object
# content map usually only has one language so this will do for now.

View File

@ -86,7 +86,7 @@ def render("following.json", %{user: user, page: page}) do
query = from(user in query, select: [:ap_id])
following = Repo.all(query)
collection(following, "#{user.ap_id}/following", page, !user.info.hide_network)
collection(following, "#{user.ap_id}/following", page, !user.info.hide_followings)
|> Map.merge(Utils.make_json_ld_header())
end
@ -99,7 +99,7 @@ def render("following.json", %{user: user}) do
"id" => "#{user.ap_id}/following",
"type" => "OrderedCollection",
"totalItems" => length(following),
"first" => collection(following, "#{user.ap_id}/following", 1, !user.info.hide_network)
"first" => collection(following, "#{user.ap_id}/following", 1, !user.info.hide_followings)
}
|> Map.merge(Utils.make_json_ld_header())
end
@ -109,7 +109,7 @@ def render("followers.json", %{user: user, page: page}) do
query = from(user in query, select: [:ap_id])
followers = Repo.all(query)
collection(followers, "#{user.ap_id}/followers", page, !user.info.hide_network)
collection(followers, "#{user.ap_id}/followers", page, !user.info.hide_followers)
|> Map.merge(Utils.make_json_ld_header())
end
@ -122,7 +122,7 @@ def render("followers.json", %{user: user}) do
"id" => "#{user.ap_id}/followers",
"type" => "OrderedCollection",
"totalItems" => length(followers),
"first" => collection(followers, "#{user.ap_id}/followers", 1, !user.info.hide_network)
"first" => collection(followers, "#{user.ap_id}/followers", 1, !user.info.hide_followers)
}
|> Map.merge(Utils.make_json_ld_header())
end

View File

@ -605,7 +605,7 @@ def followers(%{assigns: %{user: for_user}} = conn, %{"id" => id}) do
followers =
cond do
for_user && user.id == for_user.id -> followers
user.info.hide_network -> []
user.info.hide_followers -> []
true -> followers
end
@ -621,7 +621,7 @@ def following(%{assigns: %{user: for_user}} = conn, %{"id" => id}) do
followers =
cond do
for_user && user.id == for_user.id -> followers
user.info.hide_network -> []
user.info.hide_followings -> []
true -> followers
end

View File

@ -192,8 +192,12 @@ def render("card.json", %{rich_media: rich_media, page_url: page_url}) do
page_url = page_url_data |> to_string
image_url =
if rich_media[:image] != nil do
URI.merge(page_url_data, URI.parse(rich_media[:image]))
|> to_string
else
nil
end
site_name = rich_media[:site_name] || page_url_data.host

View File

@ -54,22 +54,12 @@ defp check_parsed_data(data) do
{:error, "Found metadata was invalid or incomplete: #{inspect(data)}"}
end
defp string_is_valid_unicode(data) when is_binary(data) do
data
|> :unicode.characters_to_binary()
|> clean_string()
end
defp string_is_valid_unicode(data), do: {:ok, data}
defp clean_string({:error, _, _}), do: {:error, "Invalid data"}
defp clean_string(data), do: {:ok, data}
defp clean_parsed_data(data) do
data
|> Enum.reject(fn {_, val} ->
case string_is_valid_unicode(val) do
{:ok, _} -> false
|> Enum.reject(fn {key, val} ->
with {:ok, _} <- Jason.encode(%{key => val}) do
false
else
_ -> true
end
end)

View File

@ -454,6 +454,7 @@ defmodule Pleroma.Web.Router do
scope "/", Pleroma.Web.ActivityPub do
pipe_through([:activitypub_client])
get("/api/ap/whoami", ActivityPubController, :whoami)
get("/users/:nickname/inbox", ActivityPubController, :read_inbox)
post("/users/:nickname/outbox", ActivityPubController, :update_outbox)
end

View File

@ -503,7 +503,7 @@ def followers(%{assigns: %{user: for_user}} = conn, params) do
followers =
cond do
for_user && user.id == for_user.id -> followers
user.info.hide_network -> []
user.info.hide_followers -> []
true -> followers
end
@ -523,7 +523,7 @@ def friends(%{assigns: %{user: for_user}} = conn, params) do
friends =
cond do
for_user && user.id == for_user.id -> friends
user.info.hide_network -> []
user.info.hide_followings -> []
true -> friends
end
@ -618,7 +618,7 @@ def raw_empty_array(conn, _params) do
defp build_info_cng(user, params) do
info_params =
["no_rich_text", "locked", "hide_network"]
["no_rich_text", "locked", "hide_followers", "hide_followings"]
|> Enum.reduce(%{}, fn key, res ->
if value = params[key] do
Map.put(res, key, value == "true")

View File

@ -108,7 +108,8 @@ defp do_render("user.json", %{user: user = %User{}} = assigns) do
"locked" => user.info.locked,
"default_scope" => user.info.default_scope,
"no_rich_text" => user.info.no_rich_text,
"hide_network" => user.info.hide_network,
"hide_followers" => user.info.hide_followers,
"hide_followings" => user.info.hide_followings,
"fields" => fields,
# Pleroma extension

View File

@ -0,0 +1,12 @@
defmodule Pleroma.Repo.Migrations.SplitHideNetwork do
use Ecto.Migration
def up do
execute("UPDATE users SET info = jsonb_set(info, '{hide_network}'::text[], 'false'::jsonb) WHERE NOT(info::jsonb ? 'hide_network')")
execute("UPDATE users SET info = jsonb_set(info, '{hide_followings}'::text[], info->'hide_network')")
execute("UPDATE users SET info = jsonb_set(info, '{hide_followers}'::text[], info->'hide_network')")
end
def down do
end
end

View File

@ -0,0 +1,30 @@
defmodule Pleroma.Repo.Migrations.AddCorrectDMIndex do
use Ecto.Migration
@disable_ddl_transaction true
def up do
drop_if_exists(
index(:activities, ["activity_visibility(actor, recipients, data)"],
name: :activities_visibility_index
)
)
create(
index(:activities, ["activity_visibility(actor, recipients, data)", "id DESC NULLS LAST"],
name: :activities_visibility_index,
concurrently: true,
where: "data->>'type' = 'Create'"
)
)
end
def down do
drop(
index(:activities, ["activity_visibility(actor, recipients, data)", "id DESC"],
name: :activities_visibility_index,
concurrently: true,
where: "data->>'type' = 'Create'"
)
)
end
end

View File

@ -386,9 +386,9 @@ test "it returns the followers in a collection", %{conn: conn} do
assert result["first"]["orderedItems"] == [user.ap_id]
end
test "it returns returns empty if the user has 'hide_network' set", %{conn: conn} do
test "it returns returns empty if the user has 'hide_followers' set", %{conn: conn} do
user = insert(:user)
user_two = insert(:user, %{info: %{hide_network: true}})
user_two = insert(:user, %{info: %{hide_followers: true}})
User.follow(user, user_two)
result =
@ -441,8 +441,8 @@ test "it returns the following in a collection", %{conn: conn} do
assert result["first"]["orderedItems"] == [user_two.ap_id]
end
test "it returns returns empty if the user has 'hide_network' set", %{conn: conn} do
user = insert(:user, %{info: %{hide_network: true}})
test "it returns returns empty if the user has 'hide_followings' set", %{conn: conn} do
user = insert(:user, %{info: %{hide_followings: true}})
user_two = insert(:user)
User.follow(user, user_two)

View File

@ -1101,9 +1101,9 @@ test "getting followers", %{conn: conn} do
assert id == to_string(user.id)
end
test "getting followers, hide_network", %{conn: conn} do
test "getting followers, hide_followers", %{conn: conn} do
user = insert(:user)
other_user = insert(:user, %{info: %{hide_network: true}})
other_user = insert(:user, %{info: %{hide_followers: true}})
{:ok, _user} = User.follow(user, other_user)
conn =
@ -1113,9 +1113,9 @@ test "getting followers, hide_network", %{conn: conn} do
assert [] == json_response(conn, 200)
end
test "getting followers, hide_network, same user requesting", %{conn: conn} do
test "getting followers, hide_followers, same user requesting", %{conn: conn} do
user = insert(:user)
other_user = insert(:user, %{info: %{hide_network: true}})
other_user = insert(:user, %{info: %{hide_followers: true}})
{:ok, _user} = User.follow(user, other_user)
conn =
@ -1139,8 +1139,8 @@ test "getting following", %{conn: conn} do
assert id == to_string(other_user.id)
end
test "getting following, hide_network", %{conn: conn} do
user = insert(:user, %{info: %{hide_network: true}})
test "getting following, hide_followings", %{conn: conn} do
user = insert(:user, %{info: %{hide_followings: true}})
other_user = insert(:user)
{:ok, user} = User.follow(user, other_user)
@ -1151,8 +1151,8 @@ test "getting following, hide_network", %{conn: conn} do
assert [] == json_response(conn, 200)
end
test "getting following, hide_network, same user requesting", %{conn: conn} do
user = insert(:user, %{info: %{hide_network: true}})
test "getting following, hide_followings, same user requesting", %{conn: conn} do
user = insert(:user, %{info: %{hide_followings: true}})
other_user = insert(:user)
{:ok, user} = User.follow(user, other_user)

View File

@ -1132,8 +1132,8 @@ test "it returns a given user's followers with user_id", %{conn: conn} do
)
end
test "it returns empty for a hidden network", %{conn: conn} do
user = insert(:user, %{info: %{hide_network: true}})
test "it returns empty when hide_followers is set to true", %{conn: conn} do
user = insert(:user, %{info: %{hide_followers: true}})
follower_one = insert(:user)
follower_two = insert(:user)
not_follower = insert(:user)
@ -1150,10 +1150,11 @@ test "it returns empty for a hidden network", %{conn: conn} do
assert [] == response
end
test "it returns the followers for a hidden network if requested by the user themselves", %{
test "it returns the followers when hide_followers is set to true if requested by the user themselves",
%{
conn: conn
} do
user = insert(:user, %{info: %{hide_network: true}})
user = insert(:user, %{info: %{hide_followers: true}})
follower_one = insert(:user)
follower_two = insert(:user)
_not_follower = insert(:user)
@ -1256,8 +1257,8 @@ test "it returns a given user's friends with user_id", %{conn: conn} do
)
end
test "it returns empty for a hidden network", %{conn: conn} do
user = insert(:user, %{info: %{hide_network: true}})
test "it returns empty when hide_followings is set to true", %{conn: conn} do
user = insert(:user, %{info: %{hide_followings: true}})
followed_one = insert(:user)
followed_two = insert(:user)
not_followed = insert(:user)
@ -1273,10 +1274,11 @@ test "it returns empty for a hidden network", %{conn: conn} do
assert [] == json_response(conn, 200)
end
test "it returns friends for a hidden network if the user themselves request it", %{
test "it returns friends when hide_followings is set to true if the user themselves request it",
%{
conn: conn
} do
user = insert(:user, %{info: %{hide_network: true}})
user = insert(:user, %{info: %{hide_followings: true}})
followed_one = insert(:user)
followed_two = insert(:user)
_not_followed = insert(:user)
@ -1364,27 +1366,51 @@ test "it updates a user's profile", %{conn: conn} do
assert json_response(conn, 200) == UserView.render("user.json", %{user: user, for: user})
end
test "it sets and un-sets hide_network", %{conn: conn} do
test "it sets and un-sets hide_followings", %{conn: conn} do
user = insert(:user)
conn
|> assign(:user, user)
|> post("/api/account/update_profile.json", %{
"hide_network" => "true"
"hide_followings" => "true"
})
user = Repo.get!(User, user.id)
assert user.info.hide_network == true
assert user.info.hide_followings == true
conn =
conn
|> assign(:user, user)
|> post("/api/account/update_profile.json", %{
"hide_network" => "false"
"hide_followings" => "false"
})
user = Repo.get!(User, user.id)
assert user.info.hide_network == false
assert user.info.hide_followings == false
assert json_response(conn, 200) == UserView.render("user.json", %{user: user, for: user})
end
test "it sets and un-sets hide_followers", %{conn: conn} do
user = insert(:user)
conn
|> assign(:user, user)
|> post("/api/account/update_profile.json", %{
"hide_followers" => "true"
})
user = Repo.get!(User, user.id)
assert user.info.hide_followers == true
conn =
conn
|> assign(:user, user)
|> post("/api/account/update_profile.json", %{
"hide_followers" => "false"
})
user = Repo.get!(User, user.id)
assert user.info.hide_followers == false
assert json_response(conn, 200) == UserView.render("user.json", %{user: user, for: user})
end

View File

@ -36,6 +36,7 @@ test "it returns HTTP 200", %{conn: conn} do
describe "GET /api/statusnet/config.json" do
test "it returns the managed config", %{conn: conn} do
Pleroma.Config.put([:instance, :managed_config], false)
Pleroma.Config.put([:fe], theme: "rei-ayanami-towel")
response =
conn

View File

@ -100,7 +100,8 @@ test "A user" do
"locked" => false,
"default_scope" => "public",
"no_rich_text" => false,
"hide_network" => false,
"hide_followings" => false,
"hide_followers" => false,
"fields" => [],
"pleroma" => %{
"confirmation_pending" => false,
@ -147,7 +148,8 @@ test "A user for a given other follower", %{user: user} do
"locked" => false,
"default_scope" => "public",
"no_rich_text" => false,
"hide_network" => false,
"hide_followings" => false,
"hide_followers" => false,
"fields" => [],
"pleroma" => %{
"confirmation_pending" => false,
@ -195,7 +197,8 @@ test "A user that follows you", %{user: user} do
"locked" => false,
"default_scope" => "public",
"no_rich_text" => false,
"hide_network" => false,
"hide_followings" => false,
"hide_followers" => false,
"fields" => [],
"pleroma" => %{
"confirmation_pending" => false,
@ -257,7 +260,8 @@ test "A blocked user for the blocker" do
"locked" => false,
"default_scope" => "public",
"no_rich_text" => false,
"hide_network" => false,
"hide_followings" => false,
"hide_followers" => false,
"fields" => [],
"pleroma" => %{
"confirmation_pending" => false,