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
74 changes: 74 additions & 0 deletions lib/groupher_server_web/schema/Helper/mutations.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,78 @@ defmodule GroupherServerWeb.Schema.Helper.Mutations do
end
end
end

defmacro article_pin_mutation(thread) do
quote do
@desc unquote("pin to #{thread}")
field unquote(:"pin_#{thread}"), unquote(thread) do
arg(:id, non_null(:id))
arg(:community_id, non_null(:id))
arg(:thread, unquote(:"#{thread}_thread"), default_value: unquote(thread))

middleware(M.Authorize, :login)
middleware(M.PassportLoader, source: :community)
middleware(M.Passport, claim: unquote("cms->c?->#{to_string(thread)}.pin"))
resolve(&R.CMS.pin_article/3)
end

@desc unquote("undo pin to #{thread}")
field unquote(:"undo_pin_#{thread}"), unquote(thread) do
arg(:id, non_null(:id))
arg(:community_id, non_null(:id))
arg(:thread, unquote(:"#{thread}_thread"), default_value: unquote(thread))

middleware(M.Authorize, :login)
middleware(M.PassportLoader, source: :community)
middleware(M.Passport, claim: unquote("cms->c?->#{to_string(thread)}.undo_pin"))
resolve(&R.CMS.undo_pin_article/3)
end
end
end

defmacro article_trash_mutation(thread) do
quote do
@desc unquote("trash a #{thread}, not delete")
field unquote(:"trash_#{thread}"), unquote(thread) do
arg(:id, non_null(:id))
arg(:community_id, non_null(:id))
arg(:thread, unquote(:"#{thread}_thread"), default_value: unquote(thread))

middleware(M.Authorize, :login)
middleware(M.PassportLoader, source: :community)
middleware(M.Passport, claim: unquote("cms->c?->#{to_string(thread)}.trash"))

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

@desc unquote("undo trash a #{thread}, not delete")
field unquote(:"undo_trash_#{thread}"), unquote(thread) do
arg(:id, non_null(:id))
arg(:community_id, non_null(:id))
arg(:thread, unquote(:"#{thread}_thread"), default_value: unquote(thread))

middleware(M.Authorize, :login)
middleware(M.PassportLoader, source: :community)
middleware(M.Passport, claim: unquote("cms->c?->#{to_string(thread)}.undo_trash"))

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

# TODO: if post belongs to multi communities, unset instead delete
defmacro article_delete_mutation(thread) do
quote do
@desc unquote("delete a #{thread}, not delete")
field unquote(:"delete_#{thread}"), unquote(thread) do
arg(:id, non_null(:id))

middleware(M.Authorize, :login)
middleware(M.PassportLoader, source: unquote(thread))
middleware(M.Passport, claim: unquote("owner;cms->c?->#{to_string(thread)}.delete"))

resolve(&R.CMS.delete_content/3)
end
end
end
end
64 changes: 3 additions & 61 deletions lib/groupher_server_web/schema/cms/mutations/job.ex
Original file line number Diff line number Diff line change
Expand Up @@ -72,67 +72,9 @@ defmodule GroupherServerWeb.Schema.CMS.Mutations.Job do

#############
article_upvote_mutation(:job)
article_pin_mutation(:job)
article_trash_mutation(:job)
article_delete_mutation(:job)
#############

@desc "pin a job"
field :pin_job, :job do
arg(:id, non_null(:id))
arg(:thread, :job_thread, default_value: :job)
arg(:community_id, non_null(:id))

middleware(M.Authorize, :login)
middleware(M.PassportLoader, source: :community)
middleware(M.Passport, claim: "cms->c?->job.pin")
resolve(&R.CMS.pin_article/3)
end

@desc "unpin a job"
field :undo_pin_job, :job do
arg(:id, non_null(:id))
arg(:thread, :job_thread, default_value: :job)
arg(:community_id, non_null(:id))

middleware(M.Authorize, :login)
middleware(M.PassportLoader, source: :community)
middleware(M.Passport, claim: "cms->c?->job.undo_pin")
resolve(&R.CMS.undo_pin_article/3)
end

