From 29b7e822fbd5a9001cd31705da5f46672941813c Mon Sep 17 00:00:00 2001 From: Sebastian Huebner Date: Tue, 9 Jan 2018 12:31:11 +0100 Subject: added question about activating mediaproxy to config generator --- lib/mix/tasks/generate_config.ex | 3 ++- lib/mix/tasks/sample_config.eex | 5 +++++ 2 files changed, 7 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/mix/tasks/generate_config.ex b/lib/mix/tasks/generate_config.ex index f20f93e4d..0dfbda226 100644 --- a/lib/mix/tasks/generate_config.ex +++ b/lib/mix/tasks/generate_config.ex @@ -8,11 +8,12 @@ defmodule Mix.Tasks.GenerateConfig do domain = IO.gets("What is your domain name? (e.g. pleroma.soykaf.com): ") |> String.trim name = IO.gets("What is the name of your instance? (e.g. Pleroma/Soykaf): ") |> String.trim email = IO.gets("What's your admin email address: ") |> String.trim + mediaproxy = IO.gets("Do you want to activate the mediaproxy? (true/false): ") |> String.trim secret = :crypto.strong_rand_bytes(64) |> Base.encode64 |> binary_part(0, 64) dbpass = :crypto.strong_rand_bytes(64) |> Base.encode64 |> binary_part(0, 64) resultSql = EEx.eval_file("lib/mix/tasks/sample_psql.eex", [dbpass: dbpass]) - result = EEx.eval_file("lib/mix/tasks/sample_config.eex", [domain: domain, email: email, name: name, secret: secret, dbpass: dbpass]) + result = EEx.eval_file("lib/mix/tasks/sample_config.eex", [domain: domain, email: email, name: name, secret: secret, mediaproxy: mediaproxy, dbpass: dbpass]) IO.puts("\nWriting config to config/generated_config.exs.\n\nCheck it and configure your database, then copy it to either config/dev.secret.exs or config/prod.secret.exs") File.write("config/generated_config.exs", result) diff --git a/lib/mix/tasks/sample_config.eex b/lib/mix/tasks/sample_config.eex index 85a7c554e..05f11c95d 100644 --- a/lib/mix/tasks/sample_config.eex +++ b/lib/mix/tasks/sample_config.eex @@ -10,6 +10,11 @@ config :pleroma, :instance, limit: 5000, registrations_open: true +config :pleroma, :media_proxy, + enabled: <%= mediaproxy %>, + redirect_on_failure: true, + #base_url: "https://cache.pleroma.social" + # Configure your database config :pleroma, Pleroma.Repo, adapter: Ecto.Adapters.Postgres, -- cgit v1.2.3 From d56ab43609985801c853b8743f4c33f691f59924 Mon Sep 17 00:00:00 2001 From: eal Date: Fri, 12 Jan 2018 22:38:14 +0200 Subject: Ask for proxy URL if proxy enabled. Also change the true/false question to y/n --- lib/mix/tasks/generate_config.ex | 12 ++++++++++-- lib/mix/tasks/sample_config.eex | 2 +- 2 files changed, 11 insertions(+), 3 deletions(-) (limited to 'lib') diff --git a/lib/mix/tasks/generate_config.ex b/lib/mix/tasks/generate_config.ex index 0dfbda226..2d962124f 100644 --- a/lib/mix/tasks/generate_config.ex +++ b/lib/mix/tasks/generate_config.ex @@ -8,12 +8,20 @@ defmodule Mix.Tasks.GenerateConfig do domain = IO.gets("What is your domain name? (e.g. pleroma.soykaf.com): ") |> String.trim name = IO.gets("What is the name of your instance? (e.g. Pleroma/Soykaf): ") |> String.trim email = IO.gets("What's your admin email address: ") |> String.trim - mediaproxy = IO.gets("Do you want to activate the mediaproxy? (true/false): ") |> String.trim + mediaproxy = IO.gets("Do you want to activate the mediaproxy? (y/N): ") + |> String.trim() + |> String.downcase() + |> String.starts_with?("y") + proxy_url = if mediaproxy do + IO.gets("What is the mediaproxy's URL? (e.g. https://cache.example.com): ") |> String.trim + else + "https://cache.example.com" + end secret = :crypto.strong_rand_bytes(64) |> Base.encode64 |> binary_part(0, 64) dbpass = :crypto.strong_rand_bytes(64) |> Base.encode64 |> binary_part(0, 64) resultSql = EEx.eval_file("lib/mix/tasks/sample_psql.eex", [dbpass: dbpass]) - result = EEx.eval_file("lib/mix/tasks/sample_config.eex", [domain: domain, email: email, name: name, secret: secret, mediaproxy: mediaproxy, dbpass: dbpass]) + result = EEx.eval_file("lib/mix/tasks/sample_config.eex", [domain: domain, email: email, name: name, secret: secret, mediaproxy: mediaproxy, proxy_url: proxy_url, dbpass: dbpass]) IO.puts("\nWriting config to config/generated_config.exs.\n\nCheck it and configure your database, then copy it to either config/dev.secret.exs or config/prod.secret.exs") File.write("config/generated_config.exs", result) diff --git a/lib/mix/tasks/sample_config.eex b/lib/mix/tasks/sample_config.eex index 05f11c95d..9330fae2d 100644 --- a/lib/mix/tasks/sample_config.eex +++ b/lib/mix/tasks/sample_config.eex @@ -13,7 +13,7 @@ config :pleroma, :instance, config :pleroma, :media_proxy, enabled: <%= mediaproxy %>, redirect_on_failure: true, - #base_url: "https://cache.pleroma.social" + base_url: "<%= proxy_url %>" # Configure your database config :pleroma, Pleroma.Repo, -- cgit v1.2.3 From 9717166d105cec6f9c878653aec2d409265e80a6 Mon Sep 17 00:00:00 2001 From: eal Date: Sat, 13 Jan 2018 18:24:16 +0200 Subject: Add a stats agent for storing data from expensive queries. --- lib/pleroma/application.ex | 1 + lib/pleroma/stats.ex | 38 ++++++++++++++++++++++ .../web/mastodon_api/mastodon_api_controller.ex | 12 +++++-- lib/pleroma/web/router.ex | 1 + 4 files changed, 49 insertions(+), 3 deletions(-) create mode 100644 lib/pleroma/stats.ex (limited to 'lib') diff --git a/lib/pleroma/application.ex b/lib/pleroma/application.ex index 2969ca3c4..cdfca8b1a 100644 --- a/lib/pleroma/application.ex +++ b/lib/pleroma/application.ex @@ -21,6 +21,7 @@ defmodule Pleroma.Application do ]]), worker(Pleroma.Web.Federator, []), worker(Pleroma.Web.ChatChannel.ChatChannelState, []), + worker(Pleroma.Stats, []), ] ++ if Mix.env == :test, do: [], else: [worker(Pleroma.Web.Streamer, [])] diff --git a/lib/pleroma/stats.ex b/lib/pleroma/stats.ex new file mode 100644 index 000000000..45fa27b55 --- /dev/null +++ b/lib/pleroma/stats.ex @@ -0,0 +1,38 @@ +defmodule Pleroma.Stats do + use Agent + import Ecto.Query + alias Pleroma.{User, Repo, Activity} + + def start_link do + agent = Agent.start_link(fn -> %{} end, name: __MODULE__) + schedule_update() + agent + end + + def get do + Agent.get(__MODULE__, fn stats -> stats end) + end + + def schedule_update do + update_stats() + spawn(fn -> + Process.sleep(1000 * 60 * 60 * 1) # 1 hour + schedule_update() + end) + end + + def update_stats do + peers = from(u in Pleroma.User, + select: fragment("?->'host'", u.info), + where: u.local != ^true) + |> Repo.all() |> Enum.uniq() + domain_count = Enum.count(peers) + status_query = from p in Activity, + where: p.local == ^true, + where: fragment("?->'object'->>'type' = ?", p.data, ^"Note") + status_count = Repo.aggregate(status_query, :count, :id) + Agent.update(__MODULE__, fn _ -> + %{peers: peers, domain_count: domain_count, status_count: status_count} + end) + end +end diff --git a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex index ffa6f42c8..6ca7a6076 100644 --- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex +++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex @@ -1,6 +1,6 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do use Pleroma.Web, :controller - alias Pleroma.{Repo, Activity, User, Notification} + alias Pleroma.{Repo, Activity, User, Notification, Stats} alias Pleroma.Web alias Pleroma.Web.MastodonAPI.{StatusView, AccountView, MastodonView} alias Pleroma.Web.ActivityPub.ActivityPub @@ -94,6 +94,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do def masto_instance(conn, _params) do user_count = Repo.aggregate(User.local_user_query, :count, :id) + %{domain_count: domain_count, status_count: status_count} = Stats.get() response = %{ uri: Web.base_url, title: Keyword.get(@instance, :name), @@ -104,9 +105,9 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do streaming_api: String.replace(Web.base_url, ["http","https"], "wss") }, stats: %{ - status_count: 2, + status_count: status_count, user_count: user_count, - domain_count: 3 + domain_count: domain_count }, max_toot_chars: Keyword.get(@instance, :limit) } @@ -114,6 +115,11 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do json(conn, response) end + def peers(conn, _params) do + %{peers: peers} = Stats.get() + json(conn, peers) + end + defp mastodonized_emoji do Pleroma.Formatter.get_custom_emoji() |> Enum.map(fn {shortcode, relative_url} -> diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index 8e6681e68..09104fc86 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -106,6 +106,7 @@ defmodule Pleroma.Web.Router do scope "/api/v1", Pleroma.Web.MastodonAPI do pipe_through :api get "/instance", MastodonAPIController, :masto_instance + get "/instance/peers", MastodonAPIController, :peers post "/apps", MastodonAPIController, :create_app get "/custom_emojis", MastodonAPIController, :custom_emojis -- cgit v1.2.3 From d8db39564eb21d9d9b572baae1886af8d0c047d5 Mon Sep 17 00:00:00 2001 From: eal Date: Sun, 14 Jan 2018 08:15:11 +0200 Subject: Move user count to stats agent. --- lib/pleroma/stats.ex | 13 +++++++++---- lib/pleroma/web/mastodon_api/mastodon_api_controller.ex | 11 ++--------- 2 files changed, 11 insertions(+), 13 deletions(-) (limited to 'lib') diff --git a/lib/pleroma/stats.ex b/lib/pleroma/stats.ex index 45fa27b55..938e5c343 100644 --- a/lib/pleroma/stats.ex +++ b/lib/pleroma/stats.ex @@ -4,13 +4,17 @@ defmodule Pleroma.Stats do alias Pleroma.{User, Repo, Activity} def start_link do - agent = Agent.start_link(fn -> %{} end, name: __MODULE__) + agent = Agent.start_link(fn -> {[], %{}} end, name: __MODULE__) schedule_update() agent end - def get do - Agent.get(__MODULE__, fn stats -> stats end) + def get_stats do + Agent.get(__MODULE__, fn {_, stats} -> stats end) + end + + def get_peers do + Agent.get(__MODULE__, fn {peers, _} -> peers end) end def schedule_update do @@ -31,8 +35,9 @@ defmodule Pleroma.Stats do where: p.local == ^true, where: fragment("?->'object'->>'type' = ?", p.data, ^"Note") status_count = Repo.aggregate(status_query, :count, :id) + user_count = Repo.aggregate(User.local_user_query, :count, :id) Agent.update(__MODULE__, fn _ -> - %{peers: peers, domain_count: domain_count, status_count: status_count} + {peers, %{domain_count: domain_count, status_count: status_count, user_count: user_count}} end) end end diff --git a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex index 6ca7a6076..0615ac11a 100644 --- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex +++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex @@ -93,8 +93,6 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do @instance Application.get_env(:pleroma, :instance) def masto_instance(conn, _params) do - user_count = Repo.aggregate(User.local_user_query, :count, :id) - %{domain_count: domain_count, status_count: status_count} = Stats.get() response = %{ uri: Web.base_url, title: Keyword.get(@instance, :name), @@ -104,11 +102,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do urls: %{ streaming_api: String.replace(Web.base_url, ["http","https"], "wss") }, - stats: %{ - status_count: status_count, - user_count: user_count, - domain_count: domain_count - }, + stats: Stats.get_stats, max_toot_chars: Keyword.get(@instance, :limit) } @@ -116,8 +110,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do end def peers(conn, _params) do - %{peers: peers} = Stats.get() - json(conn, peers) + json(conn, Stats.get_peers) end defp mastodonized_emoji do -- cgit v1.2.3 From 14b086eec22486d1efc8248f5f157696e7621e5b Mon Sep 17 00:00:00 2001 From: eal Date: Mon, 15 Jan 2018 22:18:17 +0200 Subject: Add a default profile picture and banner. This removes the placehold.it dependency. --- lib/pleroma/user.ex | 4 ++-- lib/pleroma/web/mastodon_api/views/account_view.ex | 6 +----- lib/pleroma/web/twitter_api/views/user_view.ex | 2 +- 3 files changed, 4 insertions(+), 8 deletions(-) (limited to 'lib') diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index 09bcf0cb4..6ba2b165c 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -29,14 +29,14 @@ defmodule Pleroma.User do def avatar_url(user) do case user.avatar do %{"url" => [%{"href" => href} | _]} -> href - _ -> "https://placehold.it/48x48" + _ -> "#{Web.base_url()}/static/avi.png" end end def banner_url(user) do case user.info["banner"] do %{"url" => [%{"href" => href} | _]} -> href - _ -> nil + _ -> "#{Web.base_url()}/static/banner.png" end end diff --git a/lib/pleroma/web/mastodon_api/views/account_view.ex b/lib/pleroma/web/mastodon_api/views/account_view.ex index 1d5918988..d2a4dd366 100644 --- a/lib/pleroma/web/mastodon_api/views/account_view.ex +++ b/lib/pleroma/web/mastodon_api/views/account_view.ex @@ -5,19 +5,15 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do alias Pleroma.Web.CommonAPI.Utils alias Pleroma.Web.MediaProxy - defp image_url(%{"url" => [ %{ "href" => href } | _ ]}), do: href - defp image_url(_), do: nil - def render("accounts.json", %{users: users} = opts) do render_many(users, AccountView, "account.json", opts) end def render("account.json", %{user: user}) do image = User.avatar_url(user) |> MediaProxy.url() + header = User.banner_url(user) |> MediaProxy.url() user_info = User.user_info(user) - header = (image_url(user.info["banner"]) || "https://placehold.it/700x335") |> MediaProxy.url() - %{ id: to_string(user.id), username: hd(String.split(user.nickname, "@")), diff --git a/lib/pleroma/web/twitter_api/views/user_view.ex b/lib/pleroma/web/twitter_api/views/user_view.ex index cc6b450fb..1b995f42f 100644 --- a/lib/pleroma/web/twitter_api/views/user_view.ex +++ b/lib/pleroma/web/twitter_api/views/user_view.ex @@ -45,7 +45,7 @@ defmodule Pleroma.Web.TwitterAPI.UserView do "screen_name" => user.nickname, "statuses_count" => user_info[:note_count], "statusnet_profile_url" => user.ap_id, - "cover_photo" => image_url(user.info["banner"]) |> MediaProxy.url(), + "cover_photo" => User.banner_url(user) |> MediaProxy.url(), "background_image" => image_url(user.info["background"]) |> MediaProxy.url(), } -- cgit v1.2.3 From b3e7d7ae6d4ab4b871ecf18706461945fd148a1b Mon Sep 17 00:00:00 2001 From: eal Date: Tue, 16 Jan 2018 15:31:03 +0200 Subject: Fetch external profile in the background. --- lib/pleroma/web/twitter_api/twitter_api.ex | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'lib') diff --git a/lib/pleroma/web/twitter_api/twitter_api.ex b/lib/pleroma/web/twitter_api/twitter_api.ex index d04a81cd4..faecebde0 100644 --- a/lib/pleroma/web/twitter_api/twitter_api.ex +++ b/lib/pleroma/web/twitter_api/twitter_api.ex @@ -316,10 +316,12 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do def get_external_profile(for_user, uri) do with {:ok, %User{} = user} <- OStatus.find_or_make_user(uri) do - with url <- user.info["topic"], - {:ok, %{body: body}} <- @httpoison.get(url, [], follow_redirect: true, timeout: 10000, recv_timeout: 20000) do - OStatus.handle_incoming(body) - end + spawn(fn -> + with url <- user.info["topic"], + {:ok, %{body: body}} <- @httpoison.get(url, [], follow_redirect: true, timeout: 10000, recv_timeout: 20000) do + OStatus.handle_incoming(body) + end + end) {:ok, UserView.render("show.json", %{user: user, for: for_user})} else _e -> {:error, "Couldn't find user"} -- cgit v1.2.3 From bc689740f09ee8201982455988bf5f4ccb02fe57 Mon Sep 17 00:00:00 2001 From: eal Date: Tue, 16 Jan 2018 02:17:55 +0200 Subject: Add remote follow path to webfinger. --- lib/pleroma/web/ostatus/ostatus.ex | 4 ++++ lib/pleroma/web/web_finger/web_finger.ex | 3 ++- 2 files changed, 6 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/pleroma/web/ostatus/ostatus.ex b/lib/pleroma/web/ostatus/ostatus.ex index 745539b3e..e65cab3ed 100644 --- a/lib/pleroma/web/ostatus/ostatus.ex +++ b/lib/pleroma/web/ostatus/ostatus.ex @@ -22,6 +22,10 @@ defmodule Pleroma.Web.OStatus do "#{user.ap_id}/salmon" end + def remote_follow_path do + "#{Web.base_url}/ostatus_subscribe?acct={uri}" + end + def handle_incoming(xml_string) do with doc when doc != :error <- parse_document(xml_string) do entries = :xmerl_xpath.string('//entry', doc) diff --git a/lib/pleroma/web/web_finger/web_finger.ex b/lib/pleroma/web/web_finger/web_finger.ex index 026d2f98b..4ae3eab59 100644 --- a/lib/pleroma/web/web_finger/web_finger.ex +++ b/lib/pleroma/web/web_finger/web_finger.ex @@ -44,7 +44,8 @@ defmodule Pleroma.Web.WebFinger do {:Link, %{rel: "http://schemas.google.com/g/2010#updates-from", type: "application/atom+xml", href: OStatus.feed_path(user)}}, {:Link, %{rel: "http://webfinger.net/rel/profile-page", type: "text/html", href: user.ap_id}}, {:Link, %{rel: "salmon", href: OStatus.salmon_path(user)}}, - {:Link, %{rel: "magic-public-key", href: "data:application/magic-public-key,#{magic_key}"}} + {:Link, %{rel: "magic-public-key", href: "data:application/magic-public-key,#{magic_key}"}}, + {:Link, %{rel: "http://ostatus.org/schema/1.0/subscribe", template: OStatus.remote_follow_path()}} ] } |> XmlBuilder.to_doc -- cgit v1.2.3 From 47e8c8db05dbe1f9d0ad7790be9ae9ed40f4e909 Mon Sep 17 00:00:00 2001 From: eal Date: Thu, 18 Jan 2018 00:22:08 +0200 Subject: Add remote follow pages. --- lib/pleroma/web/router.ex | 13 ++++++ .../web/templates/twitter_api/util/follow.html.eex | 11 +++++ .../twitter_api/util/follow_login.html.eex | 14 +++++++ .../templates/twitter_api/util/followed.html.eex | 6 +++ .../web/twitter_api/controllers/util_controller.ex | 48 ++++++++++++++++++++++ 5 files changed, 92 insertions(+) create mode 100644 lib/pleroma/web/templates/twitter_api/util/follow.html.eex create mode 100644 lib/pleroma/web/templates/twitter_api/util/follow_login.html.eex create mode 100644 lib/pleroma/web/templates/twitter_api/util/followed.html.eex (limited to 'lib') diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index 09104fc86..63dbd6245 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -28,6 +28,13 @@ defmodule Pleroma.Web.Router do plug Pleroma.Plugs.AuthenticationPlug, %{fetcher: &Router.user_fetcher/1, optional: true} end + pipeline :pleroma_html do + plug :accepts, ["html"] + plug :fetch_session + plug Pleroma.Plugs.OAuthPlug + plug Pleroma.Plugs.AuthenticationPlug, %{fetcher: &Router.user_fetcher/1, optional: true} + end + pipeline :well_known do plug :accepts, ["xml", "xrd+xml"] end @@ -51,6 +58,12 @@ defmodule Pleroma.Web.Router do get "/emoji", UtilController, :emoji end + scope "/", Pleroma.Web.TwitterAPI do + pipe_through :pleroma_html + get "/ostatus_subscribe", UtilController, :remote_follow + post "/ostatus_subscribe", UtilController, :do_remote_follow + end + scope "/api/pleroma", Pleroma.Web.TwitterAPI do pipe_through :authenticated_api post "/follow_import", UtilController, :follow_import diff --git a/lib/pleroma/web/templates/twitter_api/util/follow.html.eex b/lib/pleroma/web/templates/twitter_api/util/follow.html.eex new file mode 100644 index 000000000..06359fa6c --- /dev/null +++ b/lib/pleroma/web/templates/twitter_api/util/follow.html.eex @@ -0,0 +1,11 @@ +<%= if @error == :error do %> +

