Merge remote-tracking branch 'origin/develop' into quote-post

This commit is contained in:
Alex Gleason 2022-01-25 11:57:04 -06:00
commit 93a340668f
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7
29 changed files with 562 additions and 43 deletions

View file

@ -26,6 +26,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Ability to log slow Ecto queries by configuring `:pleroma, :telemetry, :slow_queries_logging`
- Added Phoenix LiveDashboard at `/phoenix/live_dashboard`
- Added `/manifest.json` for progressive web apps.
- MastoAPI: Support for `birthday` and `show_birthday` field in `/api/v1/accounts/update_credentials`.
- Configuration: Add `birthday_required` and `birthday_min_age` settings to provide a way to require users to enter their birth date.
- PleromaAPI: Add `GET /api/v1/pleroma/birthdays` API endpoint
### Fixed
- Subscription(Bell) Notifications: Don't create from Pipeline Ingested replies

View file

@ -257,7 +257,9 @@
password_reset_token_validity: 60 * 60 * 24,
profile_directory: true,
privileged_staff: false,
max_endorsed_users: 20
max_endorsed_users: 20,
birthday_required: false,
birthday_min_age: 0
config :pleroma, :welcome,
direct_message: [

View file

@ -957,6 +957,17 @@
type: :boolean,
description:
"Let moderators access sensitive data (e.g. updating user credentials, get password reset token, delete users, index and read private statuses and chats)"
},
%{
key: :birthday_required,
type: :boolean,
description: "Require users to enter their birthday."
},
%{
key: :birthday_min_age,
type: :integer,
description:
"Minimum required age for users to create account. Only used if birthday is required."
}
]
},

View file

@ -154,6 +154,8 @@ defmodule Pleroma.User do
field(:pinned_objects, :map, default: %{})
field(:is_suggested, :boolean, default: false)
field(:last_status_at, :naive_datetime)
field(:birthday, :date)
field(:show_birthday, :boolean, default: false)
embeds_one(
:notification_settings,
@ -470,7 +472,9 @@ def remote_user_changeset(struct \\ %User{local: false}, params) do
:actor_type,
:also_known_as,
:accepts_chat_messages,
:pinned_objects
:pinned_objects,
:birthday,
:show_birthday
]
)
|> cast(params, [:name], empty_values: [])
@ -531,9 +535,12 @@ def update_changeset(struct, params \\ %{}) do
:is_discoverable,
:actor_type,
:accepts_chat_messages,
:disclose_client
:disclose_client,
:birthday,
:show_birthday
]
)
|> validate_min_age()
|> unique_constraint(:nickname)
|> validate_format(:nickname, local_nickname_regex())
|> validate_length(:bio, max: bio_limit)
@ -738,7 +745,8 @@ def register_changeset(struct, params \\ %{}, opts \\ []) do
:password_confirmation,
:emoji,
:accepts_chat_messages,
:registration_reason
:registration_reason,
:birthday
])
|> validate_required([:name, :nickname, :password, :password_confirmation])
|> validate_confirmation(:password)
@ -760,6 +768,8 @@ def register_changeset(struct, params \\ %{}, opts \\ []) do
|> validate_length(:name, min: 1, max: name_limit)
|> validate_length(:registration_reason, max: reason_limit)
|> maybe_validate_required_email(opts[:external])
|> maybe_validate_required_birthday
|> validate_min_age()
|> put_password_hash
|> put_ap_id()
|> unique_constraint(:ap_id)
@ -776,6 +786,26 @@ def maybe_validate_required_email(changeset, _) do
end
end
defp maybe_validate_required_birthday(changeset) do
if Config.get([:instance, :birthday_required]) do
validate_required(changeset, [:birthday])
else
changeset
end
end
defp validate_min_age(changeset) do
changeset
|> validate_change(:birthday, fn :birthday, birthday ->
valid? =
Date.utc_today()
|> Date.diff(birthday) >=
Config.get([:instance, :birthday_min_age])
if valid?, do: [], else: [birthday: "Invalid age"]
end)
end
defp put_ap_id(changeset) do
ap_id = ap_id(%User{nickname: get_field(changeset, :nickname)})
put_change(changeset, :ap_id, ap_id)
@ -2560,4 +2590,13 @@ def update_last_status_at(user) do
_ -> {:error, user}
end
end
def get_friends_birthdays_query(%User{} = user, day, month) do
User.Query.build(%{
friends: user,
deactivated: false,
birthday_day: day,
birthday_month: month
})
end
end

