Commit d407f662 authored by lain's avatar lain
Browse files

Fix TwitterApiController.

parent a6812bc9
...@@ -114,6 +114,15 @@ def user_upgrade(info, params) do ...@@ -114,6 +114,15 @@ def user_upgrade(info, params) do
]) ])
end end
def profile_update(info, params) do
info
|> cast(params, [
:locked,
:no_rich_text,
:default_scope
])
end
def set_source_data(info, source_data) do def set_source_data(info, source_data) do
params = %{source_data: source_data} params = %{source_data: source_data}
......
...@@ -437,67 +437,52 @@ def raw_empty_array(conn, _params) do ...@@ -437,67 +437,52 @@ def raw_empty_array(conn, _params) do
json(conn, []) json(conn, [])
end end
def update_profile(%{assigns: %{user: user}} = conn, params) do defp build_info_cng(user, params) do
params = info_params =
if bio = params["description"] do ["no_rich_text", "locked"]
mentions = Formatter.parse_mentions(bio) |> Enum.reduce(%{}, fn key, res ->
tags = Formatter.parse_tags(bio) if value = params[key] do
Map.put(res, key, value == "true")
emoji =
(user.info["source_data"]["tag"] || [])
|> Enum.filter(fn %{"type" => t} -> t == "Emoji" end)
|> Enum.map(fn %{"icon" => %{"url" => url}, "name" => name} ->
{String.trim(name, ":"), url}
end)
bio_html = CommonUtils.format_input(bio, mentions, tags, "text/plain")
Map.put(params, "bio", bio_html |> Formatter.emojify(emoji))
else
params
end
user =
if locked = params["locked"] do
with locked <- locked == "true",
new_info <- Map.put(user.info, "locked", locked),
change <- User.info_changeset(user, %{info: new_info}),
{:ok, user} <- User.update_and_set_cache(change) do
user
else else
_e -> user res
end end
else end)
user
end
user = info_params =
if no_rich_text = params["no_rich_text"] do if value = params["default_scope"] do
with no_rich_text <- no_rich_text == "true", Map.put(info_params, "default_scope", value)
new_info <- Map.put(user.info, "no_rich_text", no_rich_text),
change <- User.info_changeset(user, %{info: new_info}),
{:ok, user} <- User.update_and_set_cache(change) do
user
else
_e -> user
end
else else
user info_params
end end
user = User.Info.profile_update(user.info, info_params)
if default_scope = params["default_scope"] do end
with new_info <- Map.put(user.info, "default_scope", default_scope),
change <- User.info_changeset(user, %{info: new_info}), defp add_profile_emoji(user, params) do
{:ok, user} <- User.update_and_set_cache(change) do if bio = params["description"] do
user mentions = Formatter.parse_mentions(bio)
else tags = Formatter.parse_tags(bio)
_e -> user
end emoji =
else (user.info.source_data["tag"] || [])
user |> Enum.filter(fn %{"type" => t} -> t == "Emoji" end)
end |> Enum.map(fn %{"icon" => %{"url" => url}, "name" => name} ->
{String.trim(name, ":"), url}
end)
bio_html = CommonUtils.format_input(bio, mentions, tags, "text/plain")
Map.put(params, "bio", bio_html |> Formatter.emojify(emoji))
else
params
end
end
def update_profile(%{assigns: %{user: user}} = conn, params) do
params = add_profile_emoji(user, params)
info_cng = build_info_cng(user, params)
with changeset <- User.update_changeset(user, params), with changeset <- User.update_changeset(user, params),
changeset <- Ecto.Changeset.put_embed(changeset, :info, info_cng),
{:ok, user} <- User.update_and_set_cache(changeset) do {:ok, user} <- User.update_and_set_cache(changeset) do
CommonAPI.update(user) CommonAPI.update(user)
render(conn, UserView, "user.json", %{user: user, for: user}) render(conn, UserView, "user.json", %{user: user, for: user})
......
...@@ -67,7 +67,7 @@ test "with credentials", %{conn: conn, user: user} do ...@@ -67,7 +67,7 @@ test "with credentials", %{conn: conn, user: user} do
describe "GET /statuses/public_timeline.json" do describe "GET /statuses/public_timeline.json" do
test "returns statuses", %{conn: conn} do test "returns statuses", %{conn: conn} do
{:ok, user} = UserBuilder.insert() user = insert(:user)
activities = ActivityBuilder.insert_list(30, %{}, %{user: user}) activities = ActivityBuilder.insert_list(30, %{}, %{user: user})
ActivityBuilder.insert_list(10, %{}, %{user: user}) ActivityBuilder.insert_list(10, %{}, %{user: user})
since_id = List.last(activities).id since_id = List.last(activities).id
...@@ -571,7 +571,7 @@ test "with credentials", %{conn: conn, user: current_user} do ...@@ -571,7 +571,7 @@ test "with credentials", %{conn: conn, user: current_user} do
|> post("/api/blocks/destroy.json", %{user_id: blocked.id}) |> post("/api/blocks/destroy.json", %{user_id: blocked.id})
current_user = Repo.get(User, current_user.id) current_user = Repo.get(User, current_user.id)
assert current_user.info["blocks"] == [] assert current_user.info.blocks == []
assert json_response(conn, 200) == assert json_response(conn, 200) ==
UserView.render("show.json", %{user: blocked, for: current_user}) UserView.render("show.json", %{user: blocked, for: current_user})
...@@ -946,7 +946,7 @@ test "it locks an account", %{conn: conn} do ...@@ -946,7 +946,7 @@ test "it locks an account", %{conn: conn} do
}) })
user = Repo.get!(User, user.id) user = Repo.get!(User, user.id)
assert user.info["locked"] == true assert user.info.locked == true
assert json_response(conn, 200) == UserView.render("user.json", %{user: user, for: user}) assert json_response(conn, 200) == UserView.render("user.json", %{user: user, for: user})
end end
...@@ -962,7 +962,7 @@ test "it unlocks an account", %{conn: conn} do ...@@ -962,7 +962,7 @@ test "it unlocks an account", %{conn: conn} do
}) })
user = Repo.get!(User, user.id) user = Repo.get!(User, user.id)
assert user.info["locked"] == false assert user.info.locked == false
assert json_response(conn, 200) == UserView.render("user.json", %{user: user, for: user}) assert json_response(conn, 200) == UserView.render("user.json", %{user: user, for: user})
end end
...@@ -1136,7 +1136,7 @@ test "it lists friend requests" do ...@@ -1136,7 +1136,7 @@ test "it lists friend requests" do
user = insert(:user, %{info: %{"locked" => true}}) user = insert(:user, %{info: %{"locked" => true}})
other_user = insert(:user) other_user = insert(:user)
{:ok, activity} = ActivityPub.follow(other_user, user) {:ok, _activity} = ActivityPub.follow(other_user, user)
user = Repo.get(User, user.id) user = Repo.get(User, user.id)
other_user = Repo.get(User, other_user.id) other_user = Repo.get(User, other_user.id)
...@@ -1158,7 +1158,7 @@ test "it approves a friend request" do ...@@ -1158,7 +1158,7 @@ test "it approves a friend request" do
user = insert(:user, %{info: %{"locked" => true}}) user = insert(:user, %{info: %{"locked" => true}})
other_user = insert(:user) other_user = insert(:user)
{:ok, activity} = ActivityPub.follow(other_user, user) {:ok, _activity} = ActivityPub.follow(other_user, user)
user = Repo.get(User, user.id) user = Repo.get(User, user.id)
other_user = Repo.get(User, other_user.id) other_user = Repo.get(User, other_user.id)
...@@ -1181,7 +1181,7 @@ test "it denies a friend request" do ...@@ -1181,7 +1181,7 @@ test "it denies a friend request" do
user = insert(:user, %{info: %{"locked" => true}}) user = insert(:user, %{info: %{"locked" => true}})
other_user = insert(:user) other_user = insert(:user)
{:ok, activity} = ActivityPub.follow(other_user, user) {:ok, _activity} = ActivityPub.follow(other_user, user)
user = Repo.get(User, user.id) user = Repo.get(User, user.id)
other_user = Repo.get(User, other_user.id) other_user = Repo.get(User, other_user.id)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment