Skip to content
This repository was archived by the owner on Nov 8, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/groupher_server/accounts/delegates/upvoted_articles.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ defmodule GroupherServer.Accounts.Delegate.UpvotedArticles do
get paged upvoted articles
"""
def paged_upvoted_articles(user_id, %{thread: thread} = filter) do
thread_upcase = thread |> to_string |> String.upcase()
where_query = dynamic([a], a.user_id == ^user_id and a.thread == ^thread_upcase)
thread = thread |> to_string |> String.upcase()
where_query = dynamic([a], a.user_id == ^user_id and a.thread == ^thread)

load_upvoted_articles(where_query, filter)
end
Expand Down
3 changes: 3 additions & 0 deletions lib/groupher_server/cms/cms.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ defmodule GroupherServer.CMS do
ArticleCURD,
ArticleCommunity,
ArticleEmotion,
CitedContent,
CommentCurd,
ArticleCollect,
ArticleUpvote,
Expand Down Expand Up @@ -96,6 +97,8 @@ defmodule GroupherServer.CMS do
defdelegate sink_article(thread, id), to: ArticleCURD
defdelegate undo_sink_article(thread, id), to: ArticleCURD

defdelegate paged_citing_contents(type, id, filter), to: CitedContent

defdelegate upvote_article(thread, article_id, user), to: ArticleUpvote
defdelegate undo_upvote_article(thread, article_id, user), to: ArticleUpvote

Expand Down
8 changes: 4 additions & 4 deletions lib/groupher_server/cms/delegates/article_collect.ex
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ defmodule GroupherServer.CMS.Delegate.ArticleCollect do
update_article_reaction_user_list(:collect, article, user_id, :add)
end)
|> Multi.run(:create_collect, fn _, _ ->
thread_upcase = thread |> to_string |> String.upcase()
args = Map.put(%{user_id: user_id, thread: thread_upcase}, info.foreign_key, article.id)
thread = thread |> to_string |> String.upcase()
args = Map.put(%{user_id: user_id, thread: thread}, info.foreign_key, article.id)

ORM.create(ArticleCollect, args)
end)
Expand Down Expand Up @@ -131,8 +131,8 @@ defmodule GroupherServer.CMS.Delegate.ArticleCollect do

defp collection_findby_args(thread, article_id, user_id) do
with {:ok, info} <- match(thread) do
thread_upcase = thread |> to_string |> String.upcase()
%{thread: thread_upcase, user_id: user_id} |> Map.put(info.foreign_key, article_id)
thread = thread |> to_string |> String.upcase()
%{thread: thread, user_id: user_id} |> Map.put(info.foreign_key, article_id)
end
end

Expand Down
10 changes: 4 additions & 6 deletions lib/groupher_server/cms/delegates/article_community.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommunity do
defp pack_pin_args(thread, article_id, community_id) do
with {:ok, info} <- match(thread),
{:ok, community} <- ORM.find(Community, community_id) do
thread_upcase = thread |> to_string |> String.upcase()
thread = thread |> to_string |> String.upcase()

Map.put(
%{community_id: community.id, thread: thread_upcase},
%{community_id: community.id, thread: thread},
info.foreign_key,
article_id
)
Expand Down Expand Up @@ -145,12 +145,10 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommunity do

# check if the thread has aready enough pinned articles
defp check_pinned_article_count(community_id, thread) do
thread_upcase = thread |> to_string |> String.upcase()
thread = thread |> to_string |> String.upcase()

query =
from(p in PinnedArticle,
where: p.community_id == ^community_id and p.thread == ^thread_upcase
)
from(p in PinnedArticle, where: p.community_id == ^community_id and p.thread == ^thread)

pinned_articles = query |> Repo.all()

Expand Down
4 changes: 2 additions & 2 deletions lib/groupher_server/cms/delegates/article_upvote.ex
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ defmodule GroupherServer.CMS.Delegate.ArticleUpvote do
Accounts.achieve(%User{id: achiever_id}, :inc, :upvote)
end)
|> Multi.run(:create_upvote, fn _, _ ->
thread_upcase = thread |> to_string |> String.upcase()
args = Map.put(%{user_id: user_id, thread: thread_upcase}, info.foreign_key, article.id)
thread = thread |> to_string |> String.upcase()
args = Map.put(%{user_id: user_id, thread: thread}, info.foreign_key, article.id)

with {:ok, _} <- ORM.create(ArticleUpvote, args) do
ORM.find(info.model, article.id)
Expand Down
86 changes: 86 additions & 0 deletions lib/groupher_server/cms/delegates/cited_content.ex
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 deletions lib/groupher_server_web/resolvers/cms_resolver.ex
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ defmodule GroupherServerWeb.Resolvers.CMS do
CMS.undo_report_article(thread, id, user)
end

def paged_citing_contents(_root, ~m(content id filter)a, _info) do
CMS.paged_citing_contents(content, id, filter)
end

# #######################
# thread reaction ..
# #######################
Expand Down
5 changes: 5 additions & 0 deletions lib/groupher_server_web/schema/cms/cms_metrics.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ defmodule GroupherServerWeb.Schema.CMS.Metrics do
value(:share)
end

enum :content do
article_values()
value(:comment)
end

enum :when_enum do
value(:today)
value(:this_week)
Expand Down
9 changes: 9 additions & 0 deletions lib/groupher_server_web/schema/cms/cms_queries.ex
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,15 @@ defmodule GroupherServerWeb.Schema.CMS.Queries do
resolve(&R.CMS.paged_reports/3)
end

@desc "paged citings list"
field :paged_citing_contents, :paged_citings do
arg(:id, non_null(:id))
arg(:content, :content, default_value: :post)
arg(:filter, :paged_filter)

resolve(&R.CMS.paged_citing_contents/3)
end

@desc "search communities by title"
field :search_communities, :paged_communities do
arg(:title, non_null(:string))
Expand Down
16 changes: 16 additions & 0 deletions lib/groupher_server_web/schema/cms/cms_types.ex
Original file line number Diff line number Diff line change
Expand Up @@ -317,13 +317,29 @@ defmodule GroupherServerWeb.Schema.CMS.Types do
timestamp_fields()
end

object :citing do
field(:id, :id)
field(:thread, :string)
field(:title, :string)
field(:block_linker, list_of(:string))
field(:comment_id, :id)
field(:user, :common_user)

timestamp_fields()
end

paged_article_objects()

object :paged_reports do
field(:entries, list_of(:abuse_report))
pagination_fields()
end

object :paged_citings do
field(:entries, list_of(:citing))
pagination_fields()
end

object :paged_categories do
field(:entries, list_of(:category))
pagination_fields()
Expand Down
16 changes: 15 additions & 1 deletion lib/helper/types.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ defmodule Helper.Types do
company: nil | String.t()
}

@type article_thread :: :post | :job | :repo
@type article_thread :: :post | :job | :repo | :blog

@type paged_filter :: %{
page: Integer.t(),
Expand Down Expand Up @@ -182,4 +182,18 @@ defmodule Helper.Types do
html fragment
"""
@type html :: String.t()