Error fetching user

+<% else %> +

Remote follow

+ +

<%= @name %>

+ <%= form_for @conn, util_path(@conn, :do_remote_follow), [as: "user"], fn f -> %> + <%= hidden_input f, :id, value: @id %> + <%= submit "Authorize" %> + <% end %> +<% end %> diff --git a/lib/pleroma/web/templates/twitter_api/util/follow_login.html.eex b/lib/pleroma/web/templates/twitter_api/util/follow_login.html.eex new file mode 100644 index 000000000..4e3a2be67 --- /dev/null +++ b/lib/pleroma/web/templates/twitter_api/util/follow_login.html.eex @@ -0,0 +1,14 @@ +<%= if @error do %> +

<%= @error %>

+<% end %> +

Log in to follow

+

<%= @name %>

+ +<%= form_for @conn, util_path(@conn, :do_remote_follow), [as: "authorization"], fn f -> %> +<%= text_input f, :name, placeholder: "Username" %> +
+<%= password_input f, :password, placeholder: "Password" %> +
+<%= hidden_input f, :id, value: @id %> +<%= submit "Authorize" %> +<% end %> diff --git a/lib/pleroma/web/templates/twitter_api/util/followed.html.eex b/lib/pleroma/web/templates/twitter_api/util/followed.html.eex new file mode 100644 index 000000000..da473d502 --- /dev/null +++ b/lib/pleroma/web/templates/twitter_api/util/followed.html.eex @@ -0,0 +1,6 @@ +<%= if @error do %> +

