cinch-hangouts 0.0.1 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.2
4
+ - 1.9.3
data/README.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # Cinch::Plugins::Hangouts
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/cinch-hangouts.png)](http://badge.fury.io/rb/cinch-hangouts)
4
+ [![Dependency Status](https://gemnasium.com/bhaberer/cinch-hangouts.png)](https://gemnasium.com/bhaberer/cinch-hangouts)
5
+ [![Build Status](https://travis-ci.org/bhaberer/cinch-hangouts.png?branch=master)](https://travis-ci.org/bhaberer/cinch-hangouts)
6
+ [![Coverage Status](https://coveralls.io/repos/bhaberer/cinch-hangouts/badge.png?branch=master)](https://coveralls.io/r/bhaberer/cinch-hangouts?branch=master)
7
+ [![Code Climate](https://codeclimate.com/github/bhaberer/cinch-hangouts.png)](https://codeclimate.com/github/bhaberer/cinch-hangouts)
8
+
3
9
  Cinch Plugin for tracking Hangout URLs linked in the channel.
4
10
 
5
11
  ## Installation
@@ -26,7 +32,7 @@ Just add the plugin to your list:
26
32
  end
27
33
  end
28
34
 
29
- Then in channel use .hangouts to get notifications of the hangouts that have been linked recently.
35
+ Then in channel use !hangouts to get notifications of the hangouts that have been linked recently.
30
36
 
31
37
  ## Contributing
32
38
 
data/Rakefile CHANGED
@@ -1 +1,7 @@
1
1
  require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new
5
+
6
+ task :default => :spec
7
+ task :test => :spec
@@ -17,8 +17,13 @@ Gem::Specification.new do |gem|
17
17
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
18
  gem.require_paths = ["lib"]
19
19
 
20
- gem.add_dependency 'cinch', '>= 2.0.0'
21
- gem.add_dependency 'cinch-storage'
22
- gem.add_dependency 'cinch-toolbox'
23
- gem.add_dependency 'time-lord', '1.0.1'
20
+ gem.add_development_dependency 'rake'
21
+ gem.add_development_dependency 'rspec'
22
+ gem.add_development_dependency 'coveralls'
23
+ gem.add_development_dependency 'cinch-test'
24
+
25
+ gem.add_dependency 'cinch', '~> 2.0.5'
26
+ gem.add_dependency 'cinch-storage', '~> 1.0.3'
27
+ gem.add_dependency 'cinch-toolbox', '~> 1.0.3'
28
+ gem.add_dependency 'time-lord', '~> 1.0.1'
24
29
  end
@@ -1,5 +1,4 @@
1
- require 'cinch-storage'
2
- require 'cinch-toolbox'
3
- require 'time-lord'
1
+ require 'cinch/plugins/hangouts'
4
2
  require 'cinch/plugins/hangouts/version'
5
- require 'cinch/plugins/hangouts/hangouts'
3
+ require 'cinch/plugins/hangouts/hangout'
4
+ require 'cinch/plugins/hangouts/subscription'
@@ -0,0 +1,101 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'cinch'
3
+ require 'cinch-storage'
4
+ require 'cinch/toolbox'
5
+ require 'time-lord'
6
+
7
+ module Cinch::Plugins
8
+ class Hangouts
9
+ include Cinch::Plugin
10
+ @@hangout_filename = ''
11
+ @@subscription_filename = ''
12
+
13
+ attr_accessor :storage
14
+
15
+ self.help = "Use .hangouts to see the info for any recent hangouts. You can also use .hangouts subscribe to sign up for notifications."
16
+
17
+ match /hangouts\z/, method: :list_hangouts
18
+ match /hangouts subscribe/, method: :subscribe
19
+ match /hangouts unsubscribe/, method: :unsubscribe
20
+
21
+ listen_to :channel
22
+
23
+ # This is the regex that captures hangout links in the following format:
24
+ # https://plus.google.com/hangouts/_/fbae432b70a47bdf7786e53a16f364895c09d9f8
25
+ #
26
+ # The regex will need to be updated if the url scheme changes in the future.
27
+ HANGOUTS_REGEX = /plus\.google\.com\/hangouts\/_\/([a-f0-9]{40})/
28
+
29
+ def initialize(*args)
30
+ super
31
+ @@hangout_filename = config[:hangout_filename] || 'yaml/hangouts.yml'
32
+ @@subscription_filename = config[:subscription_filename] || 'yaml/hangout_subscriptions.yml'
33
+
34
+ @expire_period = config[:expire_period] || 120
35
+ @response_type = config[:response_type] || :notice
36
+ end
37
+
38
+ def listen(m)
39
+ if hangout_id = m.message[HANGOUTS_REGEX, 1]
40
+ debug "#{hangout_id}"
41
+ if hangout = Hangout.find_by_id(hangout_id)
42
+ # If it's an old hangout capture a new expiration time
43
+ hangout.time = Time.now
44
+ hangout.save
45
+ else
46
+ hangout = Hangout.new(m.user.nick, hangout_id, Time.now)
47
+ hangout.save
48
+ Subscription.notify(hangout_id, @bot)
49
+ end
50
+ end
51
+ end
52
+
53
+ def subscribe(m)
54
+ nick = m.user.nick
55
+ if Subscription.for_user(nick)
56
+ msg = "You are already subscribed. "
57
+ else
58
+ sub = Subscription.new(nick)
59
+ sub.save
60
+ msg = "You are now subscribed. I will let you know when a hangout is linked. "
61
+ end
62
+ m.user.notice msg + "To unsubscribe use `.hangouts unsubscribe`."
63
+ end
64
+
65
+ def unsubscribe(m)
66
+ nick = m.user.nick
67
+ if Subscription.for_user(nick)
68
+ Subscription.for_user(nick).delete
69
+ msg = "You are now unsubscribed, and will no longer receive a messages. "
70
+ else
71
+ msg = "You are not subscribed. "
72
+ end
73
+ m.user.notice msg + "To subscribe use `.hangouts subscribe`."
74
+ end
75
+
76
+ def list_hangouts(m)
77
+ Hangout.delete_expired(@expire_period)
78
+ hangouts = Hangout.sorted
79
+
80
+ if hangouts.empty?
81
+ m.user.notice "No hangouts have been linked recently!"
82
+ else
83
+ m.user.notice "These hangouts have been linked in the last #{@expire_period} minutes. " +
84
+ "They may or may not still be going."
85
+ hangouts.each do |hangout|
86
+ m.user.notice "#{hangout.nick} started a hangout at #{Hangout.url(hangout.id)} " +
87
+ "it was last linked #{hangout.time.ago.to_words}"
88
+ end
89
+ end
90
+ end
91
+
92
+ def respond(m, message)
93
+ case @response_type
94
+ when :notice
95
+ m.user.notice message
96
+ when :pm
97
+ m.user.send message
98
+ end
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,60 @@
1
+ class Hangout < Cinch::Plugins::Hangouts
2
+ attr_accessor :nick, :id, :time
3
+
4
+ def initialize(nick, id, time)
5
+ @nick = nick
6
+ @id = id
7
+ @time = time
8
+ end
9
+
10
+ def save
11
+ storage = CinchStorage.new(@@hangout_filename)
12
+ storage.data[:hangouts] ||= Hash.new
13
+ storage.data[:hangouts][self.id] = self
14
+ storage.save
15
+ end
16
+
17
+ def to_yaml
18
+ { nick: @nick, id: @id, time: @time }
19
+ end
20
+
21
+ def self.find_by_id(id)
22
+ listing[id]
23
+ end
24
+
25
+ def self.delete_expired(expire_time)
26
+ return if listing.nil?
27
+ storage = read_file
28
+ storage.data[:hangouts].delete_if do |id, hangout|
29
+ (Time.now - hangout.time) > (expire_time * 60)
30
+ end
31
+ storage.save
32
+ end
33
+
34
+ def self.sorted
35
+ hangouts = listing.values
36
+ hangouts.sort! { |x,y| y.time <=> x.time }
37
+ return hangouts
38
+ end
39
+
40
+ def self.listing
41
+ read_file.data[:hangouts]
42
+ end
43
+
44
+ def self.url(id, shorten = true)
45
+ url = "https://plus.google.com/hangouts/_/#{id}"
46
+ return url unless shorten
47
+ Cinch::Toolbox.shorten(url)
48
+ end
49
+
50
+ private
51
+
52
+ def self.read_file
53
+ storage = CinchStorage.new(@@hangout_filename)
54
+ unless storage.data[:hangouts]
55
+ storage.data[:hangouts] = Hash.new
56
+ storage.save
57
+ end
58
+ storage
59
+ end
60
+ end
@@ -0,0 +1,52 @@
1
+ class Subscription < Cinch::Plugins::Hangouts
2
+ attr_accessor :nick, :all_links
3
+
4
+ def to_yaml
5
+ { }
6
+ end
7
+
8
+ def initialize(nick)
9
+ @nick = nick
10
+ @all_links = false
11
+ save
12
+ end
13
+
14
+ def save
15
+ subs = Subscription.storage
16
+ subs.data[self.nick] = self
17
+ subs.save
18
+ end
19
+
20
+ def delete
21
+ subs = Subscription.storage
22
+ subs.data[self.nick] = nil
23
+ subs.save
24
+ end
25
+
26
+ def self.for_user(nick)
27
+ return nil unless list.key?(nick)
28
+ list[nick]
29
+ end
30
+
31
+ def self.list
32
+ Subscription.storage.data
33
+ end
34
+
35
+ def self.notify(hangout_id, bot)
36
+ nick = Hangout.find_by_id(hangout_id).nick
37
+ Subscription.list.each do |sub|
38
+ # Don't link the person who linked it.
39
+ if nick != sub.nick
40
+ user = Cinch::User.new(sub.nick, bot)
41
+ respond(user, "#{nick} just linked a new hangout at: #{Hangout.url(hangout_id)}")
42
+ end
43
+ end
44
+ end
45
+
46
+ private
47
+
48
+ def self.storage
49
+ CinchStorage.new(@@subscription_filename)
50
+ end
51
+ end
52
+
@@ -1,7 +1,7 @@
1
1
  module Cinch
2
2
  module Plugins
3
3
  class Hangouts
4
- VERSION = "0.0.1"
4
+ VERSION = "1.0.0"
5
5
  end
6
6
  end
7
7
  end
@@ -0,0 +1,115 @@
1
+ require 'spec_helper'
2
+
3
+ describe Cinch::Plugins::Hangouts do
4
+ include Cinch::Test
5
+
6
+ before(:each) do
7
+ files = { hangout_filename: '/tmp/hangout.yml',
8
+ subscription_filename: '/tmp/subscription.yml' }
9
+ files.values.each do |file|
10
+ File.delete(file) if File.exist?(file)
11
+ end
12
+ @bot = make_bot(Cinch::Plugins::Hangouts, files)
13
+ end
14
+
15
+ describe 'handling hangout links' do
16
+ it 'should return an error if no one has linked a hangout' do
17
+ get_replies(make_message(@bot, '!hangouts', { channel: '#foo' })).first.text.
18
+ should == "No hangouts have been linked recently!"
19
+ end
20
+
21
+ it 'should not capture a malformed (invalid chars) Hangout link' do
22
+ msg = make_message(@bot, Hangout.url('82b5cc7f76b7a%19c180416c2f509027!!d8856d', false),
23
+ { :channel => '#foo' })
24
+ get_replies(msg).should be_empty
25
+ msg = make_message(@bot, '!hangouts')
26
+ get_replies(msg).first.text.
27
+ should == "No hangouts have been linked recently!"
28
+ end
29
+
30
+ it 'should not capture a malformed (wrong length) Hangout link' do
31
+ msg = make_message(@bot, Hangout.url('82b5cc', false),
32
+ { :channel => '#foo' })
33
+ get_replies(msg).should be_empty
34
+ msg = make_message(@bot, '!hangouts')
35
+ get_replies(msg).first.text.
36
+ should == "No hangouts have been linked recently!"
37
+ end
38
+
39
+ it 'should capture a legit Hangout link and store it in @storage' do
40
+ msg = make_message(@bot, Hangout.url(random_hangout_id, false), { :channel => '#foo' })
41
+ get_replies(msg).should be_empty
42
+ sleep 1 # hack until 'time-lord' fix gets released
43
+ msg = make_message(@bot, '!hangouts')
44
+ reply = get_replies(msg).last.text
45
+ reply.should include "test started a hangout at"
46
+ reply.should match(/it was last linked \d seconds? ago/)
47
+ end
48
+
49
+ it 'should capture a legit Hangout link if it has trailing params' do
50
+ msg = make_message(@bot, Hangout.url(random_hangout_id + '?hl=en', false),
51
+ { :channel => '#foo' })
52
+ get_replies(msg)
53
+ sleep 1 # hack until 'time-lord' fix gets released
54
+ msg = make_message(@bot, '!hangouts')
55
+ reply = get_replies(msg).last.text
56
+ reply.should include "test started a hangout at"
57
+ reply.should match(/it was last linked \d seconds? ago/)
58
+ end
59
+ end
60
+
61
+ describe 'subscriptions' do
62
+ it 'should allow users to subscribe' do
63
+ msg = make_message(@bot, '!hangouts subscribe')
64
+ get_replies(msg).first.text.
65
+ should include("You are now subscribed")
66
+ end
67
+
68
+ it 'should allow users to subscribe' do
69
+ msg = make_message(@bot, '!hangouts subscribe')
70
+ get_replies(msg).first.text.
71
+ should include("You are now subscribed")
72
+ end
73
+
74
+ it 'should inform users that they already subscribed' do
75
+ get_replies(make_message(@bot, '!hangouts subscribe'))
76
+ msg = make_message(@bot, '!hangouts subscribe')
77
+ get_replies(msg).first.text.
78
+ should include("You are already subscribed")
79
+ end
80
+
81
+ it 'should allow users to unsubscribe' do
82
+ get_replies(make_message(@bot, '!hangouts subscribe'))
83
+ msg = make_message(@bot, '!hangouts unsubscribe')
84
+ get_replies(msg).first.text.
85
+ should include("You are now unsubscribed")
86
+ end
87
+
88
+ it 'should inform users that they are not subscribed on an unsubscribe' do
89
+ msg = make_message(@bot, '!hangouts unsubscribe')
90
+ get_replies(msg).first.text.
91
+ should include("You are not subscribed.")
92
+ end
93
+
94
+ #it 'should notify users when a new hangout is linked' do
95
+ # get_replies(make_message(@bot, '!hangouts subscribe'), { channel: '#foo', nick: 'joe' } )
96
+ # msgs = get_replies(make_message(@bot, Hangout.url(random_hangout_id, false), { channel: '#foo', nick: 'josh' }))
97
+ # msgs.first.should_not be_nil
98
+ #end
99
+
100
+ it 'should not notify users when an old hangout is relinked' do
101
+ get_replies(make_message(@bot, '!hangouts subscribe'), { :channel => '#foo' } )
102
+ get_replies(make_message(@bot, Hangout.url(random_hangout_id, false), { :channel => '#foo' }))
103
+ msg = make_message(@bot, Hangout.url(random_hangout_id, false), { :channel => '#foo' })
104
+ get_replies(msg).
105
+ should be_empty
106
+ end
107
+ end
108
+
109
+ def random_hangout_id
110
+ chars = %w{ a b c d e f 0 1 2 3 4 5 6 7 8 9 }
111
+ string = ''
112
+ 40.times { string << chars[rand(chars.length)] }
113
+ string
114
+ end
115
+ end
@@ -0,0 +1,11 @@
1
+ require 'coveralls'
2
+ require 'simplecov'
3
+
4
+ SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
5
+ SimpleCov::Formatter::HTMLFormatter,
6
+ Coveralls::SimpleCov::Formatter
7
+ ]
8
+ SimpleCov.start
9
+
10
+ require 'cinch-hangouts'
11
+ require 'cinch/test'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cinch-hangouts
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 1.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,33 +9,33 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-05-04 00:00:00.000000000 Z
12
+ date: 2013-08-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- name: cinch
15
+ name: rake
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
20
20
  - !ruby/object:Gem::Version
21
- version: 2.0.0
22
- type: :runtime
21
+ version: '0'
22
+ type: :development
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
25
25
  none: false
26
26
  requirements:
27
27
  - - ! '>='
28
28
  - !ruby/object:Gem::Version
29
- version: 2.0.0
29
+ version: '0'
30
30
  - !ruby/object:Gem::Dependency
31
- name: cinch-storage
31
+ name: rspec
32
32
  requirement: !ruby/object:Gem::Requirement
33
33
  none: false
34
34
  requirements:
35
35
  - - ! '>='
36
36
  - !ruby/object:Gem::Version
37
37
  version: '0'
38
- type: :runtime
38
+ type: :development
39
39
  prerelease: false
40
40
  version_requirements: !ruby/object:Gem::Requirement
41
41
  none: false
@@ -44,14 +44,14 @@ dependencies:
44
44
  - !ruby/object:Gem::Version
45
45
  version: '0'
46
46
  - !ruby/object:Gem::Dependency
47
- name: cinch-toolbox
47
+ name: coveralls
48
48
  requirement: !ruby/object:Gem::Requirement
49
49
  none: false
50
50
  requirements:
51
51
  - - ! '>='
52
52
  - !ruby/object:Gem::Version
53
53
  version: '0'
54
- type: :runtime
54
+ type: :development
55
55
  prerelease: false
56
56
  version_requirements: !ruby/object:Gem::Requirement
57
57
  none: false
@@ -59,12 +59,76 @@ dependencies:
59
59
  - - ! '>='
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: cinch-test
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: cinch
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ~>
84
+ - !ruby/object:Gem::Version
85
+ version: 2.0.5
86
+ type: :runtime
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ~>
92
+ - !ruby/object:Gem::Version
93
+ version: 2.0.5
94
+ - !ruby/object:Gem::Dependency
95
+ name: cinch-storage
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ~>
100
+ - !ruby/object:Gem::Version
101
+ version: 1.0.3
102
+ type: :runtime
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ~>
108
+ - !ruby/object:Gem::Version
109
+ version: 1.0.3
110
+ - !ruby/object:Gem::Dependency
111
+ name: cinch-toolbox
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ~>
116
+ - !ruby/object:Gem::Version
117
+ version: 1.0.3
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ~>
124
+ - !ruby/object:Gem::Version
125
+ version: 1.0.3
62
126
  - !ruby/object:Gem::Dependency
63
127
  name: time-lord
64
128
  requirement: !ruby/object:Gem::Requirement
65
129
  none: false
66
130
  requirements:
67
- - - '='
131
+ - - ~>
68
132
  - !ruby/object:Gem::Version
69
133
  version: 1.0.1
70
134
  type: :runtime
@@ -72,7 +136,7 @@ dependencies:
72
136
  version_requirements: !ruby/object:Gem::Requirement
73
137
  none: false
74
138
  requirements:
75
- - - '='
139
+ - - ~>
76
140
  - !ruby/object:Gem::Version
77
141
  version: 1.0.1
78
142
  description: Cinch Plugin to track any Google Hangouts that are linked into the channel
@@ -83,14 +147,19 @@ extensions: []
83
147
  extra_rdoc_files: []
84
148
  files:
85
149
  - .gitignore
150
+ - .travis.yml
86
151
  - Gemfile
87
152
  - LICENSE.txt
88
153
  - README.md
89
154
  - Rakefile
90
155
  - cinch-hangouts.gemspec
91
156
  - lib/cinch-hangouts.rb
92
- - lib/cinch/plugins/hangouts/hangouts.rb
157
+ - lib/cinch/plugins/hangouts.rb
158
+ - lib/cinch/plugins/hangouts/hangout.rb
159
+ - lib/cinch/plugins/hangouts/subscription.rb
93
160
  - lib/cinch/plugins/hangouts/version.rb
161
+ - spec/cinch-hangouts_spec.rb
162
+ - spec/spec_helper.rb
94
163
  homepage: https://github.com/bhaberer/cinch-hangouts
95
164
  licenses: []
96
165
  post_install_message:
@@ -111,8 +180,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
111
180
  version: '0'
112
181
  requirements: []
113
182
  rubyforge_project:
114
- rubygems_version: 1.8.24
183
+ rubygems_version: 1.8.25
115
184
  signing_key:
116
185
  specification_version: 3
117
186
  summary: Cinch Plugin for racking Google Hangouts
118
- test_files: []
187
+ test_files:
188
+ - spec/cinch-hangouts_spec.rb
189
+ - spec/spec_helper.rb
190
+ has_rdoc:
@@ -1,101 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
- module Cinch::Plugins
3
- class Hangouts
4
- include Cinch::Plugin
5
-
6
- self.help = "Use .hangouts to see the info for any recent hangouts. You can also use .hangouts subscribe to sign up for notifications."
7
-
8
- match /hangouts\z/, method: :list_hangouts
9
- match /hangouts subscribe/, method: :subscribe
10
- match /hangouts unsubscribe/, method: :unsubscribe
11
-
12
- listen_to :channel
13
-
14
- def initialize(*args)
15
- super
16
- @storage = CinchStorage.new(config[:filename] || 'yaml/hangouts.yml')
17
- @storage.data[:hangouts] ||= {}
18
- @storage.data[:subscriptions] ||= []
19
- @expire_period = config[:expire_period] || 120
20
- end
21
-
22
- def listen(m)
23
- # This is the regex that captures hangout links in the following format:
24
- #
25
- # https://plus.google.com/hangouts/_/fbae432b70a47bdf7786e53a16f364895c09d9f8
26
- #
27
- # The regex will need to be updated if the url scheme changes in the future.
28
- if m.message.match(/plus.google.com\/hangouts\//)
29
- hangout_id = m.message[/[^\/?]{40}/, 0]
30
- unless hangout_id.nil?
31
- if @storage.data[:hangouts].key?(hangout_id)
32
- @storage.data[:hangouts][hangout_id][:time] = Time.now
33
- else
34
- @storage.data[:hangouts][hangout_id] = {:user => m.user.nick, :time => Time.now}
35
- end
36
-
37
- synchronize(:hangout_save) do
38
- @storage.save
39
- end
40
-
41
- @storage.data[:subscriptions].each do |user|
42
- unless m.user.nick == user
43
- Cinch::User.new(user, @bot).notice "#{m.user.nick} just linked a new hangout at #{hangout_url(hangout_id)}!"
44
- end
45
- end
46
- end
47
- end
48
- end
49
-
50
- private
51
-
52
- def list_hangouts(m)
53
- hangouts = sort_and_expire
54
- if hangouts.empty?
55
- m.user.notice "No hangouts have been linked recently!"
56
- else
57
- m.user.notice "These hangouts have been linked in the last #{@expire_period} minutes. They may or may not still be going."
58
- hangouts.each do |hangout|
59
- m.user.notice "#{hangout[:user]} started a hangout #{hangout[:time].ago.to_words} ago at #{hangout_url(hangout[:id])}"
60
- end
61
- end
62
- end
63
-
64
- def subscribe(m)
65
- nick = m.user.nick
66
- if @storage.data[:subscriptions].include?(nick)
67
- m.user.notice "You are already subscribed, to unsubscribe use `.hangouts unsubscribe`"
68
- else
69
- @storage.data[:subscriptions] << nick
70
- synchronize(:hangout_save) do
71
- @storage.save
72
- end
73
- m.user.notice "You are now subscribed, and will receive a message when a *new* hangout is linked. To unsubscribe use `.hangouts unsubscribe`."
74
- end
75
- end
76
-
77
- def unsubscribe(m)
78
- nick = m.user.nick
79
- if @storage.data[:subscriptions].include?(nick)
80
- @storage.data[:subscriptions].delete(nick)
81
- synchronize(:hangout_save) do
82
- @storage.save
83
- end
84
- m.user.notice "You are now unsubscribed, and will no longer receive a messages. To resubscribe use `.hangouts subscribe`."
85
- else
86
- m.user.notice "You are not subscribed, to subscribe use `.hangouts subscribe`"
87
- end
88
- end
89
-
90
- def sort_and_expire
91
- hangouts = @storage.data[:hangouts].each_pair.map { |x,y| y[:id] = x;y }
92
- hangouts.delete_if { |h| Time.now - h[:time] > @expire_period * 60 }
93
- hangouts.sort! { |x,y| y[:time] <=> x[:time] }
94
- return hangouts
95
- end
96
-
97
- def hangout_url(id)
98
- return Cinch::Toolbox.shorten("https://plus.google.com/hangouts/_/#{id}")
99
- end
100
- end
101
- end