View file

@ -59,7 +59,9 @@ defmodule Pleroma.User.Query do
order_by: term(),
select: term(),
limit: pos_integer(),
actor_types: [String.t()]
actor_types: [String.t()],
birthday_day: pos_integer(),
birthday_month: pos_integer()
}
| map()
@ -230,6 +232,20 @@ defp compose_query({:internal, false}, query) do
|> where([u], not like(u.nickname, "internal.%"))
end
defp compose_query({:birthday_day, day}, query) do
query
|> where([u], u.show_birthday == true)
|> where([u], not is_nil(u.birthday))
|> where([u], fragment("date_part('day', ?)", u.birthday) == ^day)
end
defp compose_query({:birthday_month, month}, query) do
query
|> where([u], u.show_birthday == true)
|> where([u], not is_nil(u.birthday))
|> where([u], fragment("date_part('month', ?)", u.birthday) == ^month)
end
defp compose_query(_unsupported_param, query), do: query
defp location_query(query, local) do

View file

@ -1501,6 +1501,18 @@ defp object_to_user_data(data) do
nil
end
birthday =
if is_binary(data["vcard:bday"]) do
case Date.from_iso8601(data["vcard:bday"]) do
{:ok, date} -> date
{:error, _} -> nil
end
else
nil
end
show_birthday = !!birthday
user_data = %{
ap_id: data["id"],
uri: get_actor_url(data["url"]),
@ -1523,7 +1535,9 @@ defp object_to_user_data(data) do
inbox: data["inbox"],
shared_inbox: shared_inbox,
accepts_chat_messages: accepts_chat_messages,
pinned_objects: pinned_objects
pinned_objects: pinned_objects,
birthday: birthday,
show_birthday: show_birthday
}
# nickname can be nil because of virtual actors

View file

