Skip to content

Commit 89e3ea8

Browse files
authored
Merge branch 'develop' into 0valt/1783/collection-caching
2 parents 5eba779 + d9256f4 commit 89e3ea8

25 files changed

Lines changed: 245 additions & 74 deletions

app/assets/javascripts/preferences.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
$(() => {
1+
document.addEventListener('DOMContentLoaded', () => {
22
$('.js-user-pref').on('change', async (ev) => {
33
const $tgt = $(ev.target);
44
let value;
@@ -10,6 +10,7 @@ $(() => {
1010
}
1111
const prefName = $tgt.attr('data-pref');
1212
const community = $tgt.attr('data-community') === 'true';
13+
1314
await QPixel.setPreference(prefName, value, community);
1415
});
1516

@@ -25,4 +26,4 @@ $(() => {
2526
$(e).addClass('is-yellow');
2627
}
2728
});
28-
});
29+
});

app/assets/javascripts/qpixel_api.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -238,13 +238,9 @@ window.QPixel = {
238238

239239
const data = await resp.json();
240240

241-
if (data.status !== 'success') {
242-
console.error(`Preference persist failed (${name})`);
243-
console.error(resp);
244-
}
245-
else {
241+
QPixel.handleJSONResponse(data, (data) => {
246242
QPixel._updatePreferencesLocally(data.preferences);
247-
}
243+
});
248244
},
249245

250246
filters: async () => {
@@ -493,6 +489,16 @@ window.QPixel = {
493489

494490
const data = await resp.json();
495491

492+
return data;
493+
},
494+
495+
renameTag: async (categoryId, tagId, name) => {
496+
const resp = await QPixel.fetchJSON(`/categories/${categoryId}/tags/${tagId}/rename`, { name }, {
497+
headers: { 'Accept': 'application/json' }
498+
});
499+
500+
const data = await resp.json();
501+
496502
return data;
497503
}
498504
};

app/assets/javascripts/tags.js

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ $(() => {
176176
const tagId = $tgt.attr('data-tag-id');
177177
const tagName = $tgt.attr('data-tag-name');
178178
const $select = $tgt.parents('.form-group').find('select');
179-
const existing = $select.find(`option[value=${tagId}]`);
179+
const existing = $select.find(`option[value='${tagName}']`);
180180
if (existing.length > 0) {
181181
$select.val([useIds ? tagId : tagName, ...($select.val() || [])]).trigger('change');
182182
}
@@ -193,17 +193,15 @@ $(() => {
193193
const tagName = $tgt.attr('data-name');
194194

195195
const renameTo = prompt(`Rename tag ${tagName} to:`);
196-
if (!!renameTo) {
197-
const resp = await QPixel.fetchJSON(`/categories/${categoryId}/tags/${tagId}/rename`, { name: renameTo });
198-
199-
const data = await resp.json();
200196

201-
if (data.success) {
202-
location.reload();
203-
}
204-
else {
205-
QPixel.createNotification('danger', `Failed to rename the tag. (${resp.status})`);
206-
}
197+
if (!renameTo) {
198+
return;
207199
}
200+
201+
const data = await QPixel.renameTag(categoryId, tagId, renameTo);
202+
203+
QPixel.handleJSONResponse(data, () => {
204+
location.reload();
205+
});
208206
});
209207
});

app/assets/stylesheets/comments.scss

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,15 @@
2222
}
2323

2424
.comment .comment-meta {
25-
font-size: 0.9em;
26-
color: #666;
2725
background-color: rgba(0, 0, 0, 0.05);
28-
padding: 0.5rem 0.75rem;
29-
margin-bottom: 0.5rem;
26+
color: #666;
3027
display: flex;
28+
flex-wrap: wrap;
29+
font-size: 0.9em;
30+
gap: 0.5em;
31+
justify-content: space-between;
32+
margin-bottom: 0.5rem;
33+
padding: 0.5rem 0.75rem;
3134

3235
.comment--reference {
3336
position: relative;
@@ -42,12 +45,14 @@
4245
}
4346

4447
.comment--links {
45-
flex-grow: 1;
4648
display: flex;
47-
justify-content: right;
4849

4950
a {
5051
margin: 0 0.25rem;
52+
53+
&:first-child {
54+
margin-left: 0;
55+
}
5156
}
5257
}
5358
}

