Merge branch 'develop' of ssh.gitgud.io:lambadalambda/pleroma into feature/follow-unfollow-by-screen-name

This commit is contained in:
dtluna 2017-04-21 18:36:03 +03:00
commit a9b3f99d48
8 changed files with 100 additions and 14 deletions

View file

@ -1,7 +1,8 @@
defmodule Pleroma.User do defmodule Pleroma.User do
use Ecto.Schema use Ecto.Schema
import Ecto.Changeset import Ecto.Changeset
alias Pleroma.{Repo, User} import Ecto.Query
alias Pleroma.{Repo, User, Activity, Object}
schema "users" do schema "users" do
field :bio, :string field :bio, :string
@ -32,6 +33,22 @@ defmodule Pleroma.User do
|> validate_required([:following]) |> validate_required([:following])
end 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 def register_changeset(struct, params \\ %{}) do
changeset = struct changeset = struct
|> cast(params, [:bio, :email, :name, :nickname, :password, :password_confirmation]) |> cast(params, [:bio, :email, :name, :nickname, :password, :password_confirmation])

View file

@ -23,12 +23,15 @@ defmodule Pleroma.Web.Router do
pipe_through :api pipe_through :api
get "/help/test", TwitterAPI.Controller, :help_test get "/help/test", TwitterAPI.Controller, :help_test
get "/statusnet/config", TwitterAPI.Controller, :config
get "/statuses/public_timeline", TwitterAPI.Controller, :public_timeline get "/statuses/public_timeline", TwitterAPI.Controller, :public_timeline
get "/statuses/public_and_external_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/user_timeline", TwitterAPI.Controller, :user_timeline
get "/statuses/show/:id", TwitterAPI.Controller, :fetch_status get "/statuses/show/:id", TwitterAPI.Controller, :fetch_status
get "/statusnet/conversation/:id", TwitterAPI.Controller, :fetch_conversation get "/statusnet/conversation/:id", TwitterAPI.Controller, :fetch_conversation
get "/statusnet/config", TwitterAPI.Controller, :config
post "/account/register", TwitterAPI.Controller, :register post "/account/register", TwitterAPI.Controller, :register
end end
@ -37,17 +40,25 @@ defmodule Pleroma.Web.Router do
get "/account/verify_credentials", TwitterAPI.Controller, :verify_credentials get "/account/verify_credentials", TwitterAPI.Controller, :verify_credentials
post "/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/home_timeline", TwitterAPI.Controller, :friends_timeline
get "/statuses/friends_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/create", TwitterAPI.Controller, :follow
post "/friendships/destroy", TwitterAPI.Controller, :unfollow post "/friendships/destroy", TwitterAPI.Controller, :unfollow
post "/statusnet/media/upload", TwitterAPI.Controller, :upload post "/statusnet/media/upload", TwitterAPI.Controller, :upload
post "/media/upload", TwitterAPI.Controller, :upload_json post "/media/upload", TwitterAPI.Controller, :upload_json
post "/favorites/create/:id", TwitterAPI.Controller, :favorite post "/favorites/create/:id", TwitterAPI.Controller, :favorite
post "/favorites/create", TwitterAPI.Controller, :favorite post "/favorites/create", TwitterAPI.Controller, :favorite
post "/favorites/destroy/:id", TwitterAPI.Controller, :unfavorite post "/favorites/destroy/:id", TwitterAPI.Controller, :unfavorite
post "/statuses/retweet/:id", TwitterAPI.Controller, :retweet
post "/qvitter/update_avatar", TwitterAPI.Controller, :update_avatar post "/qvitter/update_avatar", TwitterAPI.Controller, :update_avatar
end end
end end

View file