@ -4,6 +4,7 @@
defmodule Pleroma.Web.ActivityPub.MRF.ForceMentionsInContent do
alias Pleroma.Formatter
alias Pleroma.Object
alias Pleroma.User
@behaviour Pleroma.Web.ActivityPub.MRF.Policy
@ -34,31 +35,50 @@ defp extract_mention_uris_from_content(content) do
do_extract(tree, [])
end
defp get_replied_to_user(%{"inReplyTo" => in_reply_to}) do
case Object.normalize(in_reply_to, fetch: false) do
%Object{data: %{"actor" => actor}} -> User.get_cached_by_ap_id(actor)
_ -> nil
end
end
defp get_replied_to_user(_object), do: nil
# Ensure the replied-to user is sorted to the left
defp sort_replied_user([%User{id: user_id} | _] = users, %User{id: user_id}), do: users
defp sort_replied_user(users, %User{id: user_id} = user) do
if Enum.find(users, fn u -> u.id == user_id end) do
users = Enum.reject(users, fn u -> u.id == user_id end)
[user | users]
else
users
end
end
defp sort_replied_user(users, _), do: users
@impl true
def filter(%{"type" => "Create", "object" => %{"type" => "Note", "tag" => tag}} = object) do
def filter(%{"type" => "Create", "object" => %{"type" => "Note", "to" => to}} = object)
when is_list(to) do
# image-only posts from pleroma apparently reach this MRF without the content field
content = object["object"]["content"] || ""
# Get the replied-to user for sorting
replied_to_user = get_replied_to_user(object["object"])
mention_users =
tag
|> Enum.filter(fn tag -> tag["type"] == "Mention" end)
|> Enum.map(& &1["href"])
to
|> Enum.map(&User.get_cached_by_ap_id/1)
|> Enum.reject(&is_nil/1)
|> Enum.map(fn ap_id_or_uri ->
case User.get_or_fetch_by_ap_id(ap_id_or_uri) do
{:ok, user} -> {ap_id_or_uri, user}
_ -> {ap_id_or_uri, User.get_by_uri(ap_id_or_uri)}
end
end)
|> Enum.reject(fn {_, user} -> user == nil end)
|> Enum.into(%{})
|> sort_replied_user(replied_to_user)
explicitly_mentioned_uris = extract_mention_uris_from_content(content)
added_mentions =
Enum.reduce(mention_users, "", fn {uri, user}, acc ->
Enum.reduce(mention_users, "", fn %User{ap_id: uri} = user, acc ->
unless uri in explicitly_mentioned_uris do
acc <> Formatter.mention_from_user(user)
acc <> Formatter.mention_from_user(user, %{mentions_format: :compact}) <> " "
else
acc
end
@ -66,7 +86,7 @@ def filter(%{"type" => "Create", "object" => %{"type" => "Note", "tag" => tag}}
content =
if added_mentions != "",
do: added_mentions <> " " <> content,
do: "<span class=\"recipients-inline\">#{added_mentions}</span>" <> content,
else: content
{:ok, put_in(object["object"]["content"], content)}

View file

@ -92,6 +92,11 @@ def render("user.json", %{user: user}) do
%{}
end
birthday =
if user.show_birthday,
do: user.birthday,
else: nil
%{
"id" => user.ap_id,
"type" => user.actor_type,
@ -116,7 +121,8 @@ def render("user.json", %{user: user}) do
# Note: key name is indeed "discoverable" (not an error)
"discoverable" => user.is_discoverable,
"capabilities" => capabilities,
"alsoKnownAs" => user.also_known_as
"alsoKnownAs" => user.also_known_as,
"vcard:bday" => birthday
}
|> Map.merge(maybe_make_image(&User.avatar_url/2, "icon", user))
|> Map.merge(maybe_make_image(&User.banner_url/2, "image", user))

View file

@ -543,6 +543,12 @@ defp create_request do
type: :string,
nullable: true,
description: "Invite token required when the registrations aren't public"
},
birthday: %Schema{
type: :string,
nullable: true,
description: "User's birthday",
format: :date
}
},
example: %{
@ -720,7 +726,18 @@ defp update_credentials_request do
description:
"Discovery (listing, indexing) of this account by external services (search bots etc.) is allowed."
},
actor_type: ActorType
actor_type: ActorType,
birthday: %Schema{
type: :string,
nullable: true,
description: "User's birthday",
format: :date
},
show_birthday: %Schema{
allOf: [BooleanLike],
nullable: true,
description: "User's birthday will be visible"
}
},
example: %{
bot: false,
@ -740,7 +757,9 @@ defp update_credentials_request do
allow_following_move: false,
also_known_as: ["https://foo.bar/users/foo"],
discoverable: false,
actor_type: "Person"
actor_type: "Person",
show_birthday: false,
birthday: "2001-02-12"
}
}
end

View file

