Skip to content

Commit e2a295b

Browse files
authored
Merge pull request #1822 from codidact/0valt/assorted
Assorted improvements & fixes
2 parents 7b952d7 + 8710a2d commit e2a295b

23 files changed

+368
-182
lines changed

app/assets/javascripts/qpixel_api.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,10 @@ window.QPixel = {
131131
}
132132

133133
const myselfPromise = QPixel.fetch('/users/me', {
134-
headers: { 'Accept': 'application/json' }
134+
headers: {
135+
'Accept': 'application/json',
136+
'Cache-Control': 'no-cache',
137+
}
135138
});
136139

137140
QPixel._pendingUserResponse = myselfPromise;

app/assets/stylesheets/errors.scss

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@
2323
.error-report-summary {
2424
display: flex;
2525
align-items: center;
26+
gap: 0.15em;
2627

2728
.details {
2829
flex: 1;
2930
}
30-
}
31+
}

app/controllers/comments_controller.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,8 @@ def thread
194194
end
195195

196196
def thread_content
197+
fresh_when last_modified: @comment_thread.last_activity_at.utc, etag: @comment_thread
198+
197199
render partial: 'comment_threads/expanded', locals: { inline: params[:inline] == 'true',
198200
show_deleted: params[:show_deleted_comments] == '1',
199201
thread: @comment_thread }

app/controllers/users/sessions_controller.rb

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
class Users::SessionsController < Devise::SessionsController
2+
include Devise::Controllers::Rememberable
3+
24
protect_from_forgery except: [:create]
35

46
mattr_accessor :first_factor, default: [], instance_writer: false, instance_reader: false
57

68
# Any changes made here may also require changes to Users::SamlSessionsController#create.
79
def create
810
super do |user|
9-
return unless post_sign_in(user)
11+
remember_me = remember_me_is_active?(user)
12+
return unless post_sign_in(user, remember_me)
1013
end
1114
end
1215

@@ -29,10 +32,15 @@ def verify_code
2932
'a backup code. Please re-configure two-factor authentication via your profile.'
3033
end
3134

35+
if params[:remember_me] == 'true'
36+
remember_me(target_user)
37+
end
38+
3239
AuditLog.user_history(event_type: 'two_factor_success', related: target_user)
3340
@@first_factor.delete params[:uid].to_i
41+
3442
flash[:info] = 'Signed in successfully.'
35-
sign_in_and_redirect target_user
43+
sign_in_and_redirect(target_user)
3644
else
3745
AuditLog.user_history(event_type: 'two_factor_fail', related: target_user, comment: 'first factor not present')
3846
flash[:danger] = "You haven't entered your password yet."
@@ -56,9 +64,10 @@ def verify_code
5664
#
5765
# In general, this method should have similar behavior to the Users::SamlSessionsController#post_sign_in method.
5866
# If you make changes here, you may also have to update that method.
59-
# @param user [User]
67+
# @param user [User] currently signed in user
68+
# @param remember_me [Boolean] whether the user should be remembered after special conditions
6069
# @return [Boolean] false if the handling by the calling method should be stopped
61-
def post_sign_in(user)
70+
def post_sign_in(user, remember_me = false)
6271
# For a deleted user (banished), tell them non-specifically that there was a mistake with their credentials.
6372
if user.deleted?
6473
sign_out user
@@ -88,20 +97,20 @@ def post_sign_in(user)
8897

8998
# Enforce 2FA
9099
if user.enabled_2fa
91-
handle_2fa_login(user)
100+
handle_2fa_login(user, remember_me)
92101
return false
93102
end
94103

95104
true
96105
end
97106

98-
def handle_2fa_login(user)
107+
def handle_2fa_login(user, remember_me = false)
99108
sign_out user
100109
case user.two_factor_method
101110
when 'app'
102111
id = user.id
103112
@@first_factor << id
104-
redirect_to login_verify_2fa_path(uid: id)
113+
redirect_to login_verify_2fa_path(uid: id, remember_me: remember_me)
105114
when 'email'
106115
TwoFactorMailer.with(user: user, host: request.hostname).login_email.deliver_now
107116
flash[:notice] = nil

