ruboty-qiita_anti_spam 0.1.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9363ff370b768499cf452a39b9608bf7291a064e
4
+ data.tar.gz: 9037418dfc5e59ad1ba3c83ec2a864c9abf9b817
5
+ SHA512:
6
+ metadata.gz: 8d42d9d9d37abf8436c919b4e3e03b6fd56ab647ec0d4a1a4e0ddf5006ec9d34e8fce08d3006700dee4ead0bc8b86bf6c1406b3035b2445bd2337e6a80cfffe5
7
+ data.tar.gz: ac5f37059ef96757e7049bebff587267b3a94d7a99bbe770b5a18ec0fe0a65f51916cd22da2bbb1f59928761cdd717e066361f3952989edc014cb488acfcc3ad
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ruboty-qiita_anti_spam.gemspec
4
+ gemspec
@@ -0,0 +1,35 @@
1
+ # Ruboty::QiitaAntiSpam
2
+
3
+ Ruboty::QiitaAntiSpam provides these commands:
4
+
5
+ ```text
6
+ ruboty ham ( <UUID> | <Qiita user ID> )
7
+ ruboty spam ( <UUID> | <Qiita user ID> )
8
+ ```
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ ```ruby
15
+ gem 'ruboty-qiita_anti_spam'
16
+ ```
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+ Or install it yourself as:
23
+
24
+ $ gem install ruboty-qiita_anti_spam
25
+
26
+ ## Development
27
+
28
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
29
+
30
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
31
+
32
+ ## Contributing
33
+
34
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/ruboty-qiita_anti_spam.
35
+
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "ruboty/qiita_anti_spam"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,181 @@
1
+ require "active_support/core_ext/hash/keys"
2
+ require "akismet"
3
+ require "google/api_client/client_secrets"
4
+ require "google/apis/bigquery_v2"
5
+ require "googleauth"
6
+ require "qiita"
7
+ require "ruboty"
8
+ require "stringio"
9
+
10
+ module Ruboty
11
+ module Handlers
12
+ class QiitaAntiSpam < Base
13
+ AKISMET_SITE_ROOT_URL = "http://qiita.com"
14
+
15
+ # Note that USER_ID_REGEXP also matches a UUID string.
16
+ # When you want to distingish a string, test it with UUID_REGEXP first.
17
+ USER_ID_REGEXP = %r<\w[\w\-]+\w(?:@github)?>
18
+ UUID_REGEXP = %r<[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}>
19
+ ITEM_SPECIFIER_REGEXP = %r<#{UUID_REGEXP}|#{USER_ID_REGEXP}>
20
+
21
+ env :AKISMET_API_KEY, "Akismet API key"
22
+ env :GOOGLE_APPLICATION_CREDENTIALS_JSON, "Google application credentials as JSON string"
23
+ env :QIITA_ACCESS_TOKEN, "Qiita access token", optional: true
24
+
25
+ on(
26
+ /ham (?<item_spec>#{ITEM_SPECIFIER_REGEXP})\z/,
27
+ description: "Submit an item (article or comment), or all items posted by a user, as ham.",
28
+ name: "submit_ham",
29
+ )
30
+
31
+ on(
32
+ /spam (?<item_spec>#{ITEM_SPECIFIER_REGEXP})\z/,
33
+ description: "Submit an item (article or comment), or all items posted by a user, as spam.",
34
+ name: "submit_spam",
35
+ )
36
+
37
+ def submit_ham(message)
38
+ submit(message, :ham)
39
+ end
40
+
41
+ def submit_spam(message)
42
+ submit(message, :spam)
43
+ end
44
+
45
+ private
46
+
47
+ # @param message [Ruboty::Message]
48
+ # @param item_kind [:ham, :spam]
49
+ def submit(message, item_kind)
50
+ item_spec = message[:item_spec]
51
+ if item_spec =~ UUID_REGEXP
52
+ events = bigquery_client.fetch_check_spam_events(uuid: item_spec)
53
+ elsif
54
+ permanent_user_id = get_permanent_user_id(item_spec)
55
+ if permanent_user_id.nil?
56
+ message.reply("User #{item_spec} not found.")
57
+ return
58
+ end
59
+ events = bigquery_client.fetch_check_spam_events(permanent_user_id: permanent_user_id)
60
+ end
61
+
62
+ lines = []
63
+ begin
64
+ akismet_client.open
65
+ events.each do |ev|
66
+ akismet_client.__send__(item_kind, ev.user_ip, ev.user_agent, ev.other_params)
67
+ lines << "Submitted #{ev.target_url} to Akismet as #{item_kind}."
68
+ end
69
+ ensure
70
+ akismet_client.close
71
+ end
72
+
73
+ if lines.empty?
74
+ message.reply("No items found.")
75
+ else
76
+ message.reply(lines.join("\n"))
77
+ end
78
+ rescue
79
+ message.reply("Error: #{$!}")
80
+ end
81
+
82
+ # @param user_id [String]
83
+ # @return [Integer, nil]
84
+ def get_permanent_user_id(user_id)
85
+ qiita_client.get_user(user_id).body["permanent_id"].to_i || nil rescue nil
86
+ end
87
+
88
+ def akismet_client
89
+ @akismet_client ||= Akismet::Client.new(
90
+ ENV["AKISMET_API_KEY"],
91
+ AKISMET_SITE_ROOT_URL,
92
+ )
93
+ end
94
+
95
+ def bigquery_client
96
+ @bigquery_client ||= BigqueryClient.new(
97
+ application_credentials_json: ENV["GOOGLE_APPLICATION_CREDENTIALS_JSON"]
98
+ )
99
+ end
100
+
101
+ def qiita_client
102
+ @qiita_client ||= Qiita::Client.new(access_token: ENV["QIITA_ACCESS_TOKEN"])
103
+ end
104
+
105
+ class BigqueryClient
106
+ APPLICATION_NAME = "ruboty-qiita_anti_spam"
107
+ AUTH_URI = "https://accounts.google.com/o/oauth2/auth"
108
+ SCOPE = Google::Apis::BigqueryV2::AUTH_BIGQUERY
109
+ TOKEN_URI = "https://accounts.google.com/o/oauth2/token"
110
+
111
+ def initialize(application_credentials_json: nil)
112
+ @application_credentials_json = application_credentials_json
113
+ end
114
+
115
+ # @options opts [Integer] :permanent_user_id
116
+ # @options opts [String] :uuid
117
+ # return [Array<CheckSpamEvent>]
118
+ def fetch_check_spam_events(opts)
119
+ query_request = build_query_request(opts)
120
+ query_response = bigquery_service.query("qiita-com", query_request)
121
+ (query_response.rows || []).map do |row|
122
+ CheckSpamEvent.new(row)
123
+ end
124
+ end
125
+
126
+ private
127
+
128
+ def bigquery_service
129
+ @bigquery_service ||= begin
130
+ service = Google::Apis::BigqueryV2::BigqueryService.new
131
+ service.client_options.application_name = APPLICATION_NAME
132
+ service.client_options.application_version = Ruboty::QiitaAntiSpam::VERSION
133
+ service.authorization = authorization
134
+ service
135
+ end
136
+ end
137
+
138
+ def authorization
139
+ json_key_io = StringIO.new(@application_credentials_json)
140
+ Google::Auth::ServiceAccountCredentials.new(json_key_io: json_key_io, scope: [SCOPE])
141
+ end
142
+
143
+ # @options opts [Integer] :permanent_user_id
144
+ # @options opts [String] :uuid
145
+ # return [Google::Apis::BigqueryV2::QueryRequest]
146
+ def build_query_request(opts)
147
+ if (permanent_user_id = opts[:permanent_user_id])
148
+ cond = %Q(user_id = #{permanent_user_id})
149
+ elsif (uuid = opts[:uuid])
150
+ cond = %Q(message3 = "#{uuid}")
151
+ else
152
+ fail ArgumentError
153
+ end
154
+ # Row spec: message1 = context, message2 = akismet_params, message3 = uuid
155
+ query = <<-EOQ
156
+ SELECT message1, message2
157
+ FROM (TABLE_DATE_RANGE(qiita.events_, DATE_ADD(CURRENT_TIMESTAMP(), -7, "DAY"), CURRENT_TIMESTAMP()))
158
+ WHERE name = "check spam" AND (#{cond})
159
+ LIMIT 1000
160
+ EOQ
161
+ Google::Apis::BigqueryV2::QueryRequest.new(query: query)
162
+ end
163
+ end
164
+
165
+ class CheckSpamEvent
166
+ attr_reader :target_url
167
+ attr_reader :other_params, :user_agent, :user_ip
168
+
169
+ # @param row [Google::Apis::BigqueryV2::TableRow]
170
+ def initialize(row)
171
+ context = JSON.parse(row.f[0].v).deep_symbolize_keys
172
+ @target_url = context[:target_url]
173
+ akismet_params = JSON.parse(row.f[1].v).deep_symbolize_keys
174
+ @other_params = akismet_params[:other_params]
175
+ @user_agent = akismet_params[:user_agent]
176
+ @user_ip = akismet_params[:user_ip]
177
+ end
178
+ end
179
+ end
180
+ end
181
+ end
@@ -0,0 +1,2 @@
1
+ require "ruboty/qiita_anti_spam/version"
2
+ require "ruboty/handlers/qiita_anti_spam"
@@ -0,0 +1,5 @@
1
+ module Ruboty
2
+ module QiitaAntiSpam
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ruboty/qiita_anti_spam/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ruboty-qiita_anti_spam"
8
+ spec.version = Ruboty::QiitaAntiSpam::VERSION
9
+ spec.authors = ["Tomoki Aonuma"]
10
+ spec.email = ["uasi@uasi.jp"]
11
+
12
+ spec.summary = %q{Ruboty plug-in for Qiita's internal anti-spam system.}
13
+ spec.description = %q{Ruboty plug-in for Qiita's internal anti-spam system.}
14
+ spec.homepage = "https://github.com/increments/ruboty-qiita_anti_spam"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.10"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+
24
+ spec.add_runtime_dependency "activesupport"
25
+ spec.add_runtime_dependency "akismet"
26
+ spec.add_runtime_dependency "google-api-client", "~> 0.9.pre1"
27
+ spec.add_runtime_dependency "qiita"
28
+ spec.add_runtime_dependency "ruboty"
29
+ end
metadata ADDED
@@ -0,0 +1,151 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruboty-qiita_anti_spam
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Tomoki Aonuma
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-11-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: activesupport
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: akismet
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: google-api-client
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 0.9.pre1
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 0.9.pre1
83
+ - !ruby/object:Gem::Dependency
84
+ name: qiita
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: ruboty
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: Ruboty plug-in for Qiita's internal anti-spam system.
112
+ email:
113
+ - uasi@uasi.jp
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - Gemfile
120
+ - README.md
121
+ - Rakefile
122
+ - bin/console
123
+ - bin/setup
124
+ - lib/ruboty/handlers/qiita_anti_spam.rb
125
+ - lib/ruboty/qiita_anti_spam.rb
126
+ - lib/ruboty/qiita_anti_spam/version.rb
127
+ - ruboty-qiita_anti_spam.gemspec
128
+ homepage: https://github.com/increments/ruboty-qiita_anti_spam
129
+ licenses: []
130
+ metadata: {}
131
+ post_install_message:
132
+ rdoc_options: []
133
+ require_paths:
134
+ - lib
135
+ required_ruby_version: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - ">="
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ required_rubygems_version: !ruby/object:Gem::Requirement
141
+ requirements:
142
+ - - ">="
143
+ - !ruby/object:Gem::Version
144
+ version: '0'
145
+ requirements: []
146
+ rubyforge_project:
147
+ rubygems_version: 2.4.5.1
148
+ signing_key:
149
+ specification_version: 4
150
+ summary: Ruboty plug-in for Qiita's internal anti-spam system.
151
+ test_files: []