Skip to content

Commit

Permalink
Merge 9fbbb03 into 58967d1
Browse files Browse the repository at this point in the history
  • Loading branch information
zven21 committed Oct 7, 2018
2 parents 58967d1 + 9fbbb03 commit 843e51c
Show file tree
Hide file tree
Showing 72 changed files with 601 additions and 427 deletions.
10 changes: 10 additions & 0 deletions .formatter.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[
inputs: [
"{lib,test,priv}/**/*.{ex,exs}",
"mix.exs",
".formatter.exs",
".iex.exs",
".credo.exs"
],
import_deps: [:ecto, :plug, :phoenix]
]
32 changes: 20 additions & 12 deletions lib/mipha/accounts/accounts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ defmodule Mipha.Accounts do

case check_user_password(user, attrs.password) do
true -> {:ok, user}
_ -> {:error, "Failed auth."}
_ -> {:error, "Failed auth."}
end
end

Expand Down Expand Up @@ -181,7 +181,7 @@ defmodule Mipha.Accounts do
defp check_user_password(user, password) do
case user do
nil -> false
_ -> !is_nil(user.password_hash) && Bcrypt.checkpw(password, user.password_hash)
_ -> !is_nil(user.password_hash) && Bcrypt.checkpw(password, user.password_hash)
end
end

Expand Down Expand Up @@ -220,15 +220,16 @@ defmodule Mipha.Accounts do
|> User.update_password_changeset(attrs)
|> Repo.update()

_ -> {:error, "Invalid current password"}
_ ->
{:error, "Invalid current password"}
end
end

@doc """
Mark the current user verified
"""
def mark_as_verified(user) do
attrs = %{"email_verified_at" => Timex.now}
attrs = %{"email_verified_at" => Timex.now()}
update_user(user, attrs)
end

Expand Down Expand Up @@ -284,14 +285,14 @@ defmodule Mipha.Accounts do
def github_repositories(%User{} = user) do
user
|> github_repos_cache_key
|> Store.get!
|> Store.get!()
|> fetch_github_repos(user)
end

def github_repositories(%Team{} = team) do
team
|> github_repos_cache_key
|> Store.get!
|> Store.get!()
|> fetch_github_repos(team)
end

Expand All @@ -300,24 +301,26 @@ defmodule Mipha.Accounts do
repos =
target
|> github_repos_url
|> HTTPoison.get!
|> HTTPoison.get!()
|> handle_response

Store.put!(github_repos_cache_key(target), repos)
repos
end

defp fetch_github_repos(items, _) do
items
end

# 拉取数据,并且 Json 处理
defp handle_response(%HTTPoison.Response{body: body, status_code: 200}) do
body
|> Jason.decode!
|> Enum.map(&(Map.take(&1, ~w(name html_url watchers language description))))
|> Jason.decode!()
|> Enum.map(&Map.take(&1, ~w(name html_url watchers language description)))
|> Enum.sort(&(&1["watchers"] >= &2["watchers"]))
|> Enum.take(10)
end

defp handle_response(%HTTPoison.Response{body: _, status_code: 404}) do
[]
end
Expand All @@ -329,7 +332,9 @@ defmodule Mipha.Accounts do

# 请求获取 github 用户的 repos 的 Url
defp github_repos_url(target) do
"https://api.github.com/users/#{github_handle(target)}/repos?type=owner&sort=pushed&client_id=#{System.get_env("GITHUB_CLIENT_ID")}&client_secret=#{System.get_env("GITHUB_CLIENT_SECRET")}"
"https://api.github.com/users/#{github_handle(target)}/repos?type=owner&sort=pushed&client_id=#{
System.get_env("GITHUB_CLIENT_ID")
}&client_secret=#{System.get_env("GITHUB_CLIENT_SECRET")}"
end

@doc """
Expand All @@ -338,6 +343,7 @@ defmodule Mipha.Accounts do
def github_handle(%User{} = user) do
user.github_handle || user.username
end

def github_handle(%Team{} = team) do
team.github_handle || team.name
end
Expand Down Expand Up @@ -375,13 +381,15 @@ defmodule Mipha.Accounts do
Returns the user update_password changeset
"""
@spec user_update_password_changeset(Map.t()) :: Ecto.Changeset.t()
def user_update_password_changeset(attrs \\ %{}), do: User.update_password_changeset(%User{}, attrs)
def user_update_password_changeset(attrs \\ %{}),
do: User.update_password_changeset(%User{}, attrs)

@doc """
Returns the change user reset password.
"""
@spec change_user_reset_password(User.t(), Map.t()) :: Ecto.Changeset.t()
def change_user_reset_password(%User{} = user, attrs \\ %{}), do: User.reset_password_changeset(user, attrs)
def change_user_reset_password(%User{} = user, attrs \\ %{}),
do: User.reset_password_changeset(user, attrs)

alias Mipha.Accounts.Location

Expand Down
5 changes: 3 additions & 2 deletions lib/mipha/accounts/user.ex
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,10 @@ defmodule Mipha.Accounts.User do
|> cast(attrs, permitted_attrs)
|> validate_required(register_attrs)
|> validate_length(:username, min: 3, max: 12)
|> validate_format(:username, Regexp.username)
|> validate_format(:username, Regexp.username())
|> unique_constraint(:username)
|> validate_length(:email, min: 1, max: 20)
|> validate_format(:email, Regexp.email)
|> validate_format(:email, Regexp.email())
|> unique_constraint(:email)
|> put_pass_hash()
end
Expand Down Expand Up @@ -188,6 +188,7 @@ defmodule Mipha.Accounts.User do
case changeset do
%Ecto.Changeset{valid?: true, changes: %{password: password}} ->
put_change(changeset, :password_hash, Bcrypt.hashpwsalt(password))