Error following account

+<% else %> +

Account followed!

+<% end %> + diff --git a/lib/pleroma/web/twitter_api/controllers/util_controller.ex b/lib/pleroma/web/twitter_api/controllers/util_controller.ex index 9d5654995..a1d56e3ab 100644 --- a/lib/pleroma/web/twitter_api/controllers/util_controller.ex +++ b/lib/pleroma/web/twitter_api/controllers/util_controller.ex @@ -2,6 +2,8 @@ defmodule Pleroma.Web.TwitterAPI.UtilController do use Pleroma.Web, :controller require Logger alias Pleroma.Web + alias Pleroma.Web.OStatus + alias Comeonin.Pbkdf2 alias Pleroma.Formatter alias Pleroma.Web.ActivityPub.ActivityPub alias Pleroma.{Repo, PasswordResetToken, User} @@ -30,6 +32,52 @@ defmodule Pleroma.Web.TwitterAPI.UtilController do json(conn, "ok") end + def remote_follow(%{assigns: %{user: user}} = conn, %{"acct" => acct}) do + {err, followee} = OStatus.find_or_make_user(acct) + avatar = User.avatar_url(followee) + name = followee.nickname + id = followee.id + + if !!user do + conn + |> render("follow.html", %{error: err, acct: acct, avatar: avatar, name: name, id: id}) + else + conn + |> render("follow_login.html", %{error: false, acct: acct, avatar: avatar, name: name, id: id}) + end + end + + def do_remote_follow(conn, %{"authorization" => %{"name" => username, "password" => password, "id" => id}}) do + followee = Repo.get(User, id) + avatar = User.avatar_url(followee) + name = followee.nickname + with %User{} = user <- User.get_cached_by_nickname(username), + true <- Pbkdf2.checkpw(password, user.password_hash), + %User{} = followed <- Repo.get(User, id), + {:ok, follower} <- User.follow(user, followee), + {:ok, _activity} <- ActivityPub.follow(follower, followee) do + conn + |> render("followed.html", %{error: false}) + else + _e -> + conn + |> render("follow_login.html", %{error: "Wrong username or password", id: id, name: name, avatar: avatar}) + end + end + def do_remote_follow(%{assigns: %{user: user}} = conn, %{"user" => %{"id" => id}}) do + with %User{} = followee <- Repo.get(User, id), + {:ok, follower} <- User.follow(user, followee), + {:ok, _activity} <- ActivityPub.follow(follower, followee) do + conn + |> render("followed.html", %{error: false}) + else + e -> + Logger.debug("Remote follow failed with error #{inspect e}") + conn + |> render("followed.html", %{error: inspect(e)}) + end + end + @instance Application.get_env(:pleroma, :instance) def config(conn, _params) do case get_format(conn) do -- cgit v1.2.3 From fb5aa34092cf8aa04b3b7001e81cf1585ee0041a Mon Sep 17 00:00:00 2001 From: eal Date: Thu, 18 Jan 2018 03:42:09 +0200 Subject: Revert "Add remote follow path to webfinger." This reverts commit bc689740f09ee8201982455988bf5f4ccb02fe57. --- lib/pleroma/web/ostatus/ostatus.ex | 4 ---- lib/pleroma/web/web_finger/web_finger.ex | 3 +-- 2 files changed, 1 insertion(+), 6 deletions(-) (limited to 'lib') diff --git a/lib/pleroma/web/ostatus/ostatus.ex b/lib/pleroma/web/ostatus/ostatus.ex index e65cab3ed..745539b3e 100644 --- a/lib/pleroma/web/ostatus/ostatus.ex +++ b/lib/pleroma/web/ostatus/ostatus.ex @@ -22,10 +22,6 @@ defmodule Pleroma.Web.OStatus do "#{user.ap_id}/salmon" end - def remote_follow_path do - "#{Web.base_url}/ostatus_subscribe?acct={uri}" - end - def handle_incoming(xml_string) do with doc when doc != :error <- parse_document(xml_string) do entries = :xmerl_xpath.string('//entry', doc) diff --git a/lib/pleroma/web/web_finger/web_finger.ex b/lib/pleroma/web/web_finger/web_finger.ex index 4ae3eab59..026d2f98b 100644 --- a/lib/pleroma/web/web_finger/web_finger.ex +++ b/lib/pleroma/web/web_finger/web_finger.ex @@ -44,8 +44,7 @@ defmodule Pleroma.Web.WebFinger do {:Link, %{rel: "http://schemas.google.com/g/2010#updates-from", type: "application/atom+xml", href: OStatus.feed_path(user)}}, {:Link, %{rel: "http://webfinger.net/rel/profile-page", type: "text/html", href: user.ap_id}}, {:Link, %{rel: "salmon", href: OStatus.salmon_path(user)}}, - {:Link, %{rel: "magic-public-key", href: "data:application/magic-public-key,#{magic_key}"}}, - {:Link, %{rel: "http://ostatus.org/schema/1.0/subscribe", template: OStatus.remote_follow_path()}} + {:Link, %{rel: "magic-public-key", href: "data:application/magic-public-key,#{magic_key}"}} ] } |> XmlBuilder.to_doc -- cgit v1.2.3 From b259b4e4d1dc27528b99744fffabfb05c676ec3f Mon Sep 17 00:00:00 2001 From: eal Date: Thu, 18 Jan 2018 03:42:18 +0200 Subject: Revert "Add remote follow pages." This reverts commit 47e8c8db05dbe1f9d0ad7790be9ae9ed40f4e909. --- lib/pleroma/web/router.ex | 13 ------ .../web/templates/twitter_api/util/follow.html.eex | 11 ----- .../twitter_api/util/follow_login.html.eex | 14 ------- .../templates/twitter_api/util/followed.html.eex | 6 --- .../web/twitter_api/controllers/util_controller.ex | 48 ---------------------- 5 files changed, 92 deletions(-) delete mode 100644 lib/pleroma/web/templates/twitter_api/util/follow.html.eex delete mode 100644 lib/pleroma/web/templates/twitter_api/util/follow_login.html.eex delete mode 100644 lib/pleroma/web/templates/twitter_api/util/followed.html.eex (limited to 'lib') diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index 63dbd6245..09104fc86 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -28,13 +28,6 @@ defmodule Pleroma.Web.Router do plug Pleroma.Plugs.AuthenticationPlug, %{fetcher: &Router.user_fetcher/1, optional: true} end - pipeline :pleroma_html do - plug :accepts, ["html"] - plug :fetch_session - plug Pleroma.Plugs.OAuthPlug - plug Pleroma.Plugs.AuthenticationPlug, %{fetcher: &Router.user_fetcher/1, optional: true} - end - pipeline :well_known do plug :accepts, ["xml", "xrd+xml"] end @@ -58,12 +51,6 @@ defmodule Pleroma.Web.Router do get "/emoji", UtilController, :emoji end - scope "/", Pleroma.Web.TwitterAPI do - pipe_through :pleroma_html - get "/ostatus_subscribe", UtilController, :remote_follow - post "/ostatus_subscribe", UtilController, :do_remote_follow - end - scope "/api/pleroma", Pleroma.Web.TwitterAPI do pipe_through :authenticated_api post "/follow_import", UtilController, :follow_import diff --git a/lib/pleroma/web/templates/twitter_api/util/follow.html.eex b/lib/pleroma/web/templates/twitter_api/util/follow.html.eex deleted file mode 100644 index 06359fa6c..000000000 --- a/lib/pleroma/web/templates/twitter_api/util/follow.html.eex +++ /dev/null @@ -1,11 +0,0 @@ -<%= if @error == :error do %> -

