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.

feat: archived flag for articles & comments#424

Merged
mydearxym merged 2 commits intodevfromarchived-flag
Aug 15, 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
13 changes: 11 additions & 2 deletionsconfig/config.exs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -109,7 +109,15 @@ config :groupher_server, :article,
:pill,
:popcorn
],
digest_length: 120
digest_length: 120,
archive_threshold: %{
# for post, blog, job, works, radar, job
default: [weeks: -1],
meetups: [months: -1],
drink: [years: -99],
repo: [years: -99],
guide: [years: -99]
}

config :groupher_server, GroupherServerWeb.Gettext, default_locale: "zh_CN", locales: ~w(en zh_CN)

Expand DownExpand Up@@ -137,7 +145,8 @@ config :rihanna,
config :groupher_server, Helper.Scheduler,
jobs: [
# Every midnight
{"@daily", {Helper.Scheduler, :clear_all_cache, []}}
{"@daily", {Helper.Scheduler, :clear_all_cache, []}},
{"@daily", {Helper.Scheduler, :archive_artiments, []}}
]

import_config "#{Mix.env()}.exs"
Expand Down
4 changes: 4 additions & 0 deletionslib/groupher_server/cms/cms.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -100,6 +100,8 @@ defmodule GroupherServer.CMS do
defdelegate sink_article(thread, id), to: ArticleCURD
defdelegate undo_sink_article(thread, id), to: ArticleCURD

defdelegate archive_articles(thread), to: ArticleCURD

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

defdelegate upvote_article(thread, article_id, user), to: ArticleUpvote
Expand DownExpand Up@@ -151,6 +153,8 @@ defmodule GroupherServer.CMS do
defdelegate mark_comment_solution(comment, user), to: CommentCurd
defdelegate undo_mark_comment_solution(comment, user), to: CommentCurd

defdelegate archive_comments(), to: CommentCurd

defdelegate upvote_comment(comment_id, user), to: CommentAction
defdelegate undo_upvote_comment(comment_id, user), to: CommentAction
defdelegate reply_comment(comment_id, args, user), to: CommentAction
Expand Down
61 changes: 42 additions & 19 deletionslib/groupher_server/cms/delegates/article_curd.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -39,6 +39,8 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
alias Ecto.Multi

@active_period get_config(:article, :active_period_days)
@archive_threshold get_config(:article, :archive_threshold)

@default_emotions Embeds.ArticleEmotion.default_emotions()
@default_article_meta Embeds.ArticleMeta.default_meta()
@remove_article_hint "The content does not comply with the community norms"
Expand DownExpand Up@@ -68,26 +70,24 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
read articles for logined user
"""
def read_article(thread, id, %User{id: user_id}) do
with {:ok, info} <- match(thread) do
Multi.new()
|> Multi.run(:normal_read, fn _, _ -> read_article(thread, id) end)
|> Multi.run(:add_viewed_user, fn _, %{normal_read: article} ->
update_viewed_user_list(article, user_id)
end)
|> Multi.run(:set_viewer_has_states, fn _, %{normal_read: article} ->
article_meta = if is_nil(article.meta), do: @default_article_meta, else: article.meta
Multi.new()
|> Multi.run(:normal_read, fn _, _ -> read_article(thread, id) end)
|> Multi.run(:add_viewed_user, fn _, %{normal_read: article} ->
update_viewed_user_list(article, user_id)
end)
|> Multi.run(:set_viewer_has_states, fn _, %{normal_read: article} ->
article_meta = if is_nil(article.meta), do: @default_article_meta, else: article.meta

viewer_has_states = %{
viewer_has_collected: user_id in article_meta.collected_user_ids,
viewer_has_upvoted: user_id in article_meta.upvoted_user_ids,
viewer_has_reported: user_id in article_meta.reported_user_ids
}
viewer_has_states = %{
viewer_has_collected: user_id in article_meta.collected_user_ids,
viewer_has_upvoted: user_id in article_meta.upvoted_user_ids,
viewer_has_reported: user_id in article_meta.reported_user_ids
}

article |> Map.merge(viewer_has_states) |> done
end)
|> Repo.transaction()
|> result()
end
article |> Map.merge(viewer_has_states) |> done
end)
|> Repo.transaction()
|> result()
end

@doc """
Expand DownExpand Up@@ -134,6 +134,23 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
end
end

