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

This commit is contained in:
Roger Braun 2017-11-10 17:37:54 +01:00
commit 1d3d66a841
7 changed files with 74 additions and 7 deletions

View file

@ -8,11 +8,19 @@ defmodule Pleroma.Upload do
result_file = Path.join(upload_folder, file.filename) result_file = Path.join(upload_folder, file.filename)
File.cp!(file.path, result_file) File.cp!(file.path, result_file)
# fix content type on some image uploads
matches = Regex.named_captures(~r/\.(?<ext>(jpg|jpeg|png|gif))$/i, file.filename)
content_type = if file.content_type == "application/octet-stream" and matches do
if matches["ext"] == "jpg", do: "image/jpeg", else: "image/#{matches["ext"]}"
else
file.content_type
end
%{ %{
"type" => "Image", "type" => "Image",
"url" => [%{ "url" => [%{
"type" => "Link", "type" => "Link",
"mediaType" => file.content_type, "mediaType" => content_type,
"href" => url_for(Path.join(uuid, :cow_uri.urlencode(file.filename))) "href" => url_for(Path.join(uuid, :cow_uri.urlencode(file.filename)))
}], }],
"name" => file.filename, "name" => file.filename,

View file

@ -6,7 +6,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
alias Pleroma.Web.MastodonAPI.{StatusView, AccountView} alias Pleroma.Web.MastodonAPI.{StatusView, AccountView}
alias Pleroma.Web.ActivityPub.ActivityPub alias Pleroma.Web.ActivityPub.ActivityPub
alias Pleroma.Web.TwitterAPI.TwitterAPI alias Pleroma.Web.TwitterAPI.TwitterAPI
alias Pleroma.Web.CommonAPI alias Pleroma.Web.{CommonAPI, OStatus}
import Ecto.Query import Ecto.Query
import Logger import Logger
@ -361,11 +361,19 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
def search(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do def search(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do
accounts = User.search(query, params["resolve"] == "true") accounts = User.search(query, params["resolve"] == "true")
fetched = if Regex.match?(~r/https?:/, query) do
with {:ok, activities} <- OStatus.fetch_activity_from_url(query) do
activities
else
_e -> []
end
end || []
q = from a in Activity, q = from a in Activity,
where: fragment("?->>'type' = 'Create'", a.data), where: fragment("?->>'type' = 'Create'", a.data),
where: fragment("to_tsvector('english', ?->'object'->>'content') @@ plainto_tsquery('english', ?)", a.data, ^query), where: fragment("to_tsvector('english', ?->'object'->>'content') @@ plainto_tsquery('english', ?)", a.data, ^query),
limit: 20 limit: 20
statuses = Repo.all(q) statuses = Repo.all(q) ++ fetched
res = %{ res = %{
"accounts" => AccountView.render("accounts.json", users: accounts, for: user, as: :user), "accounts" => AccountView.render("accounts.json", users: accounts, for: user, as: :user),

View file

@ -8,10 +8,6 @@ defmodule Pleroma.Repo.Migrations.AddActorToActivity do
add :actor, :string add :actor, :string
end end
execute """
update activities set actor = data->>'actor';
"""
create index(:activities, [:actor, "id DESC NULLS LAST"], concurrently: true) create index(:activities, [:actor, "id DESC NULLS LAST"], concurrently: true)
end end

View file

@ -0,0 +1,26 @@
defmodule Pleroma.Repo.Migrations.FillActorField do
use Ecto.Migration
alias Pleroma.{Repo, Activity}
def up do
max = Repo.aggregate(Activity, :max, :id)
if max do
IO.puts("#{max} activities")
chunks = 0..(round(max / 10_000))
Enum.each(chunks, fn (i) ->
min = i * 10_000
max = min + 10_000
execute("""
update activities set actor = data->>'actor' where id > #{min} and id <= #{max};
""")
|> IO.inspect
end)
end
end
def down do
end
end

View file

@ -0,0 +1,8 @@
defmodule Pleroma.Repo.Migrations.AddSortIndexToActivities do
use Ecto.Migration
@disable_ddl_transaction true
def change do
create index(:activities, ["id desc nulls last"], concurrently: true)
end
end

View file

@ -9,5 +9,17 @@ defmodule Pleroma.UploadTest do
assert data["name"] == "an [image.jpg" assert data["name"] == "an [image.jpg"
assert List.first(data["url"])["href"] == "http://localhost:4001/media/#{data["uuid"]}/an%20%5Bimage.jpg" assert List.first(data["url"])["href"] == "http://localhost:4001/media/#{data["uuid"]}/an%20%5Bimage.jpg"
end end
test "fixes an incorrect content type" do
file = %Plug.Upload{content_type: "application/octet-stream", path: Path.absname("test/fixtures/image.jpg"), filename: "an [image.jpg"}
data = Upload.store(file)
assert hd(data["url"])["mediaType"] == "image/jpeg"
end
test "does not modify a valid content type" do
file = %Plug.Upload{content_type: "image/png", path: Path.absname("test/fixtures/image.jpg"), filename: "an [image.jpg"}
data = Upload.store(file)
assert hd(data["url"])["mediaType"] == "image/png"
end
end end
end end

View file

@ -386,6 +386,15 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
assert status["id"] == to_string(activity.id) assert status["id"] == to_string(activity.id)
end end
test "search fetches remote statuses", %{conn: conn} do
conn = conn
|> get("/api/v1/search", %{"q" => "https://shitposter.club/notice/2827873"})
assert results = json_response(conn, 200)
[status] = results["statuses"]
assert status["uri"] == "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
end
test "search fetches remote accounts", %{conn: conn} do test "search fetches remote accounts", %{conn: conn} do
conn = conn conn = conn
|> get("/api/v1/search", %{"q" => "shp@social.heldscal.la", "resolve" => "true"}) |> get("/api/v1/search", %{"q" => "shp@social.heldscal.la", "resolve" => "true"})