-
Notifications
You must be signed in to change notification settings - Fork 2
/
focacha.rb
208 lines (171 loc) · 6.17 KB
/
focacha.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
require_relative 'models/channel'
require_relative 'models/message'
require_relative 'models/stream_response'
require_relative 'models/current_topic_change_message'
require_relative 'models/user'
module Focacha
class Application < Sinatra::Base
configure do
# config_file
register Sinatra::ConfigFile
config_file 'config/config.yml.erb'
# google_plus
if settings.auth['google']['enable']
GooglePlus.api_key = settings.auth['google']['api_key']
end
# logging
enable :logging
# method override
enable :method_override
# mongoid
#Mongoid.logger = Logger.new($stdout)
#Moped.logger = Logger.new($stdout)
#Mongoid.logger.level = Logger::DEBUG
#Moped.logger.level = Logger::DEBUG
Mongoid.load! 'config/mongoid.yml', environment
# namespace
register Sinatra::Namespace
# omniauth
use OmniAuth::Builder do
settings = Focacha::Application.settings
if settings.auth['facebook']['enable']
provider :facebook, settings.auth['facebook']['app_id'], settings.auth['facebook']['secret']
end
if settings.auth['google']['enable']
provider :google_oauth2, settings.auth['google']['consumer_key'], settings.auth['google']['consumer_secret'], { access_type: 'online', approval_prompt: '' }
end
if settings.auth['twitter']['enable']
provider :twitter, settings.auth['twitter']['consumer_key'], settings.auth['twitter']['consumer_secret']
end
end
# reloader
register Sinatra::Reloader if development?
# sessions
enable :sessions
set :session_secret, settings.session['secret']
set server: 'thin', connections: []
# sinatra-partial
register Sinatra::Partial
set :partial_template_engine, :slim
enable :partial_underscores
# sinatra-r18n
register Sinatra::R18n
R18n::I18n.default = settings.i18n['default_locale']
R18n.default_places { 'config/locales' }
# static
set :static, true
# twitter
if settings.auth['twitter']['enable']
Twitter.configure do |config|
config.consumer_key = settings.auth['twitter']['consumer_key']
config.consumer_secret = settings.auth['twitter']['consumer_secret']
end
end
end
helpers do
def current_user
@user ||= User.find_by(uid: session[:uid]) if session[:uid]
end
def facebook_enabled?
settings.auth['facebook']['enable']
end
def google_enabled?
settings.auth['google']['enable']
end
def html_pipeline
@html_pipeline ||= HTML::Pipeline.new [
HTML::Pipeline::MarkdownFilter,
HTML::Pipeline::SanitizationFilter,
HTML::Pipeline::CamoFilter,
HTML::Pipeline::ImageMaxWidthFilter,
HTML::Pipeline::HttpsFilter,
HTML::Pipeline::MentionFilter,
HTML::Pipeline::EmojiFilter,
HTML::Pipeline::SyntaxHighlightFilter
], { asset_root: '/images/' }
end
def twitter_enabled?
settings.auth['twitter']['enable']
end
end
before do
pass if request.path_info =~ /\/auth\//
unless current_user
halt 401, slim(:'auth/sign_in', layout: :'layouts/unauthorized')
end
end
get '/' do
redirect '/channels'
end
get '/stream', provides: 'text/event-stream' do
stream :keep_open do |out|
# store connection for later on
settings.connections << { owner: current_user, connection: out }
# remove connection when closed properly
out.callback { settings.connections.delete out }
# remove connection when closed due to an error
out.errback do
logger.warn 'We just lost a connection!'
settings.connections.delete out
end
end
end
namespace '/auth' do
get '/:provider/callback' do
auth = request.env['omniauth.auth']
user = User.find_by(provider: auth['provider'], uid: auth['uid']) || User.create_with_omniauth(auth)
session[:uid] = user.uid
redirect '/'
end
get '/failure' do
slim :'auth/failure', layout: :'layouts/unauthorized'
end
delete '/destroy' do
session[:uid] = nil
redirect '/'
end
end
namespace '/channels' do
get do
slim :'channels/index', layout: :'layouts/focacha', locals: { channels: Channel.all, channel: Channel.new }
end
post do
channel = Channel.new params[:channel]
channel.user = current_user
if channel.valid?
channel.save
redirect '/'
else
slim :'channels/index', layout: :'layouts/focacha', locals: { channels: Channel.all, channel: Channel.new }
end
end
get '/:id' do
channel = Channel.find_by(id: params[:id])
slim :'channels/show', layout: :'layouts/focacha', locals: { channel: channel }
end
put '/:id/change_current_topic', provides: 'json' do
channel = Channel.find_by(id: params[:id])
channel.update_attributes params[:channel]
channel.messages.create({ text: channel.current_topic, user: current_user }, CurrentTopicChangeMessage)
stream_response = StreamResponse.new(:topic_changed, { new_topic: channel.current_topic })
settings.connections.each { |connection| stream_response.build(connection[:connection]) unless connection[:owner] == current_user }
halt(200, stream_response.data.to_json)
end
post '/:id/messages', provides: 'json' do
channel = Channel.find_by(id: params[:id])
message = channel.messages.new(text: params[:message])
message.user = current_user
if message.valid?
message.save
stream_response = StreamResponse.new(:new_message, { user: message.user.name , message: message })
settings.connections.each do |conn|
stream_response.build(conn[:connection]) unless conn[:owner] == message.user
end
halt(201, stream_response.data.to_json)
else
status 424
end
end
end
end
end