Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Nov 8, 2022. It is now read-only.

refactor(pined-article): use common table#331

Merged
mydearxym merged 8 commits intodevfromrefactor-pined-article
Apr 29, 2021
Merged
Show file tree
Hide file tree
Changes fromall 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 deletionslib/groupher_server/cms/article_comment.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -20,7 +20,7 @@ defmodule GroupherServer.CMS.ArticleComment do

@required_fields ~w(body_html author_id)a
@optional_fields ~w(post_id job_id reply_to_id replies_count is_folded is_reported is_deleted floor is_article_author)a
@updatable_fields ~w(is_folded is_reported is_deleted floor upvotes_countis_pined)a
@updatable_fields ~w(is_folded is_reported is_deleted floor upvotes_countis_pinned)a

@max_participator_count 5
@max_parent_replies_count 3
Expand DownExpand Up@@ -66,7 +66,7 @@ defmodule GroupherServer.CMS.ArticleComment do
field(:upvotes_count, :integer, default: 0)

# 是否置顶
field(:is_pined, :boolean, default: false)
field(:is_pinned, :boolean, default: false)

belongs_to(:author, Accounts.User, foreign_key: :author_id)
belongs_to(:post, Post, foreign_key: :post_id)
Expand Down
4 changes: 2 additions & 2 deletionslib/groupher_server/cms/cms.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -86,8 +86,8 @@ defmodule GroupherServer.CMS do
# ArticleOperation
# >> set flag on article, like: pin / unpin article
defdelegate set_community_flags(community_info, queryable, attrs), to: ArticleOperation
defdelegatepin_content(queryable, community_id), to: ArticleOperation
defdelegateundo_pin_content(queryable, community_id), to: ArticleOperation
defdelegatepin_article(thread, id, community_id), to: ArticleOperation
defdelegateundo_pin_article(thread, id, community_id), to: ArticleOperation

defdelegate lock_article_comment(content), to: ArticleOperation

Expand Down
4 changes: 4 additions & 0 deletionslib/groupher_server/cms/community.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -19,10 +19,14 @@ defmodule GroupherServer.CMS.Community do
CommunityCheatsheet
}

@max_pinned_article_count_per_thread 2

@required_fields ~w(title desc user_id logo raw)a
# @required_fields ~w(title desc user_id)a
@optional_fields ~w(label geo_info index aka)a

def max_pinned_article_count_per_thread, do: @max_pinned_article_count_per_thread

schema "communities" do
field(:title, :string)
field(:aka, :string)
Expand Down
12 changes: 6 additions & 6 deletionslib/groupher_server/cms/delegates/article_comment.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -46,7 +46,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
def list_article_comments(thread, article_id, filters, mode, user \\ nil)

def list_article_comments(thread, article_id, filters, :timeline, user) do
where_query = dynamic([c], not c.is_folded and not c.is_reported and not c.is_pined)
where_query = dynamic([c], not c.is_folded and not c.is_reported and not c.is_pinned)
do_list_article_comment(thread, article_id, filters, where_query, user)
end

Expand All@@ -57,19 +57,19 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
where_query =
dynamic(
[c],
is_nil(c.reply_to_id) and not c.is_folded and not c.is_reported and not c.is_pined
is_nil(c.reply_to_id) and not c.is_folded and not c.is_reported and not c.is_pinned
)

do_list_article_comment(thread, article_id, filters, where_query, user)
end

def list_folded_article_comments(thread, article_id, filters) do
where_query = dynamic([c], c.is_folded and not c.is_reported and not c.is_pined)
where_query = dynamic([c], c.is_folded and not c.is_reported and not c.is_pinned)
do_list_article_comment(thread, article_id, filters, where_query, nil)
end

def list_folded_article_comments(thread, article_id, filters, user) do
where_query = dynamic([c], c.is_folded and not c.is_reported and not c.is_pined)
where_query = dynamic([c], c.is_folded and not c.is_reported and not c.is_pinned)
do_list_article_comment(thread, article_id, filters, where_query, user)
end

