Skip to content

Commit

Permalink
Merge pull request #109 from CaptainFact/staging
Browse files Browse the repository at this point in the history
Release 0.9.3
  • Loading branch information
Betree authored Jan 5, 2019
2 parents e182aaf + ee6b2b1 commit aaa8cfb
Show file tree
Hide file tree
Showing 19 changed files with 143 additions and 62 deletions.
4 changes: 3 additions & 1 deletion apps/cf/config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ config :cf,
env: Mix.env(),
ecto_repos: [DB.Repo],
oauth: [facebook: []],
invitation_system: false
invitation_system: false,
soft_limitations_period: 15 * 60,
hard_limitations_period: 3 * 60 * 60

# Configure scheduler
config :cf, CF.Scheduler,
Expand Down
33 changes: 27 additions & 6 deletions apps/cf/lib/accounts/user_permissions.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ defmodule CF.Accounts.UserPermissions do
defexception message: "forbidden", plug_status: 403
end

# 24 hours
@daily_limit 24 * 60 * 60

@error_not_enough_reputation "not_enough_reputation"
@error_limit_reached "limit_reached"

Expand Down Expand Up @@ -48,7 +45,7 @@ defmodule CF.Accounts.UserPermissions do
iex> alias CF.Accounts.UserPermissions
iex> user = DB.Factory.insert(:user, %{reputation: 45})
iex> UserPermissions.check(user, :create, :comment)
{:ok, 20}
{:ok, 7}
iex> UserPermissions.check(%{user | reputation: -42}, :remove, :statement)
{:error, "not_enough_reputation"}
"""
Expand Down Expand Up @@ -109,14 +106,38 @@ defmodule CF.Accounts.UserPermissions do
@doc """
Count the number of occurences of this user / action type in limited perdiod.
"""
def action_count(user, action_type = :add, entity = :video) do
Actions.count(user, action_type, entity, hard_limitations_period())
end

def action_count(user, action_type, entity) do
if is_wildcard_limitation(action_type) do
Actions.count_wildcard(user, action_type, @daily_limit)
Actions.count_wildcard(user, action_type, soft_limitations_period())
else
Actions.count(user, action_type, entity, @daily_limit)
Actions.count(user, action_type, entity, soft_limitations_period())
end
end

@fifteen_minutes 15 * 60
@three_hours 3 * 60 * 60

@doc """
Get the limit, in seconds, on which the actions are analyzed for limitations.
This uses a variable from env to make it configurable on the fly.
"""
def soft_limitations_period,
do: Application.get_env(:cf, :soft_limitations_period, @fifteen_minutes)

@doc """
Same as `soft_limitations_period/0` but less permissive, only used when adding
new videos.
"""
def hard_limitations_period,
do: Application.get_env(:cf, :hard_limitations_period, @three_hours)

@doc """
Get the limitations for a given user and action type.
"""
def limitation(user = %User{}, action_type, entity) do
case level(user) do
-1 ->
Expand Down
20 changes: 18 additions & 2 deletions apps/cf/lib/videos/videos.ex
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,19 @@ defmodule CF.Videos do
def videos_list(filters, false),
do: Repo.all(Video.query_list(Video, filters))

@doc """
Get videos added by given user. This will return all videos, included the ones
marked as `unlisted`.
"""
def added_by_user(user, paginate_options \\ []) do
Video
|> join(:inner, [v], a in DB.Schema.UserAction, a.video_id == v.id)
|> where([_, a], a.user_id == ^user.id)
|> where([_, a], a.type == ^:add and a.entity == ^:video)
|> DB.Query.order_by_last_inserted_desc()
|> Repo.paginate(paginate_options)
end

@doc """
Return the corresponding video if it has already been added, `nil` otherwise
"""
Expand All @@ -61,14 +74,17 @@ defmodule CF.Videos do
Returns video if success or {:error, reason} if something bad append.
Can also throw if bad permissions.
"""
def create!(user, video_url, is_partner \\ nil) do
def create!(user, video_url, params \\ []) do
UserPermissions.check!(user, :add, :video)

with metadata_fetcher when not is_nil(metadata_fetcher) <- get_metadata_fetcher(video_url),
{:ok, metadata} <- metadata_fetcher.(video_url) do
# Videos posted by publishers are recorded as partner unless explicitely
# specified otherwise (false)
base_video = %Video{is_partner: user.is_publisher && is_partner != false}
base_video = %Video{
is_partner: user.is_publisher && Keyword.get(params, :is_partner) != false,
unlisted: Keyword.get(params, :unlisted, false)
}

