/
helpers.rb
165 lines (132 loc) · 4.37 KB
/
helpers.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
class Symbol
def not
return Sequel.~( self )
end
end
class Hash
def sql_or
return Sequel.or(self)
end
end
class NilClass
def strftime(x)
return "error"
end
end
class Sinatra::Base
helpers do
def uu
return URL_BASE
end
def logger( text )
puts Time.now.strftime("%H:%M:%S") + " " + text
return nil
end
def escape_sphinx(input)
return input
end
def clean_sphinx(input)
return input.gsub(/\W+/, " ")
end
def get_sources( )
sources = CACHE_CLIENT.get( "/get_sources" )
if sources.nil?
sources = Source.all
CACHE_CLIENT.set( "/get_sources", sources )
end
return sources
end
def get_posts_hl( opts = {}, query )
post = Text.filter( opts ).
left_outer_join( :nodes, :nodes__id => :texts__node ).
left_outer_join( :sources, :sources__id => :nodes__source ).
left_outer_join( :users.as(:sender), :sender__id => :texts__sender ).
left_outer_join( :users.as(:receiver), :receiver__id => :texts__receiver ).
select{ [
:nodes__id.as( :node_id ),
:nodes__title.as( :node_title ),
:sources__title.as( :source_title ),
:sources__url.as( :source_url ),
:sources__notes.as( :source_notes ),
:texts__date.as( :text_date ),
:texts__id.as( :text_id ),
:texts__title.as( :text_title ),
:ts_headline.sql_function( "public.polish", :texts__content, :plainto_tsquery.sql_function( "public.polish", query ) ).as( :text_content ) ,
:texts__sender.as( :sender_id ),
:texts__receiver.as( :receiver_id ),
:sender__login.as( :sender_login ),
:receiver__login.as( :receiver_login ),
:texts__rating.as( :text_rating )
]}.order( :texts__date.desc )
return post
end
def get_posts( opts = {} )
post = Text.filter( opts ).
left_outer_join( :nodes, :nodes__id => :texts__node ).
left_outer_join( :sources, :sources__id => :nodes__source ).
left_outer_join( :users.as(:sender), :sender__id => :texts__sender ).
left_outer_join( :users.as(:receiver), :receiver__id => :texts__receiver ).
select{ [
:nodes__id.as( :node_id ),
:nodes__title.as( :node_title ),
:sources__title.as( :source_title ),
:sources__url.as( :source_url ),
:sources__notes.as( :source_notes ),
:texts__date.as( :text_date ),
:texts__id.as( :text_id ),
:texts__title.as( :text_title ),
:texts__content.as( :text_content ),
:texts__sender.as( :sender_id ),
:texts__receiver.as( :receiver_id ),
:sender__login.as( :sender_login ),
:receiver__login.as( :receiver_login ),
:texts__rating.as( :text_rating )
]}.order( :texts__date.desc )
return post
end
def get_users( opts = {} )
user = User.filter( opts ).
left_outer_join( :sources, :sources__id => :users__source ).
select{ [
:users__id.as( :user_id ),
:users__login.as( :user_login ),
:users__email.as( :user_email ),
:users__hash.as( :user_hash ),
:users__pass.as( :user_pass ),
:users__id.as( :user_id ),
:sources__title.as( :source_title ),
:sources__url.as( :source_url ),
:sources__notes.as( :source_notes )
] }
return user
end
def get_user( id )
cache_key = OpenSSL::Digest::MD5.hexdigest(id.to_s)
cached = CACHE_CLIENT.get("/get_user/#{cache_key}")
return cached unless cached.nil?
cached = User.filter( :id => id.to_i ).first
CACHE_CLIENT.set("/get_user/#{cache_key}", cached)
return cached
end
def get_topics( opts = {} )
Node.filter( opts )
end
def get_texts( opts = {} )
Text.filter( opts )
end
def html_strip( content )
return content.gsub("<b>", "[b]").gsub("</b>", "[/b]").gsub( /\<img.*?\/\>/imx, "" ).gsub( /\<\!\-\-.*?\-\-\>/imx, "" ).gsub( /\</imx, "<" ).gsub( /\>/imx, ">" )
end
def bbcode_strip( content )
return content.gsub( /\[\/?(?:b|i|u|url|quote|code|img|color|size)*?.*?\]/imx, "" ).gsub( /(.)\"\](.)/imx, "$1 $2" )
end
def bbcode( content )
c = html_strip( content )
c = c.gsub( /\[b.*?\]/imx, "<b>" ).gsub( /\[\/b.*?\]/imx, "</b>" )
c = c.gsub( /\[quote.*?"(.*?)".*?\]/imx, "<blockquote>::\\1::" )
c = c.gsub( /\[quote.*?\]/imx, "<blockquote>" ).gsub( /\[\/quote.*?\]/imx, "</blockquote>" )
c = c.gsub( /\<blockquote\>::(.*?)::(.*?)\<\/blockquote\>/imx, "<blockquote>\\2<footer><cite>\\1</cite></footer></blockquote>")
return ::Nokogiri::HTML::DocumentFragment.parse( bbcode_strip( c ) ).to_html
end
end
end