Expand DownExpand Up@@ -129,7 +129,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
end
end)
|> Multi.run(:update_comment_flag, fn _, _ ->
ORM.update(comment, %{is_pined: true})
ORM.update(comment, %{is_pinned: true})
end)
|> Multi.run(:add_pined_comment, fn _, _ ->
ArticlePinedComment
Expand All@@ -147,7 +147,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
with {:ok, comment} <- ORM.find(ArticleComment, comment_id) do
Multi.new()
|> Multi.run(:update_comment_flag, fn _, _ ->
ORM.update(comment, %{is_pined: false})
ORM.update(comment, %{is_pinned: false})
end)
|> Multi.run(:remove_pined_comment, fn _, _ ->
ORM.findby_delete(ArticlePinedComment, %{article_comment_id: comment.id})
Expand Down
55 changes: 11 additions & 44 deletionslib/groupher_server/cms/delegates/article_curd.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -3,15 +3,18 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
CURD operation on post/job ...
"""
import Ecto.Query, warn: false
import GroupherServer.CMS.Utils.Matcher

import GroupherServer.CMS.Utils.Matcher2

import GroupherServer.CMS.Utils.Matcher, only: [match_action: 2, dynamic_where: 2]
import Helper.Utils, only: [done: 1, pick_by: 2, integerfy: 1]
import Helper.ErrorCode
import ShortMaps

alias GroupherServer.{Accounts, CMS, Delivery, Email, Repo, Statistics}

alias Accounts.User
alias CMS.{Author, Community, Embeds, Delegate, Tag}
alias CMS.{Author, Community,PinnedArticle,Embeds, Delegate, Tag}

alias Delegate.ArticleOperation
alias Helper.{Later, ORM, QueryBuilder}
Expand DownExpand Up@@ -271,28 +274,22 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
end

defp read_state_query(queryable, %{read: false} = _filter, _user) do
# queryable
# |> join(:left, [content, f, c], viewers in assoc(content, :viewers))
# |> where([content, f, c, viewers], viewers.user_id != ^user.id)
# |> where([content, f, c, viewers], content.id != viewers.post_id)
# |> IO.inspect(label: "query")
queryable
end

defp read_state_query(queryable, _, _), do: queryable

# only first page need pin contents
# TODO: use seperate pined table, which is much more smaller
defp add_pin_contents_ifneed(contents, CMS.Post, %{community: community} = filter) do
defp add_pin_contents_ifneed(contents, querable, %{community: _community} = filter) do
with {:ok, _} <- should_add_pin?(filter),
{:ok, info} <- match(querable),
{:ok, normal_contents} <- contents,
true <- Map.has_key?(filter, :community),
true <- 1 == Map.get(normal_contents, :page_number) do
{:ok, pined_content} =
CMS.PinedPost
PinnedArticle
|> join(:inner, [p], c in assoc(p, :community))
|> join(:inner, [p], content in assoc(p,:post))
|> where([p, c, content], c.raw == ^community)
|> join(:inner, [p], content in assoc(p,^info.thread))
|> where([p, c, content], c.raw == ^filter.community)
|> select([p, c, content], content)
# 10 pined contents per community/thread, at most
|> ORM.paginater(%{page: 1, size: 10})
Expand All@@ -305,38 +302,8 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
end
end

defp add_pin_contents_ifneed(contents, CMS.Job, %{community: _community} = filter) do
merge_pin_contents(contents, :job, CMS.PinedJob, filter)
end

defp add_pin_contents_ifneed(contents, CMS.Repo, %{community: _community} = filter) do
merge_pin_contents(contents, :repo, CMS.PinedRepo, filter)
end

defp add_pin_contents_ifneed(contents, _querable, _filter), do: contents

defp merge_pin_contents(contents, thread, pin_schema, %{community: _community} = filter) do
with {:ok, _} <- should_add_pin?(filter),
{:ok, normal_contents} <- contents,
true <- Map.has_key?(filter, :community),
true <- 1 == Map.get(normal_contents, :page_number) do
{:ok, pined_content} =
pin_schema
|> join(:inner, [p], c in assoc(p, :community))
|> join(:inner, [p], content in assoc(p, ^thread))
|> where([p, c, content], c.raw == ^filter.community)
|> select([p, c, content], content)
# 10 pined contents per community/thread, at most
|> ORM.paginater(%{page: 1, size: 10})
|> done()

concat_contents(pined_content, normal_contents)
else
_error ->
contents
end
end

# if filter contains like: tags, sort.., then don't add pin content
defp should_add_pin?(%{page: 1, tag: :all, sort: :desc_inserted, read: :all} = filter) do
filter
Expand All@@ -356,7 +323,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
pind_entries =
pined_content
|> Map.get(:entries)
|> Enum.map(&struct(&1, %{pin: true}))
|> Enum.map(&struct(&1, %{is_pinned: true}))

normal_entries = normal_contents |> Map.get(:entries)

Expand Down
95 changes: 42 additions & 53 deletionslib/groupher_server/cms/delegates/article_operation.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -4,9 +4,12 @@ defmodule GroupherServer.CMS.Delegate.ArticleOperation do
"""
import GroupherServer.CMS.Utils.Matcher
import Ecto.Query, warn: false
# import Helper.ErrorCode

import Helper.ErrorCode
import ShortMaps
import GroupherServer.CMS.Utils.Matcher2

alias Helper.Types, as: T
alias Helper.ORM

alias GroupherServer.CMS.{
Expand All@@ -18,59 +21,43 @@ defmodule GroupherServer.CMS.Delegate.ArticleOperation do
JobCommunityFlag,
RepoCommunityFlag,
Tag,
PinedPost,
PinedJob,
PinedRepo
PinnedArticle
}

alias GroupherServer.CMS.Repo, as: CMSRepo
alias GroupherServer.Repo

def pin_content(%Post{id: post_id}, %Community{id: community_id}) do
with {:ok, pined} <-
ORM.findby_or_insert(
PinedPost,
~m(post_id community_id)a,
~m(post_id community_id)a
) do
Post |> ORM.find(pined.post_id)
end
end

def pin_content(%Job{id: job_id}, %Community{id: community_id}) do
attrs = ~m(job_id community_id)a
@max_pinned_article_count_per_thread Community.max_pinned_article_count_per_thread()

with {:ok, pined} <- ORM.findby_or_insert(PinedJob, attrs, attrs) do
Job |> ORM.find(pined.job_id)
@spec pin_article(T.article_thread(), Integer.t(), Integer.t()) :: {:ok, PinnedArticle.t()}
def pin_article(thread, article_id, community_id) do
with {:ok, info} <- match(thread),
args <- pack_pin_args(thread, article_id, community_id),
{:ok, _} <- check_pinned_article_count(args.community_id, thread),
{:ok, _} <- ORM.create(PinnedArticle, args) do
ORM.find(info.model, article_id)
end
end

def pin_content(%CMSRepo{id: repo_id}, %Community{id: community_id}) do
attrs = ~m(repo_id community_id)a

with {:ok, pined} <- ORM.findby_or_insert(PinedRepo, attrs, attrs) do
CMSRepo |> ORM.find(pined.repo_id)
@spec undo_pin_article(T.article_thread(), Integer.t(), Integer.t()) :: {:ok, PinnedArticle.t()}
def undo_pin_article(thread, article_id, community_id) do
with {:ok, info} <- match(thread),
args <- pack_pin_args(thread, article_id, community_id) do
ORM.findby_delete(PinnedArticle, args)
ORM.find(info.model, article_id)
end
end

def undo_pin_content(%Post{id: post_id}, %Community{id: community_id}) do
with {:ok, pined} <- ORM.find_by(PinedPost, ~m(post_id community_id)a),
{:ok, deleted} <- ORM.delete(pined) do
Post |> ORM.find(deleted.post_id)
end
end
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()

def undo_pin_content(%Job{id: job_id}, %Community{id: community_id}) do
with {:ok, pined} <- ORM.find_by(PinedJob, ~m(job_id community_id)a),
{:ok, deleted} <- ORM.delete(pined) do
Job |> ORM.find(deleted.job_id)
end
end

def undo_pin_content(%CMSRepo{id: repo_id}, %Community{id: community_id}) do
with {:ok, pined} <- ORM.find_by(PinedRepo, ~m(repo_id community_id)a),
{:ok, deleted} <- ORM.delete(pined) do
CMSRepo |> ORM.find(deleted.repo_id)
Map.put(
%{community_id: community.id, thread: thread_upcase},
info.foreign_key,
article_id
)
end
end

Expand DownExpand Up@@ -255,18 +242,20 @@ defmodule GroupherServer.CMS.Delegate.ArticleOperation do
|> Repo.update()
end

# make sure the reuest tag is in the current community thread
# example: you can't set a other thread tag to this thread's article
# check if the thread has aready enough pined articles
defp check_pinned_article_count(community_id, thread) do
thread_upcase = thread |> to_string |> String.upcase()

# defp tag_in_community_thread?(%Community{id: communityId}, thread, tag) do
# with {:ok, community} <- ORM.find(Community, communityId) do
# matched_tags =
# Tag
# |> where([t], t.community_id == ^community.id)
# |> where([t], t.thread == ^to_string(thread))
# |> Repo.all()
query =
from(p in PinnedArticle,
where: p.community_id == ^community_id and p.thread == ^thread_upcase
)

# tag in matched_tags
# end
# end
pinned_articles = query |> Repo.all()

case length(pinned_articles) >= @max_pinned_article_count_per_thread do
true -> raise_error(:too_much_pinned_article, "too much pinned article")
_ -> {:ok, :pass}
end
end
end
2 changes: 1 addition & 1 deletionlib/groupher_server/cms/job.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -56,7 +56,7 @@ defmodule GroupherServer.CMS.Job do
has_many(:community_flags, {"jobs_communities_flags", JobCommunityFlag})

# NOTE: this one is tricky, pin is dynamic changed when return by func: add_pin_contents_ifneed
field(:pin, :boolean,default_value: false, virtual: true)
field(:is_pinned, :boolean,default: false, virtual: true)
field(:trash, :boolean, default_value: false, virtual: true)

has_many(:article_comments, {"articles_comments", ArticleComment})
Expand Down
30 changes: 0 additions & 30 deletionslib/groupher_server/cms/pined_job.ex
View file
Open in desktop

This file was deleted.

30 changes: 0 additions & 30 deletionslib/groupher_server/cms/pined_post.ex
View file
Open in desktop

This file was deleted.

Loading

[8]ページ先頭

©2009-2025 Movatter.jp