Skip to content
This repository was archived by the owner on Nov 8, 2022. It is now read-only.

Commit bdc1ce8

Browse files
committed
chore: merge branch 'dev'
2 parents db67f50 + 8807413 commit bdc1ce8

File tree

13 files changed

+238
-57
lines changed

13 files changed

+238
-57
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# migrate old social fields in account table to user_socials table
2+
alias Helper.ORM
3+
4+
alias MastaniServer.Accounts.{User, Social}
5+
alias Helper.Patch.SocialMigrater
6+
7+
defmodule Helper.Patch.SocialMigrater do
8+
def insert_social_records(id, map) when map_size(map) == 0, do: IO.puts("pass robot user")
9+
10+
def insert_social_records(id, attrs) do
11+
IO.inspect(id, label: "user id")
12+
IO.inspect(attrs, label: "attrs ")
13+
14+
Social |> ORM.upsert_by([user_id: id], attrs)
15+
end
16+
end
17+
18+
filter = %{page: 1, size: 320}
19+
{:ok, accounts} = User |> ORM.find_all(filter)
20+
21+
social_keys = [
22+
:qq,
23+
:weibo,
24+
:weichat,
25+
:github,
26+
:zhihu,
27+
:douban,
28+
:twitter,
29+
:facebook,
30+
:dribble,
31+
:instagram,
32+
:pinterest,
33+
:huaban
34+
]
35+
36+
Enum.each(accounts.entries, fn user ->
37+
social_attrs =
38+
user
39+
|> Map.take(social_keys)
40+
|> Enum.reject(fn {k, v} -> is_nil(v) end)
41+
|> Map.new()
42+
43+
SocialMigrater.insert_social_records(user.id, social_attrs)
44+
end)

lib/mastani_server/accounts/delegates/profile.ex

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ defmodule MastaniServer.Accounts.Delegate.Profile do
88

99
alias MastaniServer.Accounts
1010
alias Helper.{Guardian, ORM, QueryBuilder, RadarSearch}
11-
alias MastaniServer.Accounts.{Achievement, GithubUser, User}
11+
alias MastaniServer.Accounts.{Achievement, GithubUser, User, Social}
1212
alias MastaniServer.{CMS, Repo}
1313

1414
alias Ecto.Multi
@@ -23,25 +23,11 @@ defmodule MastaniServer.Accounts.Delegate.Profile do
2323
user
2424
|> Ecto.Changeset.change(attrs)
2525

26-
changeset =
27-
cond do
28-
Map.has_key?(attrs, :education_backgrounds) ->
29-
changeset
30-
|> Ecto.Changeset.put_embed(:education_backgrounds, attrs.education_backgrounds)
31-
32-
Map.has_key?(attrs, :work_backgrounds) ->
33-
changeset
34-
|> Ecto.Changeset.put_embed(:work_backgrounds, attrs.work_backgrounds)
35-
36-
Map.has_key?(attrs, :other_embeds) ->
37-
changeset
38-
|> Ecto.Changeset.put_embed(:other_embeds, attrs.other_embeds)
39-
40-
true ->
41-
changeset
42-
end
43-
44-
changeset |> User.update_changeset(attrs) |> Repo.update()
26+
changeset
27+
|> update_social_ifneed(user, attrs)
28+
|> embed_background_ifneed(changeset)
29+
|> User.update_changeset(attrs)
30+
|> Repo.update()
4531
end
4632

