Class: SelfSDK::Storage

Inherits:
Object
  • Object
show all
Defined in:
lib/storage.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app_id, app_device, storage_folder, _key_id) ⇒ Storage

Returns a new instance of Storage.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/storage.rb', line 8

def initialize(app_id, app_device, storage_folder, _key_id)
  @app_id = sid(app_id, app_device)

  # Create the storage folder if it does not exist
  create_directory_skel("#{storage_folder}/identities/#{app_id}/devices/#{app_device}")

  # Create the database
  @db = SQLite3::Database.new(File.join("#{storage_folder}/identities/#{app_id}/devices/#{app_device}/", 'self.db'))
  set_pragmas
  create_accounts_table
  create_sessions_table
  m = StorageMigrator.new(@db, "#{storage_folder}/apps", @app_id)
  m.migrate
end

Instance Attribute Details

#app_idObject

Returns the value of attribute app_id.



6
7
8
# File 'lib/storage.rb', line 6

def app_id
  @app_id
end

Instance Method Details

#account_create(olm) ⇒ Object



39
40
41
42
# File 'lib/storage.rb', line 39

def (olm)
  @db.execute("INSERT INTO accounts (as_identifier, offset, olm_account) VALUES (?, ?, ?);", [ @app_id.encode("UTF-8"), 0, olm.encode("UTF-8") ])
rescue
end

#account_exists?Boolean

Returns:

  • (Boolean)


34
35
36
37
# File 'lib/storage.rb', line 34

def 
  row = @db.execute("SELECT olm_account FROM accounts WHERE as_identifier = ?", [@app_id.encode("UTF-8")]).first
  !row.nil?
end

#account_offsetObject



55
56
57
58
59
60
# File 'lib/storage.rb', line 55

def 
  row = @db.execute("SELECT offset FROM accounts WHERE as_identifier = ?;", @app_id.encode("UTF-8")).first
  return nil unless row

  row.first
end

#account_olmObject



48
49
50
51
52
53
# File 'lib/storage.rb', line 48

def 
  row = @db.execute("SELECT olm_account FROM accounts WHERE as_identifier = ?;", [@app_id.encode("UTF-8")]).first
  return nil unless row

  row.first
end

#account_set_offset(offset) ⇒ Object



62
63
64
# File 'lib/storage.rb', line 62

def (offset)
  @db.execute("UPDATE accounts SET offset = ? WHERE as_identifier = ?;", [ offset, @app_id.encode("UTF-8") ])
end

#account_update(olm) ⇒ Object



44
45
46
# File 'lib/storage.rb', line 44

def (olm)
  @db.execute("UPDATE accounts SET olm_account = ? WHERE as_identifier = ?", [ olm.encode("UTF-8"), @app_id.encode("UTF-8") ])
end

#session_create(sid, olm) ⇒ Object



66
67
68
# File 'lib/storage.rb', line 66

def session_create(sid, olm)
  @db.execute("INSERT INTO sessions (as_identifier, with_identifier, olm_session) VALUES (?, ?, ?);", [ @app_id.encode("UTF-8"), sid.encode("UTF-8"), olm.encode("UTF-8") ])
end

#session_get_olm(sid) ⇒ Object



79
80
81
82
83
84
# File 'lib/storage.rb', line 79

def session_get_olm(sid)
  row = @db.execute("SELECT olm_session FROM sessions WHERE as_identifier = ? AND with_identifier = ?", [@app_id.encode("UTF-8"), sid.encode("UTF-8")]).first
  return nil if row.nil?

  row.first
end

#session_update(sid, olm) ⇒ Object



70
71
72
73
74
75
76
77
# File 'lib/storage.rb', line 70

def session_update(sid, olm)
  row = @db.execute("SELECT olm_session FROM sessions WHERE as_identifier = ? AND with_identifier = ?", [@app_id.encode("UTF-8"), sid.encode("UTF-8")]).first
  if row.nil?
    session_create(sid, olm)
  else
    @db.execute("UPDATE sessions SET olm_session = ? WHERE as_identifier = ? AND with_identifier = ?;", [ olm.encode("UTF-8"), @app_id.encode("UTF-8"), sid.encode("UTF-8") ])
  end
end

#sid(selfid, device) ⇒ Object



86
87
88
# File 'lib/storage.rb', line 86

def sid(selfid, device)
  "#{selfid}:#{device}"
end

#txObject



23
24
25
26
27
28
29
30
31
32
# File 'lib/storage.rb', line 23

def tx
  @db.transaction
  yield
  @db.commit
rescue SQLite3::Exception => e
  @db.rollback
rescue => e
  @db.rollback
  raise e
end