settings: add github integration, move pushover to external - merged with i18n

This commit is contained in:
Carl Chenet 2017-05-20 13:33:46 +02:00
parent 92e8507d5e
commit 52fb11b7e4
7 changed files with 94 additions and 7 deletions

View file

@ -46,6 +46,8 @@ class User < ActiveRecord::Base
s.boolean :show_submitted_story_threads, :default => false
s.boolean :hide_dragons, :default => false
s.string :totp_secret
s.string :github_oauth_token
s.string :github_username
end
validates :email, :format => { :with => /\A[^@ ]+@[^@ ]+\.[^@ ]+\Z/ },

View file

@ -230,6 +230,43 @@
<br>
<br>
<div class="legend">
External Accounts
</div>
<% if Pushover.enabled? %>
<div class="boxline">
<%= label_tag :pushover_user_key,
raw("<a href=\"https://pushover.net/\">Pushover</a>:"),
:class => "required" %>
<%= link_to((@edit_user.pushover_user_key.present??
"Manage Pushover Subscription" : "Subscribe With Pushover"),
"/settings/pushover_auth", :class => "pushover_button",
:method => :post) %>
<span class="hint">
For optional comment and message notifications above
</span>
</div>
<% end %>
<% if Github.enabled? %>
<div class="boxline">
<%= label_tag :github_username, "GitHub:", :class => "required" %>
<% if @edit_user.github_username.present? %>
Linked to
<strong><a href="https://github.com/<%= h(@edit_user.github_username)
%>"><%= h(@edit_user.github_username) %></a></strong>
(<%= link_to "Disconnect", "/settings/github_disconnect",
:method => :post %>)
<% else %>
<a href="/settings/github_auth">Connect</a>
<% end %>
</div>
<% end %>
<br>
<br>
<a name="invite"></a>
<div class="legend">
<%= t('.inviteuser') %>

View file

@ -300,7 +300,7 @@ en:
commentreplynotificationsettings: "Comment Reply Notification Settings"
receiveemail: "Receive E-mail:"
receivepushover: "Receive Pushover Alert:"
requirepushover: "Requires Pushover subscription above"
requirepushover: "Requires Pushover subscription below"
commentmentionnotificationsettings: "Comment Mention Notification Settings"
privatemessagenotificationsettings: "Private Message Notification Settings"
submittedstorycommentsettings: "Submitted Story Comment Settings"

View file

@ -310,7 +310,7 @@ fr:
commentreplynotificationsettings: "Paramètres de notification de réponse à un commentaire"
receiveemail: "Recevoir un e-mail :"
receivepushover: "Recevoir une alerte Pushover :"
requirepushover: "Requière un abonnement Pushover ci-dessus"
requirepushover: "Requière un abonnement Pushover ci-dessous"
commentmentionnotificationsettings: "Paramètres de notification de mention d'un commentaire"
privatemessagenotificationsettings: "Paramètres de notification de message privé"
submittedstorycommentsettings: "Paramètres de commentaires relatifs à vos infos"

View file

@ -105,8 +105,6 @@ Lobsters::Application.routes.draw do
get "/settings" => "settings#index"
post "/settings" => "settings#update"
post "/settings/pushover" => "settings#pushover"
get "/settings/pushover_callback" => "settings#pushover_callback"
post "/settings/delete_account" => "settings#delete_account",
:as => "delete_account"
get "/settings/2fa" => "settings#twofa", :as => "twofa"
@ -118,6 +116,12 @@ Lobsters::Application.routes.draw do
post "/settings/2fa_update" => "settings#twofa_update",
:as => "twofa_update"
post "/settings/pushover_auth" => "settings#pushover_auth"
get "/settings/pushover_callback" => "settings#pushover_callback"
get "/settings/github_auth" => "settings#github_auth"
get "/settings/github_callback" => "settings#github_callback"
post "/settings/github_disconnect" => "settings#github_disconnect"
get "/filters" => "filters#index"
post "/filters" => "filters#update"

40
extras/github.rb Normal file
View file

@ -0,0 +1,40 @@
class Github
cattr_accessor :CLIENT_ID, :CLIENT_SECRET
# these need to be overridden in config/initializers/production.rb
@@CLIENT_ID = nil
@@CLIENT_SECRET = nil
def self.enabled?
self.CLIENT_ID.present?
end
def self.oauth_consumer
OAuth::Consumer.new(self.CLIENT_ID, self.CLIENT_SECRET,
{ :site => "https://api.github.com" })
end
def self.token_and_user_from_code(code)
s = Sponge.new
res = s.fetch("https://github.com/login/oauth/access_token", :post,
{ :client_id => self.CLIENT_ID, :client_secret => self.CLIENT_SECRET,
:code => code })
ps = CGI.parse(res)
tok = ps["access_token"].first
if tok.present?
res = s.fetch("https://api.github.com/user?access_token=#{tok}")
js = JSON.parse(res)
if js && js["login"].present?
return [ tok, js["login"] ]
end
end
return [ nil, nil ]
end
def self.oauth_auth_url(state)
"https://github.com/login/oauth/authorize?client_id=#{self.CLIENT_ID}&" <<
"state=#{state}"
end
end

View file

@ -3,8 +3,12 @@ class Pushover
cattr_accessor :API_TOKEN
cattr_accessor :SUBSCRIPTION_CODE
def self.enabled?
self.API_TOKEN.present?
end
def self.push(user, params)
if !@@API_TOKEN
if !self.enabled?
return
end
@ -15,7 +19,7 @@ class Pushover
s = Sponge.new
s.fetch("https://api.pushover.net/1/messages.json", :post, {
:token => @@API_TOKEN,
:token => self.API_TOKEN,
:user => user,
}.merge(params))
rescue => e
@ -24,7 +28,7 @@ class Pushover
end
def self.subscription_url(params)
u = "https://pushover.net/subscribe/#{@@SUBSCRIPTION_CODE}"
u = "https://pushover.net/subscribe/#{self.SUBSCRIPTION_CODE}"
u << "?success=#{CGI.escape(params[:success])}"
u << "&failure=#{CGI.escape(params[:failure])}"
u