@ -15,6 +15,8 @@ defmodule Pleroma.Web.TwitterAPI.Representers.UserRepresenter do
false false
end end
user_info = User.user_info(user)
map = %{ map = %{
"id" => user.id, "id" => user.id,
"name" => user.name, "name" => user.name,
@ -23,9 +25,9 @@ defmodule Pleroma.Web.TwitterAPI.Representers.UserRepresenter do
"following" => following, "following" => following,
# Fake fields # Fake fields
"favourites_count" => 0, "favourites_count" => 0,
"statuses_count" => 0, "statuses_count" => user_info[:note_count],
"friends_count" => 0, "friends_count" => user_info[:following_count],
"followers_count" => 0, "followers_count" => user_info[:follower_count],
"profile_image_url" => image, "profile_image_url" => image,
"profile_image_url_https" => image, "profile_image_url_https" => image,
"profile_image_url_profile_size" => image, "profile_image_url_profile_size" => image,

View file

@ -85,6 +85,11 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
|> activities_to_statuses(%{for: user}) |> activities_to_statuses(%{for: user})
end 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 def fetch_conversation(user, id) do
query = from activity in Activity, query = from activity in Activity,
where: fragment("? @> ?", activity.data, ^%{ statusnetConversationId: id}), where: fragment("? @> ?", activity.data, ^%{ statusnetConversationId: id}),
@ -313,7 +318,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
actor = get_in(activity.data, ["actor"]) actor = get_in(activity.data, ["actor"])
user = User.get_cached_by_ap_id(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 = 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) User.get_cached_by_ap_id(ap_id)
end) end)
|> Enum.filter(&(&1)) |> Enum.filter(&(&1))

View file

@ -57,6 +57,14 @@ defmodule Pleroma.Web.TwitterAPI.Controller do
end end
end end
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, params) do def follow(%{assigns: %{user: user}} = conn, params) do
case TwitterAPI.follow(user, params) do case TwitterAPI.follow(user, params) do
{ :ok, user, followed, _activity } -> { :ok, user, followed, _activity } ->

View file

@ -19,7 +19,18 @@ defmodule Pleroma.Web.TwitterAPI.Representers.UserRepresenterTest do
assert represented["profile_image_url"] == image assert represented["profile_image_url"] == image
end 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" image = "https://placehold.it/48x48"
represented = %{ represented = %{
@ -29,9 +40,9 @@ defmodule Pleroma.Web.TwitterAPI.Representers.UserRepresenterTest do
"description" => user.bio, "description" => user.bio,
# Fake fields # Fake fields
"favourites_count" => 0, "favourites_count" => 0,
"statuses_count" => 0, "statuses_count" => 1,
"friends_count" => 0, "friends_count" => 1,
"followers_count" => 0, "followers_count" => 2,
"profile_image_url" => image, "profile_image_url" => image,
"profile_image_url_https" => image, "profile_image_url_https" => image,
"profile_image_url_profile_size" => image, "profile_image_url_profile_size" => image,
@ -55,7 +66,7 @@ defmodule Pleroma.Web.TwitterAPI.Representers.UserRepresenterTest do
"favourites_count" => 0, "favourites_count" => 0,
"statuses_count" => 0, "statuses_count" => 0,
"friends_count" => 0, "friends_count" => 0,
"followers_count" => 0, "followers_count" => 1,
"profile_image_url" => image, "profile_image_url" => image,
"profile_image_url_https" => image, "profile_image_url_https" => image,
"profile_image_url_profile_size" => image, "profile_image_url_profile_size" => image,

View file

@ -114,6 +114,27 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
end end
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 describe "GET /statuses/user_timeline.json" do
setup [:valid_user] setup [:valid_user]
test "without any params", %{conn: conn} do test "without any params", %{conn: conn} do
@ -346,7 +367,7 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
end end
defp valid_user(_context) do defp valid_user(_context) do
{ :ok, user } = UserBuilder.insert(%{nickname: "lambda", ap_id: "lambda"}) user = insert(:user)
[user: user] [user: user]
end end

View file

@ -102,6 +102,17 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do
assert Enum.at(statuses, 1) == ActivityRepresenter.to_map(direct_activity, %{user: direct_activity_user, mentioned: [user]}) assert Enum.at(statuses, 1) == ActivityRepresenter.to_map(direct_activity, %{user: direct_activity_user, mentioned: [user]})
end 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 test "get a user by params" do
user1_result = {:ok, user1} = UserBuilder.insert(%{ap_id: "some id", email: "test@pleroma"}) 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"}) {:ok, user2} = UserBuilder.insert(%{ap_id: "some other id", nickname: "testname2", email: "test2@pleroma"})