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(follow-viewer-state): debug & re-org#449

Merged
mydearxym merged 1 commit intodevfromfollow-staff-debug
Dec 16, 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
2 changes: 1 addition & 1 deletionlib/groupher_server/accounts/accounts.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -22,7 +22,7 @@ defmodule GroupherServer.Accounts do

defdelegate update_profile(user, attrs), to: Profile
defdelegate update_geo(user, remote_ip), to: Profile
defdelegateupdate_subscribe_count(user), to: Profile
defdelegateupdate_subscribe_state(user), to: Profile
defdelegate github_signin(github_user), to: Profile
defdelegate default_subscribed_communities(filter), to: Profile
defdelegate subscribed_communities(user, filter), to: Profile
Expand Down
24 changes: 19 additions & 5 deletionslib/groupher_server/accounts/delegates/profile.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -3,7 +3,7 @@ defmodule GroupherServer.Accounts.Delegate.Profile do
accounts profile
"""
import Ecto.Query, warn: false
import Helper.Utils, only: [done: 1, get_config: 2]
import Helper.Utils, only: [done: 1, get_config: 2, ensure: 2]
import ShortMaps

alias GroupherServer.{Accounts, CMS, Email, Repo, Statistics}
Expand DownExpand Up@@ -80,11 +80,25 @@ defmodule GroupherServer.Accounts.Delegate.Profile do
@doc """
update user's subscribed communities count
"""
defupdate_subscribe_count(user_id) do
defupdate_subscribe_state(user_id) do
with {:ok, user} <- ORM.find(User, user_id) do
{:ok, count} = from(s in CommunitySubscriber, where: s.user_id == ^user.id) |> ORM.count()

user |> ORM.update(%{subscribed_communities_count: count})
query =
from(s in CommunitySubscriber,
where: s.user_id == ^user.id,
join: c in assoc(s, :community),
select: c.id
)

subscribed_communities_ids = query |> Repo.all()
subscribed_communities_count = subscribed_communities_ids |> length

user_meta = ensure(user.meta, @default_user_meta)
meta = %{user_meta | subscribed_communities_ids: subscribed_communities_ids}

user
|> ORM.update_meta(meta,
changes: %{subscribed_communities_count: subscribed_communities_count}
)
end
end

Expand Down
5 changes: 4 additions & 1 deletionlib/groupher_server/accounts/models/embeds/user_meta.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -38,7 +38,8 @@ defmodule GroupherServer.Accounts.Model.Embeds.UserMeta do
has_illegal_articles: false,
illegal_articles: [],
has_illegal_comments: false,
illegal_comments: []
illegal_comments: [],
subscribed_communities_ids: []
}

@optional_fields Map.keys(@general_options) ++
Expand DownExpand Up@@ -69,6 +70,8 @@ defmodule GroupherServer.Accounts.Model.Embeds.UserMeta do
field(:illegal_articles, {:array, :string}, default: [])
field(:illegal_comments, {:array, :string}, default: [])

field(:subscribed_communities_ids, {:array, :id}, default: [])

published_article_count_fields()
end

Expand Down
11 changes: 8 additions & 3 deletionslib/groupher_server/cms/cms.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -35,9 +35,11 @@ defmodule GroupherServer.CMS do
# Community CURD: editors, thread, tag
defdelegate read_community(args), to: CommunityCURD
defdelegate read_community(args, user), to: CommunityCURD
defdelegate paged_communities(filter, user), to: CommunityCURD
defdelegate paged_communities(filter), to: CommunityCURD
defdelegate create_community(args), to: CommunityCURD
defdelegate update_community(id, args), to: CommunityCURD
defdelegate apply_community(args), to: CommunityCURD
defdelegate update_community(id, args), to: CommunityCURD
defdelegate approve_community_apply(id), to: CommunityCURD
defdelegate deny_community_apply(id), to: CommunityCURD
defdelegate is_community_exist?(raw), to: CommunityCURD
Expand All@@ -52,6 +54,7 @@ defmodule GroupherServer.CMS do
defdelegate community_geo_info(community), to: CommunityCURD
# >> subscribers / editors
defdelegate community_members(type, community, filters), to: CommunityCURD
defdelegate community_members(type, community, filters, user), to: CommunityCURD
# >> category
defdelegate create_category(category_attrs, user), to: CommunityCURD
defdelegate update_category(category_attrs), to: CommunityCURD
Expand DownExpand Up@@ -240,8 +243,10 @@ defmodule GroupherServer.CMS do

# search
defdelegate search_articles(thread, args), to: Search
defdelegate search_communities(args), to: Search
defdelegate search_communities(args, category), to: Search
defdelegate search_communities(filter), to: Search
defdelegate search_communities(filter, user), to: Search
defdelegate search_communities(filter, category), to: Search
defdelegate search_communities(filter, category, user), to: Search

# seeds
defdelegate seed_communities(opt), to: Seeds
Expand Down
8 changes: 8 additions & 0 deletionslib/groupher_server/cms/delegates/Seeds/helper.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -88,6 +88,14 @@ defmodule GroupherServer.CMS.Delegate.Seeds.Helper do
ORM.find_all(Category, %{page: 1, size: 20})
end

def seed_user(name) do
nickname = name
login = name
avatar = "https://avatars1.githubusercontent.com/u/6184465?s=460&v=4"

User |> ORM.findby_or_insert(~m(nickname avatar)a, ~m(nickname avatar login)a)
end

def seed_bot() do
case ORM.find(User, 1) do
{:ok, user} ->
Expand Down
7 changes: 5 additions & 2 deletionslib/groupher_server/cms/delegates/article_curd.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -69,15 +69,18 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
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
article = Repo.preload(article, :original_community)
article_meta = ensure(article.meta, @default_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
}

article |> Map.merge(viewer_has_states) |> done
article
|> Map.merge(viewer_has_states)
|> done
end)
|> Repo.transaction()
|> result()
Expand Down
8 changes: 4 additions & 4 deletionslib/groupher_server/cms/delegates/comment_curd.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -261,11 +261,11 @@ defmodule GroupherServer.CMS.Delegate.CommentCURD do
illegal_comments = user_meta.illegal_comments -- illegal_comments
has_illegal_comments = not Enum.empty?(illegal_comments)

meta =
Map.merge(user_meta, %{
has_illegal_comments: has_illegal_comments,
meta = %{
user_meta
| has_illegal_comments: has_illegal_comments,
illegal_comments: illegal_comments
})
}

ORM.update_meta(user, meta)
end
Expand Down
47 changes: 45 additions & 2 deletionslib/groupher_server/cms/delegates/community_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.CommunityCURD do
community curd
"""
import Ecto.Query, warn: false
import Helper.Utils, only: [done: 1, strip_struct: 1, get_config: 2, plural: 1]
import Helper.Utils, only: [done: 1, strip_struct: 1, get_config: 2, plural: 1, ensure: 2]
import GroupherServer.CMS.Delegate.ArticleCURD, only: [ensure_author_exists: 1]
import GroupherServer.CMS.Helper.Matcher
import ShortMaps
Expand All@@ -29,6 +29,7 @@ defmodule GroupherServer.CMS.Delegate.CommunityCURD do
@default_meta Embeds.CommunityMeta.default_meta()
@article_threads get_config(:article, :threads)