@type cite_info :: %{
id: Integer.t(),
thread: article_thread,
title: String.t(),
inserted_at: String.t(),
block_linker: [String.t()],
comment_id: Integer.t() | nil,
user: %{
login: String.t(),
avatar: String.t(),
nickname: String.t()
}
}
end
60 changes: 57 additions & 3 deletions test/groupher_server/cms/cite_contents/cite_blog_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ defmodule GroupherServer.Test.CMS.CiteContent.Blog do
assert blog.meta.citing_count == 0
end

@tag :wip
test "cited comment itself should not work", ~m(user blog)a do
{:ok, cited_comment} = CMS.create_comment(:blog, blog.id, mock_rich_text("hello"), user)

Expand All @@ -91,7 +90,6 @@ defmodule GroupherServer.Test.CMS.CiteContent.Blog do
assert cited_comment.meta.citing_count == 0
end

@tag :wip
test "can cite blog's comment in blog", ~m(community user blog blog2 blog_attrs)a do
{:ok, comment} = CMS.create_comment(:blog, blog.id, mock_rich_text("hello"), user)

Expand All @@ -111,7 +109,6 @@ defmodule GroupherServer.Test.CMS.CiteContent.Blog do
assert cite_content.cited_by_type == "COMMENT"
end

@tag :wip
test "can cite a comment in a comment", ~m(user blog)a do
{:ok, cited_comment} = CMS.create_comment(:blog, blog.id, mock_rich_text("hello"), user)

Expand Down Expand Up @@ -164,4 +161,61 @@ defmodule GroupherServer.Test.CMS.CiteContent.Blog do
assert blog5.meta.citing_count == 1
end
end

describe "[cite pagi]" do
test "can get paged cited articles.", ~m(user community blog2 blog_attrs)a do
{:ok, comment} =
CMS.create_comment(
:blog,
blog2.id,
mock_comment(~s(the <a href=#{@site_host}/blog/#{blog2.id} />)),
user
)

Process.sleep(1000)

body =
mock_rich_text(
~s(the <a href=#{@site_host}/blog/#{blog2.id} />),
~s(the <a href=#{@site_host}/blog/#{blog2.id} />)
)

blog_attrs = blog_attrs |> Map.merge(%{body: body})
{:ok, blog_x} = CMS.create_article(community, :blog, blog_attrs, user)

Process.sleep(1000)
body = mock_rich_text(~s(the <a href=#{@site_host}/blog/#{blog2.id} />))
blog_attrs = blog_attrs |> Map.merge(%{body: body})
{:ok, blog_y} = CMS.create_article(community, :blog, blog_attrs, user)

CiteTasks.handle(blog_x)
CiteTasks.handle(comment)
CiteTasks.handle(blog_y)

{:ok, result} = CMS.paged_citing_contents("BLOG", blog2.id, %{page: 1, size: 10})

entries = result.entries

result_comment = entries |> List.first()
result_blog_x = entries |> Enum.at(1)
result_blog_y = entries |> List.last()

article_map_keys = [:block_linker, :id, :inserted_at, :thread, :title, :user]

assert result_comment.comment_id == comment.id
assert result_comment.id == blog2.id
assert result_comment.title == blog2.title

assert result_blog_x.id == blog_x.id
assert result_blog_x.block_linker |> length == 2
assert result_blog_x |> Map.keys() == article_map_keys

assert result_blog_y.id == blog_y.id
assert result_blog_y.block_linker |> length == 1
assert result_blog_y |> Map.keys() == article_map_keys

assert result |> is_valid_pagination?(:raw)
assert result.total_count == 3
end
end
end
Loading