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.

chore(deps): bump sentry from 6.4.1 to 7.0.2#92

Closed
dependabot-preview wants to merge170 commits intodevfromdependabot/hex/sentry-7.0.2
Closed
Show file tree
Hide file tree
Changes fromall commits
Commits
Show all changes
170 commits
Select commitHold shift + click to select a range
14d04ab
build(deps-dev): bump excoveralls from 0.9.2 to 0.10.0
dependabot-supportAug 27, 2018
64b769c
build(deps): bump phoenix_ecto from 3.3.0 to 3.4.0
dependabot-supportSep 10, 2018
ab0c67b
build(deps): bump dataloader from 1.0.3 to 1.0.4
dependabot-supportSep 17, 2018
1287541
build(deps-dev): bump inch_ex from 1.0.0 to 1.0.1
dependabot-supportSep 17, 2018
0da40c2
Merge pull request #73 from coderplanets/dependabot/hex/dataloader-1.0.4
mydearxymSep 24, 2018
41e5e54
Merge pull request #74 from coderplanets/dependabot/hex/inch_ex-1.0.1
mydearxymSep 24, 2018
4c05c6c
Merge pull request #70 from coderplanets/dependabot/hex/phoenix_ecto-…
mydearxymSep 24, 2018
65d2ac2
build(deps): bump guardian from 1.1.0 to 1.1.1
dependabot-supportSep 24, 2018
ed86377
build(deps-dev): bump mix_test_watch from 0.8.0 to 0.9.0
dependabot-supportSep 24, 2018
e2a22ca
build(deps-dev): bump credo from 0.10.0 to 0.10.1
dependabot-supportSep 24, 2018
12fa5f1
Merge pull request #65 from coderplanets/dependabot/hex/guardian-1.1.1
mydearxymSep 24, 2018
06fd5b7
Merge pull request #66 from coderplanets/dependabot/hex/excoveralls-0…
mydearxymSep 24, 2018
eb990fd
Merge pull request #71 from coderplanets/dependabot/hex/mix_test_watc…
mydearxymSep 24, 2018
9ac99e7
Merge pull request #72 from coderplanets/dependabot/hex/credo-0.10.1
mydearxymSep 24, 2018
302104b
build: production
mydearxymSep 23, 2018
b0e7063
ci(fix apollo schema check): schema check broken fix
mydearxymSep 24, 2018
dc46e65
chore(deps): upgrade
mydearxymSep 24, 2018
b443237
build(postgresql): update to 10
mydearxymSep 24, 2018
c0c6d4a
ci(postgresql): fallback to 9.4
mydearxymSep 24, 2018
214a66a
fix(profile test): fix work backgorund test
mydearxymSep 24, 2018
f8f86aa
build: production
mydearxymSep 24, 2018
6d39990
fix(cms video): missing field publish_at
mydearxymSep 25, 2018
6b59ffa
feat(geoinfo): user info geo & community geo
mydearxymSep 26, 2018
531bdcc
Merge branch 'geo-service' into dev
mydearxymSep 26, 2018
584fcab
chore(wip clean up): clean up wip
mydearxymSep 26, 2018
5b635cd
Update README.md
mydearxymSep 28, 2018
b5d07cf
fix: add missing cms_thread
mydearxymSep 28, 2018
a32ffdc
refactor(cms.repo): use new repo fields
mydearxymSep 28, 2018
76ead2f
refactor(repo thread): change fields to match frontend
mydearxymSep 29, 2018
3af77fc
Merge branch 'repo-refactor' into dev
mydearxymSep 29, 2018
c932c26
feat(repo thread): debug with frontend, feature is done
mydearxymSep 29, 2018
c02018c
Merge branch 'repo-refactor' into dev
mydearxymSep 29, 2018
35cf03b
refactor(comments): replace body:string -> body:text
mydearxymSep 30, 2018
d98d512
feat(video comments): basic curd comments
mydearxymSep 30, 2018
94cbaec
feat(video comments): add like/dilike
mydearxymSep 30, 2018
ae7c30b
feat(repo comment test): done with test cover
mydearxymSep 30, 2018
e7e010c
Merge branch 'comments-for-other-threads' into dev
mydearxymSep 30, 2018
be7ee5e
refactor(comments): add a bug tag for spec case
mydearxymSep 30, 2018
cb895ca
Merge branch 'comments-for-other-threads' into dev
mydearxymSep 30, 2018
9b5ad22
feat(wiki thread): add wiki with test
mydearxymSep 30, 2018
bec08ec
feat(wiki thread): add basic wiki query
mydearxymSep 30, 2018
8fc9f67
feat(wiki thread): add sync content and add contributor
mydearxymSep 30, 2018
37a2eca
chore(wiki thread): clean up wiki
mydearxymSep 30, 2018
b303c10
feat(cheatsheet): add feature just like wiki
mydearxymSep 30, 2018
8136cce
chore(clean up): warnings and wip tag
mydearxymSep 30, 2018
05805e0
refactor(factory): auto camelize factory attrs if need
mydearxymOct 1, 2018
1daab98
Merge branch 'mapkeys' into dev
mydearxymOct 1, 2018
266aa53
feat(json encode): config Jason in absinthe , ecto
mydearxymOct 2, 2018
00fb4da
refactor(test): clean up warning
mydearxymOct 2, 2018
b250f5c
style: fmt
mydearxymOct 2, 2018
b8ff8df
fix: missing field
mydearxymOct 2, 2018
e10a33e
Merge branch 'wiki-thread' into dev
mydearxymOct 2, 2018
04d7ac4
refactor(reactions): extract comment fields and fix job reaction
mydearxymOct 7, 2018
531e1cf
Merge branch 'cheatsheet-thread' into dev
mydearxymOct 7, 2018
85d8b67
feat(video reactions): add basic favorite / star
mydearxymOct 7, 2018
a344032
chore(reactions): add reactions test
mydearxymOct 7, 2018
fb5847b
fix(video reaction): job_id -> video_id
mydearxymOct 7, 2018
5177dfe
Merge branch 'reactions' into dev
mydearxymOct 7, 2018
5850812
test(video comments): add like/dislike tests
mydearxymOct 8, 2018
8c51ac7
refactor(rename): comments -> pagedComments
mydearxymOct 8, 2018
5050661
test(repo tests): badic query tests
mydearxymOct 8, 2018
fef0793
test: add mutation tests for video/repo comments
mydearxymOct 8, 2018
5f6bee2
feat(job comment): add like / dislike & test
mydearxymOct 8, 2018
1dfd5d9
chore(clean up): remove @wip tag
mydearxymOct 8, 2018
f0c4714
Merge branch 'comments' into dev
mydearxymOct 8, 2018
e4fa80d
refactor(reaction users): more tests on paged reaction users
mydearxymOct 9, 2018
468b8c1
chore(clean up): remove @wip & add @doc
mydearxymOct 9, 2018
6cbc9ce
fix: followersCount makes right by rm dataloader
mydearxymOct 10, 2018
199a447
style: fmt
mydearxymOct 10, 2018
ef71466
refactor(user favrites cats): enhance support for jobs / videos
mydearxymOct 11, 2018
b462963
style: fmt
mydearxymOct 11, 2018
3651fdd
Merge branch 'reaction-users' into dev
mydearxymOct 11, 2018
81c7497
feat(user achievement): add source contribute info
mydearxymOct 11, 2018
d93a11b
feat(user signin): init a empty achievement when new signin
mydearxymOct 12, 2018
c45b72b
feat(favorite cats): add last_updated field
mydearxymOct 12, 2018
1cee80b
feat(user): add editable-communities query field
mydearxymOct 12, 2018
f0addbf
refactor(user query): rm unneed filter on some query
mydearxymOct 12, 2018
1906d2b
feat(user page): user published contents done right
mydearxymOct 12, 2018
7c2c86d
chore(clean up): rm :wip tag
mydearxymOct 12, 2018
4e6acca
test(stared contents): add more test on query part
mydearxymOct 12, 2018
8c2e339
refactor(uer favorites test): extract total_count -> @total_count
mydearxymOct 12, 2018
029d502
chore(tests): clean warnings & rm :wip tag
mydearxymOct 12, 2018
113b8c5
Merge branch 'userpage' into dev
mydearxymOct 12, 2018
1f818cf
test(thread comments): add test for particlepators
mydearxymOct 14, 2018
40fea00
feat(cms.contents): add favroted_category_id query field
mydearxymOct 14, 2018
3f49934
test: restore some job tests & clean up
mydearxymOct 14, 2018
ebc85cd
fix(user favorite): totalcount mismatch when switch cats
mydearxymOct 14, 2018
08d30e2
Merge branch 'userpage' into dev
mydearxymOct 14, 2018
bbda464
fix(user reputation): fix reputation calc by refactor
mydearxymOct 15, 2018
1451208
refactor(contents reaction): block favorite action from react contents
mydearxymOct 15, 2018
adde568
Merge branch 'userpage' into dev
mydearxymOct 15, 2018
c885767
test(favrotie reaction tests): removed, use favroted category instead
mydearxymOct 16, 2018
4e27a43
feat(user published): publisehd comments
mydearxymOct 16, 2018
68488e7
refactor(comments): extract comments macro
mydearxymOct 16, 2018
7ec06a9
chore: cleanup warnings
mydearxymOct 16, 2018
eac987a
refactor(published comments): support parent content info
mydearxymOct 16, 2018
ade4a93
Merge branch 'published' into dev
mydearxymOct 16, 2018
49c4590
fix(cms passport): return empty instead of nil
mydearxymOct 17, 2018
8d62d88
feat(comments): add comemntsCount for job/video/repo
mydearxymOct 17, 2018
5513c76
fix: missing tags
mydearxymOct 20, 2018
046255d
Merge branch 'auth' into dev
mydearxymOct 20, 2018
10ce7d0
feat(reactions): repo favorite with basic test
mydearxymOct 20, 2018
b97ae50
test(reactions): more test & doc for repo-favorite
mydearxymOct 20, 2018
c20a171
chore(cleanup): rm wip tags
mydearxymOct 20, 2018
7a16b4b
Merge branch 'reactions' into dev
mydearxymOct 20, 2018
cd668d3
test(repo reaction): remove unneed tests on favroites
mydearxymOct 20, 2018
392b7f5
Merge branch 'reactions' into dev
mydearxymOct 20, 2018
752af96
feat(comments): comments participators for other
mydearxymOct 20, 2018
723ff2b
Merge branch 'comments' into dev
mydearxymOct 20, 2018
ca87c12
feat(cms repo): add missing favorite fields
mydearxymOct 20, 2018
4c46c88
Merge branch 'reactions' into dev
mydearxymOct 20, 2018
8529c04
chore(clean up): wip tag
mydearxymOct 20, 2018
89448da
chore(docs): comments dataloader
mydearxymOct 20, 2018
e059058
Merge branch 'comments' into dev
mydearxymOct 20, 2018
8bd0761
refactor(comments): add paged participators
mydearxymOct 20, 2018
d18e0fe
Merge branch 'comments' into dev
mydearxymOct 20, 2018
0e319b4
test: missing fields
mydearxymOct 22, 2018
68020fd
fix(follow choas): follow / undofollow done right
mydearxymOct 22, 2018
00d7783
chore(clean up): test file warnings
mydearxymOct 22, 2018
ac8c503
test(favoreted repos): add missing test for f-repos
mydearxymOct 22, 2018
14f14bc
refactor(cms repos): rename last_fetch_time -> last_sync
mydearxymOct 22, 2018
29d508a
Merge branch 'repo' into dev
mydearxymOct 22, 2018
3e8990c
fix(repo favrote): totalCount query
mydearxymOct 22, 2018
87b9ba5
feat(user): add views field
mydearxymOct 23, 2018
327c3ef
feat(posts): add copy_right fields
mydearxymOct 23, 2018
90056bf
fix: missing copy_right field
mydearxymOct 23, 2018
9e7d122
Merge branch 'userpage' into dev
mydearxymOct 23, 2018
b64f9f5
feat(viewers): add viewer_has_viewed to posts
mydearxymOct 23, 2018
d149449
feat(has_viewed): add for jobs
mydearxymOct 23, 2018
b428bc8
feat(viewer_has_viewed): for videos
mydearxymOct 23, 2018
9dcbb1f
feat(viewer_has_viewd): for repos
mydearxymOct 23, 2018
9a12b5f
chore(clean up): wip tags
mydearxymOct 23, 2018
fefb700
Merge branch 'content-viewer' into dev
mydearxymOct 23, 2018
8902126
fix(contents viewer): add edge case to test dataloader
mydearxymOct 24, 2018
a2259ae
Merge branch 'content-viewer' into dev
mydearxymOct 24, 2018
c209f34
refactor(tests): improve with multi argss
mydearxymOct 24, 2018
ee761e8
feat(job): simplify job atts, old one is complex
mydearxymOct 25, 2018
610722e
Merge branch 'jobs' into dev
mydearxymOct 25, 2018
455edf2
fix(job): missing fields
mydearxymOct 25, 2018
4f29efa
fix(graphql parse error): fix graphql error crash server
mydearxymOct 25, 2018
3be43da
fix(job create): clean up & refactor
mydearxymOct 25, 2018
70cdfb1
Merge branch 'jobs' into dev
mydearxymOct 25, 2018
8e55f30
feat(topic): basic topic concept
mydearxymOct 28, 2018
1747494
refactor(topic on posts): impl topic & test only on posts
mydearxymOct 28, 2018
0024269
feat(tags): add topic support
mydearxymOct 28, 2018
63ed78a
chore(clean up): wip tag
mydearxymOct 28, 2018
f5d630c
Merge branch 'topic' into dev
mydearxymOct 28, 2018
3867185
fix(topic): default args error when topic create
mydearxymOct 30, 2018
5e2a0b4
refactor(general): make thread & topic downcase in db
mydearxymOct 31, 2018
e68b08b
fix(passport): community check use raw, not title
mydearxymOct 31, 2018
9697c41
refactor(topic): change default topic to posts
mydearxymNov 1, 2018
d6510de
chore(clean up): wip tag
mydearxymNov 1, 2018
cba7826
refactor(accounts): make inline-subcommunities return paged-version
mydearxymNov 1, 2018
73159f5
refactor(account): remove account endpoint, use general user
mydearxymNov 1, 2018
87d00c7
fix(subscribed community): mismatch cur_user
mydearxymNov 1, 2018
6b938c6
fix: remote ip mismatch
mydearxymNov 2, 2018
f898726
Merge branch 'topic' into dev
mydearxymNov 2, 2018
954381f
feat(content counts): add & refactor cms contents count
mydearxymNov 2, 2018
2858be7
chore(clean up): warnings
mydearxymNov 2, 2018
43861e6
Merge branch 'totalCount' into dev
mydearxymNov 2, 2018
eb29d67
refactor(pin contents): use seperate table to pin contens
mydearxymNov 2, 2018
83926cc
refactor: reduce boil-code
mydearxymNov 2, 2018
c02d827
refactor(pined contents): remove old pin state from communityFlags
mydearxymNov 2, 2018
a820979
chore(clean up): wip tags
mydearxymNov 2, 2018
6756e7c
Merge branch 'flags' into dev
mydearxymNov 2, 2018
32c6e2f
refactor(customization): basic set/get workflow
mydearxymNov 3, 2018
3260a71
Merge branch 'customization' into dev
mydearxymNov 3, 2018
05058d2
fix(c11n): filter nil when user has no c11n & clean up
mydearxymNov 4, 2018
725d403
Merge branch 'customization' into dev
mydearxymNov 4, 2018
448a2fd
chore(deps): bump sentry from 6.4.1 to 7.0.2
dependabot-supportNov 5, 2018
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: 2 additions & 0 deletionsMakefile
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -125,6 +125,8 @@ test.watch.wip:
mix test.watch --only wip
test.watch.wip2:
mix test.watch --only wip2
test.watch.bug:
mix test.watch --only bug
test.db_reset:
env MIX_ENV=test mix ecto.drop
env MIX_ENV=test mix ecto.create
Expand Down
4 changes: 4 additions & 0 deletionsMakefile.include.mk
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -130,6 +130,10 @@ define test.help
@echo " ....................................................."
@echo " test.watch.wip : run @wip test in watch mode"
@echo " ....................................................."
@echo " test.watch.wip2 : shortcut for lots of @wip around"
@echo " ....................................................."
@echo " test.watch.bug : sometimes fails for unkown reason"
@echo " ....................................................."
@echo " test.db_reset : reset test database"
@echo " | needed when add new migration"
@echo " ....................................................."
Expand Down
2 changes: 1 addition & 1 deletionREADME.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@