@default_user_meta Accounts.Model.Embeds.UserMeta.default_meta()
@community_normal Constant.pending(:normal)
@community_applying Constant.pending(:applying)

Expand All@@ -37,6 +38,25 @@ defmodule GroupherServer.CMS.Delegate.CommunityCURD do
def read_community(raw, user), do: read_community(raw) |> viewer_has_states(user)
def read_community(raw), do: do_read_community(raw)

def paged_communities(filter, %User{id: user_id, meta: meta}) do
with {:ok, paged_communtiies} <- paged_communities(filter) do
%{entries: entries} = paged_communtiies

entries =
Enum.map(entries, fn community ->
viewer_has_subscribed = community.id in meta.subscribed_communities_ids
%{community | viewer_has_subscribed: viewer_has_subscribed}
end)

%{paged_communtiies | entries: entries} |> done
end
end

def paged_communities(filter) do
filter = filter |> Enum.reject(fn {_k, v} -> is_nil(v) end) |> Enum.into(%{})
Community |> ORM.find_all(filter)
end

@doc """
create a community
"""
Expand DownExpand Up@@ -212,6 +232,22 @@ defmodule GroupherServer.CMS.Delegate.CommunityCURD do
load_community_members(community, CommunityEditor, filters)
end

def community_members(:subscribers, %Community{id: id} = community, filters, %User{meta: meta})
when not is_nil(id) do
with {:ok, members} <- community_members(:subscribers, community, filters) do
user_meta = ensure(meta, @default_user_meta)

%{entries: entries} = members

entries =
Enum.map(entries, fn member ->
%{member | viewer_has_followed: member.id in user_meta.following_user_ids}
end)

%{members | entries: entries} |> done
end
end

def community_members(:subscribers, %Community{id: id} = community, filters)
when not is_nil(id) do
load_community_members(community, CommunitySubscriber, filters)
Expand DownExpand Up@@ -295,7 +331,14 @@ defmodule GroupherServer.CMS.Delegate.CommunityCURD do

defp do_read_community(raw) do
with {:ok, community} <- find_community(raw) do
community |> ORM.read(inc: :views)
case community.meta do
nil ->
{:ok, community} = ORM.update_meta(community, @default_meta)
community |> ORM.read(inc: :views)

_ ->
community |> ORM.read(inc: :views)
end
end
end