_ ->
changeset
end
Expand Down
4 changes: 2 additions & 2 deletions lib/mipha/collections/queries.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ defmodule Mipha.Collections.Queries do

defp filter_from_clauses(opts) do
cond do
Keyword.has_key?(opts, :user) -> opts |> Keyword.get(:user) |> Collection.by_user
Keyword.has_key?(opts, :topic) -> opts |> Keyword.get(:topic) |> Collection.by_topic
Keyword.has_key?(opts, :user) -> opts |> Keyword.get(:user) |> Collection.by_user()
Keyword.has_key?(opts, :topic) -> opts |> Keyword.get(:topic) |> Collection.by_topic()
true -> Collection
end
end
Expand Down
10 changes: 7 additions & 3 deletions lib/mipha/follows/follows.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ defmodule Mipha.Follows do

import Ecto.Query, warn: false
alias Ecto.Multi

alias Mipha.{
Repo,
Follows,
Accounts,
Notifications
}

alias Follows.Follow
alias Accounts.User

Expand Down Expand Up @@ -124,7 +126,7 @@ defmodule Mipha.Follows do
def delete_follow(clauses) when length(clauses) == 2 do
clauses
|> get_follow
|> Repo.delete
|> Repo.delete()
end

@doc """
Expand Down Expand Up @@ -155,6 +157,7 @@ defmodule Mipha.Follows do
@spec unfollow_user(Keyword.t()) :: {:ok, Follow.t()} | {:error, any()}
def unfollow_user(follower: follower, user: user) do
opts = [follower: follower, user: user]

if can_follow?(opts) do
if has_followed?(opts) do
delete_follow(user_id: user.id, follower_id: follower.id)
Expand All @@ -180,6 +183,7 @@ defmodule Mipha.Follows do
@spec follow_user(Keyword.t()) :: {:ok, Follow.t()} | {:error, any()}
def follow_user(follower: follower, user: user) do
opts = [follower: follower, user: user]

if can_follow?(opts) do
if has_followed?(opts) do
{:error, "Follow already."}
Expand All @@ -200,7 +204,7 @@ defmodule Mipha.Follows do
%User{id: follower_id} = Keyword.get(clauses, :follower)
%User{id: user_id} = Keyword.get(clauses, :user)

user_id == follower_id && false || true
(user_id == follower_id && false) || true
end

@doc """
Expand Down Expand Up @@ -292,7 +296,7 @@ defmodule Mipha.Follows do
query =
clauses
|> Keyword.get(:user)
|> Follow.by_user
|> Follow.by_user()

query
|> join(:inner, [c], u in assoc(c, :follower))
Expand Down
4 changes: 2 additions & 2 deletions lib/mipha/follows/queries.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ defmodule Mipha.Follows.Queries do

defp filter_from_clauses(opts) do
cond do
Keyword.has_key?(opts, :follower) -> opts |> Keyword.get(:follower) |> Follow.by_follower
Keyword.has_key?(opts, :user) -> opts |> Keyword.get(:user) |> Follow.by_user
Keyword.has_key?(opts, :follower) -> opts |> Keyword.get(:follower) |> Follow.by_follower()
Keyword.has_key?(opts, :user) -> opts |> Keyword.get(:user) |> Follow.by_user()
true -> Follow
end
end
Expand Down
10 changes: 5 additions & 5 deletions lib/mipha/markdown/embed_video_replacer.ex
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
defmodule Mipha.Markdown.EmbedVideoReplacer do
@moduledoc false

@youtube_url_regex ~r{(\s|^|<div>|<br>)(https?://)(www.)?(youtube\.com/watch\?v=|youtu\.be/|youtube\.com/watch\?feature=player_embedded&v=)([A-Za-z0-9_\-]*)(\&\S+)?(\?\S+)?}
@youku_url_regex ~r{(\s|^|<div>|<br>)(http?://)(v\.youku\.com/v_show/id_)([a-zA-Z0-9\-_\=]*)(\.html)(\&\S+)?(\?\S+)?}
@youku_base_url "//player.youku.com/embed/"
@youtube_base_url "//www.youtube.com/embed/"
@youtube_url_regex ~r{(\s|^|<div>|<br>)(https?://)(www.)?(youtube\.com/watch\?v=|youtu\.be/|youtube\.com/watch\?feature=player_embedded&v=)([A-Za-z0-9_\-]*)(\&\S+)?(\?\S+)?}
@youku_url_regex ~r{(\s|^|<div>|<br>)(http?://)(v\.youku\.com/v_show/id_)([a-zA-Z0-9\-_\=]*)(\.html)(\&\S+)?(\?\S+)?}
@youku_base_url "//player.youku.com/embed/"
@youtube_base_url "//www.youtube.com/embed/"

def run(body) do
cond do
Regex.match?(@youtube_url_regex, body) == true ->
Regex.match?(@youtube_url_regex, body) == true ->
@youtube_url_regex
|> Regex.replace(body, &youtube_replacer/1)

Expand Down

0 comments on commit 843e51c

Please sign in to comment.