Document everything with YARD and fix some bugs along the way.
[mailshears.git] / lib / common / agendav_plugin.rb
1 require 'common/plugin'
2 require 'common/user'
3
4 # Code that all Agendav plugins ({AgendavPrune}, {AgendavRm},
5 # {AgendavMv}) share.
6 module AgendavPlugin
7
8 # We implement the Plugin "interface."
9 include Plugin
10
11
12 # Initialize this Agendav {Plugin} with values in *cfg*.
13 #
14 # @param cfg [Configuration] the configuration for this plugin.
15 #
16 def initialize(cfg)
17 @db_host = cfg.agendav_dbhost
18 @db_port = cfg.agendav_dbport
19 @db_opts = cfg.agendav_dbopts
20 @db_tty = cfg.agendav_dbtty
21 @db_name = cfg.agendav_dbname
22 @db_user = cfg.agendav_dbuser
23 @db_pass = cfg.agendav_dbpass
24 end
25
26
27 # Return a list of Agendav users.
28 #
29 # @return [Array<User>] a list of users contained in the
30 # Agendav database.
31 #
32 def list_users()
33 users = []
34
35 connection = PGconn.connect(@db_host, @db_port, @db_opts, @db_tty,
36 @db_name, @db_user, @db_pass)
37
38 sql_query = '(SELECT username FROM prefs)'
39 sql_query += 'UNION'
40 sql_query += '(SELECT user_from FROM shared);'
41
42 connection.query(sql_query) do |result|
43 users = result.field_values('username')
44 end
45
46 connection.close()
47
48 return users.map{ |u| User.new(u) }
49 end
50
51 end