@ -4,6 +4,7 @@
defmodule Pleroma.Web.ApiSpec.PleromaAccountOperation do
alias OpenApiSpex.Operation
alias OpenApiSpex.Schema
alias Pleroma.Web.ApiSpec.AccountOperation
alias Pleroma.Web.ApiSpec.Schemas.AccountRelationship
alias Pleroma.Web.ApiSpec.Schemas.ApiError
@ -112,6 +113,34 @@ def unsubscribe_operation do
}
end
def birthdays_operation do
%Operation{
tags: ["Retrieve account information"],
summary: "Birthday reminders",
description: "Birthday reminders about users you follow.",
operationId: "PleromaAPI.AccountController.birthdays",
parameters: [
Operation.parameter(
:day,
:query,
%Schema{type: :integer},
"Day of users' birthdays"
),
Operation.parameter(
:month,
:query,
%Schema{type: :integer},
"Month of users' birthdays"
)
],
security: [%{"oAuth" => ["read:accounts"]}],
responses: %{
200 =>
Operation.response("Accounts", "application/json", AccountOperation.array_of_accounts())
}
}
end
defp id_param do
Operation.parameter(:id, :path, FlakeID, "Account ID",
example: "9umDrYheeY451cQnEe",

View file

@ -47,12 +47,14 @@ defmodule Pleroma.Web.ApiSpec.Schemas.Account do
description: "whether the user allows automatically follow moved following accounts"
},
background_image: %Schema{type: :string, nullable: true, format: :uri},
birthday: %Schema{type: :string, nullable: true, format: :date},
chat_token: %Schema{type: :string},
is_confirmed: %Schema{
type: :boolean,
description:
"whether the user account is waiting on email confirmation to be activated"
},
show_birthday: %Schema{type: :boolean, nullable: true},
hide_favorites: %Schema{type: :boolean},
hide_followers_count: %Schema{
type: :boolean,
@ -202,7 +204,8 @@ defmodule Pleroma.Web.ApiSpec.Schemas.Account do
},
"settings_store" => %{
"pleroma-fe" => %{}
}
},
"birthday" => "2001-02-12"
},
"source" => %{
"fields" => [],

View file

@ -191,7 +191,8 @@ def update_credentials(%{assigns: %{user: user}, body_params: params} = conn, _p
:skip_thread_containment,
:allow_following_move,
:also_known_as,
:accepts_chat_messages
:accepts_chat_messages,
:show_birthday
]
|> Enum.reduce(%{}, fn key, acc ->
Maps.put_if_present(acc, key, params[key], &{:ok, Params.truthy_param?(&1)})
@ -219,6 +220,7 @@ def update_credentials(%{assigns: %{user: user}, body_params: params} = conn, _p
|> Maps.put_if_present(:is_locked, params[:locked])
# Note: param name is indeed :discoverable (not an error)
|> Maps.put_if_present(:is_discoverable, params[:discoverable])
|> Maps.put_if_present(:birthday, params[:birthday])
# What happens here:
#

View file

@ -311,6 +311,7 @@ defp do_render("show.json", %{user: user} = opts) do
|> maybe_put_unread_conversation_count(user, opts[:for])
|> maybe_put_unread_notification_count(user, opts[:for])
|> maybe_put_email_address(user, opts[:for])
|> maybe_show_birthday(user, opts[:for])
end
defp username_from_nickname(string) when is_binary(string) do
@ -344,6 +345,7 @@ defp maybe_put_settings(
|> Kernel.put_in([:source, :privacy], user.default_scope)
|> Kernel.put_in([:source, :pleroma, :show_role], user.show_role)
|> Kernel.put_in([:source, :pleroma, :no_rich_text], user.no_rich_text)
|> Kernel.put_in([:source, :pleroma, :show_birthday], user.show_birthday)
end
defp maybe_put_settings(data, _, _, _), do: data
@ -432,6 +434,20 @@ defp maybe_put_email_address(data, %User{id: user_id}, %User{id: user_id} = user
defp maybe_put_email_address(data, _, _), do: data
defp maybe_show_birthday(data, %User{id: user_id} = user, %User{id: user_id}) do
data
|> Kernel.put_in([:pleroma, :birthday], user.birthday)
end
defp maybe_show_birthday(data, %User{show_birthday: true} = user, _) do
data
|> Kernel.put_in([:pleroma, :birthday], user.birthday)
end
defp maybe_show_birthday(data, _, _) do
data
end
defp image_url(%{"url" => [%{"href" => href} | _]}), do: href
defp image_url(_), do: nil
end

View file

@ -46,7 +46,9 @@ def render("show.json", _) do
federation: federation(),
fields_limits: fields_limits(),
post_formats: Config.get([:instance, :allowed_post_formats]),
privileged_staff: Config.get([:instance, :privileged_staff])
privileged_staff: Config.get([:instance, :privileged_staff]),
birthday_required: Config.get([:instance, :birthday_required]),
birthday_min_age: Config.get([:instance, :birthday_min_age])
},
stats: %{mau: Pleroma.User.active_user_count()},
vapid_public_key: Keyword.get(Pleroma.Web.Push.vapid_config(), :public_key)

View file

@ -51,6 +51,11 @@ defmodule Pleroma.Web.PleromaAPI.AccountController do
when action == :endorsements
)
plug(
OAuthScopesPlug,
%{scopes: ["read:accounts"]} when action == :birthdays
)
plug(RateLimiter, [name: :account_confirmation_resend] when action == :confirmation_resend)
plug(
@ -137,4 +142,18 @@ def unsubscribe(%{assigns: %{user: user, account: subscription_target}} = conn,
{:error, message} -> json_response(conn, :forbidden, %{error: message})
end
end
@doc "GET /api/v1/pleroma/birthdays"
def birthdays(%{assigns: %{user: %User{} = user}} = conn, %{day: day, month: month} = _params) do
birthdays =
User.get_friends_birthdays_query(user, day, month)
|> Pleroma.Repo.all()
conn
|> render("index.json",
for: user,
users: birthdays,
as: :user
)
end
end

View file

@ -448,6 +448,8 @@ defmodule Pleroma.Web.Router do
post("/accounts/:id/subscribe", AccountController, :subscribe)
post("/accounts/:id/unsubscribe", AccountController, :unsubscribe)
get("/birthdays", AccountController, :birthdays)
end
post("/accounts/confirmation_resend", AccountController, :confirmation_resend)

View file

@ -20,6 +20,7 @@ def register_user(params, opts \\ []) do
|> Map.put(:name, Map.get(params, :fullname, params[:username]))
|> Map.put(:password_confirmation, params[:password])
|> Map.put(:registration_reason, params[:reason])
|> Map.put(:birthday, params[:birthday])
if Pleroma.Config.get([:instance, :registrations_open]) do
create_user(params, opts)

View file

@ -0,0 +1,12 @@
defmodule Pleroma.Repo.Migrations.AddBirthdayToUsers do
use Ecto.Migration
def change do
alter table(:users) do
add_if_not_exists(:birthday, :date)
add_if_not_exists(:show_birthday, :boolean, default: false, null: false)
end
create_if_not_exists(index(:users, [:show_birthday]))
end
end

View file

@ -0,0 +1,11 @@
defmodule Pleroma.Repo.Migrations.AddBirthdayMonthDayIndexToUsers do
use Ecto.Migration
def change do
create(
index(:users, ["date_part('month', birthday)", "date_part('day', birthday)"],
name: :users_birthday_month_day_index
)
)
end
end

View file

@ -56,7 +56,12 @@ defmodule Pleroma.HTML.Scrubber.Default do
Meta.allow_tag_with_these_attributes(:u, [])
Meta.allow_tag_with_these_attributes(:ul, [])
Meta.allow_tag_with_this_attribute_values(:span, "class", ["h-card", "quote-inline"])
Meta.allow_tag_with_this_attribute_values(:span, "class", [
"h-card",
"quote-inline",
"recipients-inline"
])
Meta.allow_tag_with_these_attributes(:span, [])
Meta.allow_tag_with_this_attribute_values(:code, "class", ["inline"])

View file

@ -36,7 +36,8 @@
"alsoKnownAs": {
"@id": "as:alsoKnownAs",
"@type": "@id"
}
},
"vcard": "http://www.w3.org/2006/vcard/ns#"
}
]
}

View file

@ -0,0 +1 @@
{"@context":["https://www.w3.org/ns/activitystreams","https://w3id.org/security/v1",{"manuallyApprovesFollowers":"as:manuallyApprovesFollowers","sensitive":"as:sensitive","Hashtag":"as:Hashtag","quoteUrl":"as:quoteUrl","toot":"http://joinmastodon.org/ns#","Emoji":"toot:Emoji","featured":"toot:featured","discoverable":"toot:discoverable","schema":"http://schema.org#","PropertyValue":"schema:PropertyValue","value":"schema:value","misskey":"https://misskey.io/ns#","_misskey_content":"misskey:_misskey_content","_misskey_quote":"misskey:_misskey_quote","_misskey_reaction":"misskey:_misskey_reaction","_misskey_votes":"misskey:_misskey_votes","_misskey_talk":"misskey:_misskey_talk","isCat":"misskey:isCat","vcard":"http://www.w3.org/2006/vcard/ns#"}],"type":"Person","id":"https://misskey.io/users/8dhi2ne167","inbox":"https://misskey.io/users/8dhi2ne167/inbox","outbox":"https://misskey.io/users/8dhi2ne167/outbox","followers":"https://misskey.io/users/8dhi2ne167/followers","following":"https://misskey.io/users/8dhi2ne167/following","sharedInbox":"https://misskey.io/inbox","endpoints":{"sharedInbox":"https://misskey.io/inbox"},"url":"https://misskey.io/@mkljczk","preferredUsername":"mkljczk","name":null,"summary":null,"icon":null,"image":null,"tag":[],"manuallyApprovesFollowers":false,"discoverable":true,"publicKey":{"id":"https://misskey.io/users/8dhi2ne167#main-key","type":"Key","owner":"https://misskey.io/users/8dhi2ne167","publicKeyPem":"-----BEGIN PUBLIC KEY-----\nMIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEA7CI3Ol1M0TDdLL+E8Uhd\nJ8l/RTEtxl39MKxsqSCZr9itf/EBn4dGTifK9LN3XZD2fjmX4hdwaxndp2HYVDqn\ndc6O57u8dHxFv9wTwXQrLzEonOzbrBec6WB42ZpkFHi4XEyqg8iYGu5Yy7ttXJ21\nOfWqi+eytttcTErKuu4z8MX1L1IlmpfSmH1trMyDZLFMRqVJ0416/qI0K3l3cmIf\n8cuWbJ57UxVbYxp9242der/3vrNIU24rAouYQYe1atUgFPKil3w8dCY7magy36Wg\nOXC1hdRsFcsVW54/3cSQ9fc/+1HIg16/zlS+AWb4dVDhrAUJLYIBrkMPRnu/cDuI\ndvyL+KtZUxhDBoSO0JLrd1+GZGt0WD+mfutCugJS8IGlWQmGq8WRmM2vYfZgEYkq\nCv4392VSsWvg4iluKz0eX+8l7QKHseJwGBvk89Txlz6f7QkooBXYuuyHZS1ZLZBW\nfooK+RNAquDU+cVUu1gVt1V5yt3IxF1qvMRtlElNJKN5NUJT9/K2YcVX6UoMXhDd\noSOpARqPm9E2pdjI62pAOBbCplMSoBprhoCYm0iozf9QhNyUBGWDcTsFDDgqOwy4\nYjGQ5jsnCrkhSzRkTViWD+Pgw+Ar4fxcjySGUf0x7HkNfteDPSdLMD8J2vTJXfoB\nGAQQmGMZmFgONC62FrDphlsCAwEAAQ==\n-----END PUBLIC KEY-----\n"},"isCat":true,"vcard:bday":"2001-02-12"}

View file

@ -755,6 +755,54 @@ test "it restricts length of registration reason" do
end
end
describe "user registration, with :birthday_required and :birthday_min_age" do
@full_user_data %{
bio: "A guy",
name: "my name",
nickname: "nick",
password: "test",
password_confirmation: "test",
email: "email@example.com"
}
setup do
clear_config([:instance, :birthday_required], true)
clear_config([:instance, :birthday_min_age], 18 * 365)
end
test "it passes when correct birth date is provided" do
today = Date.utc_today()
birthday = Date.add(today, -19 * 365)
params =
@full_user_data
|> Map.put(:birthday, birthday)
changeset = User.register_changeset(%User{}, params)
assert changeset.valid?
end
test "it fails when birth date is not provided" do
changeset = User.register_changeset(%User{}, @full_user_data)
refute changeset.valid?
end
test "it fails when provided invalid birth date" do
today = Date.utc_today()
birthday = Date.add(today, -17 * 365)
params =
@full_user_data
|> Map.put(:birthday, birthday)
changeset = User.register_changeset(%User{}, params)
refute changeset.valid?
end
end
describe "get_or_fetch/1" do
test "gets an existing user by nickname" do
user = insert(:user)

View file

@ -389,6 +389,26 @@ test "fetches user featured collection without embedded object" do
assert %{data: %{"id" => ^object_url}} = Object.get_by_ap_id(object_url)
end
test "fetches user birthday information from misskey" do
user_id = "https://misskey.io/@mkljczk"
Tesla.Mock.mock(fn
%{
method: :get,
url: ^user_id
} ->
%Tesla.Env{
status: 200,
body: File.read!("test/fixtures/birthdays/misskey-user.json"),
headers: [{"content-type", "application/activity+json"}]
}
end)
{:ok, user} = ActivityPub.make_user_from_ap_id(user_id)
assert user.birthday == ~D[2001-02-12]
end
end
test "it fetches the appropriate tag-restricted posts" do

View file

@ -3,22 +3,40 @@
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.ActivityPub.MRF.ForceMentionsInContentTest do
alias Pleroma.Web.ActivityPub.MRF.ForceMentionsInContent
import Pleroma.Factory
use Pleroma.DataCase
require Pleroma.Constants
alias Pleroma.Constants
alias Pleroma.Object
alias Pleroma.Web.ActivityPub.MRF.ForceMentionsInContent
alias Pleroma.Web.CommonAPI
import Pleroma.Factory
test "adds mentions to post content" do
users = %{
"lain@lain.com" => "https://lain.com/users/lain",
"coolboymew@shitposter.club" => "https://shitposter.club/users/coolboymew",
"dielan@shitposter.club" => "https://shitposter.club/users/dielan",
"hakui@tuusin.misono-ya.info" => "https://tuusin.misono-ya.info/users/hakui",
"fence@xyzzy.link" => "https://xyzzy.link/users/fence"
}
Enum.each(users, fn {nickname, ap_id} ->
insert(:user, ap_id: ap_id, nickname: nickname, local: false)
end)
[lain, coolboymew, dielan, hakui, fence] = [
insert(:user, ap_id: "https://lain.com/users/lain", nickname: "lain@lain.com", local: false),
insert(:user,
ap_id: "https://shitposter.club/users/coolboymew",
nickname: "coolboymew@shitposter.club",
local: false
),
insert(:user,
ap_id: "https://shitposter.club/users/dielan",
nickname: "dielan@shitposter.club",
local: false
),
insert(:user,
ap_id: "https://tuusin.misono-ya.info/users/hakui",
nickname: "hakui@tuusin.misono-ya.info",
local: false
),
insert(:user,
ap_id: "https://xyzzy.link/users/fence",
nickname: "fence@xyzzy.link",
local: false
)
]
object = File.read!("test/fixtures/soapbox_no_mentions_in_content.json") |> Jason.decode!()
@ -29,6 +47,42 @@ test "adds mentions to post content" do
}
{:ok, %{"object" => %{"content" => filtered}}} = ForceMentionsInContent.filter(activity)
Enum.each(users, fn {nickname, _} -> assert filtered =~ nickname end)
assert filtered ==
"<span class=\"recipients-inline\"><span class=\"h-card\"><a class=\"u-url mention\" data-user=\"#{dielan.id}\" href=\"https://shitposter.club/users/dielan\" rel=\"ugc\">@<span>dielan</span></a></span> <span class=\"h-card\"><a class=\"u-url mention\" data-user=\"#{coolboymew.id}\" href=\"https://shitposter.club/users/coolboymew\" rel=\"ugc\">@<span>coolboymew</span></a></span> <span class=\"h-card\"><a class=\"u-url mention\" data-user=\"#{fence.id}\" href=\"https://xyzzy.link/users/fence\" rel=\"ugc\">@<span>fence</span></a></span> <span class=\"h-card\"><a class=\"u-url mention\" data-user=\"#{hakui.id}\" href=\"https://tuusin.misono-ya.info/users/hakui\" rel=\"ugc\">@<span>hakui</span></a></span> <span class=\"h-card\"><a class=\"u-url mention\" data-user=\"#{lain.id}\" href=\"https://lain.com/users/lain\" rel=\"ugc\">@<span>lain</span></a></span> </span><p>Haha yeah, you can control who you reply to.</p>"
end
test "the replied-to user is sorted to the left" do
[mario, luigi, wario] = [
insert(:user, nickname: "mario"),
insert(:user, nickname: "luigi"),
insert(:user, nickname: "wario")
]
{:ok, post1} = CommonAPI.post(mario, %{status: "Letsa go!"})
{:ok, post2} =
CommonAPI.post(luigi, %{status: "Oh yaah", in_reply_to_id: post1.id, to: [mario.ap_id]})
activity = %{
"type" => "Create",
"actor" => wario.ap_id,
"object" => %{
"type" => "Note",
"actor" => wario.ap_id,
"content" => "WHA-HA!",
"to" => [
mario.ap_id,
luigi.ap_id,
Constants.as_public()
],
"inReplyTo" => Object.normalize(post2).data["id"]
}
}
{:ok, %{"object" => %{"content" => filtered}}} = ForceMentionsInContent.filter(activity)
assert filtered ==
"<span class=\"recipients-inline\"><span class=\"h-card\"><a class=\"u-url mention\" data-user=\"#{luigi.id}\" href=\"#{luigi.ap_id}\" rel=\"ugc\">@<span>luigi</span></a></span> <span class=\"h-card\"><a class=\"u-url mention\" data-user=\"#{mario.id}\" href=\"#{mario.ap_id}\" rel=\"ugc\">@<span>mario</span></a></span> </span>WHA-HA!"
end
end

