diff options
author | Lee Starnes <lee@canned-death.us> | 2018-10-12 00:19:43 -0500 |
---|---|---|
committer | William Pitcock <nenolod@dereferenced.org> | 2018-11-01 09:17:02 +0000 |
commit | 585b29337ce66eb2c574e71588db542044574609 (patch) | |
tree | dcee8b8572d6c6e82d9890b84021fa4a5c816291 /lib | |
parent | eba9a62024a6acd4722745850cb7c0ea0502304f (diff) | |
download | pleroma-585b29337ce66eb2c574e71588db542044574609.tar.gz |
Ensure filters have a filter_id
Diffstat (limited to 'lib')
-rw-r--r-- | lib/pleroma/filter.ex | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/lib/pleroma/filter.ex b/lib/pleroma/filter.ex index fe904df3a..25ed38f34 100644 --- a/lib/pleroma/filter.ex +++ b/lib/pleroma/filter.ex @@ -36,6 +36,34 @@ defmodule Pleroma.Filter do Repo.all(query) end + def create(%Pleroma.Filter{user_id: user_id, filter_id: nil} = filter) do + # If filter_id wasn't given, use the max filter_id for this user plus 1. + # XXX This could result in a race condition if a user tries to add two + # different filters for their account from two different clients at the + # same time, but that should be unlikely. + + max_id_query = + from( + f in Pleroma.Filter, + where: f.user_id == ^user_id, + select: max(f.filter_id) + ) + + filter_id = + case Repo.one(max_id_query) do + # Start allocating from 1 + nil -> + 1 + + max_id -> + max_id + 1 + end + + filter + |> Map.put(:filter_id, filter_id) + |> Repo.insert() + end + def create(%Pleroma.Filter{} = filter) do Repo.insert(filter) end |