@doc """
archive articles based on thread
called every day by scheuler job
"""
def archive_articles(thread) do
with {:ok, info} <- match(thread) do
now = Timex.now()
threshold = @archive_threshold[thread] || @archive_threshold[:default]
archive_threshold = Timex.shift(now, threshold)

info.model
|> where([article], article.inserted_at < ^archive_threshold)
|> Repo.update_all(set: [is_archived: true, archived_at: now])
|> done()
end
end

defp mark_viewer_has_states(%{entries: []} = articles, _), do: articles

defp mark_viewer_has_states(%{entries: entries} = articles, user) do
Expand DownExpand Up@@ -237,6 +254,9 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
@doc """
update a article(post/job ...)
"""
def update_article(%{is_archived: true}, _attrs),
do: raise_error(:archived, "article is archived, can not be edit or delete")

def update_article(article, attrs) do
Multi.new()
|> Multi.run(:update_article, fn _, _ ->
Expand DownExpand Up@@ -317,7 +337,8 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
"""
def mark_delete_article(thread, id) do
with {:ok, info} <- match(thread),
{:ok, article} <- ORM.find(info.model, id, preload: :communities) do
{:ok, article} <- ORM.find(info.model, id, preload: :communities),
false <- article.is_archived do
Multi.new()
|> Multi.run(:update_article, fn _, _ ->
ORM.update(article, %{mark_delete: true})
Expand All@@ -327,6 +348,8 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
end)
|> Repo.transaction()
|> result()
else
true -> raise_error(:archived, "article is archived, can not be edit or delete")
end
end

Expand Down
25 changes: 24 additions & 1 deletionlib/groupher_server/cms/delegates/comment_curd.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -3,7 +3,7 @@ defmodule GroupherServer.CMS.Delegate.CommentCurd do
CURD and operations for article comments
"""
import Ecto.Query, warn: false
import Helper.Utils, only: [done: 1, ensure: 2]
import Helper.Utils, only: [done: 1, ensure: 2, get_config: 2]
import Helper.ErrorCode

import GroupherServer.CMS.Delegate.Helper,
Expand DownExpand Up@@ -31,6 +31,8 @@ defmodule GroupherServer.CMS.Delegate.CommentCurd do
@default_comment_meta Embeds.CommentMeta.default_meta()
@pinned_comment_limit Comment.pinned_comment_limit()

@archive_threshold get_config(:article, :archive_threshold)

@doc """
[timeline-mode] list paged article comments
"""
Expand DownExpand Up@@ -171,6 +173,9 @@ defmodule GroupherServer.CMS.Delegate.CommentCurd do
not article_meta.is_comment_locked
end

def update_comment(%{is_archived: true}, _body),
do: raise_error(:archived, "comment is archived, can not be edit or delete")

@doc """
update a comment for article like psot, job ...
"""
Expand DownExpand Up@@ -247,6 +252,9 @@ defmodule GroupherServer.CMS.Delegate.CommentCurd do

def batch_update_question_flag(_), do: {:ok, :pass}

def delete_comment(%{is_archived: true}),
do: raise_error(:archived, "article is archived, can not be edit or delete")

@doc "delete article comment"
def delete_comment(%Comment{} = comment) do
Multi.new()
Expand DownExpand Up@@ -322,6 +330,21 @@ defmodule GroupherServer.CMS.Delegate.CommentCurd do
end
end

@doc """
archive comments
called every day by scheuler job
"""
def archive_comments() do
now = Timex.now() |> DateTime.truncate(:second)
threshold = @archive_threshold[:default]
archive_threshold = Timex.shift(now, threshold)

Comment
|> where([c], c.inserted_at < ^archive_threshold)
|> Repo.update_all(set: [is_archived: true, archived_at: now])
|> done()
end

defp do_paged_comment(thread, article_id, filters, where_query, user) do
%{page: page, size: size} = filters
sort = Map.get(filters, :sort, :asc_inserted)
Expand Down
4 changes: 4 additions & 0 deletionslib/groupher_server/cms/helper/macros.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -200,6 +200,10 @@ defmodule GroupherServer.CMS.Helper.Macros do
comment_fields()

field(:active_at, :utc_datetime_usec)

field(:is_archived, :boolean)
field(:archived_at, :utc_datetime_usec)

# TODO:
# related_articles
timestamps()
Expand Down
3 changes: 3 additions & 0 deletionslib/groupher_server/cms/models/comment.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -83,6 +83,9 @@ defmodule GroupherServer.CMS.Model.Comment do

has_many(:upvotes, {"comments_upvotes", CommentUpvote})

field(:is_archived, :boolean, default: false)
field(:archived_at, :utc_datetime)

article_belongs_to_fields()
timestamps(type: :utc_datetime)
end
Expand Down
1 change: 1 addition & 0 deletionslib/groupher_server/cms/models/embeds/article_meta.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -32,6 +32,7 @@ defmodule GroupherServer.CMS.Model.Embeds.ArticleMeta do
field(:is_edited, :boolean, default: false)
field(:is_comment_locked, :boolean, default: false)
field(:folded_comment_count, :integer, default: 0)

# reaction history
field(:upvoted_user_ids, {:array, :integer}, default: [])
field(:collected_user_ids, {:array, :integer}, default: [])
Expand Down
3 changes: 3 additions & 0 deletionslib/groupher_server_web/schema/Helper/fields.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -38,6 +38,9 @@ defmodule GroupherServerWeb.Schema.Helper.Fields do
field(:viewer_has_upvoted, :boolean)
field(:viewer_has_viewed, :boolean)
field(:viewer_has_reported, :boolean)

field(:is_archived, :boolean)
field(:archived_at, :datetime)
end
end

Expand Down
3 changes: 3 additions & 0 deletionslib/groupher_server_web/schema/cms/cms_types.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -342,6 +342,9 @@ defmodule GroupherServerWeb.Schema.CMS.Types do
field(:is_for_question, :boolean)
field(:is_solution, :boolean)

field(:is_archived, :boolean)
field(:archived_at, :datetime)

timestamp_fields()
end

Expand Down
1 change: 1 addition & 0 deletionslib/helper/error_code.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -52,6 +52,7 @@ defmodule Helper.ErrorCode do
def ecode(:article_comments_locked), do: @article_base + 8
def ecode(:require_questioner), do: @article_base + 9
def ecode(:cite_artilce), do: @article_base + 10
def ecode(:archived), do: @article_base + 11
# def ecode(:already_solved), do: @article_base + 10

def ecode, do: @default_base
Expand Down
17 changes: 16 additions & 1 deletionlib/helper/scheduler.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -3,7 +3,11 @@ defmodule Helper.Scheduler do
cron-like job scheduler
"""
use Quantum.Scheduler, otp_app: :groupher_server
# alias Helper.Cache

import Helper.Utils, only: [get_config: 2]
alias GroupherServer.CMS

@article_threads get_config(:article, :threads)

@doc """
clear all the cache in Cachex
Expand All@@ -12,4 +16,15 @@ defmodule Helper.Scheduler do
def clear_all_cache do
# Cache.clear_all()
end

@doc """
archive articles and comments based on config
"""
def archive_artiments() do
Enum.map(@article_threads, &CMS.archive_articles(&1))
end

def arthive_comments() do
CMS.archive_comments()
end
end
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
defmodule GroupherServer.Repo.Migrations.AddArchiveFieldsToArticles do
use Ecto.Migration

def change do
alter table(:cms_posts) do
add(:is_archived, :boolean, default: false)
add(:archived_at, :utc_datetime)
end

alter table(:cms_jobs) do
add(:is_archived, :boolean, default: false)
add(:archived_at, :utc_datetime)
end

alter table(:cms_repos) do
add(:is_archived, :boolean, default: false)
add(:archived_at, :utc_datetime)
end

alter table(:cms_blogs) do
add(:is_archived, :boolean, default: false)
add(:archived_at, :utc_datetime)
end

alter table(:cms_works) do
add(:is_archived, :boolean, default: false)
add(:archived_at, :utc_datetime)
end

alter table(:cms_radars) do
add(:is_archived, :boolean, default: false)
add(:archived_at, :utc_datetime)
end

alter table(:cms_guides) do
add(:is_archived, :boolean, default: false)
add(:archived_at, :utc_datetime)
end

alter table(:cms_meetups) do
add(:is_archived, :boolean, default: false)
add(:archived_at, :utc_datetime)
end

alter table(:cms_drinks) do
add(:is_archived, :boolean, default: false)
add(:archived_at, :utc_datetime)
end
end
end
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
defmodule GroupherServer.Repo.Migrations.AddArchiveFieldsToComments do
use Ecto.Migration

def change do
alter table(:comments) do
add(:is_archived, :boolean, default: false)
add(:archived_at, :utc_datetime)
end
end
end
Loading

[8]ページ先頭

©2009-2025 Movatter.jp