Expand Down
20 changes: 10 additions & 10 deletionslib/groupher_server/cms/delegates/community_operation.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -117,8 +117,8 @@ defmodule GroupherServer.CMS.Delegate.CommunityOperation do
|> Multi.run(:update_community_count, fn _, %{subscribed_community: community} ->
CommunityCURD.update_community_count_field(community, user_id, :subscribers_count, :inc)
end)
|> Multi.run(:update_user_subscribe_count, fn _, _ ->
Accounts.update_subscribe_count(user_id)
|> Multi.run(:update_user_subscribe_state, fn _, _ ->
Accounts.update_subscribe_state(user_id)
end)
|> Repo.transaction()
|> result()
Expand All@@ -137,8 +137,8 @@ defmodule GroupherServer.CMS.Delegate.CommunityOperation do
|> Multi.run(:update_community_count, fn _, %{subscribed_community: community} ->
CommunityCURD.update_community_count_field(community, user_id, :subscribers_count, :inc)
end)
|> Multi.run(:update_user_subscribe_count, fn _, _ ->
Accounts.update_subscribe_count(user_id)
|> Multi.run(:update_user_subscribe_state, fn _, _ ->
Accounts.update_subscribe_state(user_id)
end)
|> Repo.transaction()
|> result()
Expand All@@ -158,8 +158,8 @@ defmodule GroupherServer.CMS.Delegate.CommunityOperation do
|> Multi.run(:update_community_count, fn _, _ ->
CommunityCURD.update_community_count_field(community, user_id, :subscribers_count, :dec)
end)
|> Multi.run(:update_user_subscribe_count, fn _, _ ->
Accounts.update_subscribe_count(user_id)
|> Multi.run(:update_user_subscribe_state, fn _, _ ->
Accounts.update_subscribe_state(user_id)
end)
|> Repo.transaction()
|> result()
Expand All@@ -186,8 +186,8 @@ defmodule GroupherServer.CMS.Delegate.CommunityOperation do
|> Multi.run(:update_community_count, fn _, _ ->
CommunityCURD.update_community_count_field(community, user_id, :subscribers_count, :dec)
end)
|> Multi.run(:update_user_subscribe_count, fn _, _ ->
Accounts.update_subscribe_count(user_id)
|> Multi.run(:update_user_subscribe_state, fn _, _ ->
Accounts.update_subscribe_state(user_id)
end)
|> Multi.run(:update_community_geo, fn _, _ ->
update_community_geo(community_id, user_id, remote_ip, :dec)
Expand DownExpand Up@@ -217,8 +217,8 @@ defmodule GroupherServer.CMS.Delegate.CommunityOperation do
|> Multi.run(:update_community_count, fn _, _ ->
CommunityCURD.update_community_count_field(community, user_id, :subscribers_count, :dec)
end)
|> Multi.run(:update_user_subscribe_count, fn _, _ ->
Accounts.update_subscribe_count(user_id)
|> Multi.run(:update_user_subscribe_state, fn _, _ ->
Accounts.update_subscribe_state(user_id)
end)
|> Multi.run(:update_community_geo_city, fn _, _ ->
update_community_geo_map(community.id, city, :dec)
Expand Down
28 changes: 26 additions & 2 deletionslib/groupher_server/cms/delegates/search.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -3,12 +3,17 @@ defmodule GroupherServer.CMS.Delegate.Search do
search for community, post, job ...
"""

import Helper.Utils, only: [done: 1]
import Helper.Utils, only: [done: 1, ensure: 2]
import Ecto.Query, warn: false
import GroupherServer.CMS.Helper.Matcher

alias Helper.ORM
alias GroupherServer.CMS.Model.{Community}

alias GroupherServer.{Accounts, CMS}
alias CMS.Model.{Community}
alias Accounts.Model.{User}

@default_user_meta Accounts.Model.Embeds.UserMeta.default_meta()

@search_items_count 15

Expand All@@ -19,6 +24,25 @@ defmodule GroupherServer.CMS.Delegate.Search do
do_search_communities(Community, title)
end

def search_communities(title, %User{meta: meta}) do
with {:ok, communities} <- do_search_communities(Community, title) do
user_meta = ensure(meta, @default_user_meta)
%{entries: entries} = communities

entries =
Enum.map(entries, fn community ->
viewer_has_subscribed = community.id in user_meta.subscribed_communities_ids
%{community | viewer_has_subscribed: viewer_has_subscribed}
end)

%{communities | entries: entries} |> done
end
end

def search_communities(title, category, %User{meta: meta}) do
search_communities(title, category)
end

def search_communities(title, category) do
from(
c in Community,
Expand Down
23 changes: 23 additions & 0 deletionslib/groupher_server_web/middleware/debug.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
# ---
# Absinthe.Middleware behaviour
# ---
defmodule GroupherServerWeb.Middleware.Debug do
@moduledoc """
authorize gateway, mainly for login check
"""

@behaviour Absinthe.Middleware

import Helper.Utils, only: [handle_absinthe_error: 3]
import Helper.ErrorCode

def call(%{context: %{cur_user: _}} = resolution, _info) do
IO.inspect(resolution.value.original_community.viewer_has_subscribed, label: "## resolution")
resolution
end

def call(resolution, _) do
resolution
|> handle_absinthe_error("Authorize: need login", ecode(:account_login))
end
end
Loading

[8]ページ先頭

©2009-2025 Movatter.jp