Merge branch 'develop' of ssh.gitgud.io:lambadalambda/pleroma into develop

This commit is contained in:
Roger Braun 2017-04-23 10:37:22 +02:00
commit 361a8c4219
9 changed files with 309 additions and 42 deletions

View file

@ -1,7 +1,8 @@
defmodule Pleroma.User do
use Ecto.Schema
import Ecto.Changeset
alias Pleroma.{Repo, User}
import Ecto.Query
alias Pleroma.{Repo, User, Activity, Object}
schema "users" do
field :bio, :string
@ -39,6 +40,22 @@ def follow_changeset(struct, params \\ %{}) do
|> validate_required([:following])
end
def user_info(%User{} = user) do
note_count_query = from a in Object,
where: fragment("? @> ?", a.data, ^%{actor: user.ap_id, type: "Note"}),
select: count(a.id)
follower_count_query = from u in User,
where: fragment("? @> ?", u.following, ^User.ap_followers(user)),
select: count(u.id)
%{
following_count: length(user.following),
note_count: Repo.one(note_count_query),
follower_count: Repo.one(follower_count_query)
}
end
def register_changeset(struct, params \\ %{}) do
changeset = struct
|> cast(params, [:bio, :email, :name, :nickname, :password, :password_confirmation])
@ -62,6 +79,10 @@ def register_changeset(struct, params \\ %{}) do
def follow(%User{} = follower, %User{} = followed) do
ap_followers = User.ap_followers(followed)
if following?(follower, followed) do
{ :error,
"Could not follow user: #{followed.nickname} is already on your list." }
else
following = [ap_followers | follower.following]
|> Enum.uniq
@ -69,15 +90,20 @@ def follow(%User{} = follower, %User{} = followed) do
|> follow_changeset(%{following: following})
|> Repo.update
end
end
def unfollow(%User{} = follower, %User{} = followed) do
ap_followers = User.ap_followers(followed)
if following?(follower, followed) do
following = follower.following
|> List.delete(ap_followers)
follower
|> follow_changeset(%{following: following})
|> Repo.update
else
{ :error, "Not subscribed!" }
end
end
def following?(%User{} = follower, %User{} = followed) do

View file

@ -133,6 +133,13 @@ def fetch_activities(recipients, opts \\ %{}) do
query
end
query = if opts["actor_id"] do
from activity in query,
where: fragment("? @> ?", activity.data, ^%{actor: opts["actor_id"]})
else
query
end
Repo.all(query)
|> Enum.reverse
end

View file

@ -27,11 +27,15 @@ def user_fetcher(username) do
pipe_through :api
get "/help/test", TwitterAPI.Controller, :help_test
get "/statusnet/config", TwitterAPI.Controller, :config
get "/statuses/public_timeline", TwitterAPI.Controller, :public_timeline
get "/statuses/public_and_external_timeline", TwitterAPI.Controller, :public_timeline
get "/statuses/user_timeline", TwitterAPI.Controller, :user_timeline
get "/statuses/show/:id", TwitterAPI.Controller, :fetch_status
get "/statusnet/conversation/:id", TwitterAPI.Controller, :fetch_conversation
get "/statusnet/config", TwitterAPI.Controller, :config
post "/account/register", TwitterAPI.Controller, :register
end
@ -40,17 +44,25 @@ def user_fetcher(username) do
get "/account/verify_credentials", TwitterAPI.Controller, :verify_credentials
post "/account/verify_credentials", TwitterAPI.Controller, :verify_credentials
post "/statuses/update", TwitterAPI.Controller, :status_update
get "/statuses/home_timeline", TwitterAPI.Controller, :friends_timeline
get "/statuses/friends_timeline", TwitterAPI.Controller, :friends_timeline
get "/statuses/mentions", TwitterAPI.Controller, :mentions_timeline
get "/statuses/mentions_timeline", TwitterAPI.Controller, :mentions_timeline
post "/statuses/update", TwitterAPI.Controller, :status_update
post "/statuses/retweet/:id", TwitterAPI.Controller, :retweet
post "/friendships/create", TwitterAPI.Controller, :follow
post "/friendships/destroy", TwitterAPI.Controller, :unfollow
post "/statusnet/media/upload", TwitterAPI.Controller, :upload
post "/media/upload", TwitterAPI.Controller, :upload_json
post "/favorites/create/:id", TwitterAPI.Controller, :favorite
post "/favorites/create", TwitterAPI.Controller, :favorite
post "/favorites/destroy/:id", TwitterAPI.Controller, :unfavorite
post "/statuses/retweet/:id", TwitterAPI.Controller, :retweet
post "/qvitter/update_avatar", TwitterAPI.Controller, :update_avatar
end