Error fetching user

-<% else %> -

Remote follow

- -

<%= @name %>

- <%= form_for @conn, util_path(@conn, :do_remote_follow), [as: "user"], fn f -> %> - <%= hidden_input f, :id, value: @id %> - <%= submit "Authorize" %> - <% end %> -<% end %> diff --git a/lib/pleroma/web/templates/twitter_api/util/follow_login.html.eex b/lib/pleroma/web/templates/twitter_api/util/follow_login.html.eex deleted file mode 100644 index 4e3a2be67..000000000 --- a/lib/pleroma/web/templates/twitter_api/util/follow_login.html.eex +++ /dev/null @@ -1,14 +0,0 @@ -<%= if @error do %> -

<%= @error %>

-<% end %> -

Log in to follow

-

<%= @name %>

- -<%= form_for @conn, util_path(@conn, :do_remote_follow), [as: "authorization"], fn f -> %> -<%= text_input f, :name, placeholder: "Username" %> -
-<%= password_input f, :password, placeholder: "Password" %> -
-<%= hidden_input f, :id, value: @id %> -<%= submit "Authorize" %> -<% end %> diff --git a/lib/pleroma/web/templates/twitter_api/util/followed.html.eex b/lib/pleroma/web/templates/twitter_api/util/followed.html.eex deleted file mode 100644 index da473d502..000000000 --- a/lib/pleroma/web/templates/twitter_api/util/followed.html.eex +++ /dev/null @@ -1,6 +0,0 @@ -<%= if @error do %> -

