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

Commit 1e45b8e

Browse files
author
mydearxym
committed
Merge branch 'dev'
2 parents 6eadf31 + 3f3820f commit 1e45b8e

33 files changed

+225
-209
lines changed

lib/groupher_server/accounts/delegates/fans.ex

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ defmodule GroupherServer.Accounts.Delegate.Fans do
4040
false ->
4141
{:error, [message: "can't follow yourself", code: ecode(:self_conflict)]}
4242

43-
{:error, error} ->
44-
{:error, [message: error, code: ecode(:not_exsit)]}
43+
{:error, reason} ->
44+
{:error, [message: reason, code: ecode(:not_exsit)]}
4545
end
4646
end
4747

@@ -89,8 +89,8 @@ defmodule GroupherServer.Accounts.Delegate.Fans do
8989
false ->
9090
{:error, [message: "can't undo follow yourself", code: ecode(:self_conflict)]}
9191

92-
{:error, error} ->
93-
{:error, [message: error, code: ecode(:not_exsit)]}
92+
{:error, reason} ->
93+
{:error, [message: reason, code: ecode(:not_exsit)]}
9494
end
9595
end
9696

lib/groupher_server/accounts/delegates/favorite_category.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ defmodule GroupherServer.Accounts.Delegate.FavoriteCategory do
149149
{:error, _} ->
150150
case CMS.reaction(thread, :favorite, content_id, user) do
151151
{:ok, _} -> find_content_favorite(thread, content_id, user.id)
152-
{:error, error} -> {:error, error}
152+
{:error, reason} -> {:error, reason}
153153
end
154154
end
155155
end)
@@ -160,7 +160,7 @@ defmodule GroupherServer.Accounts.Delegate.FavoriteCategory do
160160
|> ORM.update(%{total_count: max(old_category.total_count - 1, 0)})
161161
else
162162
true -> {:ok, ""}
163-
error -> {:error, error}
163+
reason -> {:error, reason}
164164
end
165165
end)
166166
|> Multi.run(:update_content_category_id, fn _, %{favorite_content: content_favorite} ->

lib/groupher_server/cms/cms.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ defmodule GroupherServer.CMS do
8484
defdelegate favorited_category(thread, content_id, user), to: FavoritedContents
8585
# ArticleOperation
8686
# >> set flag on article, like: pin / unpin article
87-
defdelegate set_community_flags(queryable, community_id, attrs), to: ArticleOperation
87+
defdelegate set_community_flags(community_info, queryable, attrs), to: ArticleOperation
8888
defdelegate pin_content(queryable, community_id, topic), to: ArticleOperation
8989
defdelegate undo_pin_content(queryable, community_id, topic), to: ArticleOperation
9090
defdelegate pin_content(queryable, community_id), to: ArticleOperation

lib/groupher_server/cms/delegates/article_curd.ex

Lines changed: 110 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -63,60 +63,32 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
6363
{:error, %Ecto.Changeset{}}
6464
6565
"""
66-
def create_content(
67-
%Community{id: community_id},
68-
thread,
69-
attrs,
70-
%User{id: user_id}
71-
) do
72-
with {:ok, author} <- ensure_author_exists(%User{id: user_id}),
66+
def create_content(%Community{id: cid}, thread, attrs, %User{id: uid}) do
67+
with {:ok, author} <- ensure_author_exists(%User{id: uid}),
7368
{:ok, action} <- match_action(thread, :community),
74-
{:ok, community} <- ORM.find(Community, community_id) do
69+
{:ok, community} <- ORM.find(Community, cid) do
7570
Multi.new()
7671
|> Multi.run(:create_content, fn _, _ ->
77-
action.target
78-
|> struct()
79-
|> action.target.changeset(attrs)
80-
|> Ecto.Changeset.put_change(:author_id, author.id)
81-
|> Ecto.Changeset.put_change(:origial_community_id, integerfy(community_id))
82-
|> Repo.insert()
72+
exec_create_content(action.target, attrs, author, community)
8373
end)
8474
|> Multi.run(:set_community, fn _, %{create_content: content} ->
8575
ArticleOperation.set_community(community, thread, content.id)
8676
end)
8777
|> Multi.run(:set_topic, fn _, %{create_content: content} ->
88-
topic_title =
89-
case attrs |> Map.has_key?(:topic) do
90-
true -> attrs.topic
91-
false -> "posts"
92-
end
93-
94-
ArticleOperation.set_topic(%Topic{title: topic_title}, thread, content.id)
78+
exec_set_topic(thread, content.id, attrs)
9579
end)
9680
|> Multi.run(:set_community_flag, fn _, %{create_content: content} ->
97-
# TODO: remove this judge, as content should have a flag
98-
case action |> Map.has_key?(:flag) do
99-
true ->
100-
ArticleOperation.set_community_flags(content, community.id, %{
101-
trash: false
102-
})
103-
104-
false ->
105-
{:ok, :pass}
106-
end
81+
exec_set_community_flag(community, content, action)
10782
end)
10883
|> Multi.run(:set_tag, fn _, %{create_content: content} ->
109-
case attrs |> Map.has_key?(:tags) do
110-
true -> set_tags(thread, content.id, attrs.tags)
111-
false -> {:ok, :pass}
112-
end
84+
exec_set_tag(thread, content.id, attrs)
11385
end)
11486
|> Multi.run(:mention_users, fn _, %{create_content: content} ->
115-
Delivery.mention_from_content(community.raw, thread, content, attrs, %User{id: user_id})
87+
Delivery.mention_from_content(community.raw, thread, content, attrs, %User{id: uid})
11688
{:ok, :pass}
11789
end)
11890
|> Multi.run(:log_action, fn _, _ ->
119-
Statistics.log_publish_action(%User{id: user_id})
91+
Statistics.log_publish_action(%User{id: uid})
12092
end)
12193
|> Repo.transaction()
12294
|> create_content_result()
@@ -132,7 +104,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
132104
ORM.update(content, args)
133105
end)
134106
|> Multi.run(:update_tag, fn _, _ ->
135-
update_tags(content, args.tags)
107+
exec_update_tags(content, args.tags)
136108
end)
137109
|> Repo.transaction()
138110
|> update_content_result()
@@ -158,6 +130,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
158130
end
159131
end
160132

133+
@spec ensure_author_exists(User.t()) :: {:ok, User.t()}
161134
def ensure_author_exists(%User{} = user) do
162135
# unique_constraint: avoid race conditions, make sure user_id unique
163136
# foreign_key_constraint: check foreign key: user_id exsit or not
@@ -368,58 +341,34 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
368341

369342
defp should_add_pin?(_filter), do: {:error, :pass}
370343

344+
defp concat_contents(%{total_count: 0}, normal_contents), do: {:ok, normal_contents}
345+
371346
defp concat_contents(pined_content, normal_contents) do
372-
case pined_content |> Map.get(:total_count) do
373-
0 ->
374-
{:ok, normal_contents}
375-
376-
_ ->
377-
pind_entries =
378-
pined_content
379-
|> Map.get(:entries)
380-
|> Enum.map(&struct(&1, %{pin: true}))
381-
382-
normal_entries = normal_contents |> Map.get(:entries)
383-
384-
# pind_count = pined_content |> Map.get(:total_count)
385-
normal_count = normal_contents |> Map.get(:total_count)
386-
387-
# remote the pined content from normal_entries (if have)
388-
pind_ids = pick_by(pind_entries, :id)
389-
normal_entries = Enum.reject(normal_entries, &(&1.id in pind_ids))
390-
391-
normal_contents
392-
|> Map.put(:entries, pind_entries ++ normal_entries)
393-
# those two are equals
394-
# |> Map.put(:total_count, pind_count + normal_count - pind_count)
395-
|> Map.put(:total_count, normal_count)
396-
|> done
397-
end
398-
end
347+
pind_entries =
348+
pined_content
349+
|> Map.get(:entries)
350+
|> Enum.map(&struct(&1, %{pin: true}))
399351

400-
defp create_content_result({:ok, %{create_content: result}}) do
401-
# Later.exec({__MODULE__, :nofify_admin_new_content, [result]})
402-
nofify_admin_new_content(result)
403-
{:ok, result}
404-
end
352+
normal_entries = normal_contents |> Map.get(:entries)
405353

406-
def nofify_admin_new_content(%{id: id} = result) do
407-
target = result.__struct__
408-
preload = [:origial_community, author: :user]
354+
# pind_count = pined_content |> Map.get(:total_count)
355+
normal_count = normal_contents |> Map.get(:total_count)
409356

410-
with {:ok, content} <- ORM.find(target, id, preload: preload) do
411-
info = %{
412-
id: content.id,
413-
title: content.title,
414-
digest: content.digest,
415-
author_name: content.author.user.nickname,
416-
community_raw: content.origial_community.raw,
417-
type:
418-
result.__struct__ |> to_string |> String.split(".") |> List.last() |> String.downcase()
419-
}
357+
# remote the pined content from normal_entries (if have)
358+
pind_ids = pick_by(pind_entries, :id)
359+
normal_entries = Enum.reject(normal_entries, &(&1.id in pind_ids))
420360

421-
Email.notify_admin(info, :new_content)
422-
end
361+
normal_contents
362+
|> Map.put(:entries, pind_entries ++ normal_entries)
363+
# those two are equals
364+
# |> Map.put(:total_count, pind_count + normal_count - pind_count)
365+
|> Map.put(:total_count, normal_count)
366+
|> done
367+
end
368+
369+
defp create_content_result({:ok, %{create_content: result}}) do
370+
Later.exec({__MODULE__, :notify_admin_new_content, [result]})
371+
{:ok, result}
423372
end
424373

425374
defp create_content_result({:error, :create_content, %Ecto.Changeset{} = result, _steps}) do
@@ -450,10 +399,41 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
450399
{:error, [message: "log action", code: ecode(:create_fails)]}
451400
end
452401

453-
defp set_tags(thread, content_id, tags) do
402+
# except Job, other content will just pass, should use set_tag function instead
403+
# defp exec_update_tags(_, _tags_ids), do: {:ok, :pass}
404+
405+
defp update_content_result({:ok, %{update_content: result}}), do: {:ok, result}
406+
defp update_content_result({:error, :update_content, result, _steps}), do: {:error, result}
407+
defp update_content_result({:error, :update_tag, result, _steps}), do: {:error, result}
408+
409+
defp content_id(:post, id), do: %{post_id: id}
410+
defp content_id(:job, id), do: %{job_id: id}
411+
defp content_id(:repo, id), do: %{repo_id: id}
412+
defp content_id(:video, id), do: %{video_id: id}
413+
414+
# for create content step in Multi.new
415+
defp exec_create_content(target, attrs, %Author{id: aid}, %Community{id: cid}) do
416+
target
417+
|> struct()
418+
|> target.changeset(attrs)
419+
|> Ecto.Changeset.put_change(:author_id, aid)
420+
|> Ecto.Changeset.put_change(:origial_community_id, integerfy(cid))
421+
|> Repo.insert()
422+
end
423+
424+
defp exec_set_topic(thread, id, %{topic: topic}) do
425+
ArticleOperation.set_topic(%Topic{title: topic}, thread, id)
426+
end
427+
428+
# if topic is not provide, use posts as default
429+
defp exec_set_topic(thread, id, _attrs) do
430+
ArticleOperation.set_topic(%Topic{title: "posts"}, thread, id)
431+
end
432+
433+
defp exec_set_tag(thread, id, %{tags: tags}) do
454434
try do
455435
Enum.each(tags, fn tag ->
456-
{:ok, _} = ArticleOperation.set_tag(thread, %Tag{id: tag.id}, content_id)
436+
{:ok, _} = ArticleOperation.set_tag(thread, %Tag{id: tag.id}, id)
457437
end)
458438

459439
{:ok, "psss"}
@@ -462,58 +442,59 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
462442
end
463443
end
464444

465-
defp update_tags(_content, tags_ids) when length(tags_ids) == 0, do: {:ok, :pass}
445+
defp exec_set_tag(_thread, _id, _attrs), do: {:ok, :pass}
466446

467-
# Job is special, the tags in job only represent city, so everytime update
468-
# tags on job content, should be override the old ones, in this way, every
469-
# communiies contains this job will have the same city info
470-
defp update_tags(%CMS.Job{} = content, tags_ids) do
471-
with {:ok, content} <- ORM.find(CMS.Job, content.id, preload: :tags) do
472-
concat_tags(content, tags_ids)
473-
end
474-
end
475-
476-
defp update_tags(%CMS.Post{} = content, tags_ids) do
477-
with {:ok, content} <- ORM.find(CMS.Post, content.id, preload: :tags) do
478-
concat_tags(content, tags_ids)
479-
end
447+
# TODO: flag 逻辑似乎有问题
448+
defp exec_set_community_flag(%Community{} = community, content, %{flag: _flag}) do
449+
ArticleOperation.set_community_flags(community, content, %{
450+
trash: false
451+
})
480452
end
481453

482-
defp update_tags(%CMS.Video{} = content, tags_ids) do
483-
with {:ok, content} <- ORM.find(CMS.Video, content.id, preload: :tags) do
484-
concat_tags(content, tags_ids)
485-
end
454+
defp exec_set_community_flag(_community, _content, _action) do
455+
{:ok, :pass}
486456
end
487457

488-
# except Job, other content will just pass, should use set_tag function instead
489-
defp update_tags(_, _tags_ids), do: {:ok, :pass}
458+
defp exec_update_tags(_content, tags_ids) when length(tags_ids) == 0, do: {:ok, :pass}
490459

491-
defp concat_tags(content, tags_ids) do
492-
tags =
493-
Enum.reduce(tags_ids, [], fn t, acc ->
494-
{:ok, tag} = ORM.find(Tag, t.id)
460+
defp exec_update_tags(content, tags_ids) do
461+
with {:ok, content} <- ORM.find(content.__struct__, content.id, preload: :tags) do
462+
tags =
463+
Enum.reduce(tags_ids, [], fn t, acc ->
464+
{:ok, tag} = ORM.find(Tag, t.id)
495465

496-
case tag.title == "refined" do
497-
true ->
498-
acc
466+
case tag.title == "refined" do
467+
true ->
468+
acc
499469

500-
false ->
501-
acc ++ [tag]
502-
end
503-
end)
470+
false ->
471+
acc ++ [tag]
472+
end
473+
end)
504474

505-
content
506-
|> Ecto.Changeset.change()
507-
|> Ecto.Changeset.put_assoc(:tags, tags)
508-
|> Repo.update()
475+
content
476+
|> Ecto.Changeset.change()
477+
|> Ecto.Changeset.put_assoc(:tags, tags)
478+
|> Repo.update()
479+
end
509480
end
510481

511-
defp update_content_result({:ok, %{update_content: result}}), do: {:ok, result}
512-
defp update_content_result({:error, :update_content, result, _steps}), do: {:error, result}
513-
defp update_content_result({:error, :update_tag, result, _steps}), do: {:error, result}
482+
defp notify_admin_new_content(%{id: id} = result) do
483+
target = result.__struct__
484+
preload = [:origial_community, author: :user]
514485

515-
defp content_id(:post, id), do: %{post_id: id}
516-
defp content_id(:job, id), do: %{job_id: id}
517-
defp content_id(:repo, id), do: %{repo_id: id}
518-
defp content_id(:video, id), do: %{video_id: id}
486+
with {:ok, content} <- ORM.find(target, id, preload: preload) do
487+
info = %{
488+
id: content.id,
489+
title: content.title,
490+
digest: Map.get(content, :digest, content.title),
491+
author_name: content.author.user.nickname,
492+
community_raw: content.origial_community.raw,
493+
type:
494+
result.__struct__ |> to_string |> String.split(".") |> List.last() |> String.downcase()
495+
}
496+
497+
Email.notify_admin(info, :new_content)
498+
end
499+
end
519500
end

lib/groupher_server/cms/delegates/article_operation.ex

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,14 @@ defmodule GroupherServer.CMS.Delegate.ArticleOperation do
9797
@doc """
9898
trash / untrash articles
9999
"""
100-
def set_community_flags(content, community_id, attrs) do
100+
def set_community_flags(%Community{id: cid}, content, attrs) do
101+
with {:ok, content} <- ORM.find(content.__struct__, content.id),
102+
{:ok, record} <- insert_flag_record(content, cid, attrs) do
103+
{:ok, struct(content, %{trash: record.trash})}
104+
end
105+
end
106+
107+
def set_community_flags(community_id, content, attrs) do
101108
with {:ok, content} <- ORM.find(content.__struct__, content.id),
102109
{:ok, community} <- ORM.find(Community, community_id),
103110
{:ok, record} <- insert_flag_record(content, community.id, attrs) do

0 commit comments

Comments
 (0)