View file

@ -1608,6 +1608,60 @@ test "returns an error if captcha is invalid", %{conn: conn} do
end
end
describe "create account with required birth date" do
setup %{conn: conn} do
clear_config([:instance, :birthday_required], true)
clear_config([:instance, :birthday_min_age], 18 * 365)
app_token = insert(:oauth_token, user: nil)
conn =
conn
|> put_req_header("authorization", "Bearer " <> app_token.token)
|> put_req_header("content-type", "multipart/form-data")
[conn: conn]
end
test "creates an account if provided valid birth date", %{conn: conn} do
birthday =
Date.utc_today()
|> Date.add(-19 * 365)
|> Date.to_string()
params = %{
username: "mkljczk",
email: "mkljczk@example.org",
password: "dupa.8",
agreement: true,
birthday: birthday
}
res =
conn
|> post("/api/v1/accounts", params)
assert json_response_and_validate_schema(res, 200)
end
test "returns an error if missing birth date", %{conn: conn} do
params = %{
username: "mkljczk",
email: "mkljczk@example.org",
password: "dupa.8",
agreement: true
}
res =
conn
|> post("/api/v1/accounts", params)
assert json_response_and_validate_schema(res, 400) == %{
"error" => "{\"birthday\":[\"can't be blank\"]}"
}
end
end
describe "GET /api/v1/accounts/:id/lists - account_lists" do
test "returns lists to which the account belongs" do
%{user: user, conn: conn} = oauth_access(["read:lists"])