Multi.new()
|> Multi.insert(:video_without_hash_id, Video.changeset(base_video, metadata))
Expand Down
2 changes: 1 addition & 1 deletion apps/cf/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule CF.Mixfile do
def project do
[
app: :cf,
version: "0.9.2",
version: "0.9.3",
build_path: "../../_build",
compilers: [:phoenix, :gettext] ++ Mix.compilers(),
config_path: "../../config/config.exs",
Expand Down
20 changes: 10 additions & 10 deletions apps/cf/priv/limitations.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,33 @@
#-------------------------------------------------------------------------
# Specific limitations by entity type
create:
comment: [ 2 , 5 , 10 , 20 , 45 , 75 , 100 , 150 , 200 ]
statement: [ 0 , 3 , 6 , 12 , 30 , 50 , 100 , 100 , 100 ]
comment: [ 1 , 3 , 5 , 7 , 10 , 20 , 30 , 40 , 40 ]
statement: [ 0 , 3 , 6 , 12 , 15 , 25 , 30 , 30 , 30 ]
speaker: [ 0 , 0 , 0 , 3 , 6 , 15 , 30 , 40 , 50 ]
add:
video: [ 0 , 0 , 0 , 0 , 1 , 2 , 3 , 5 , 10 ]
speaker: [ 0 , 0 , 0 , 3 , 6 , 15 , 30 , 40 , 50 ]
video: [ 0 , 0 , 0 , 0 , 1 , 2 , 3 , 5 , 10 ]
speaker: [ 0 , 0 , 0 , 3 , 6 , 15 , 30 , 40 , 50 ]
update:
statement: [ 0 , 0 , 5 , 15 , 30 , 75 , 125 , 150 , 200 ]
speaker: [ 0 , 0 , 3 , 7 , 15 , 30 , 60 , 80 , 100 ]
video: [ 0 , 0 , 0 , 0 , 3 , 6 , 12 , 25 , 40 ]
user: [ 1 , 10 , 15 , 20 , 20 , 30 , 30 , 50 , 50 ]
delete:
comment: [ 1 , 20 , 30 , 50 , 75 , 300 , 300 , 300 , 300 ]
comment: [ 1 , 3 , 5 , 7 , 10 , 20 , 30 , 40 , 40 ]
remove:
statement: [ 0 , 0 , 0 , 2 , 5 , 10 , 15 , 20 , 25 ]
statement: [ 0 , 0 , 0 , 2 , 5 , 10 , 15 , 20 , 30 ]
speaker: [ 0 , 0 , 0 , 0 , 2 , 3 , 5 , 10 , 20 ]
restore:
statement: [ 0 , 0 , 0 , 0 , 10 , 15 , 20 , 30 , 50 ]
statement: [ 0 , 0 , 0 , 0 , 5 , 15 , 20 , 30 , 50 ]
speaker: [ 0 , 0 , 0 , 0 , 0 , 5 , 10 , 30 , 60 ]
flag:
comment: [ 0 , 0 , 3 , 5 , 10 , 15 , 20 , 25 , 30 ]

# Wildcards actions
vote_up: [ 0 , 3 , 6 , 10 , 20 , 30 , 50 , 75 , 100 ]
vote_up: [ 0 , 3 , 5 , 7 , 10 , 30 , 50 , 60 , 75 ]
vote_down: [ 0 , 0 , 2 , 5 , 10 , 20 , 40 , 80 , 100 ]
self_vote: [ 0 , 0 , 0 , 0 , 0 , 0 , 3 , 5 , 10 ]
revert_vote_up: [ 0 , 10 , 25 , 30 , 50 , 75 , 75 , 100 , 100 ]
revert_vote_down: [ 0 , 10 , 25 , 30 , 50 , 75 , 75 , 100 , 100 ]
revert_vote_up: [ 0 , 10 , 25 , 30 , 50 , 50 , 50 , 75 , 75 ]
revert_vote_down: [ 0 , 10 , 25 , 30 , 50 , 50 , 50 , 75 , 75 ]
revert_self_vote: [ 0 , 0 , 0 , 0 , 0 , 0 , 10 , 20 , 30 ]
collective_moderation: [ 0 , 0 , 0 , 0 , 0 , 3 , 5 , 10 , 50 ]
8 changes: 4 additions & 4 deletions apps/cf/test/videos/videos_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ defmodule CF.VideosTest do
publisher = insert(:user, is_publisher: true)

{:ok, video_unspecified} = Videos.create!(publisher, test_url())
{:ok, video_nil} = Videos.create!(publisher, test_url(), nil)
{:ok, video_true} = Videos.create!(publisher, test_url(), true)
{:ok, video_false} = Videos.create!(publisher, test_url(), false)
{:ok, video_nil} = Videos.create!(publisher, test_url(), is_partner: nil)
{:ok, video_true} = Videos.create!(publisher, test_url(), is_partner: true)
{:ok, video_false} = Videos.create!(publisher, test_url(), is_partner: false)

assert video_unspecified.is_partner == true
assert video_nil.is_partner == true
Expand All @@ -44,7 +44,7 @@ defmodule CF.VideosTest do
test "regular user cannot set is_partner" do
regular_user = insert(:user, reputation: 50_000)
{:ok, video} = Videos.create!(regular_user, test_url())
{:ok, video_2} = Videos.create!(regular_user, test_url(), true)
{:ok, video_2} = Videos.create!(regular_user, test_url(), is_partner: true)

assert video.is_partner == false
assert video_2.is_partner == false
Expand Down
2 changes: 1 addition & 1 deletion apps/cf_atom_feed/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule CF.AtomFeed.Mixfile do
def project do
[
app: :cf_atom_feed,
version: "0.9.2",
version: "0.9.3",
build_path: "../../_build",
config_path: "../../config/config.exs",
deps_path: "../../deps",
Expand Down
20 changes: 17 additions & 3 deletions apps/cf_graphql/lib/resolvers/users.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ defmodule CF.GraphQL.Resolvers.Users do
Resolver for `DB.Schema.User`
"""

import Ecto.Query

alias Kaur.Result

alias DB.Repo
alias DB.Schema.User
alias DB.Schema.UserAction
alias DB.Query.Actions

@doc """
Resolve a user by its id or username
Expand Down Expand Up @@ -40,16 +41,29 @@ defmodule CF.GraphQL.Resolvers.Users do
end

@watched_entities ~w(video speaker statement comment fact)a
@action_banned [
:action_banned_bad_language,
:action_banned_spam,
:action_banned_irrelevant,
:action_banned_not_constructive
]

@doc """
Resolve user actions history
"""
def activity_log(user, %{offset: offset, limit: limit}, _) do
UserAction
|> Actions.by_user(user)
|> Actions.matching_entities(@watched_entities)
|> where([a], a.user_id == ^user.id and a.entity in ^@watched_entities)
|> or_where([a], a.target_user_id == ^user.id and a.type in ^@action_banned)
|> DB.Query.order_by_last_inserted_desc()
|> Repo.paginate(page: offset, page_size: limit)
|> Result.ok()
end

@doc """
Get videos added by this user
"""
def videos_added(user, %{offset: offset, limit: limit}, _) do
{:ok, CF.Videos.added_by_user(user, page: offset, page_size: limit)}
end
end
8 changes: 8 additions & 0 deletions apps/cf_graphql/lib/schema/types/user.ex
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ defmodule CF.GraphQL.Schema.Types.User do
arg(:limit, :integer, default_value: 10)
resolve(&Resolvers.Users.activity_log/3)
end

@desc "A paginated list of videos added by this user"
field :videos_added, :paginated_videos do
complexity(join_complexity())
arg(:offset, :integer, default_value: 1)
arg(:limit, :integer, default_value: 10)
resolve(&Resolvers.Users.videos_added/3)
end
end

@desc "A paginated list of user actions"
Expand Down
2 changes: 1 addition & 1 deletion apps/cf_graphql/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule CF.GraphQL.Mixfile do
def project do
[
app: :cf_graphql,
version: "0.9.2",
version: "0.9.3",
build_path: "../../_build",
config_path: "../../config/config.exs",
deps_path: "../../deps",
Expand Down
2 changes: 1 addition & 1 deletion apps/cf_jobs/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule CF.Jobs.Mixfile do
def project do
[
app: :cf_jobs,
version: "0.9.2",
version: "0.9.3",
build_path: "../../_build",
config_path: "../../config/config.exs",
deps_path: "../../deps",
Expand Down
2 changes: 1 addition & 1 deletion apps/cf_opengraph/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule CF.Opengraph.MixProject do
def project do
[
app: :cf_opengraph,
version: "0.9.2",
version: "0.9.3",
elixir: "~> 1.6",
start_permanent: Mix.env() == :prod,
deps: deps(),
Expand Down
2 changes: 1 addition & 1 deletion apps/cf_rest_api/lib/controllers/video_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ defmodule CF.RestApi.VideoController do
nil ->
conn
|> Guardian.Plug.current_resource()
|> create!(url, params["is_partner"])
|> create!(url, is_partner: params["is_partner"], unlisted: params["unlisted"])
|> case do
{:error, error} when is_binary(error) ->
conn
Expand Down
3 changes: 2 additions & 1 deletion apps/cf_rest_api/lib/views/video_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ defmodule CF.RestApi.VideoView do
posted_at: video.inserted_at,
speakers: render_many(video.speakers, CF.RestApi.SpeakerView, "speaker.json"),
language: video.language,
is_partner: video.is_partner
is_partner: video.is_partner,
unlisted: video.unlisted
}
end
end
2 changes: 1 addition & 1 deletion apps/cf_rest_api/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule CF.RestApi.Mixfile do
def project do
[
app: :cf_rest_api,
version: "0.9.2",
version: "0.9.3",
build_path: "../../_build",
compilers: [:phoenix, :gettext] ++ Mix.compilers(),
config_path: "../../config/config.exs",
Expand Down
2 changes: 1 addition & 1 deletion apps/db/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule DB.Mixfile do
def project do
[
app: :db,
version: "0.9.2",
version: "0.9.3",
build_path: "../../_build",
config_path: "../../config/config.exs",
deps_path: "../../deps",
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ defmodule CF.Umbrella.Mixfile do

# ---- Test and Dev
{:excoveralls, "~> 0.8", only: :test},
{:credo, "~> 0.10.0", only: [:dev, :test], runtime: false},
{:credo, "~> 1.0.0", only: [:dev, :test], runtime: false},
{:mix_test_watch, "~> 0.6", only: :dev, runtime: false}
]
end
Expand Down
Loading

0 comments on commit aaa8cfb

Please sign in to comment.