Class: Tempest::AccountsStore
- Inherits:
-
Object
- Object
- Tempest::AccountsStore
- Defined in:
- lib/tempest/accounts_store.rb
Overview
Tracks which Bluesky accounts tempest knows about and which one is currently the default. The on-disk representation is ‘<config_base>/accounts.json`, always rewritten via tmp + rename for atomicity (see `write_atomic`).
On construction, also performs an orphan-recovery sweep so that any ‘accounts/<did>/session.json` left behind by a partial login or migration appears in accounts.json the next time tempest starts.
Defined Under Namespace
Classes: Account
Constant Summary collapse
- SCHEMA_VERSION =
1
Instance Attribute Summary collapse
-
#accounts ⇒ Object
readonly
Returns the value of attribute accounts.
-
#default ⇒ Object
readonly
Returns the value of attribute default.
Instance Method Summary collapse
- #add_account(did:, handle:, identifier:, pds_host:, added_at: Time.now.utc) ⇒ Object
-
#initialize(env: ENV, path: nil, logger: nil) ⇒ AccountsStore
constructor
A new instance of AccountsStore.
- #resolve(value) ⇒ Object
- #set_default(value) ⇒ Object
- #update_handle(did:, handle:) ⇒ Object
Constructor Details
#initialize(env: ENV, path: nil, logger: nil) ⇒ AccountsStore
Returns a new instance of AccountsStore.
24 25 26 27 28 29 30 31 32 |
# File 'lib/tempest/accounts_store.rb', line 24 def initialize(env: ENV, path: nil, logger: nil) @env = env @path = path || Tempest::AccountPaths.accounts_json_path(env) @logger = logger @default = nil @accounts = [] load self_heal end |
Instance Attribute Details
#accounts ⇒ Object (readonly)
Returns the value of attribute accounts.
34 35 36 |
# File 'lib/tempest/accounts_store.rb', line 34 def accounts @accounts end |
#default ⇒ Object (readonly)
Returns the value of attribute default.
34 35 36 |
# File 'lib/tempest/accounts_store.rb', line 34 def default @default end |
Instance Method Details
#add_account(did:, handle:, identifier:, pds_host:, added_at: Time.now.utc) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/tempest/accounts_store.rb', line 53 def add_account(did:, handle:, identifier:, pds_host:, added_at: Time.now.utc) existing = @accounts.find { |a| a.did == did } effective_added_at = existing ? existing.added_at : added_at replacement = Account.new( did: did, handle: handle, identifier: identifier, pds_host: pds_host, added_at: effective_added_at, ) list = @accounts.reject { |a| a.did == did } + [replacement] @accounts = list.sort_by(&:added_at).freeze @default ||= did persist replacement end |
#resolve(value) ⇒ Object
36 37 38 39 40 41 |
# File 'lib/tempest/accounts_store.rb', line 36 def resolve(value) return nil if value.nil? || value.empty? by_did = @accounts.find { |a| a.did == value } return by_did if by_did @accounts.find { |a| a.handle == value } end |
#set_default(value) ⇒ Object
43 44 45 46 47 48 49 50 51 |
# File 'lib/tempest/accounts_store.rb', line 43 def set_default(value) account = resolve(value) raise ArgumentError, "unknown user: #{value}" if account.nil? return account.did if @default == account.did @default = account.did persist @logger&.info("accounts", event: "set_default", handle: account.handle, did: account.did) account.did end |
#update_handle(did:, handle:) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/tempest/accounts_store.rb', line 71 def update_handle(did:, handle:) target = @accounts.find { |a| a.did == did } return if target.nil? return if target.handle == handle old_handle = target.handle replacement = Account.new( did: target.did, handle: handle, identifier: target.identifier, pds_host: target.pds_host, added_at: target.added_at, ) list = @accounts.reject { |a| a.did == did } + [replacement] @accounts = list.sort_by(&:added_at).freeze persist @logger&.info("accounts", event: "handle_changed", did: did, old_handle: old_handle, new_handle: handle) end |