Refactor follow API
This commit is contained in:
parent
3ff917b5dd
commit
594dd01ab2
2 changed files with 15 additions and 20 deletions
|
@ -101,8 +101,8 @@ def fetch_status(user, id) do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def follow(%User{} = follower, %{ "user_id" => followed_id }) do
|
def follow(%User{} = follower, params) do
|
||||||
with %User{} = followed <- Repo.get(User, followed_id),
|
with %User{} = followed <- get_user(params),
|
||||||
{ :ok, follower } <- User.follow(follower, followed),
|
{ :ok, follower } <- User.follow(follower, followed),
|
||||||
{ :ok, activity } <- ActivityPub.insert(%{
|
{ :ok, activity } <- ActivityPub.insert(%{
|
||||||
"type" => "Follow",
|
"type" => "Follow",
|
||||||
|
@ -115,20 +115,6 @@ def follow(%User{} = follower, %{ "user_id" => followed_id }) do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def follow(%User{} = follower, %{ "screen_name" => followed_name }) do
|
|
||||||
with %User{} = followed <- Repo.get_by(User, nickname: followed_name),
|
|
||||||
{ :ok, follower } <- User.follow(follower, followed),
|
|
||||||
{ :ok, activity } <- ActivityPub.insert(%{
|
|
||||||
"type" => "Follow",
|
|
||||||
"actor" => follower.ap_id,
|
|
||||||
"object" => followed.ap_id,
|
|
||||||
"published" => make_date()
|
|
||||||
})
|
|
||||||
do
|
|
||||||
{ :ok, follower, followed, activity }
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def unfollow(%User{} = follower, followed_id) do
|
def unfollow(%User{} = follower, followed_id) do
|
||||||
with %User{} = followed <- Repo.get(User, followed_id),
|
with %User{} = followed <- Repo.get(User, followed_id),
|
||||||
{ :ok, follower } <- User.unfollow(follower, followed)
|
{ :ok, follower } <- User.unfollow(follower, followed)
|
||||||
|
@ -202,4 +188,13 @@ defp activity_to_status(activity, opts) do
|
||||||
defp make_date do
|
defp make_date do
|
||||||
DateTime.utc_now() |> DateTime.to_iso8601
|
DateTime.utc_now() |> DateTime.to_iso8601
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp get_user(params) do
|
||||||
|
case params do
|
||||||
|
%{ "user_id" => user_id } ->
|
||||||
|
Repo.get(User, user_id)
|
||||||
|
%{ "screen_name" => nickname } ->
|
||||||
|
Repo.get_by(User, nickname: nickname)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -44,18 +44,18 @@ def friends_timeline(%{assigns: %{user: user}} = conn, params) do
|
||||||
end
|
end
|
||||||
|
|
||||||
def follow(%{assigns: %{user: user}} = conn, params) do
|
def follow(%{assigns: %{user: user}} = conn, params) do
|
||||||
{ :ok, _user, follower, _activity } = TwitterAPI.follow(user, params)
|
{ :ok, user, followed, _activity } = TwitterAPI.follow(user, params)
|
||||||
|
|
||||||
response = follower |> UserRepresenter.to_json(%{for: user})
|
response = followed |> UserRepresenter.to_json(%{for: user})
|
||||||
|
|
||||||
conn
|
conn
|
||||||
|> json_reply(200, response)
|
|> json_reply(200, response)
|
||||||
end
|
end
|
||||||
|
|
||||||
def unfollow(%{assigns: %{user: user}} = conn, %{ "user_id" => followed_id }) do
|
def unfollow(%{assigns: %{user: user}} = conn, %{ "user_id" => followed_id }) do
|
||||||
{ :ok, user, follower } = TwitterAPI.unfollow(user, followed_id)
|
{ :ok, user, followed } = TwitterAPI.unfollow(user, followed_id)
|
||||||
|
|
||||||
response = follower |> UserRepresenter.to_json(%{for: user})
|
response = followed |> UserRepresenter.to_json(%{for: user})
|
||||||
|
|
||||||
conn
|
conn
|
||||||
|> json_reply(200, response)
|
|> json_reply(200, response)
|
||||||
|
|
Loading…
Reference in a new issue