![CPS Brand](https://github.com/mydearxym/mastani_server/blob/dev/docs/snapshots/cps_logo_md.png)
![CPS Brand](https://github.com/mydearxym/mastani_server/blob/dev/docs/snapshots/cps_logo_md.png?raw=true)
=========
[![Build Status](https://travis-ci.org/coderplanets/coderplanets_server.svg?branch=dev)](https://travis-ci.org/coderplanets/coderplanets_server)
[![codecov](https://codecov.io/gh/coderplanets/coderplanets_server/branch/dev/graph/badge.svg)](https://codecov.io/gh/coderplanets/coderplanets_server)
Expand Down
11 changes: 11 additions & 0 deletionsconfig/config.exs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -21,6 +21,7 @@ config :logger, :console,
metadata: [:request_id]

config :phoenix, :format_encoders, json: Jason
config :ecto, json_library: Jason

# TODO move this config to secret later
config :mastani_server, Helper.Guardian,
Expand DownExpand Up@@ -50,6 +51,16 @@ config :mastani_server, :general,
user_achieve_favorite_weight: 2,
user_achieve_follow_weight: 3

config :mastani_server, :customization,
theme: "cyan",
community_chart: false,
brainwash_free: false,
banner_layout: "digest",
contents_layout: "digest",
content_divider: false,
mark_viewed: true,
display_density: "20"

config :mastani_server, MastaniServerWeb.Gettext, default_locale: "zh_CN", locales: ~w(en zh_CN)

import_config "#{Mix.env()}.exs"
Expand Down
Binary file modifieddeploy/production/api_server.tar.gz
View file
Open in desktop
Binary file not shown.
85 changes: 85 additions & 0 deletionslib/helper/PublicIpPlug.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
# https://www.cogini.com/blog/getting-the-client-public-ip-address-in-phoenix/
defmodule Helper.PublicIpPlug do
@moduledoc "Get public IP address of request from x-forwarded-for header"
@behaviour Plug
@app :mastani_server

def init(opts), do: opts

def call(%{assigns: %{ip: _}} = conn, _opts), do: conn

def call(conn, _opts) do
process(conn, Plug.Conn.get_req_header(conn, "x-forwarded-for"))
end

def process(conn, []) do
Plug.Conn.assign(conn, :ip, to_string(:inet.ntoa(get_peer_ip(conn))))
end

def process(conn, vals) do
if Application.get_env(@app, :trust_x_forwarded_for, false) do
ip_address = get_ip_address(conn, vals)

# Rewrite standard remote_ip field with value from header
# See https://hexdocs.pm/plug/Plug.Conn.html
conn = %{conn | remote_ip: ip_address}

Plug.Conn.assign(conn, :ip, to_string(:inet.ntoa(ip_address)))
else
Plug.Conn.assign(conn, :ip, to_string(:inet.ntoa(get_peer_ip(conn))))
end
end

defp get_ip_address(conn, vals)
defp get_ip_address(conn, []), do: get_peer_ip(conn)

defp get_ip_address(conn, [val | _]) do
# Split into multiple values
comps =
val
|> String.split(~r{\s*,\s*}, trim: true)
# Get rid of "unknown" values
|> Enum.filter(&(&1 != "unknown"))
# Split IP from port, if any
|> Enum.map(&hd(String.split(&1, ":")))
# Filter out blanks
|> Enum.filter(&(&1 != ""))
# Parse address into :inet.ip_address tuple
|> Enum.map(&parse_address(&1))
# Elminate internal IP addreses, e.g. 192.168.1.1
|> Enum.filter(&is_public_ip(&1))

case comps do
[] -> get_peer_ip(conn)
[comp | _] -> comp
end
end

@spec get_peer_ip(Plug.Conn.t()) :: :inet.ip_address()
defp get_peer_ip(conn) do
{ip, _port} = conn.peer
ip
end

@spec parse_address(String.t()) :: :inet.ip_address()
defp parse_address(ip) do
case :inet.parse_ipv4strict_address(to_charlist(ip)) do
{:ok, ip_address} -> ip_address
{:error, :einval} -> :einval
end
end

# Whether the input is a valid, public IP address
# http://en.wikipedia.org/wiki/Private_network
@spec is_public_ip(:inet.ip_address() | atom) :: boolean
defp is_public_ip(ip_address) do
case ip_address do
{10, _, _, _} -> false
{192, 168, _, _} -> false
{172, second, _, _} when second >= 16 and second <= 31 -> false
{127, 0, 0, _} -> false
{_, _, _, _} -> true
:einval -> false
end
end
end
32 changes: 32 additions & 0 deletionslib/helper/query_builder.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -27,6 +27,24 @@ defmodule Helper.QueryBuilder do
|> select([f], count(f.id))
end

def members_pack(queryable, %{count: _, type: :job}) do
queryable
|> group_by([f], f.job_id)
|> select([f], count(f.id))
end

def members_pack(queryable, %{count: _, type: :video}) do
queryable
|> group_by([f], f.video_id)
|> select([f], count(f.id))
end

def members_pack(queryable, %{count: _, type: :repo}) do
queryable
|> group_by([f], f.repo_id)
|> select([f], count(f.id))
end

def members_pack(queryable, %{count: _, type: :community}) do
queryable
|> group_by([f], f.community_id)
Expand DownExpand Up@@ -167,6 +185,13 @@ defmodule Helper.QueryBuilder do
where: t.title == ^tag_name
)

{:topic, topic}, queryable ->
from(
q in queryable,
join: t in assoc(q, :topics),
where: t.raw == ^topic
)

{:category, catetory_raw}, queryable ->
from(
q in queryable,
Expand All@@ -181,6 +206,13 @@ defmodule Helper.QueryBuilder do
where: t.raw == ^community_raw
)

{:one_community, community_raw}, queryable ->
from(
q in queryable,
join: t in assoc(q, :community),
where: t.raw == ^community_raw
)

{:first, first}, queryable ->
queryable |> limit(^first)

Expand Down
59 changes: 59 additions & 0 deletionslib/helper/radar_search.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
defmodule Helper.RadarSearch do
@moduledoc """
find city info by ip
refer: https://lbs.amap.com/api/webservice/guide/api/ipconfig/?sug_index=0
"""
use Tesla, only: [:get]
import Helper.Utils, only: [get_config: 2]

@endpoint "https://restapi.amap.com/v3/ip"
@ip_service_key get_config(:radar_search, :ip_service)
@timeout_limit 5000

# plug(Tesla.Middleware.BaseUrl, "https://restapi.amap.com/v3/ip")
plug(Tesla.Middleware.Retry, delay: 200, max_retries: 2)
plug(Tesla.Middleware.Timeout, timeout: @timeout_limit)
plug(Tesla.Middleware.JSON)

@doc """
this is only match fail situation in test
"""
def locate_city(ip \\ "14.196.0.0")

def locate_city(:fake_ip) do
{:error, "not found"}
end

# http://ip.yqie.com/search.aspx?searchword=%E6%88%90%E9%83%BD%E5%B8%82
def locate_city(ip) do
query = [ip: ip, key: @ip_service_key]

with true <- Mix.env() !== :test do
case get(@endpoint, query: query) do
%{status: 200, body: body} ->
handle_result({:ok, body["city"]})

_ ->
{:error, "error"}
end
else
_ ->
{:ok, "成都"}
# {:error, "error"}
end
end

defp handle_result({:ok, result}) do
case result do
[] -> {:error, "not found"}
_ -> cut_tail({:ok, result})
end
end

defp cut_tail({:ok, result}) do
case String.last(result) == "市" do
true -> {:ok, String.trim_trailing(result, "市")}
false -> {:ok, result}
end
end
end
72 changes: 66 additions & 6 deletionslib/helper/utils.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -6,10 +6,20 @@ defmodule Helper.Utils do
import Helper.ErrorHandler
import Helper.ErrorCode

def get_config(section, key, app \\ :mastani_server) do
def get_config(section, key, app \\ :mastani_server)

def get_config(section, :all, app) do
app
|> Application.get_env(section)
|> case do
nil -> ""
config -> config
end
end

def get_config(section, key, app) do
app
|> Application.get_env(section)
# |> IO.inspect(label: "debug ci")
|> case do
nil -> ""
config -> Keyword.get(config, key)
Expand All@@ -23,6 +33,7 @@ defmodule Helper.Utils do
def done(_, :boolean), do: {:ok, true}
def done(nil, err_msg), do: {:error, err_msg}
def done({:ok, _}, with: result), do: {:ok, result}
def done({:error, error}, with: _result), do: {:error, error}

def done({:ok, %{id: id}}, :status), do: {:ok, %{done: true, id: id}}
def done({:error, _}, :status), do: {:ok, %{done: false}}
Expand DownExpand Up@@ -69,6 +80,39 @@ defmodule Helper.Utils do
map |> Enum.reduce(%{}, fn {key, val}, acc -> Map.put(acc, to_string(key), val) end)
end

@doc """
Recursivly camelize the map keys
usage: convert factory attrs to used for simu Graphql parmas
"""
def camelize_map_key(map) do
map_list =
Enum.map(map, fn {k, v} ->
v =
cond do
is_datetime?(v) ->
DateTime.to_iso8601(v)

is_map(v) ->
camelize_map_key(safe_map(v))

true ->
v
end

map_to_camel({k, v})
end)

Enum.into(map_list, %{})
end

defp safe_map(%{__struct__: _} = map), do: Map.from_struct(map)
defp safe_map(map), do: map

defp map_to_camel({k, v}), do: {Recase.to_camel(to_string(k)), v}

def is_datetime?(%DateTime{}), do: true
def is_datetime?(_), do: false

def deep_merge(left, right) do
Map.merge(left, right, &deep_resolve/3)
end
Expand All@@ -87,10 +131,15 @@ defmodule Helper.Utils do
def map_atom_value(attrs, :string) do
results =
Enum.map(attrs, fn {k, v} ->
if is_atom(v) do
{k, to_string(v)}
else
{k, v}
cond do
v == true or v == false ->
{k, v}

is_atom(v) ->
{k, v |> to_string() |> String.downcase()}

true ->
{k, v}
end
end)

Expand All@@ -106,4 +155,15 @@ defmodule Helper.Utils do
# NOT a map. We fall back to standard merge behavior, preferring
# the value on the right.
defp deep_resolve(_key, _left, right), do: right

@doc """
["a", "b", "c", "c"] => %{"a" => 1, "b" => 1, "c" => 2}
"""
def count_words(words) when is_list(words) do
Enum.reduce(words, %{}, &update_word_count/2)
end

defp update_word_count(word, acc) do
Map.update(acc, to_string(word), 1, &(&1 + 1))
end
end
Loading

[8]ページ先頭

©2009-2025 Movatter.jp