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
feat: Guide thread#416
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
5 changes: 3 additions & 2 deletionsconfig/config.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
48 changes: 48 additions & 0 deletionslib/groupher_server/cms/models/guide.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,48 @@ | ||
defmodule GroupherServer.CMS.Model.Guide do | ||
@moduledoc false | ||
alias __MODULE__ | ||
use Ecto.Schema | ||
use Accessible | ||
import Ecto.Changeset | ||
import GroupherServer.CMS.Helper.Macros | ||
alias GroupherServer.CMS | ||
alias CMS.Model.Embeds | ||
@timestamps_opts [type: :utc_datetime_usec] | ||
@required_fields ~w(title digest)a | ||
@article_cast_fields general_article_cast_fields() | ||
@optional_fields @article_cast_fields | ||
@type t :: %Guide{} | ||
schema "cms_guides" do | ||
article_tags_field(:guide) | ||
article_communities_field(:guide) | ||
general_article_fields(:guide) | ||
end | ||
@doc false | ||
def changeset(%Guide{} = guide, attrs) do | ||
guide | ||
|> cast(attrs, @optional_fields ++ @required_fields) | ||
|> validate_required(@required_fields) | ||
|> cast_embed(:meta, required: false, with: &Embeds.ArticleMeta.changeset/2) | ||
|> generl_changeset | ||
end | ||
@doc false | ||
def update_changeset(%Guide{} = guide, attrs) do | ||
guide | ||
|> cast(attrs, @optional_fields ++ @required_fields) | ||
|> generl_changeset | ||
end | ||
defp generl_changeset(changeset) do | ||
changeset | ||
|> validate_length(:title, min: 3, max: 50) | ||
|> cast_embed(:emotions, with: &Embeds.ArticleEmotion.changeset/2) | ||
end | ||
end |
47 changes: 47 additions & 0 deletionslib/groupher_server/cms/models/guide_document.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,47 @@ | ||
defmodule GroupherServer.CMS.Model.GuideDocument do | ||
@moduledoc """ | ||
mainly for full-text search | ||
""" | ||
alias __MODULE__ | ||
use Ecto.Schema | ||
use Accessible | ||
import Ecto.Changeset | ||
import Helper.Utils, only: [get_config: 2] | ||
alias GroupherServer.CMS | ||
alias CMS.Model.Guide | ||
@timestamps_opts [type: :utc_datetime_usec] | ||
@max_body_length get_config(:article, :max_length) | ||
@min_body_length get_config(:article, :min_length) | ||
@required_fields ~w(body body_html guide_id)a | ||
@optional_fields [] | ||
@type t :: %GuideDocument{} | ||
schema "guide_documents" do | ||
belongs_to(:guide, Guide, foreign_key: :guide_id) | ||
field(:body, :string) | ||
field(:body_html, :string) | ||
field(:toc, :map) | ||
end | ||
@doc false | ||
def changeset(%GuideDocument{} = guide, attrs) do | ||
guide | ||
|> cast(attrs, @optional_fields ++ @required_fields) | ||
|> validate_required(@required_fields) | ||
|> validate_length(:body, min: @min_body_length, max: @max_body_length) | ||
end | ||
@doc false | ||
def update_changeset(%GuideDocument{} = guide, attrs) do | ||
guide | ||
|> cast(attrs, @optional_fields ++ @required_fields) | ||
|> validate_length(:body, min: @min_body_length, max: @max_body_length) | ||
end | ||
end |
7 changes: 7 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
12 changes: 12 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
52 changes: 52 additions & 0 deletionslib/groupher_server_web/schema/cms/mutations/guide.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,52 @@ | ||
defmodule GroupherServerWeb.Schema.CMS.Mutations.Guide do | ||
@moduledoc """ | ||
CMS mutations for guide | ||
""" | ||
use Helper.GqlSchemaSuite | ||
import GroupherServerWeb.Schema.Helper.Mutations | ||
object :cms_guide_mutations do | ||
@desc "create a guide" | ||
field :create_guide, :guide do | ||
arg(:title, non_null(:string)) | ||
arg(:body, non_null(:string)) | ||
arg(:digest, non_null(:string)) | ||
arg(:community_id, non_null(:id)) | ||
arg(:thread, :thread, default_value: :guide) | ||
arg(:article_tags, list_of(:id)) | ||
middleware(M.Authorize, :login) | ||
middleware(M.PublishThrottle) | ||
resolve(&R.CMS.create_article/3) | ||
middleware(M.Statistics.MakeContribute, for: [:user, :community]) | ||
end | ||
@desc "update a cms/guide" | ||
field :update_guide, :guide do | ||
arg(:id, non_null(:id)) | ||
arg(:title, :string) | ||
arg(:body, :string) | ||
arg(:digest, :string) | ||
arg(:article_tags, list_of(:id)) | ||
# ... | ||
middleware(M.Authorize, :login) | ||
middleware(M.PassportLoader, source: :guide) | ||
middleware(M.Passport, claim: "owner;cms->c?->guide.edit") | ||
resolve(&R.CMS.update_article/3) | ||
end | ||
article_react_mutations(:guide, [ | ||
:upvote, | ||
:pin, | ||
:mark_delete, | ||
:delete, | ||
:emotion, | ||
:report, | ||
:sink, | ||
:lock_comment | ||
]) | ||
end | ||
end |
32 changes: 32 additions & 0 deletionspriv/repo/migrations/20210626054252_create_guide.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
defmodule GroupherServer.Repo.Migrations.CreateGuide do | ||
use Ecto.Migration | ||
def change do | ||
create table(:cms_guides) do | ||
add(:thread, :string) | ||
add(:title, :string) | ||
add(:digest, :string) | ||
add(:views, :integer, default: 0) | ||
add(:mark_delete, :boolean, default: false) | ||
add(:meta, :map) | ||
add(:emotions, :map) | ||
add(:original_community_id, references(:communities, on_delete: :delete_all)) | ||
add(:author_id, references(:cms_authors, on_delete: :delete_all), null: false) | ||
add(:active_at, :utc_datetime) | ||
# reaction | ||
add(:upvotes_count, :integer, default: 0) | ||
add(:collects_count, :integer, default: 0) | ||
# comments | ||
add(:comments_participants_count, :integer, default: 0) | ||
add(:comments_count, :integer, default: 0) | ||
add(:comments_participants, :map) | ||
timestamps() | ||
end | ||
create(index(:cms_guides, [:author_id])) | ||
end | ||
end |
13 changes: 13 additions & 0 deletionspriv/repo/migrations/20210626054616_create_guide_document.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
defmodule GroupherServer.Repo.Migrations.CreateGuideDocument do | ||
use Ecto.Migration | ||
def change do | ||
create table(:guide_documents) do | ||
add(:guide_id, references(:cms_guides, on_delete: :delete_all), null: false) | ||
add(:body, :text) | ||
add(:body_html, :text) | ||
add(:markdown, :text) | ||
add(:toc, :map) | ||
end | ||
end | ||
end |
12 changes: 12 additions & 0 deletionspriv/repo/migrations/20210626054717_create_communities_join_guides.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
defmodule GroupherServer.Repo.Migrations.CreateCommunitiesJoinGuides do | ||
use Ecto.Migration | ||
def change do | ||
create table(:communities_join_guides) do | ||
add(:community_id, references(:communities, on_delete: :delete_all), null: false) | ||
add(:guide_id, references(:cms_guides, on_delete: :delete_all), null: false) | ||
end | ||
create(unique_index(:communities_join_guides, [:community_id, :guide_id])) | ||
end | ||
end |
41 changes: 41 additions & 0 deletionspriv/repo/migrations/20210626054903_add_guide_id_to_others.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
defmodule GroupherServer.Repo.Migrations.AddGuideIdToOthers do | ||
use Ecto.Migration | ||
def change do | ||
alter table(:articles_join_tags) do | ||
add(:guide_id, references(:cms_guides, on_delete: :delete_all)) | ||
end | ||
alter table(:abuse_reports) do | ||
add(:guide_id, references(:cms_guides, on_delete: :delete_all)) | ||
end | ||
alter table(:article_collects) do | ||
add(:guide_id, references(:cms_guides, on_delete: :delete_all)) | ||
end | ||
alter table(:article_upvotes) do | ||
add(:guide_id, references(:cms_guides, on_delete: :delete_all)) | ||
end | ||
alter table(:comments) do | ||
add(:guide_id, references(:cms_guides, on_delete: :delete_all)) | ||
end | ||
alter table(:pinned_comments) do | ||
add(:guide_id, references(:cms_guides, on_delete: :delete_all)) | ||
end | ||
alter table(:articles_users_emotions) do | ||
add(:guide_id, references(:cms_guides, on_delete: :delete_all)) | ||
end | ||
alter table(:pinned_articles) do | ||
add(:guide_id, references(:cms_guides, on_delete: :delete_all)) | ||
end | ||
alter table(:cited_artiments) do | ||
add(:guide_id, references(:cms_guides, on_delete: :delete_all)) | ||
end | ||
end | ||
end |
93 changes: 93 additions & 0 deletionstest/groupher_server/accounts/published/published_guide_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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
defmodule GroupherServer.Test.Accounts.Published.Guide do | ||
use GroupherServer.TestTools | ||
alias GroupherServer.{Accounts, CMS} | ||
alias Accounts.Model.User | ||
alias Helper.ORM | ||
@publish_count 10 | ||
setup do | ||
{:ok, user} = db_insert(:user) | ||
{:ok, user2} = db_insert(:user) | ||
{:ok, guide} = db_insert(:guide) | ||
{:ok, community} = db_insert(:community) | ||
{:ok, community2} = db_insert(:community) | ||
{:ok, ~m(user user2 guide community community2)a} | ||
end | ||
describe "[publised guides]" do | ||
test "create guide should update user published meta", ~m(community user)a do | ||
guide_attrs = mock_attrs(:guide, %{community_id: community.id}) | ||
{:ok, _guide} = CMS.create_article(community, :guide, guide_attrs, user) | ||
{:ok, _guide} = CMS.create_article(community, :guide, guide_attrs, user) | ||
{:ok, user} = ORM.find(User, user.id) | ||
assert user.meta.published_guides_count == 2 | ||
end | ||
test "fresh user get empty paged published guides", ~m(user)a do | ||
{:ok, results} = Accounts.paged_published_articles(user, :guide, %{page: 1, size: 20}) | ||
assert results |> is_valid_pagination?(:raw) | ||
assert results.total_count == 0 | ||
end | ||
test "user can get paged published guides", ~m(user user2 community community2)a do | ||
pub_guides = | ||
Enum.reduce(1..@publish_count, [], fn _, acc -> | ||
guide_attrs = mock_attrs(:guide, %{community_id: community.id}) | ||
{:ok, guide} = CMS.create_article(community, :guide, guide_attrs, user) | ||
acc ++ [guide] | ||
end) | ||
pub_guides2 = | ||
Enum.reduce(1..@publish_count, [], fn _, acc -> | ||
guide_attrs = mock_attrs(:guide, %{community_id: community2.id}) | ||
{:ok, guide} = CMS.create_article(community, :guide, guide_attrs, user) | ||
acc ++ [guide] | ||
end) | ||
# unrelated other user | ||
Enum.reduce(1..5, [], fn _, acc -> | ||
guide_attrs = mock_attrs(:guide, %{community_id: community.id}) | ||
{:ok, guide} = CMS.create_article(community, :guide, guide_attrs, user2) | ||
acc ++ [guide] | ||
end) | ||
{:ok, results} = Accounts.paged_published_articles(user, :guide, %{page: 1, size: 20}) | ||
assert results |> is_valid_pagination?(:raw) | ||
assert results.total_count == @publish_count * 2 | ||
random_guide_id = pub_guides |> Enum.random() |> Map.get(:id) | ||
random_guide_id2 = pub_guides2 |> Enum.random() |> Map.get(:id) | ||
assert results.entries |> Enum.any?(&(&1.id == random_guide_id)) | ||
assert results.entries |> Enum.any?(&(&1.id == random_guide_id2)) | ||
end | ||
end | ||
describe "[publised guide comments]" do | ||
test "can get published article comments", ~m(guide user)a do | ||
total_count = 10 | ||
Enum.reduce(1..total_count, [], fn _, acc -> | ||
{:ok, comment} = CMS.create_comment(:guide, guide.id, mock_comment(), user) | ||
acc ++ [comment] | ||
end) | ||
filter = %{page: 1, size: 20} | ||
{:ok, articles} = Accounts.paged_published_comments(user, :guide, filter) | ||
entries = articles.entries | ||
article = entries |> List.first() | ||
assert article.article.id == guide.id | ||
assert article.article.title == guide.title | ||
end | ||
end | ||
end |
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.