Error following account

-<% else %> -

Account followed!

-<% end %> - diff --git a/lib/pleroma/web/twitter_api/controllers/util_controller.ex b/lib/pleroma/web/twitter_api/controllers/util_controller.ex index a1d56e3ab..9d5654995 100644 --- a/lib/pleroma/web/twitter_api/controllers/util_controller.ex +++ b/lib/pleroma/web/twitter_api/controllers/util_controller.ex @@ -2,8 +2,6 @@ defmodule Pleroma.Web.TwitterAPI.UtilController do use Pleroma.Web, :controller require Logger alias Pleroma.Web - alias Pleroma.Web.OStatus - alias Comeonin.Pbkdf2 alias Pleroma.Formatter alias Pleroma.Web.ActivityPub.ActivityPub alias Pleroma.{Repo, PasswordResetToken, User} @@ -32,52 +30,6 @@ defmodule Pleroma.Web.TwitterAPI.UtilController do json(conn, "ok") end - def remote_follow(%{assigns: %{user: user}} = conn, %{"acct" => acct}) do - {err, followee} = OStatus.find_or_make_user(acct) - avatar = User.avatar_url(followee) - name = followee.nickname - id = followee.id - - if !!user do - conn - |> render("follow.html", %{error: err, acct: acct, avatar: avatar, name: name, id: id}) - else - conn - |> render("follow_login.html", %{error: false, acct: acct, avatar: avatar, name: name, id: id}) - end - end - - def do_remote_follow(conn, %{"authorization" => %{"name" => username, "password" => password, "id" => id}}) do - followee = Repo.get(User, id) - avatar = User.avatar_url(followee) - name = followee.nickname - with %User{} = user <- User.get_cached_by_nickname(username), - true <- Pbkdf2.checkpw(password, user.password_hash), - %User{} = followed <- Repo.get(User, id), - {:ok, follower} <- User.follow(user, followee), - {:ok, _activity} <- ActivityPub.follow(follower, followee) do - conn - |> render("followed.html", %{error: false}) - else - _e -> - conn - |> render("follow_login.html", %{error: "Wrong username or password", id: id, name: name, avatar: avatar}) - end - end - def do_remote_follow(%{assigns: %{user: user}} = conn, %{"user" => %{"id" => id}}) do - with %User{} = followee <- Repo.get(User, id), - {:ok, follower} <- User.follow(user, followee), - {:ok, _activity} <- ActivityPub.follow(follower, followee) do - conn - |> render("followed.html", %{error: false}) - else - e -> - Logger.debug("Remote follow failed with error #{inspect e}") - conn - |> render("followed.html", %{error: inspect(e)}) - end - end - @instance Application.get_env(:pleroma, :instance) def config(conn, _params) do case get_format(conn) do -- cgit v1.2.3 From 57d413e65541f47a3c303be88c0ac6d4bfed33a9 Mon Sep 17 00:00:00 2001 From: eal Date: Thu, 18 Jan 2018 18:42:32 +0200 Subject: Revert "Revert "Add remote follow path to webfinger."" This reverts commit fb5aa34092cf8aa04b3b7001e81cf1585ee0041a. --- lib/pleroma/web/ostatus/ostatus.ex | 4 ++++ lib/pleroma/web/web_finger/web_finger.ex | 3 ++- 2 files changed, 6 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/pleroma/web/ostatus/ostatus.ex b/lib/pleroma/web/ostatus/ostatus.ex index 745539b3e..e65cab3ed 100644 --- a/lib/pleroma/web/ostatus/ostatus.ex +++ b/lib/pleroma/web/ostatus/ostatus.ex @@ -22,6 +22,10 @@ defmodule Pleroma.Web.OStatus do "#{user.ap_id}/salmon" end + def remote_follow_path do + "#{Web.base_url}/ostatus_subscribe?acct={uri}" + end + def handle_incoming(xml_string) do with doc when doc != :error <- parse_document(xml_string) do entries = :xmerl_xpath.string('//entry', doc) diff --git a/lib/pleroma/web/web_finger/web_finger.ex b/lib/pleroma/web/web_finger/web_finger.ex index 026d2f98b..4ae3eab59 100644 --- a/lib/pleroma/web/web_finger/web_finger.ex +++ b/lib/pleroma/web/web_finger/web_finger.ex @@ -44,7 +44,8 @@ defmodule Pleroma.Web.WebFinger do {:Link, %{rel: "http://schemas.google.com/g/2010#updates-from", type: "application/atom+xml", href: OStatus.feed_path(user)}}, {:Link, %{rel: "http://webfinger.net/rel/profile-page", type: "text/html", href: user.ap_id}}, {:Link, %{rel: "salmon", href: OStatus.salmon_path(user)}}, - {:Link, %{rel: "magic-public-key", href: "data:application/magic-public-key,#{magic_key}"}} + {:Link, %{rel: "magic-public-key", href: "data:application/magic-public-key,#{magic_key}"}}, + {:Link, %{rel: "http://ostatus.org/schema/1.0/subscribe", template: OStatus.remote_follow_path()}} ] } |> XmlBuilder.to_doc -- cgit v1.2.3 From beb13bf3a9b207119d70bb817ff1ea92f17a11be Mon Sep 17 00:00:00 2001 From: eal Date: Thu, 18 Jan 2018 18:42:44 +0200 Subject: Revert "Revert "Add remote follow pages."" This reverts commit b259b4e4d1dc27528b99744fffabfb05c676ec3f. --- lib/pleroma/web/router.ex | 13 ++++++ .../web/templates/twitter_api/util/follow.html.eex | 11 +++++ .../twitter_api/util/follow_login.html.eex | 14 +++++++ .../templates/twitter_api/util/followed.html.eex | 6 +++ .../web/twitter_api/controllers/util_controller.ex | 48 ++++++++++++++++++++++ 5 files changed, 92 insertions(+) create mode 100644 lib/pleroma/web/templates/twitter_api/util/follow.html.eex create mode 100644 lib/pleroma/web/templates/twitter_api/util/follow_login.html.eex create mode 100644 lib/pleroma/web/templates/twitter_api/util/followed.html.eex (limited to 'lib') diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index 09104fc86..63dbd6245 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -28,6 +28,13 @@ defmodule Pleroma.Web.Router do plug Pleroma.Plugs.AuthenticationPlug, %{fetcher: &Router.user_fetcher/1, optional: true} end + pipeline :pleroma_html do + plug :accepts, ["html"] + plug :fetch_session + plug Pleroma.Plugs.OAuthPlug + plug Pleroma.Plugs.AuthenticationPlug, %{fetcher: &Router.user_fetcher/1, optional: true} + end + pipeline :well_known do plug :accepts, ["xml", "xrd+xml"] end @@ -51,6 +58,12 @@ defmodule Pleroma.Web.Router do get "/emoji", UtilController, :emoji end + scope "/", Pleroma.Web.TwitterAPI do + pipe_through :pleroma_html + get "/ostatus_subscribe", UtilController, :remote_follow + post "/ostatus_subscribe", UtilController, :do_remote_follow + end + scope "/api/pleroma", Pleroma.Web.TwitterAPI do pipe_through :authenticated_api post "/follow_import", UtilController, :follow_import diff --git a/lib/pleroma/web/templates/twitter_api/util/follow.html.eex b/lib/pleroma/web/templates/twitter_api/util/follow.html.eex new file mode 100644 index 000000000..06359fa6c --- /dev/null +++ b/lib/pleroma/web/templates/twitter_api/util/follow.html.eex @@ -0,0 +1,11 @@ +<%= if @error == :error do %> +