@desc "trash a job, not delete"
field :trash_job, :job do
arg(:id, non_null(:id))
arg(:thread, :job_thread, default_value: :job)
arg(:community_id, non_null(:id))

middleware(M.Authorize, :login)
middleware(M.PassportLoader, source: :community)
middleware(M.Passport, claim: "cms->c?->job.trash")

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

@desc "trash a job, not delete"
field :undo_trash_job, :job do
arg(:id, non_null(:id))
arg(:thread, :job_thread, default_value: :job)
arg(:community_id, non_null(:id))

middleware(M.Authorize, :login)
middleware(M.PassportLoader, source: :community)
middleware(M.Passport, claim: "cms->c?->job.undo_trash")

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

@desc "delete a job"
field :delete_job, :job do
arg(:id, non_null(:id))

middleware(M.Authorize, :login)
middleware(M.PassportLoader, source: :job)
middleware(M.Passport, claim: "owner;cms->c?->job.delete")

resolve(&R.CMS.delete_content/3)
end
end
end
65 changes: 3 additions & 62 deletions lib/groupher_server_web/schema/cms/mutations/post.ex
Original file line number Diff line number Diff line change
Expand Up @@ -46,68 +46,9 @@ defmodule GroupherServerWeb.Schema.CMS.Mutations.Post do

#############
article_upvote_mutation(:post)
article_pin_mutation(:post)
article_trash_mutation(:post)
article_delete_mutation(:post)
#############

@desc "pin a post"
field :pin_post, :post do
arg(:id, non_null(:id))
arg(:community_id, non_null(:id))
arg(:thread, :post_thread, default_value: :post)

middleware(M.Authorize, :login)
middleware(M.PassportLoader, source: :community)
middleware(M.Passport, claim: "cms->c?->post.pin")
resolve(&R.CMS.pin_article/3)
end

@desc "unpin a post"
field :undo_pin_post, :post do
arg(:id, non_null(:id))
arg(:thread, :post_thread, default_value: :post)
arg(:community_id, non_null(:id))

middleware(M.Authorize, :login)
middleware(M.PassportLoader, source: :community)
middleware(M.Passport, claim: "cms->c?->post.undo_pin")
resolve(&R.CMS.undo_pin_article/3)
end

@desc "trash a post, not delete"
field :trash_post, :post do
arg(:id, non_null(:id))
arg(:thread, :post_thread, default_value: :post)
arg(:community_id, non_null(:id))

middleware(M.Authorize, :login)
middleware(M.PassportLoader, source: :community)
middleware(M.Passport, claim: "cms->c?->post.trash")

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

@desc "trash a post, not delete"
field :undo_trash_post, :post do
arg(:id, non_null(:id))
arg(:thread, :post_thread, default_value: :post)
arg(:community_id, non_null(:id))

middleware(M.Authorize, :login)
middleware(M.PassportLoader, source: :community)
middleware(M.Passport, claim: "cms->c?->post.undo_trash")

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

@desc "delete a cms/post"
# TODO: if post belongs to multi communities, unset instead delete
field :delete_post, :post do
arg(:id, non_null(:id))

middleware(M.Authorize, :login)
middleware(M.PassportLoader, source: :post)
middleware(M.Passport, claim: "owner;cms->c?->post.delete")

