Class: Lita::Gsuite

Inherits:
Handler
  • Object
show all
Defined in:
lib/lita/gsuite.rb

Constant Summary collapse

TIMER_INTERVAL =
60
OOB_OAUTH_URI =
'urn:ietf:wg:oauth:2.0:oob'
COMMANDS =
[
  Commands::AccountSummary,
  Commands::ListAdmins,
  Commands::EmptyGroups,
  Commands::NoOrgUnit,
  Commands::TwoFactorOff,
  Commands::TwoFactorStats,
  Commands::SuspensionCandidates,
  Commands::DeletionCandidates,
].map { |cmd|
  [cmd.new.name, cmd]
}.to_h
WINDOW_COMMANDS =
[
  Commands::ListActivities
].map { |cmd|
  [cmd.new.name, cmd]
}.to_h

Instance Method Summary collapse

Instance Method Details

#account_summary(response) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/lita/gsuite.rb', line 63

def (response)
  return unless confirm_user_authenticated(response)

  Commands::AccountSummary.new.run(
    robot,
    Source.new(room: response.room),
    gateway(response.user)
  )
end

#deletion_candidates(response) ⇒ Object



73
74
75
76
77
78
79
80
81
82
# File 'lib/lita/gsuite.rb', line 73

def deletion_candidates(response)
  return unless confirm_user_authenticated(response)

  Commands::DeletionCandidates.new.run(
    robot,
    Source.new(room: response.room),
    gateway(response.user),
    negative_ack: true
  )
end

#empty_groups(response) ⇒ Object



84
85
86
87
88
89
90
91
92
93
# File 'lib/lita/gsuite.rb', line 84

def empty_groups(response)
  return unless confirm_user_authenticated(response)

  Commands::EmptyGroups.new.run(
    robot,
    Source.new(room: response.room),
    gateway(response.user),
    negative_ack: true
  )
end

#list_admins(response) ⇒ Object



95
96
97
98
99
100
101
102
103
104
# File 'lib/lita/gsuite.rb', line 95

def list_admins(response)
  return unless confirm_user_authenticated(response)

  Commands::ListAdmins.new.run(
    robot,
    Source.new(room: response.room),
    gateway(response.user),
    negative_ack: true
  )
end

#no_org_unit(response) ⇒ Object



106
107
108
109
110
111
112
113
114
115
# File 'lib/lita/gsuite.rb', line 106

def no_org_unit(response)
  return unless confirm_user_authenticated(response)

  Commands::NoOrgUnit.new.run(
    robot,
    Source.new(room: response.room),
    gateway(response.user),
    negative_ack: true
  )
end

#schedule_add_weekly(response) ⇒ Object



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/lita/gsuite.rb', line 201

def schedule_add_weekly(response)
  return unless confirm_user_authenticated(response)

  _, day, time, cmd_name = *response.match_data

  schedule = WeeklySchedule.new(
    id: SecureRandom.hex(3),
    day: day,
    time: time,
    cmd: COMMANDS.fetch(cmd_name.downcase, nil),
    user_id: response.user.id,
    room_id: response.room.id,
  )
  if schedule.valid?
    redis.hmset("weekly-schedule", schedule.id, schedule.to_json)
    response.reply("scheduled command")
  else
    response.reply("invalid command")
  end
rescue StandardError => e
  response.reply("Error: #{e.class} #{e.message}")
end

#schedule_add_window(response) ⇒ Object



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/lita/gsuite.rb', line 224

def schedule_add_window(response)
  return unless confirm_user_authenticated(response)

  cmd_name = response.match_data[1].to_s
  schedule = WindowSchedule.new(
    id: SecureRandom.hex(3),
    cmd: WINDOW_COMMANDS.fetch(cmd_name.downcase, nil),
    user_id: response.user.id,
    room_id: response.room.id,
  )
  if schedule.valid?
    redis.hmset("window-schedule", schedule.id, schedule.to_json)
    response.reply("scheduled command")
  else
    response.reply("invalid command")
  end
rescue StandardError => e
  response.reply("Error: #{e.class} #{e.message}")
end

#schedule_commands(response) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/lita/gsuite.rb', line 187