Error fetching user

+<% else %> +

Remote follow

+ +

<%= @name %>

+ <%= form_for @conn, util_path(@conn, :do_remote_follow), [as: "user"], fn f -> %> + <%= hidden_input f, :id, value: @id %> + <%= submit "Authorize" %> + <% end %> +<% end %> diff --git a/lib/pleroma/web/templates/twitter_api/util/follow_login.html.eex b/lib/pleroma/web/templates/twitter_api/util/follow_login.html.eex new file mode 100644 index 000000000..4e3a2be67 --- /dev/null +++ b/lib/pleroma/web/templates/twitter_api/util/follow_login.html.eex @@ -0,0 +1,14 @@ +<%= if @error do %> +

<%= @error %>

+<% end %> +

Log in to follow

+

<%= @name %>

+ +<%= form_for @conn, util_path(@conn, :do_remote_follow), [as: "authorization"], fn f -> %> +<%= text_input f, :name, placeholder: "Username" %> +
+<%= password_input f, :password, placeholder: "Password" %> +
+<%= hidden_input f, :id, value: @id %> +<%= submit "Authorize" %> +<% end %> diff --git a/lib/pleroma/web/templates/twitter_api/util/followed.html.eex b/lib/pleroma/web/templates/twitter_api/util/followed.html.eex new file mode 100644 index 000000000..da473d502 --- /dev/null +++ b/lib/pleroma/web/templates/twitter_api/util/followed.html.eex @@ -0,0 +1,6 @@ +<%= if @error do %> +