resolve(&R.CMS.delete_content/3)
end
end
end
54 changes: 5 additions & 49 deletions lib/groupher_server_web/schema/cms/mutations/repo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defmodule GroupherServerWeb.Schema.CMS.Mutations.Repo do
CMS mutations for job
"""
use Helper.GqlSchemaSuite
import GroupherServerWeb.Schema.Helper.Mutations

object :cms_repo_mutations do
@desc "create a repo"
Expand Down Expand Up @@ -69,55 +70,10 @@ defmodule GroupherServerWeb.Schema.CMS.Mutations.Repo do
resolve(&R.CMS.update_content/3)
end

@desc "pin a repo"
field :pin_repo, :repo do
arg(:id, non_null(:id))
arg(:thread, :repo_thread, default_value: :repo)
arg(:community_id, non_null(:id))

middleware(M.Authorize, :login)
middleware(M.PassportLoader, source: :community)
middleware(M.Passport, claim: "cms->c?->repo.pin")
resolve(&R.CMS.pin_article/3)
end

@desc "unpin a repo"
field :undo_pin_repo, :repo do
arg(:id, non_null(:id))
arg(:thread, :repo_thread, default_value: :repo)
arg(:community_id, non_null(:id))

middleware(M.Authorize, :login)
middleware(M.PassportLoader, source: :community)
middleware(M.Passport, claim: "cms->c?->repo.undo_pin")
resolve(&R.CMS.undo_pin_article/3)
end

@desc "trash a repo, not delete"
field :trash_repo, :repo do
arg(:id, non_null(:id))
arg(:thread, :repo_thread, default_value: :repo)
arg(:community_id, non_null(:id))

middleware(M.Authorize, :login)
middleware(M.PassportLoader, source: :community)
middleware(M.Passport, claim: "cms->c?->repo.trash")

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

@desc "trash a repo, not delete"
field :undo_trash_repo, :repo do
arg(:id, non_null(:id))
arg(:thread, :repo_thread, default_value: :repo)
arg(:community_id, non_null(:id))

middleware(M.Authorize, :login)
middleware(M.PassportLoader, source: :community)
middleware(M.Passport, claim: "cms->c?->repo.undo_trash")

resolve(&R.CMS.undo_trash_content/3)
end
#############
article_pin_mutation(:repo)
article_trash_mutation(:repo)
#############

@desc "delete a repo"
field :delete_repo, :repo do
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule GroupherServer.Test.Mutation.JobFlag do
defmodule GroupherServer.Test.Mutation.Flags.JobFlag do
use GroupherServer.TestTools

alias GroupherServer.CMS
Expand All @@ -25,6 +25,7 @@ defmodule GroupherServer.Test.Mutation.JobFlag do
}
}
"""
@tag :wip2
test "auth user can trash job", ~m(community job)a do
variables = %{id: job.id, communityId: community.id}

Expand All @@ -37,6 +38,7 @@ defmodule GroupherServer.Test.Mutation.JobFlag do
assert updated["trash"] == true
end

@tag :wip2
test "unauth user trash job fails", ~m(user_conn guest_conn job community)a do
variables = %{id: job.id, communityId: community.id}
rule_conn = simu_conn(:user, cms: %{"what.ever" => true})
Expand All @@ -54,6 +56,7 @@ defmodule GroupherServer.Test.Mutation.JobFlag do
}
}
"""
@tag :wip2
test "auth user can undo trash job", ~m(community job)a do
variables = %{id: job.id, communityId: community.id}

Expand Down Expand Up @@ -84,6 +87,7 @@ defmodule GroupherServer.Test.Mutation.JobFlag do
}
}
"""
@tag :wip2
test "auth user can pin job", ~m(community job)a do
variables = %{id: job.id, communityId: community.id}

Expand All @@ -95,6 +99,7 @@ defmodule GroupherServer.Test.Mutation.JobFlag do
assert updated["id"] == to_string(job.id)
end

@tag :wip2
test "unauth user pin job fails", ~m(user_conn guest_conn community job)a do
variables = %{id: job.id, communityId: community.id}
rule_conn = simu_conn(:user, cms: %{"what.ever" => true})
Expand All @@ -112,7 +117,7 @@ defmodule GroupherServer.Test.Mutation.JobFlag do
}
}
"""
@tag :wip
@tag :wip2
test "auth user can undo pin job", ~m(community job)a do
variables = %{id: job.id, communityId: community.id}

Expand All @@ -126,6 +131,7 @@ defmodule GroupherServer.Test.Mutation.JobFlag do
assert updated["isPinned"] == false
end

@tag :wip2
test "unauth user undo pin job fails", ~m(user_conn guest_conn community job)a do
variables = %{id: job.id, communityId: community.id}
rule_conn = simu_conn(:user, cms: %{"what.ever" => true})
Expand Down
Loading