This repository was archived by the owner on Nov 8, 2022. It is now read-only.
- Notifications
You must be signed in to change notification settings - Fork20
Paged cited contents workflow#402
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
4 commits Select commitHold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
4 changes: 2 additions & 2 deletionslib/groupher_server/accounts/delegates/upvoted_articles.ex
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletionslib/groupher_server/cms/cms.ex
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
8 changes: 4 additions & 4 deletionslib/groupher_server/cms/delegates/article_collect.ex
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
10 changes: 4 additions & 6 deletionslib/groupher_server/cms/delegates/article_community.ex
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletionslib/groupher_server/cms/delegates/article_upvote.ex
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletionslib/groupher_server/cms/delegates/cited_content.ex
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
defmodule GroupherServer.CMS.Delegate.CitedContent do | ||
@moduledoc """ | ||
CURD operation on post/job ... | ||
""" | ||
import Ecto.Query, warn: false | ||
import Helper.Utils, only: [done: 1, get_config: 2] | ||
import ShortMaps | ||
alias Helper.Types, as: T | ||
alias GroupherServer.{CMS, Repo} | ||
alias Helper.{ORM, QueryBuilder} | ||
alias CMS.Model.CitedContent | ||
@article_threads get_config(:article, :threads) | ||
@article_preloads @article_threads |> Enum.map(&Keyword.new([{&1, [author: :user]}])) | ||
@comment_article_preloads @article_threads |> Enum.map(&Keyword.new([{:comment, &1}])) | ||
@cited_preloads @article_preloads ++ [[comment: :author] ++ @comment_article_preloads] | ||
@doc "get paged citing contents" | ||
def paged_citing_contents(cited_by_type, cited_by_id, %{page: page, size: size} = filter) do | ||
cited_by_type = cited_by_type |> to_string |> String.upcase() | ||
CitedContent | ||
|> where([c], c.cited_by_id == ^cited_by_id and c.cited_by_type == ^cited_by_type) | ||
|> QueryBuilder.filter_pack(Map.merge(filter, %{sort: :asc_inserted})) | ||
|> ORM.paginater(~m(page size)a) | ||
|> extract_contents | ||
|> done | ||
end | ||
def extract_contents(%{entries: entries} = paged_contents) do | ||
entries = entries |> Repo.preload(@cited_preloads) |> Enum.map(&shape_article(&1)) | ||
Map.put(paged_contents, :entries, entries) | ||
end | ||
defp thread_to_atom(thread), do: thread |> String.downcase() |> String.to_atom() | ||
# shape comment cite | ||
@spec shape_article(CitedContent.t()) :: T.cite_info() | ||
defp shape_article(%CitedContent{comment_id: comment_id} = cited) when not is_nil(comment_id) do | ||
%{ | ||
block_linker: block_linker, | ||
cited_by_type: cited_by_type, | ||
comment: comment, | ||
inserted_at: inserted_at | ||
} = cited | ||
comment_thread = comment.thread |> String.downcase() |> String.to_atom() | ||
article = comment |> Map.get(comment_thread) | ||
user = comment.author |> Map.take([:login, :nickname, :avatar]) | ||
article | ||
|> Map.take([:id, :title]) | ||
|> Map.merge(%{ | ||
inserted_at: inserted_at, | ||
user: user, | ||
thread: thread_to_atom(cited_by_type), | ||
comment_id: comment.id, | ||
block_linker: block_linker | ||
}) | ||
end | ||
# shape general article cite | ||
defp shape_article(%CitedContent{} = cited) do | ||
%{block_linker: block_linker, cited_by_type: cited_by_type, inserted_at: inserted_at} = cited | ||
thread = thread_to_atom(cited_by_type) | ||
article = Map.get(cited, thread) | ||
user = get_in(article, [:author, :user]) |> Map.take([:login, :nickname, :avatar]) | ||
article | ||
|> Map.take([:id, :title]) | ||
|> Map.merge(%{ | ||
user: user, | ||
thread: thread, | ||
block_linker: block_linker, | ||
inserted_at: inserted_at | ||
}) | ||
end | ||
end |
4 changes: 4 additions & 0 deletionslib/groupher_server_web/resolvers/cms_resolver.ex
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletionslib/groupher_server_web/schema/cms/cms_metrics.ex
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletionslib/groupher_server_web/schema/cms/cms_queries.ex
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletionslib/groupher_server_web/schema/cms/cms_types.ex
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
16 changes: 15 additions & 1 deletionlib/helper/types.ex
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
60 changes: 57 additions & 3 deletionstest/groupher_server/cms/cite_contents/cite_blog_test.exs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.