Error following account

+<% else %> +

Account followed!

+<% end %> + diff --git a/lib/pleroma/web/twitter_api/controllers/util_controller.ex b/lib/pleroma/web/twitter_api/controllers/util_controller.ex index 9d5654995..a1d56e3ab 100644 --- a/lib/pleroma/web/twitter_api/controllers/util_controller.ex +++ b/lib/pleroma/web/twitter_api/controllers/util_controller.ex @@ -2,6 +2,8 @@ defmodule Pleroma.Web.TwitterAPI.UtilController do use Pleroma.Web, :controller require Logger alias Pleroma.Web + alias Pleroma.Web.OStatus + alias Comeonin.Pbkdf2 alias Pleroma.Formatter alias Pleroma.Web.ActivityPub.ActivityPub alias Pleroma.{Repo, PasswordResetToken, User} @@ -30,6 +32,52 @@ defmodule Pleroma.Web.TwitterAPI.UtilController do json(conn, "ok") end + def remote_follow(%{assigns: %{user: user}} = conn, %{"acct" => acct}) do + {err, followee} = OStatus.find_or_make_user(acct) + avatar = User.avatar_url(followee) + name = followee.nickname + id = followee.id + + if !!user do + conn + |> render("follow.html", %{error: err, acct: acct, avatar: avatar, name: name, id: id}) + else + conn + |> render("follow_login.html", %{error: false, acct: acct, avatar: avatar, name: name, id: id}) + end + end + + def do_remote_follow(conn, %{"authorization" => %{"name" => username, "password" => password, "id" => id}}) do + followee = Repo.get(User, id) + avatar = User.avatar_url(followee) + name = followee.nickname + with %User{} = user <- User.get_cached_by_nickname(username), + true <- Pbkdf2.checkpw(password, user.password_hash), + %User{} = followed <- Repo.get(User, id), + {:ok, follower} <- User.follow(user, followee), + {:ok, _activity} <- ActivityPub.follow(follower, followee) do + conn + |> render("followed.html", %{error: false}) + else + _e -> + conn + |> render("follow_login.html", %{error: "Wrong username or password", id: id, name: name, avatar: avatar}) + end + end + def do_remote_follow(%{assigns: %{user: user}} = conn, %{"user" => %{"id" => id}}) do + with %User{} = followee <- Repo.get(User, id), + {:ok, follower} <- User.follow(user, followee), + {:ok, _activity} <- ActivityPub.follow(follower, followee) do + conn + |> render("followed.html", %{error: false}) + else + e -> + Logger.debug("Remote follow failed with error #{inspect e}") + conn + |> render("followed.html", %{error: inspect(e)}) + end + end + @instance Application.get_env(:pleroma, :instance) def config(conn, _params) do case get_format(conn) do -- cgit v1.2.3 From 0f722a8c223c1953c9c9fb45e73cc9ace92839e2 Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Sat, 20 Jan 2018 18:38:30 +0100 Subject: Stats: Make less crashy and improve query. --- lib/pleroma/stats.ex | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) (limited to 'lib') diff --git a/lib/pleroma/stats.ex b/lib/pleroma/stats.ex index 938e5c343..0484f1a2d 100644 --- a/lib/pleroma/stats.ex +++ b/lib/pleroma/stats.ex @@ -5,7 +5,7 @@ defmodule Pleroma.Stats do def start_link do agent = Agent.start_link(fn -> {[], %{}} end, name: __MODULE__) - schedule_update() + spawn(fn -> schedule_update() end) agent end @@ -18,23 +18,22 @@ defmodule Pleroma.Stats do end def schedule_update do - update_stats() spawn(fn -> Process.sleep(1000 * 60 * 60 * 1) # 1 hour schedule_update() end) + update_stats() end def update_stats do peers = from(u in Pleroma.User, - select: fragment("?->'host'", u.info), + select: fragment("distinct ?->'host'", u.info), where: u.local != ^true) - |> Repo.all() |> Enum.uniq() + |> Repo.all() domain_count = Enum.count(peers) - status_query = from p in Activity, - where: p.local == ^true, - where: fragment("?->'object'->>'type' = ?", p.data, ^"Note") - status_count = Repo.aggregate(status_query, :count, :id) + status_query = from(u in User.local_user_query, + select: fragment("sum((?->>'note_count')::int)", u.info)) + status_count = Repo.one(status_query) |> IO.inspect user_count = Repo.aggregate(User.local_user_query, :count, :id) Agent.update(__MODULE__, fn _ -> {peers, %{domain_count: domain_count, status_count: status_count, user_count: user_count}} -- cgit v1.2.3