def schedule_commands(response)
  msg = "The following commands are available for scheduling weekly:\n\n"
  COMMANDS.each do |cmd_name, cmd_klass|
    msg += "- #{cmd_name}\n"
  end
  msg += "The following commands are available for scheduling for sliding windows:\n\n"
  WINDOW_COMMANDS.each do |cmd_name, cmd_klass|
    msg += "- #{cmd_name}\n"
  end
  response.reply(msg)
rescue StandardError => e
  response.reply("Error: #{e.class} #{e.message}")
end

#schedule_delete(response) ⇒ Object



244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/lita/gsuite.rb', line 244

def schedule_delete(response)
  cmd_id = response.match_data[1].to_s

  count = redis.hdel("weekly-schedule", cmd_id)
  count += redis.hdel("window-schedule", cmd_id)
  if count > 0
    response.reply("scheduled command #{cmd_id} deleted")
  else
    response.reply("no scheduled command with ID #{cmd_id} found")
  end
rescue StandardError => e
  response.reply("Error: #{e.class} #{e.message}")
end

#schedule_list(response) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/lita/gsuite.rb', line 172

def schedule_list(response)
  room_commands = (weekly_commands_for_room(response.room.name) + window_commands_for_room(response.room.name)).select { |cmd|
    cmd.room_id == response.room.id
  }
  if room_commands.any?
    room_commands.each do |cmd|
      response.reply("#{cmd.human}")
    end
  else
    response.reply("no scheduled commands for this room")
  end
rescue StandardError => e
  response.reply("Error: #{e.class} #{e.message}")
end

#set_token(response) ⇒ Object



163
164
165
166
167
168
169
170
# File 'lib/lita/gsuite.rb', line 163

def set_token(response)
  auth_code = response.match_data[1].to_s

  google_authorizer.get_and_store_credentials_from_code(user_id: response.user.id, code: auth_code, base_url: OOB_OAUTH_URI)
  response.reply("#{response.user.name} now authorized")
rescue StandardError => e
  response.reply("Error: #{e.class} #{e.message}")
end

#start_auth(response) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
# File 'lib/lita/gsuite.rb', line 151

def start_auth(response)
  credentials = google_credentials_for_user(response.user)
  url = google_authorizer.get_authorization_url(base_url: OOB_OAUTH_URI)
  if credentials.nil?
    response.reply "Open the following URL in your browser and enter the resulting code via the 'gsuite set-token <foo>' command:\n\n#{url}"
  else
    response.reply "#{response.user.name} is already authorized with Google. To re-authorize, open the following URL in your browser and enter the resulting code via the 'gsuite set-token <foo>' command:\n\n#{url}"
  end
rescue StandardError => e
  response.reply("Error: #{e.class} #{e.message}")
end

#start_timers(payload) ⇒ Object



58
59
60
61
# File 'lib/lita/gsuite.rb', line 58

def start_timers(payload)
  weekly_commands_timer
  window_commands_timer
end

#suspension_candidates(response) ⇒ Object



117
118
119
120
121
122
123
124
125
126
# File 'lib/lita/gsuite.rb', line 117

def suspension_candidates(response)
  return unless confirm_user_authenticated(response)

  Commands::SuspensionCandidates.new.run(
    robot,
    Source.new(room: response.room),
    gateway(response.user),
    negative_ack: true
  )
end

#two_factor_off(response) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
# File 'lib/lita/gsuite.rb', line 128

def two_factor_off(response)
  return unless confirm_user_authenticated(response)

  ou_path = response.match_data[1].to_s
  Commands::TwoFactorOff.new(ou_path).run(
    robot,
    Source.new(room: response.room),
    gateway(response.user),
    negative_ack: true
  )
end

#two_factor_stats(response) ⇒ Object



140
141
142
143
144
145
146
147
148
149
# File 'lib/lita/gsuite.rb', line 140

def two_factor_stats(response)
  return unless confirm_user_authenticated(response)

  Commands::TwoFactorStats.new.run(
    robot,
    Source.new(room: response.room),
    gateway(response.user),
    negative_ack: true
  )
end