4733
@doc """
@@ -153,6 +139,9 @@ defmodule MastaniServer.Accounts.Delegate.Profile do
153139
|> Multi.run(:create_profile, fn _, %{create_user: user} ->
154140
create_profile(user, github_profile, :github)
155141
end)
142+
|> Multi.run(:update_profile_social, fn _, %{create_user: user} ->
143+
update_profile_social(user, github_profile, :github)
144+
end)
156145
|> Multi.run(:init_achievement, fn _, %{create_user: user} ->
157146
Achievement |> ORM.upsert_by([user_id: user.id], %{user_id: user.id})
158147
end)
@@ -171,6 +160,9 @@ defmodule MastaniServer.Accounts.Delegate.Profile do
171160
defp register_github_result({:error, :create_profile, _result, _steps}),
172161
do: {:error, "Accounts create_profile internal error"}
173162

163+
defp register_github_result({:error, :update_profile_social, _result, _steps}),
164+
do: {:error, "Accounts update_profile_social error"}
165+
174166
defp gen_token(%User{} = user) do
175167
with {:ok, token, _info} <- Guardian.jwt_encode(user) do
176168
{:ok, %{token: token, user: user}}
@@ -181,7 +173,6 @@ defmodule MastaniServer.Accounts.Delegate.Profile do
181173
attrs = %{
182174
login: String.downcase(profile["login"]),
183175
nickname: profile["login"],
184-
github: "https://github.com/#{profile["login"]}",
185176
avatar: profile["avatar_url"],
186177
bio: profile["bio"],
187178
location: profile["location"],
@@ -203,6 +194,14 @@ defmodule MastaniServer.Accounts.Delegate.Profile do
203194
Repo.insert(changeset)
204195
end
205196

197+
def update_profile_social(user, profile, :github) do
198+
update_social_ifneed(user, %{
199+
social: %{
200+
github: "https://github.com/#{profile["login"]}"
201+
}
202+
})
203+
end
204+
206205
defp create_profile(user, github_profile, :github) do
207206
# attrs = github_user |> Map.merge(%{github_id: github_user.id, user_id: 1}) |> Map.delete(:id)
208207
attrs =
@@ -215,4 +214,31 @@ defmodule MastaniServer.Accounts.Delegate.Profile do
215214
|> GithubUser.changeset(attrs)
216215
|> Repo.insert()
217216
end
217+
218+
defp update_social_ifneed(%User{} = user, %{social: attrs}) do
219+
attrs = Map.merge(%{user_id: user.id}, attrs)
220+
Social |> ORM.upsert_by([user_id: user.id], attrs)
221+
end
222+
223+
defp update_social_ifneed(changeset, %User{} = user, %{social: attrs}) do
224+
Social |> ORM.upsert_by([user_id: user.id], attrs)
225+
changeset
226+
end
227+
228+
defp update_social_ifneed(changeset, _user, _attrs), do: changeset
229+
230+
defp embed_background_ifneed(%Ecto.Changeset{} = changeset, attrs) do
231+
cond do
232+
Map.has_key?(attrs, :education_backgrounds) ->
233+
changeset
234+
|> Ecto.Changeset.put_embed(:education_backgrounds, attrs.education_backgrounds)
235+
236+
Map.has_key?(attrs, :work_backgrounds) ->
237+
changeset
238+
|> Ecto.Changeset.put_embed(:work_backgrounds, attrs.work_backgrounds)
239+
240+
true ->
241+
changeset
242+
end
243+
end
218244
end
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
defmodule MastaniServer.Accounts.Social do
2+
@moduledoc false
3+
alias __MODULE__
4+
5+
use Ecto.Schema
6+
import Ecto.Changeset
7+
8+
alias MastaniServer.Accounts.User
9+
10+
@required_fields ~w(user_id)a
11+
@optional_fields ~w(github twitter facebook zhihu dribble huaban douban pinterest instagram qq weichat weibo)a
12+
13+
@type t :: %Social{}
14+
schema "user_socials" do
15+
belongs_to(:user, User)
16+
17+
field(:github, :string)
18+
field(:twitter, :string)
19+
field(:facebook, :string)
20+
field(:zhihu, :string)
21+
field(:dribble, :string)
22+
field(:huaban, :string)
23+
field(:douban, :string)
24+
25+
field(:pinterest, :string)
26+
field(:instagram, :string)
27+
28+
field(:qq, :string)
29+
field(:weichat, :string)
30+
field(:weibo, :string)
31+
32+
# timestamps(type: :utc_datetime)
33+
end
34+
35+
@doc false
36+
def changeset(%Social{} = social, attrs) do
37+
social
38+
|> cast(attrs, @optional_fields ++ @required_fields)
39+
|> validate_required(@required_fields)
40+
|> foreign_key_constraint(:user_id)
41+
end
42+
43+
@doc false
44+
def update_changeset(%Social{} = social, attrs) do
45+
social
46+
|> cast(attrs, @optional_fields ++ @required_fields)
47+
|> foreign_key_constraint(:user_id)
48+
end
49+
end

lib/mastani_server/accounts/user.ex

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@ defmodule MastaniServer.Accounts.User do
1616
Purchase,
1717
UserFollower,
1818
UserFollowing,
19-
WorkBackground
19+
WorkBackground,
20+
Social
2021
}
2122

2223
alias MastaniServer.CMS
2324

2425
@required_fields ~w(nickname avatar)a
25-
@optional_fields ~w(login nickname bio remote_ip sex location douban dribble email facebook pinterest pinterest github huaban qq weibo weichat twitter zhihu)a
26+
@optional_fields ~w(login nickname bio remote_ip sex location email)a
2627

2728
@type t :: %User{}
2829
schema "users" do
@@ -39,11 +40,12 @@ defmodule MastaniServer.Accounts.User do
3940

4041
field(:views, :integer, default: 0)
4142

42-
sscial_fields()
43-
4443
embeds_many(:education_backgrounds, EducationBackground)
4544
embeds_many(:work_backgrounds, WorkBackground)
4645

46+
has_one(:social, Social)
47+
social_fields()
48+
4749
has_one(:achievement, Achievement)
4850
has_one(:github_profile, GithubUser)
4951
has_one(:cms_passport, CMS.Passport)
@@ -95,8 +97,9 @@ defmodule MastaniServer.Accounts.User do
9597
|> validate_inclusion(:sex, ["dude", "girl"])
9698
|> validate_format(:email, ~r/@/)
9799
|> validate_length(:location, min: 2, max: 30)
98-
|> validate_length(:qq, min: 8, max: 15)
99-
|> validate_length(:weichat, min: 3, max: 30)
100-
|> validate_length(:weibo, min: 3, max: 30)
100+
101+
# |> validate_length(:qq, min: 8, max: 15)
102+
# |> validate_length(:weichat, min: 3, max: 30)
103+
# |> validate_length(:weibo, min: 3, max: 30)
101104
end
102105
end

lib/mastani_server_web/resolvers/accounts_resolver.ex

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ defmodule MastaniServerWeb.Resolvers.Accounts do
4646
do: Map.merge(profile, %{work_backgrounds: args.work_backgrounds}),
4747
else: profile
4848

49+
profile =
50+
if Map.has_key?(args, :social),
51+
do: Map.merge(profile, %{social: args.social}),
52+
else: profile
53+
4954
Accounts.update_profile(%User{id: cur_user.id}, profile)
5055
end
5156

lib/mastani_server_web/schema/account/account_misc.ex

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,10 @@ defmodule MastaniServerWeb.Schema.Account.Misc do
4848
field(:sex, :string)
4949
field(:location, :string)
5050
field(:email, :string)
51-
# social
52-
sscial_fields()
51+
end
52+
53+
input_object :social_input do
54+
social_fields()
5355
end
5456

5557
enum :cus_banner_layout_num do

lib/mastani_server_web/schema/account/account_mutations.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ defmodule MastaniServerWeb.Schema.Account.Mutations do
1818
@desc "update user's profile"
1919
field :update_profile, :user do
2020
arg(:profile, non_null(:user_profile_input))
21+
arg(:social, :social_input)
2122
arg(:work_backgrounds, list_of(:work_background_input))
2223
arg(:education_backgrounds, list_of(:edu_background_input))
2324

lib/mastani_server_web/schema/account/account_types.ex

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,6 @@ defmodule MastaniServerWeb.Schema.Account.Types do
1414
field(:is_valid, :boolean)
1515
end
1616

17-
object :education_background do
18-
field(:school, :string)
19-
field(:major, :string)
20-
end
21-
22-
object :work_background do
23-
field(:company, :string)
24-
field(:title, :string)
25-
end
26-
2717
object :user do
2818
meta(:cache, max_age: 30)
2919
field(:id, :id)
@@ -38,7 +28,8 @@ defmodule MastaniServerWeb.Schema.Account.Types do
3828

3929
field(:views, :integer)
4030

41-
sscial_fields()
31+
social_fields()
32+
field(:social, :social_map, resolve: dataloader(Accounts, :social))
4233

4334
field(:inserted_at, :datetime)
4435
field(:updated_at, :datetime)
@@ -326,6 +317,20 @@ defmodule MastaniServerWeb.Schema.Account.Types do
326317
field(:public_gists, :integer)
327318
end
328319

320+
object :education_background do
321+
field(:school, :string)
322+
field(:major, :string)
323+
end
324+
325+
object :work_background do
326+
field(:company, :string)
327+
field(:title, :string)
328+
end
329+
330+
object :social_map do
331+
social_fields()
332+
end
333+
329334
object :favorites_category do
330335
field(:id, :id)
331336
field(:title, :string)

lib/mastani_server_web/schema/utils/helper.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ defmodule MastaniServerWeb.Schema.Utils.Helper do
4040
end
4141
end
4242

43-
defmacro sscial_fields do
43+
defmacro social_fields do
4444
quote do
4545
field(:qq, :string)
4646
field(:weibo, :string)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
defmodule MastaniServer.Repo.Migrations.AddUserSocials do
2+
use Ecto.Migration
3+
4+
def change do
5+
create table(:user_socials) do
6+
add(:user_id, references(:users, on_delete: :delete_all), null: false)
7+
8+
add(:github, :string)
9+
add(:twitter, :string)
10+
add(:facebook, :string)
11+
add(:zhihu, :string)
12+
add(:dribble, :string)
13+
add(:huaban, :string)
14+
add(:douban, :string)
15+
16+
add(:pinterest, :string)
17+
add(:instagram, :string)
18+
19+
add(:qq, :string)
20+
add(:weichat, :string)
21+
add(:weibo, :string)
22+
end
23+
24+
create(unique_index(:user_socials, [:user_id]))
25+
end
26+
end

0 commit comments

Comments
 (0)