View file

@ -370,6 +370,26 @@ test "update fields", %{conn: conn} do
]
end
test "updates birth date", %{conn: conn} do
res =
patch(conn, "/api/v1/accounts/update_credentials", %{
"birthday" => "2001-02-12"
})
assert user_data = json_response_and_validate_schema(res, 200)
assert user_data["pleroma"]["birthday"] == "2001-02-12"
end
test "updates the user's show_birthday status", %{conn: conn} do
res =
patch(conn, "/api/v1/accounts/update_credentials", %{
"show_birthday" => true
})
assert user_data = json_response_and_validate_schema(res, 200)
assert user_data["source"]["pleroma"]["show_birthday"] == true
end
test "emojis in fields labels", %{conn: conn} do
fields = [
%{"name" => ":firefox:", "value" => "is best 2hu"},

View file

@ -494,6 +494,40 @@ test "shows email only to the account owner" do
end
end
describe "hiding birthday" do
test "doesn't show birthday if hidden" do
user =
insert(:user, %{
birthday: "2001-02-12",
show_birthday: false
})
other_user = insert(:user)
user = User.get_cached_by_ap_id(user.ap_id)
assert AccountView.render(
"show.json",
%{user: user, for: other_user}
)[:birthday] == nil
end
test "shows hidden birthday to the account owner" do
user =
insert(:user, %{
birthday: "2001-02-12",
show_birthday: false
})
user = User.get_cached_by_ap_id(user.ap_id)
assert AccountView.render(
"show.json",
%{user: user, for: user}
)[:birthday] == nil
end
end
describe "follow requests counter" do
test "shows zero when no follow requests are pending" do
user = insert(:user)

View file

@ -304,4 +304,59 @@ test "returns 404 error when specified user is not exist", %{conn: conn} do
assert json_response_and_validate_schema(conn, 404) == %{"error" => "Record not found"}
end
end
describe "birthday reminders" do
test "returns a list of friends having birthday on specified day" do
%{user: user, conn: conn} = oauth_access(["read:accounts"])
%{id: id1} =
user1 =
insert(:user, %{
birthday: "2001-02-12",
show_birthday: true
})
user2 =
insert(:user, %{
birthday: "2001-02-14",
show_birthday: true
})
user3 = insert(:user)
CommonAPI.follow(user, user1)
CommonAPI.follow(user, user2)
CommonAPI.follow(user, user3)
[%{"id" => ^id1}] =
conn
|> get("/api/v1/pleroma/birthdays?day=12&month=2")
|> json_response_and_validate_schema(:ok)
end
test "the list doesn't list friends with hidden birth date" do
%{user: user, conn: conn} = oauth_access(["read:accounts"])
user1 =
insert(:user, %{
birthday: "2001-02-12",
show_birthday: false
})
%{id: id2} =
user2 =
insert(:user, %{
birthday: "2001-02-12",
show_birthday: true
})
CommonAPI.follow(user, user1)
CommonAPI.follow(user, user2)
[%{"id" => ^id2}] =
conn
|> get("/api/v1/pleroma/birthdays?day=12&month=2")
|> json_response_and_validate_schema(:ok)
end
end
end