View file

@ -11,6 +11,8 @@ def to_map(user, opts) do
false
end
user_info = User.user_info(user)
map = %{
"id" => user.id,
"name" => user.name,
@ -19,9 +21,9 @@ def to_map(user, opts) do
"following" => following,
# Fake fields
"favourites_count" => 0,
"statuses_count" => 0,
"friends_count" => 0,
"followers_count" => 0,
"statuses_count" => user_info[:note_count],
"friends_count" => user_info[:following_count],
"followers_count" => user_info[:follower_count],
"profile_image_url" => image,
"profile_image_url_https" => image,
"profile_image_url_profile_size" => image,

View file

@ -80,6 +80,16 @@ def fetch_public_statuses(user, opts \\ %{}) do
|> activities_to_statuses(%{for: user})
end
def fetch_user_statuses(user, opts \\ %{}) do
ActivityPub.fetch_activities([], opts)
|> activities_to_statuses(%{for: user})
end
def fetch_mentions(user, opts \\ %{}) do
ActivityPub.fetch_activities([user.ap_id], opts)
|> activities_to_statuses(%{for: user})
end
def fetch_conversation(user, id) do
query = from activity in Activity,
where: fragment("? @> ?", activity.data, ^%{ statusnetConversationId: id}),
@ -114,6 +124,8 @@ def follow(%User{} = follower, followed_id) do
})
do
{ :ok, follower, followed, activity }
else
err -> err
end
end
@ -122,6 +134,8 @@ def unfollow(%User{} = follower, followed_id) do
{ :ok, follower } <- User.unfollow(follower, followed)
do
{ :ok, follower, followed }
else
err -> err
end
end
@ -244,12 +258,37 @@ def register_user(params) do
{:ok, UserRepresenter.to_map(user)}
else
{:error, changeset} ->
errors = Ecto.Changeset.traverse_errors(changeset, fn {msg, opts} -> msg end)
errors = Ecto.Changeset.traverse_errors(changeset, fn {msg, _opts} -> msg end)
|> Poison.encode!
{:error, %{error: errors}}
end
end
def get_user(user, params) do
case params do
%{ "user_id" => user_id } ->
case target = Repo.get(User, user_id) do
nil ->
{:error, "No user with such user_id"}
_ ->
{:ok, target}
end
%{ "screen_name" => nickname } ->
case target = Repo.get_by(User, nickname: nickname) do
nil ->
{:error, "No user with such screen_name"}
_ ->
{:ok, target}
end
_ ->
if user do
{:ok, user}
else
{:error, "You need to specify screen_name or user_id"}
end
end
end
defp activities_to_statuses(activities, opts) do
Enum.map(activities, fn(activity) ->
activity_to_status(activity, opts)
@ -279,7 +318,7 @@ defp activity_to_status(activity, opts) do
actor = get_in(activity.data, ["actor"])
user = User.get_cached_by_ap_id(actor)
# mentioned_users = Repo.all(from user in User, where: user.ap_id in ^activity.data["to"])
mentioned_users = Enum.map(activity.data["to"], fn (ap_id) ->
mentioned_users = Enum.map(activity.data["to"] || [], fn (ap_id) ->
User.get_cached_by_ap_id(ap_id)
end)
|> Enum.filter(&(&1))

View file

@ -45,22 +45,45 @@ def friends_timeline(%{assigns: %{user: user}} = conn, params) do
|> json_reply(200, json)
end
def follow(%{assigns: %{user: user}} = conn, %{ "user_id" => followed_id }) do
{ :ok, user, follower, _activity } = TwitterAPI.follow(user, followed_id)
def user_timeline(%{assigns: %{user: user}} = conn, params) do
case TwitterAPI.get_user(user, params) do
{:ok, target_user} ->
params = Map.merge(params, %{"actor_id" => target_user.ap_id})
statuses = TwitterAPI.fetch_user_statuses(user, params)
conn
|> json_reply(200, statuses |> Poison.encode!)
{:error, msg} ->
bad_request_reply(conn, msg)
end
end
response = follower |> UserRepresenter.to_json(%{for: user})
def mentions_timeline(%{assigns: %{user: user}} = conn, params) do
statuses = TwitterAPI.fetch_mentions(user, params)
{:ok, json} = Poison.encode(statuses)
conn
|> json_reply(200, json)
end
def follow(%{assigns: %{user: user}} = conn, %{ "user_id" => followed_id }) do
case TwitterAPI.follow(user, followed_id) do
{ :ok, user, followed, _activity } ->
response = followed |> UserRepresenter.to_json(%{for: user})
conn
|> json_reply(200, response)
{ :error, msg } -> forbidden_json_reply(conn, msg)
end
end
def unfollow(%{assigns: %{user: user}} = conn, %{ "user_id" => followed_id }) do
{ :ok, user, follower } = TwitterAPI.unfollow(user, followed_id)
response = follower |> UserRepresenter.to_json(%{for: user})
case TwitterAPI.unfollow(user, followed_id) do
{ :ok, user, followed } ->
response = followed |> UserRepresenter.to_json(%{for: user})
conn
|> json_reply(200, response)
{ :error, msg } -> forbidden_json_reply(conn, msg)
end
end
def fetch_status(%{assigns: %{user: user}} = conn, %{ "id" => id }) do
@ -159,9 +182,21 @@ def update_avatar(%{assigns: %{user: user}} = conn, params) do
|> json_reply(200, response)
end
defp bad_request_reply(conn, error_message) do
json = Poison.encode!(%{"error" => error_message})
json_reply(conn, 400, json)
end
defp json_reply(conn, status, json) do
conn
|> put_resp_content_type("application/json")
|> send_resp(status, json)
end
defp forbidden_json_reply(conn, error_message) do
json = %{"error" => error_message, "request" => conn.request_path}
|> Poison.encode!
json_reply(conn, 403, json)
end
end

View file

@ -19,7 +19,18 @@ test "A user with an avatar object", %{user: user} do
assert represented["profile_image_url"] == image
end
test "A user", %{user: user} do
test "A user" do
note_activity = insert(:note_activity)
user = User.get_cached_by_ap_id(note_activity.data["actor"])
follower = insert(:user)
second_follower = insert(:user)
User.follow(follower, user)
User.follow(second_follower, user)
User.follow(user, follower)
user = Repo.get!(User, user.id)
image = "https://placehold.it/48x48"
represented = %{
@ -29,9 +40,9 @@ test "A user", %{user: user} do
"description" => user.bio,
# Fake fields
"favourites_count" => 0,
"statuses_count" => 0,
"friends_count" => 0,
"followers_count" => 0,
"statuses_count" => 1,
"friends_count" => 1,
"followers_count" => 2,
"profile_image_url" => image,
"profile_image_url_https" => image,
"profile_image_url_profile_size" => image,
@ -55,7 +66,7 @@ test "A user for a given other follower", %{user: user} do
"favourites_count" => 0,
"statuses_count" => 0,
"friends_count" => 0,
"followers_count" => 0,
"followers_count" => 1,
"profile_image_url" => image,
"profile_image_url_https" => image,
"profile_image_url_profile_size" => image,

View file

@ -114,6 +114,93 @@ test "with credentials", %{conn: conn, user: current_user} do
end
end
describe "GET /statuses/mentions.json" do
setup [:valid_user]
test "without valid credentials", %{conn: conn} do
conn = get conn, "/api/statuses/mentions.json"
assert json_response(conn, 403) == %{"error" => "Invalid credentials."}
end
test "with credentials", %{conn: conn, user: current_user} do
{:ok, activity} = ActivityBuilder.insert(%{"to" => [current_user.ap_id]}, %{user: current_user})
conn = conn
|> with_credentials(current_user.nickname, "test")
|> get("/api/statuses/mentions.json")
response = json_response(conn, 200)
assert length(response) == 1
assert Enum.at(response, 0) == ActivityRepresenter.to_map(activity, %{user: current_user, mentioned: [current_user]})
end
end
describe "GET /statuses/user_timeline.json" do
setup [:valid_user]
test "without any params", %{conn: conn} do
conn = get(conn, "/api/statuses/user_timeline.json")
assert json_response(conn, 400) == %{"error" => "You need to specify screen_name or user_id"}
end
test "with user_id", %{conn: conn} do
user = insert(:user)
{:ok, activity} = ActivityBuilder.insert(%{"id" => 1}, %{user: user})
conn = get(conn, "/api/statuses/user_timeline.json", %{"user_id" => user.id})
response = json_response(conn, 200)
assert length(response) == 1
assert Enum.at(response, 0) == ActivityRepresenter.to_map(activity, %{user: user})
end
test "with screen_name", %{conn: conn} do
user = insert(:user)
{:ok, activity} = ActivityBuilder.insert(%{"id" => 1}, %{user: user})
conn = get(conn, "/api/statuses/user_timeline.json", %{"screen_name" => user.nickname})
response = json_response(conn, 200)
assert length(response) == 1
assert Enum.at(response, 0) == ActivityRepresenter.to_map(activity, %{user: user})
end
test "with credentials", %{conn: conn, user: current_user} do
{:ok, activity} = ActivityBuilder.insert(%{"id" => 1}, %{user: current_user})
conn = conn
|> with_credentials(current_user.nickname, "test")
|> get("/api/statuses/user_timeline.json")
response = json_response(conn, 200)
assert length(response) == 1
assert Enum.at(response, 0) == ActivityRepresenter.to_map(activity, %{user: current_user})
end
test "with credentials with user_id", %{conn: conn, user: current_user} do
user = insert(:user)
{:ok, activity} = ActivityBuilder.insert(%{"id" => 1}, %{user: user})
conn = conn
|> with_credentials(current_user.nickname, "test")
|> get("/api/statuses/user_timeline.json", %{"user_id" => user.id})
response = json_response(conn, 200)
assert length(response) == 1
assert Enum.at(response, 0) == ActivityRepresenter.to_map(activity, %{user: user})
end
test "with credentials screen_name", %{conn: conn, user: current_user} do
user = insert(:user)
{:ok, activity} = ActivityBuilder.insert(%{"id" => 1}, %{user: user})
conn = conn
|> with_credentials(current_user.nickname, "test")
|> get("/api/statuses/user_timeline.json", %{"screen_name" => user.nickname})
response = json_response(conn, 200)
assert length(response) == 1
assert Enum.at(response, 0) == ActivityRepresenter.to_map(activity, %{user: user})
end
end
describe "POST /friendships/create.json" do
setup [:valid_user]
test "without valid credentials", %{conn: conn} do
@ -280,7 +367,7 @@ test "it returns errors on a problem", %{conn: conn} do
end
defp valid_user(_context) do
{ :ok, user } = UserBuilder.insert(%{nickname: "lambda", ap_id: "lambda"})
user = insert(:user)
[user: user]
end

View file

@ -102,6 +102,49 @@ test "fetch friends' statuses" do
assert Enum.at(statuses, 1) == ActivityRepresenter.to_map(direct_activity, %{user: direct_activity_user, mentioned: [user]})
end
test "fetch user's mentions" do
user = insert(:user)
{:ok, activity} = ActivityBuilder.insert(%{"to" => [user.ap_id]})
activity_user = Repo.get_by(User, ap_id: activity.data["actor"])
statuses = TwitterAPI.fetch_mentions(user)
assert length(statuses) == 1
assert Enum.at(statuses, 0) == ActivityRepresenter.to_map(activity, %{user: activity_user, mentioned: [user]})
end
test "get a user by params" do
user1_result = {:ok, user1} = UserBuilder.insert(%{ap_id: "some id", email: "test@pleroma"})
{:ok, user2} = UserBuilder.insert(%{ap_id: "some other id", nickname: "testname2", email: "test2@pleroma"})
assert {:error, "You need to specify screen_name or user_id"} == TwitterAPI.get_user(nil, nil)
assert user1_result == TwitterAPI.get_user(nil, %{"user_id" => user1.id})
assert user1_result == TwitterAPI.get_user(nil, %{"screen_name" => user1.nickname})
assert user1_result == TwitterAPI.get_user(user1, nil)
assert user1_result == TwitterAPI.get_user(user2, %{"user_id" => user1.id})
assert user1_result == TwitterAPI.get_user(user2, %{"screen_name" => user1.nickname})
assert {:error, "No user with such screen_name"} == TwitterAPI.get_user(nil, %{"screen_name" => "Satan"})
assert {:error, "No user with such user_id"} == TwitterAPI.get_user(nil, %{"user_id" => 666})
end
test "fetch user's statuses" do
{:ok, user1} = UserBuilder.insert(%{ap_id: "some id", email: "test@pleroma"})
{:ok, user2} = UserBuilder.insert(%{ap_id: "some other id", nickname: "testname2", email: "test2@pleroma"})
{:ok, status1} = ActivityBuilder.insert(%{"id" => 1}, %{user: user1})
{:ok, status2} = ActivityBuilder.insert(%{"id" => 2}, %{user: user2})
user1_statuses = TwitterAPI.fetch_user_statuses(user1, %{"actor_id" => user1.ap_id})
assert length(user1_statuses) == 1
assert Enum.at(user1_statuses, 0) == ActivityRepresenter.to_map(status1, %{user: user1})
user2_statuses = TwitterAPI.fetch_user_statuses(user1, %{"actor_id" => user2.ap_id})
assert length(user2_statuses) == 1
assert Enum.at(user2_statuses, 0) == ActivityRepresenter.to_map(status2, %{user: user2})
end
test "fetch a single status" do
{:ok, activity} = ActivityBuilder.insert()
{:ok, user} = UserBuilder.insert()
@ -114,26 +157,31 @@ test "fetch a single status" do
test "Follow another user" do
user = insert(:user)
following = insert(:user)
followed = insert(:user)
{:ok, user, following, activity } = TwitterAPI.follow(user, following.id)
{ :ok, user, followed, activity } = TwitterAPI.follow(user, followed.id)
user = Repo.get(User, user.id)
follow = Repo.get(Activity, activity.id)
assert user.following == [User.ap_followers(following)]
assert user.following == [User.ap_followers(followed)]
assert follow == activity
{ :error, msg } = TwitterAPI.follow(user, followed.id)
assert msg == "Could not follow user: #{followed.nickname} is already on your list."
end
test "Unfollow another user" do
following = insert(:user)
user = insert(:user, %{following: [User.ap_followers(following)]})
followed = insert(:user)
user = insert(:user, %{following: [User.ap_followers(followed)]})
{:ok, user, _following } = TwitterAPI.unfollow(user, following.id)
{ :ok, user, _followed } = TwitterAPI.unfollow(user, followed.id)
user = Repo.get(User, user.id)
assert user.following == []
{ :error, msg } = TwitterAPI.unfollow(user, followed.id)
assert msg == "Not subscribed!"
end
test "fetch statuses in a context using the conversation id" do