Skip to content

Commit c04f45d

Browse files
committed
Move user struct to own file, under Invidious namespace
1 parent fb36155 commit c04f45d

File tree

6 files changed

+34
-33
lines changed

6 files changed

+34
-33
lines changed

src/invidious.cr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,7 @@ add_handler AuthHandler.new
548548
add_handler DenyFrame.new
549549
add_context_storage_type(Array(String))
550550
add_context_storage_type(Preferences)
551-
add_context_storage_type(User)
551+
add_context_storage_type(Invidious::User)
552552

553553
Kemal.config.logger = LOGGER
554554
Kemal.config.host_binding = Kemal.config.host_binding != "0.0.0.0" ? Kemal.config.host_binding : CONFIG.host_binding

src/invidious/search.cr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ end
176176

177177
def process_search_query(query, page, user, region)
178178
if user
179-
user = user.as(User)
179+
user = user.as(Invidious::User)
180180
view_name = "subscriptions_#{sha256(user.email)}"
181181
end
182182

src/invidious/user/user.cr

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
require "db"
2+
3+
struct Invidious::User
4+
include DB::Serializable
5+
6+
property updated : Time
7+
property notifications : Array(String)
8+
property subscriptions : Array(String)
9+
property email : String
10+
11+
@[DB::Field(converter: Invidious::User::PreferencesConverter)]
12+
property preferences : Preferences
13+
property password : String?
14+
property token : String
15+
property watched : Array(String)
16+
property feed_needs_update : Bool?
17+
18+
module PreferencesConverter
19+
def self.from_rs(rs)
20+
begin
21+
Preferences.from_json(rs.read(String))
22+
rescue ex
23+
Preferences.from_json("{}")
24+
end
25+
end
26+
end
27+
end

src/invidious/users.cr

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,6 @@ require "crypto/bcrypt/password"
33
# Materialized views may not be defined using bound parameters (`$1` as used elsewhere)
44
MATERIALIZED_VIEW_SQL = ->(email : String) { "SELECT cv.* FROM channel_videos cv WHERE EXISTS (SELECT subscriptions FROM users u WHERE cv.ucid = ANY (u.subscriptions) AND u.email = E'#{email.gsub({'\'' => "\\'", '\\' => "\\\\"})}') ORDER BY published DESC" }
55

6-
struct User
7-
include DB::Serializable
8-
9-
property updated : Time
10-
property notifications : Array(String)
11-
property subscriptions : Array(String)
12-
property email : String
13-
14-
@[DB::Field(converter: User::PreferencesConverter)]
15-
property preferences : Preferences
16-
property password : String?
17-
property token : String
18-
property watched : Array(String)
19-
property feed_needs_update : Bool?
20-
21-
module PreferencesConverter
22-
def self.from_rs(rs)
23-
begin
24-
Preferences.from_json(rs.read(String))
25-
rescue ex
26-
Preferences.from_json("{}")
27-
end
28-
end
29-
end
30-
end
31-
326
def get_user(sid, headers, refresh = true)
337
if email = Invidious::Database::SessionIDs.select_email(sid)
348
user = Invidious::Database::Users.select!(email: email)
@@ -84,7 +58,7 @@ def fetch_user(sid, headers)
8458

8559
token = Base64.urlsafe_encode(Random::Secure.random_bytes(32))
8660

87-
user = User.new({
61+
user = Invidious::User.new({
8862
updated: Time.utc,
8963
notifications: [] of String,
9064
subscriptions: channels,
@@ -102,7 +76,7 @@ def create_user(sid, email, password)
10276
password = Crypto::Bcrypt::Password.create(password, cost: 10)
10377
token = Base64.urlsafe_encode(Random::Secure.random_bytes(32))
10478

105-
user = User.new({
79+
user = Invidious::User.new({
10680
updated: Time.utc,
10781
notifications: [] of String,
10882
subscriptions: [] of String,

src/invidious/views/preferences.ecr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@
252252
<% end %>
253253
<% end %>
254254

255-
<% if env.get?("user") && CONFIG.admins.includes? env.get?("user").as(User).email %>
255+
<% if env.get?("user") && CONFIG.admins.includes? env.get?("user").as(Invidious::User).email %>
256256
<legend><%= translate(locale, "preferences_category_admin") %></legend>
257257

258258
<div class="pure-control-group">

src/invidious/views/template.ecr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
</div>
5353
<div class="pure-u-1-4">
5454
<a id="notification_ticker" title="<%= translate(locale, "Subscriptions") %>" href="/feed/subscriptions" class="pure-menu-heading">
55-
<% notification_count = env.get("user").as(User).notifications.size %>
55+
<% notification_count = env.get("user").as(Invidious::User).notifications.size %>
5656
<% if notification_count > 0 %>
5757
<span id="notification_count"><%= notification_count %></span> <i class="icon ion-ios-notifications"></i>
5858
<% else %>
@@ -67,7 +67,7 @@
6767
</div>
6868
<% if env.get("preferences").as(Preferences).show_nick %>
6969
<div class="pure-u-1-4">
70-
<span id="user_name"><%= env.get("user").as(User).email %></span>
70+
<span id="user_name"><%= env.get("user").as(Invidious::User).email %></span>
7171
</div>
7272
<% end %>
7373
<div class="pure-u-1-4">

0 commit comments

Comments
 (0)