Current section
28 Versions
Jump to
Current section
28 Versions
Compare versions
11
files changed
+115
additions
-280
deletions
| @@ -3,21 +3,19 @@ | |
| 3 3 | {<<"description">>,<<"Presence client in Elixir for new arch">>}. |
| 4 4 | {<<"elixir">>,<<"~> 1.10.4">>}. |
| 5 5 | {<<"files">>, |
| 6 | - [<<"config">>,<<"config/config.exs">>,<<"lib">>,<<"lib/presence_ex.ex">>, |
| 7 | - <<"lib/presence_ex">>,<<"lib/presence_ex/utils">>, |
| 8 | - <<"lib/presence_ex/utils/murmur.ex">>,<<"lib/presence_ex/config.ex">>, |
| 9 | - <<"lib/presence_ex/repo.ex">>,<<"lib/presence_ex/schemas">>, |
| 10 | - <<"lib/presence_ex/schemas/status_subscribe.ex">>, |
| 11 | - <<"lib/presence_ex/schemas/status.ex">>,<<"lib/presence_ex/utils.ex">>, |
| 12 | - <<"lib/presence_ex/release_manager.ex">>, |
| 13 | - <<"lib/presence_ex/application.ex">>,<<"priv">>,<<"priv/replica">>, |
| 14 | - <<"priv/replica/migrations">>, |
| 15 | - <<"priv/replica/migrations/20220213145853_multi.exs">>, |
| 16 | - <<"priv/replica/migrations/20240129122556_change_status_subscribe.exs">>, |
| 6 | + [<<"config">>,<<"config/config.exs">>,<<"lib">>,<<"lib/presence_ex">>, |
| 7 | + <<"lib/presence_ex/repo.ex">>,<<"lib/presence_ex/utils.ex">>, |
| 8 | + <<"lib/presence_ex/config.ex">>,<<"lib/presence_ex/application.ex">>, |
| 9 | + <<"lib/presence_ex/utils">>,<<"lib/presence_ex/utils/murmur.ex">>, |
| 10 | + <<"lib/presence_ex/schemas">>, |
| 11 | + <<"lib/presence_ex/schemas/be_subscribed.ex">>, |
| 12 | + <<"lib/presence_ex/schemas/subscribe.ex">>, |
| 13 | + <<"lib/presence_ex/schemas/status.ex">>,<<"lib/presence_ex.ex">>,<<"priv">>, |
| 17 14 | <<"priv/repo">>,<<"priv/repo/migrations">>, |
| 18 | - <<"priv/repo/migrations/20220213145853_multi.exs">>, |
| 19 | - <<"priv/repo/migrations/20240129122556_change_status_subscribe.exs">>, |
| 20 | - <<"mix.exs">>,<<"mix.lock">>]}. |
| 15 | + <<"priv/repo/migrations/20220213145853_multi.exs">>,<<"priv/replica">>, |
| 16 | + <<"priv/replica/migrations">>, |
| 17 | + <<"priv/replica/migrations/20220213145853_multi.exs">>,<<"mix.exs">>, |
| 18 | + <<"mix.lock">>]}. |
| 21 19 | {<<"licenses">>,[<<"MIT">>]}. |
| 22 20 | {<<"links">>,[{<<"Github">>,<<"https://github.com/tigertext/presence_ex">>}]}. |
| 23 21 | {<<"name">>,<<"presence_ex">>}. |
| @@ -32,4 +30,4 @@ | |
| 32 30 | {<<"optional">>,false}, |
| 33 31 | {<<"repository">>,<<"hexpm">>}, |
| 34 32 | {<<"requirement">>,<<"~> 0.15.9">>}]]}. |
| 35 | - {<<"version">>,<<"0.2.8-6">>}. |
| 33 | + {<<"version">>,<<"0.2.8">>}. |
| @@ -6,8 +6,8 @@ defmodule PresenceEx do | |
| 6 6 | alias PresenceEx.Repo |
| 7 7 | alias PresenceEx.Schemas.Status |
| 8 8 | alias PresenceEx.Utils |
| 9 | - alias PresenceEx.Schemas.StatusSubscribe |
| 10 | - require Logger |
| 9 | + alias PresenceEx.Schemas.Subscribe |
| 10 | + alias PresenceEx.Schemas.BeSubscribed |
| 11 11 | |
| 12 12 | def insert(account, resource, node, status, features) do |
| 13 13 | %Status{} |
| @@ -125,102 +125,51 @@ defmodule PresenceEx do | |
| 125 125 | |> Enum.map(&Repo.delete_all(query, prefix: Utils.prefix(&1))) |
| 126 126 | end |
| 127 127 | |
| 128 | - def upsert_subscribe(subscriber, resource, subscribed, timestamp) do |
| 129 | - param = %{ |
| 130 | - subscriber_token: subscriber, |
| 131 | - subscriber_resource: resource, |
| 132 | - timestamp: timestamp, |
| 133 | - subscribed_token: subscribed |
| 134 | - } |
| 135 | - |
| 136 | - verified_param = StatusSubscribe.verify_input(param) |
| 137 | - |
| 138 | - case verified_param.valid? do |
| 139 | - true -> |
| 140 | - case Repo.insert(verified_param, |
| 141 | - prefix: Utils.prefix(subscriber), |
| 142 | - on_conflict: {:replace, [:timestamp]}, |
| 143 | - conflict_target: [:subscriber_token, :subscriber_resource, :subscribed_token] |
| 144 | - ) do |
| 145 | - {:ok, _e} -> |
| 146 | - :ok |
| 147 | - |
| 148 | - {:error, error} -> |
| 149 | - Logger.error("error insert_subscribe #{inspect(error)}") |
| 150 | - :failed |
| 151 | - end |
| 152 | - |
| 153 | - false -> |
| 154 | - Logger.info("unexpected insert_subscribe error #{inspect(verified_param)}") |
| 155 | - :failed |
| 156 | - end |
| 157 | - end |
| 158 | - |
| 159 | - def get_subscribe(subscriber, resource, subscribed) do |
| 160 | - conditions = [ |
| 161 | - subscriber_token: subscriber, |
| 162 | - subscriber_resource: resource, |
| 163 | - subscribed_token: subscribed |
| 164 | - ] |
| 165 | - |
| 166 | - from(StatusSubscribe, where: ^conditions) |
| 167 | - |> Repo.replica().all(prefix: Utils.prefix(subscriber)) |
| 168 | - end |
| 169 | - |
| 170 | - def delete_subscribe(subscriber, resource) do |
| 171 | - from(StatusSubscribe, where: [subscriber_token: ^subscriber, subscriber_resource: ^resource]) |
| 172 | - |> Repo.delete_all(prefix: Utils.prefix(subscriber)) |
| 173 | - end |
| 174 | - |
| 175 | - def delete_subscribe(subscriber, resource, subscribed) do |
| 176 | - from(StatusSubscribe, |
| 177 | - where: [ |
| 178 | - subscriber_token: ^subscriber, |
| 179 | - subscriber_resource: ^resource, |
| 180 | - subscribed_token: ^subscribed |
| 181 | - ] |
| 128 | + def upsert_subscribe(account, resource, subscribe) do |
| 129 | + %Subscribe{} |
| 130 | + |> Subscribe.changeset(%{ |
| 131 | + account: account, |
| 132 | + resource: resource, |
| 133 | + subscribe_map: subscribe |
| 134 | + }) |
| 135 | + |> Repo.insert( |
| 136 | + prefix: Utils.prefix(account), |
| 137 | + on_conflict: {:replace, [:subscribe_map]}, |
| 138 | + conflict_target: [:account, :resource] |
| 182 139 | ) |
| 183 | - |> Repo.delete_all(prefix: Utils.prefix(subscriber)) |
| 184 140 | end |
| 185 141 | |
| 186 | - def delete_subscribe(subscribed) do |
| 187 | - query = from(StatusSubscribe, where: [subscribed_token: ^subscribed]) |
| 188 | - |
| 189 | - {Utils.all_schema_ids() |
| 190 | - |> Enum.reduce( |
| 191 | - 0, |
| 192 | - fn id, acc -> |
| 193 | - {n, nil} = Repo.delete_all(query, prefix: Utils.prefix(id)) |
| 194 | - acc + n |
| 195 | - end |
| 196 | - ), nil} |
| 142 | + def get_subscribe(account, resource) do |
| 143 | + from(Subscribe, where: [account: ^account, resource: ^resource]) |
| 144 | + |> Repo.replica().all(prefix: Utils.prefix(account)) |
| 197 145 | end |
| 198 146 | |
| 199 | - def get_all_subscribe() do |
| 200 | - # for debug use, not used in prod!! |
| 201 | - Utils.all_schema_ids() |
| 202 | - |> Enum.flat_map(&Repo.replica().all(StatusSubscribe, prefix: Utils.prefix(&1))) |
| 147 | + def delete_subscribe(account, resource) do |
| 148 | + from(Subscribe, where: [account: ^account, resource: ^resource]) |
| 149 | + |> Repo.delete_all(prefix: Utils.prefix(account)) |
| 203 150 | end |
| 204 151 | |
| 205 | - def get_subscribed(subscriber, resource) do |
| 206 | - conditions = [ |
| 207 | - subscriber_token: subscriber, |
| 208 | - subscriber_resource: resource |
| 209 | - ] |
| 210 | - |
| 211 | - from(StatusSubscribe, where: ^conditions) |
| 212 | - |> Repo.replica().all(prefix: Utils.prefix(subscriber)) |
| 152 | + def upsert_be_subscribed(account, subscribe) do |
| 153 | + %BeSubscribed{} |
| 154 | + |> BeSubscribed.changeset(%{ |
| 155 | + account: account, |
| 156 | + be_subscribed_map: subscribe |
| 157 | + }) |
| 158 | + |> Repo.insert( |
| 159 | + prefix: Utils.prefix(account), |
| 160 | + on_conflict: {:replace, [:be_subscribed_map]}, |
| 161 | + conflict_target: [:account] |
| 162 | + ) |
| 213 163 | end |
| 214 164 | |
| 215 | - def get_subscriber(subscribed) do |
| 216 | - query = from(StatusSubscribe, where: [subscribed_token: ^subscribed]) |
| 217 | - |
| 218 | - Utils.all_schema_ids() |
| 219 | - |> Enum.flat_map(&Repo.replica().all(query, prefix: Utils.prefix(&1))) |
| 165 | + def get_be_subscribed(account) do |
| 166 | + from(BeSubscribed, where: [account: ^account]) |
| 167 | + |> Repo.replica().all(prefix: Utils.prefix(account)) |
| 220 168 | end |
| 221 169 | |
| 222 | - def migrate() do |
| 223 | - PresenceEx.ReleaseManager.migrate() |
| 170 | + def delete_be_subscribed(account) do |
| 171 | + from(BeSubscribed, where: [account: ^account]) |
| 172 | + |> Repo.delete_all(prefix: Utils.prefix(account)) |
| 224 173 | end |
| 225 174 | |
| 226 175 | def start(_, _) do |
| @@ -1,109 +0,0 @@ | |
| 1 | - defmodule PresenceEx.ReleaseManager do |
| 2 | - require Ecto.Migrator |
| 3 | - require Logger |
| 4 | - |
| 5 | - @moduledoc false |
| 6 | - |
| 7 | - @doc """ |
| 8 | - Migrate the database. Defaults to migrating to the latest, `[all: true]` |
| 9 | - Also accepts `[step: 1]`, or `[to: 20200118045751]` |
| 10 | - """ |
| 11 | - def migrate(opts \\ [all: true]) do |
| 12 | - Logger.debug("[*] Starting database migration with opts:#{inspect(opts)}.") |
| 13 | - |
| 14 | - for repo <- repos() do |
| 15 | - {:ok, _, _} = Ecto.Migrator.with_repo(repo, &Ecto.Migrator.run(&1, :up, opts)) |
| 16 | - end |
| 17 | - |
| 18 | - Logger.debug("[*] Finished database migration.") |
| 19 | - end |
| 20 | - |
| 21 | - @doc """ |
| 22 | - Migrate the database in th `manual_migrations` path. Defaults to migrating to the latest, `[all: true]` |
| 23 | - Also accepts `[step: 1]`, or `[to: 20200118045751]` |
| 24 | - """ |
| 25 | - def manual_migrate(opts \\ [all: true]) do |
| 26 | - Logger.debug("[*] Starting manually database migration with opts:#{inspect(opts)}.") |
| 27 | - |
| 28 | - for repo <- repos() do |
| 29 | - path = Ecto.Migrator.migrations_path(repo, "manual_migrations") |
| 30 | - {:ok, _, _} = Ecto.Migrator.with_repo(repo, &Ecto.Migrator.run(&1, path, :up, opts)) |
| 31 | - end |
| 32 | - |
| 33 | - Logger.debug("[*] Finished manually database migration.") |
| 34 | - end |
| 35 | - |
| 36 | - @doc """ |
| 37 | - Rollbacks the database to the according version (migration number) for the EctoRepo |
| 38 | - """ |
| 39 | - def rollback(repo, version) do |
| 40 | - Logger.debug( |
| 41 | - "[*] Starting database rollback for #{inspect(repo)} and version:#{inspect(version)}." |
| 42 | - ) |
| 43 | - |
| 44 | - {:ok, _, _} = Ecto.Migrator.with_repo(repo, &Ecto.Migrator.run(&1, :down, to: version)) |
| 45 | - :ok = rollback_manual(repo, version) |
| 46 | - Logger.debug("[*] Finished rollback for #{inspect(repo)} and version:#{inspect(version)}.") |
| 47 | - end |
| 48 | - |
| 49 | - defp rollback_manual(repo, version) do |
| 50 | - path = Ecto.Migrator.migrations_path(repo, "manual_migrations") |
| 51 | - {:ok, _, _} = Ecto.Migrator.with_repo(repo, &Ecto.Migrator.run(&1, path, :down, to: version)) |
| 52 | - :ok |
| 53 | - end |
| 54 | - |
| 55 | - defp repos do |
| 56 | - [PresenceEx.Repo] |
| 57 | - end |
| 58 | - |
| 59 | - @doc """ |
| 60 | - Print the migration status for configured Repos' migrations. |
| 61 | - """ |
| 62 | - def migration_status do |
| 63 | - for repo <- repos(), do: print_migrations_for(repo) |
| 64 | - end |
| 65 | - |
| 66 | - defp print_migrations_for(repo) do |
| 67 | - paths = repo_migrations_path(repo) |
| 68 | - |
| 69 | - {:ok, repo_status, _} = |
| 70 | - Ecto.Migrator.with_repo(repo, &Ecto.Migrator.migrations(&1, paths), mode: :temporary) |
| 71 | - |
| 72 | - IO.puts( |
| 73 | - """ |
| 74 | - Repo: #{inspect(repo)} |
| 75 | - Status Migration ID Migration Name |
| 76 | - -------------------------------------------------- |
| 77 | - """ <> |
| 78 | - Enum.map_join(repo_status, "\n", fn {status, number, description} -> |
| 79 | - " #{pad(status, 10)}#{pad(number, 16)}#{description}" |
| 80 | - end) <> "\n" |
| 81 | - ) |
| 82 | - end |
| 83 | - |
| 84 | - @doc """ |
| 85 | - Returns true if there are automatic migrations to be run. False otherwise |
| 86 | - """ |
| 87 | - def auto_migrations_to_run(repo) do |
| 88 | - paths = repo_migrations_path(repo) |
| 89 | - |
| 90 | - {:ok, repo_status, _} = |
| 91 | - Ecto.Migrator.with_repo(repo, &Ecto.Migrator.migrations(&1, paths), mode: :temporary) |
| 92 | - |
| 93 | - Enum.any?(repo_status, fn {status, _num, _desc} -> |
| 94 | - status == :down |
| 95 | - end) |
| 96 | - end |
| 97 | - |
| 98 | - defp repo_migrations_path(repo) do |
| 99 | - config = repo.config() |
| 100 | - priv = config[:priv] || "priv/#{repo |> Module.split() |> List.last() |> Macro.underscore()}" |
| 101 | - config |> Keyword.fetch!(:otp_app) |> Application.app_dir() |> Path.join(priv) |
| 102 | - end |
| 103 | - |
| 104 | - defp pad(content, pad) do |
| 105 | - content |
| 106 | - |> to_string |
| 107 | - |> String.pad_trailing(pad) |
| 108 | - end |
| 109 | - end |
| @@ -0,0 +1,17 @@ | |
| 1 | + defmodule PresenceEx.Schemas.BeSubscribed do |
| 2 | + @moduledoc false |
| 3 | + |
| 4 | + use Ecto.Schema |
| 5 | + import Ecto.Changeset |
| 6 | + |
| 7 | + @primary_key false |
| 8 | + schema "be_subscribed" do |
| 9 | + field(:account, :string, primary_key: true) |
| 10 | + field(:be_subscribed_map, :map) |
| 11 | + end |
| 12 | + |
| 13 | + def changeset(struct, params) do |
| 14 | + struct |
| 15 | + |> change(params) |
| 16 | + end |
| 17 | + end |
| @@ -1,22 +0,0 @@ | |
| 1 | - defmodule PresenceEx.Schemas.StatusSubscribe do |
| 2 | - @moduledoc false |
| 3 | - use Ecto.Schema |
| 4 | - import Ecto.Changeset |
| 5 | - |
| 6 | - @primary_key false |
| 7 | - schema "status_subscribe" do |
| 8 | - field(:subscriber_token, :string, primary_key: true) |
| 9 | - field(:subscriber_resource, :string, primary_key: true) |
| 10 | - field(:subscribed_token, :string, primary_key: true) |
| 11 | - field(:timestamp, :string) |
| 12 | - end |
| 13 | - |
| 14 | - def verify_input(params) do |
| 15 | - %PresenceEx.Schemas.StatusSubscribe{} |
| 16 | - |> cast( |
| 17 | - params, |
| 18 | - [:subscriber_token, :subscriber_resource, :timestamp, :subscribed_token] |
| 19 | - ) |
| 20 | - |> validate_required([:subscriber_token, :subscriber_resource, :timestamp, :subscribed_token]) |
| 21 | - end |
| 22 | - end |
Loading more files…