app/controllers/application_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ def enforce_signed_in
318318
path.start_with?('/assets/') ||
319319
path.end_with?('.css') || path.end_with?('.js')
320320

321-
# Make available to controller that the we should not leak posts in the sidebar
321+
# Used by derived controllers to avoid leaking featured posts to the sidebar
322322
@prevent_sidebar = true
323323

324324
# Allow /help (help center), /help/* and /policy/* depending on settings

app/controllers/comments_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ def post
276276

277277
def post_follow
278278
@post = Post.find(params[:post_id])
279-
if CommentThread.post_followed?(@post, current_user)
279+
if @post.followed_by?(current_user)
280280
ThreadFollower.where(post: @post, user: current_user).destroy_all
281281
else
282282
ThreadFollower.create(post: @post, user: current_user)

app/controllers/posts_controller.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,9 @@ def unless_locked
725725
check_if_locked(@post)
726726
end
727727

728+
# Attempts to actually delete a post draft
729+
# @param path [String] draft path to delete
730+
# @return [Boolean] status of the operation
728731
def do_draft_delete(path)
729732
keys = [:body, :comment, :excerpt, :license, :saved_at, :tags, :tag_name, :title].map do |key|
730733
pfx = key == :saved_at ? 'saved_post_at' : 'saved_post'

app/controllers/tags_controller.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,14 @@ def rename
141141
end
142142
end
143143

144-
render json: { success: status, tag: @tag }, status: status ? :ok : :bad_request
144+
if status
145+
render json: { status: 'success', tag: @tag }
146+
else
147+
render json: { status: 'failed',
148+
message: I18n.t('tags.errors.rename_generic'),
149+
tag: @tag },
150+
status: :bad_request
151+
end
145152
end
146153

147154
def select_merge; end

app/controllers/users_controller.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,12 +193,14 @@ def set_preference
193193
community_key = "prefs.#{current_user.id}.community.#{RequestContext.community_id}"
194194
key = params[:community].present? && params[:community] ? community_key : global_key
195195
current_user.validate_prefs!
196-
render json: { status: 'success', success: true,
196+
render json: { status: 'success',
197197
count: RequestContext.redis.hset(key, params[:name], params[:value].to_s),
198198
preferences: current_user.preferences }
199199
else
200-
render json: { status: 'failed', success: false, errors: ['Both name and value parameters are required'] },
201-
status: 400
200+
render json: { status: 'failed',
201+
message: 'Failed to save the preference',
202+
errors: ['Both name and value parameters are required'] },
203+
status: :bad_request
202204
end
203205
end
204206

app/models/category.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ def top_level_post_types
4444
post_types.where(is_top_level: true)
4545
end
4646

47+
# Are there new posts for a given user since their last visit?
48+
# @param user [User] user to check
49+
# @return [Boolean] check result
4750
def new_posts_for?(user)
4851
key = "#{community_id}/#{user.id}/#{id}/last_visit"
4952
Rails.cache.fetch key, expires_in: 5.minutes do
@@ -72,6 +75,9 @@ def self.accessible_to(user)
7275
Category.where('IFNULL(min_view_trust_level, -1) <= ?', trust_level)
7376
end
7477

78+
# Gets category matching a given name
79+
# @param name [String] name of the category
80+
# @return [Category, nil]
7581
def self.by_lowercase_name(name)
7682
categories = Rails.cache.fetch 'categories/by_lowercase_name' do
7783
Category.all.to_h { |c| [c.name.downcase, c.id] }
@@ -87,6 +93,9 @@ def self.by_id(id)
8793
categories[id]
8894
end
8995

96+
# Gets a collection of categories matching a given search term
97+
# @param term [String] search term
98+
# @return [ActiveRecord::Relation<Category>]
9099
def self.search(term)
91100
where('name LIKE ?', "%#{sanitize_sql_like(term)}%")
92101
end

0 commit comments

Comments
 (0)