app/helpers/abilities_helper.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
module AbilitiesHelper
2-
##
2+
# Gets a maybe_ability to the specified ability
3+
# @param ability [Ability, String] ability or its internal id to link to
4+
# @return [ActiveSupport::SafeBuffer]
5+
def ability_link(maybe_ability)
6+
ability = maybe_ability.is_a?(String) ? Ability.find_by(internal_id: maybe_ability) : maybe_ability
7+
8+
link_to ability.name,
9+
ability_url(ability.internal_id, host: ability.community.host)
10+
end
11+
312
# Linearizes the Wilson-score progress used by ability calculations. For example, 0.98 and 0.99 are not far away on a
413
# linear scale, but mean a change of about 2x for the actual limit used by the algorithm. This method takes that into
514
# account and provides an indicator of progress on a linear scale, for use in progress bars.
@@ -10,7 +19,6 @@ def linearize_progress(score)
1019
[0, linear_score].max.to_f
1120
end
1221

13-
##
1422
# Provides an error message for when a user is unable to complete an ability-restricted action, either because the
1523
# user doesn't have the ability or because it has been suspended.
1624
# @param internal_id [String] The +internal_id+ attribute of the ability in question.

app/models/comment_thread.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ def can_access?(user)
4949
post.can_access?(user)
5050
end
5151

52+
# Gets last activity date and time on the thread
53+
# @return [DateTime] last activity date and time
54+
def last_activity_at
55+
[created_at, locked_at, updated_at].compact.max
56+
end
57+
5258
# Gets a list of user IDs who should be pingable in the thread.
5359
# @return [Array<Integer>]
5460
def pingable

app/models/post.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,24 @@ def reaction_list
246246
.to_h { |_k, v| [v.first.reaction_type, v] }
247247
end
248248

249+
# Gets a list of related posts scoped for a given user
250+
# @param user [User, nil] user to check access for
251+
# @return [ActiveRecord::Relation<Post>]
252+
def related_posts_for(user)
253+
if user&.can_see_deleted_posts?
254+
inbound_duplicates
255+
else
256+
inbound_duplicates.undeleted
257+
end
258+
end
259+
260+
# Checks if the post has related posts (scoped for a given user)
261+
# @param user [User, nil] user to check access for
262+
# @return [Boolean] check result
263+
def related_posts_for?(user)
264+
related_posts_for(user).any?
265+
end
266+
249267
# Are new threads on this post followed by a given user?
250268
# @param post [Post] post to check
251269
# @param user [User] user to check

app/views/admin/_error_report.html.erb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<strong><%= report.klass %></strong><br/>
55
<span class="has-font-size-caption has-color-tertiary-600"><%= t('g.at') %> <%= report.request_uri %></span>
66
</span>
7-
<span class="has-float-right has-color-tertiary-600">
7+
<span class="has-padding-right-2 has-float-right has-color-tertiary-600">
88
<%= report.created_at.iso8601 %>
99
</span>
1010
</summary>
@@ -26,5 +26,8 @@
2626
<span class="raw-markdown"><%= report.uuid %></span>
2727
</p>
2828

29-
<pre class="error-trace raw-markdown"><%= report.backtrace.split("\n").select { |l| l.include? Rails.root.to_s }.join("\n") %></pre>
30-
</details>
29+
<% backtrace_lines = report.backtrace.split("\n").select { |l| l.include? Rails.root.to_s } %>
30+
<% if backtrace_lines.any? %>
31+
<pre class="error-trace raw-markdown"><%= backtrace_lines.join("\n") %></pre>
32+
<% end %>
33+
</details>

app/views/layouts/_sidebar.html.erb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@
4949
<% end %>
5050

5151
<%# Related Posts widget %>
52-
<% if defined?(@post) && @post.inbound_duplicates.any? %>
52+
<% if defined?(@post) && @post.related_posts_for?(current_user) %>
5353
<% collapse_related = user_preference('collapse_related_posts') == 'true' %>
54-
<% cache [@post, @post.inbound_duplicates] do %>
54+
<% cache [@post, @post.related_posts_for(current_user)] do %>
5555
<div class="widget has-margin-4 is-green" data-collapsed="<%= user_preference('collapse_related_posts') %>">
5656
<div class="widget--header">
5757
Related Posts
@@ -60,7 +60,7 @@
6060
<i class="fas <%= collapse_related ? 'fa-chevron-down' : 'fa-chevron-up' %>"></i>
6161
</button>
6262
</div>
63-
<% @post.inbound_duplicates.each do |dp| %>
63+
<% @post.related_posts_for(current_user).each do |dp| %>
6464
<div class="widget--body <%= 'hidden' if collapse_related %>">
6565
<% unless dp.category.nil? %>
6666
<%= dp.category.name %>

0 commit comments

Comments
 (0)