Class: Vines::Storage::Sql

Inherits:
Vines::Storage show all
Defined in:
lib/vines/storage/sql.rb

Defined Under Namespace

Classes: Follower, Person, User

Instance Attribute Summary

Attributes inherited from Vines::Storage

#ldap

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Vines::Storage

defer, fiber, from_name, #ldap?, register, wrap_ldap

Methods included from Log

#log

Constructor Details

#initialize(&block) ⇒ Sql

Returns a new instance of Sql.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/vines/storage/sql.rb', line 35

def initialize(&block)
  raise "You configured lygneo-sql adapter without Lygneo" unless defined? AppConfig
  @config = {
    :adapter => AppConfig.adapter.to_s,
    :database => AppConfig.database.to_s,
    :host => AppConfig.host.to_s,
    :port => AppConfig.port.to_i,
    :username => AppConfig.username.to_s,
    :password => AppConfig.password.to_s
  }

  required = [:adapter, :database]
  required << [:host, :port] unless @config[:adapter] == 'sqlite3'
  required.flatten.each {|key| raise "Must provide #{key}" unless @config[key] }
  [:username, :password].each {|key| @config.delete(key) if empty?(@config[key]) }
  establish_connection
end

Class Method Details

.with_connection(method, args = {}) ⇒ Object

Wrap the method with ActiveRecord connection pool logic, so we properly return connections to the pool when we’re finished with them. This also defers the original method by pushing it onto the EM thread pool because ActiveRecord uses blocking IO.



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

def self.with_connection(method, args={})
  deferrable = args.key?(:defer) ? args[:defer] : true
  old = instance_method(method)
  define_method method do |*args|
    ActiveRecord::Base.connection_pool.with_connection do
      old.bind(self).call(*args)
    end
  end
  defer(method) if deferrable
end

Instance Method Details

#authenticate(username, password) ⇒ Object



87
88
89
90
91
92
93
94
95
96
# File 'lib/vines/storage/sql.rb', line 87

def authenticate(username, password)
  user = find_user(username)

  dbhash = BCrypt::Password.new(user.password) rescue nil
  hash = BCrypt::Engine.hash_secret("#{password}#{Config.instance.pepper}", dbhash.salt) rescue nil

  userAuth = ((hash && dbhash) && hash == dbhash)
  tokenAuth = ((password && user.password) && password == user.password)
  (tokenAuth || userAuth)? user : nil
end

#find_fragment(jid, node) ⇒ Object



115
116
117
118
# File 'lib/vines/storage/sql.rb', line 115

def find_fragment(jid, node)
  # do nothing
  nil
end

#find_user(jid) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/vines/storage/sql.rb', line 53

def find_user(jid)
  jid = JID.new(jid).bare.to_s
  return if jid.empty?
  xuser = user_by_jid(jid)
  return Vines::User.new(jid: jid).tap do |user|
    user.name, user.password = xuser.username, xuser.authentication_token

    xuser.followers.each do |follower|
      handle = follower.person.lygneo_handle
      ask = 'none'
      subscription = 'none'

      if follower.sharing && follower.receiving
        subscription = 'both'
      elsif follower.sharing && !follower.receiving
        ask = 'suscribe'
        subscription = 'from'
      elsif !follower.sharing && follower.receiving
        subscription = 'to'
      else
        ask = 'suscribe'
      end
      # finally build the roster entry
      user.roster << Vines::Follower.new(
        jid: handle,
        name: handle.gsub(/\@.*?$/, ''),
        subscription: subscription,
        ask: ask
      ) if handle
    end
  end if xuser
end

#find_vcard(jid) ⇒ Object



104
105
106
107
# File 'lib/vines/storage/sql.rb', line 104

def find_vcard(jid)
  # do nothing
  nil
end

#save_fragment(jid, node) ⇒ Object



121
122
123
# File 'lib/vines/storage/sql.rb', line 121

def save_fragment(jid, node)
  # do nothing
end

#save_user(user) ⇒ Object



98
99
100
101
# File 'lib/vines/storage/sql.rb', line 98

def save_user(user)
  # do nothing
  #log.error("You cannot save a user via XMPP server!")
end

#save_vcard(jid, card) ⇒ Object



110
111
112
# File 'lib/vines/storage/sql.rb', line 110

def save_vcard(jid, card)
  # do nothing
end