Skip to content

Commit f8d8272

Browse files
committed
Cleaned up TopicUserSpec, introduces clearing of pinned topics
1 parent 3af2ab9 commit f8d8272

27 files changed

Lines changed: 3069 additions & 748 deletions

Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
source 'https://rubygems.org'
1+
source 'http://rubygems.org'
22

33
gem 'active_model_serializers', git: 'git://github.com/rails-api/active_model_serializers.git'
44
gem 'ember-rails', git: 'git://github.com/emberjs/ember-rails.git' # so we get the pre version

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ PATH
7171
rails (~> 3.1)
7272

7373
GEM
74-
remote: https://rubygems.org/
74+
remote: http://rubygems.org/
7575
specs:
7676
actionmailer (3.2.12)
7777
actionpack (= 3.2.12)

app/assets/javascripts/discourse/controllers/topic_controller.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,15 @@ Discourse.TopicController = Discourse.ObjectController.extend({
255255
this.get('content').toggleStar();
256256
},
257257

258+
/**
259+
Clears the pin from a topic for the currentUser
260+
261+
@method clearPin
262+
**/
263+
clearPin: function() {
264+
this.get('content').clearPin();
265+
},
266+
258267
// Receive notifications for this topic
259268
subscribe: function() {
260269
var bus,

app/assets/javascripts/discourse/models/topic.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,27 @@ Discourse.Topic = Discourse.Model.extend({
329329
});
330330
},
331331

332+
/**
333+
Clears the pin from a topic for the currentUser
334+
335+
@method clearPin
336+
**/
337+
clearPin: function() {
338+
339+
var topic = this;
340+
341+
// Clear the pin optimistically from the object
342+
topic.set('pinned', false);
343+
344+
$.ajax("/t/" + this.get('id') + "/clear-pin", {
345+
type: 'PUT',
346+
error: function() {
347+
// On error, put the pin back
348+
topic.set('pinned', true);
349+
}
350+
});
351+
},
352+
332353
// Is the reply to a post directly below it?
333354
isReplyDirectlyBelow: function(post) {
334355
var postBelow, posts;

app/assets/javascripts/discourse/views/topic_footer_buttons_view.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,30 @@ Discourse.TopicFooterButtonsView = Ember.ContainerView.extend({
6868
buffer.push("<i class='icon icon-share'></i>");
6969
}
7070
}));
71+
72+
// Add our clear pin button
73+
this.addObject(Discourse.ButtonView.createWithMixins({
74+
textKey: 'topic.clear_pin.title',
75+
helpKey: 'topic.clear_pin.help',
76+
classNameBindings: ['unpinned'],
77+
78+
// Hide the button if it becomes unpinned
79+
unpinned: function() {
80+
// When not logged in don't show the button
81+
if (!Discourse.get('currentUser')) return 'hidden'
82+
83+
return this.get('controller.pinned') ? null : 'hidden';
84+
}.property('controller.pinned'),
85+
86+
click: function(buffer) {
87+
this.get('controller').clearPin();
88+
},
89+
90+
renderIcon: function(buffer) {
91+
buffer.push("<i class='icon icon-pushpin'></i>");
92+
}
93+
}));
94+
7195
}
7296

7397
this.addObject(Discourse.ButtonView.createWithMixins({

app/controllers/topics_controller.rb

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ class TopicsController < ApplicationController
1414
:mute,
1515
:unmute,
1616
:set_notifications,
17-
:move_posts]
17+
:move_posts,
18+
:clear_pin]
19+
1820
before_filter :consider_user_for_promotion, only: :show
1921

2022
skip_before_filter :check_xhr, only: [:avatar, :show, :feed]
@@ -127,16 +129,21 @@ def move_posts
127129
end
128130
end
129131

130-
def timings
132+
def clear_pin
133+
topic = Topic.where(id: params[:topic_id].to_i).first
134+
guardian.ensure_can_see!(topic)
135+
topic.clear_pin_for(current_user)
136+
render nothing: true
137+
end
131138

139+
def timings
132140
PostTiming.process_timings(
133-
current_user,
134-
params[:topic_id].to_i,
135-
params[:highest_seen].to_i,
136-
params[:topic_time].to_i,
137-
(params[:timings] || []).map{|post_number, t| [post_number.to_i, t.to_i]}
141+
current_user,
142+
params[:topic_id].to_i,
143+
params[:highest_seen].to_i,
144+
params[:topic_time].to_i,
145+
(params[:timings] || []).map{|post_number, t| [post_number.to_i, t.to_i]}
138146
)
139-
140147
render nothing: true
141148
end
142149

app/models/post.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ module HiddenReason
5454
after_commit :store_unique_post_key, on: :create
5555

5656
after_create do
57-
TopicUser.auto_track(user_id, topic_id, TopicUser::NotificationReasons::CREATED_POST)
57+
TopicUser.auto_track(user_id, topic_id, TopicUser.notification_reasons[:created_post])
5858
end
5959

6060
scope :by_newest, order('created_at desc, id desc')

app/models/post_alert_observer.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def create_notification(user, type, post, opts={})
8080
return unless Guardian.new(user).can_see?(post)
8181

8282
# skip if muted on the topic
83-
return if TopicUser.get(post.topic, user).try(:notification_level) == TopicUser::NotificationLevel::MUTED
83+
return if TopicUser.get(post.topic, user).try(:notification_level) == TopicUser.notification_levels[:muted]
8484

8585
# Don't notify the same user about the same notification on the same post
8686
return if user.notifications.exists?(notification_type: type, topic_id: post.topic_id, post_number: post.post_number)
@@ -132,7 +132,7 @@ def notify_post_users(post)
132132
exclude_user_ids << extract_mentioned_users(post).map(&:id)
133133
exclude_user_ids << extract_quoted_users(post).map(&:id)
134134
exclude_user_ids.flatten!
135-
TopicUser.where(topic_id: post.topic_id, notification_level: TopicUser::NotificationLevel::WATCHING).includes(:user).each do |tu|
135+
TopicUser.where(topic_id: post.topic_id, notification_level: TopicUser.notification_levels[:watching]).includes(:user).each do |tu|
136136
create_notification(tu.user, Notification.types[:posted], post) unless exclude_user_ids.include?(tu.user_id)
137137
end
138138
end

app/models/topic.rb

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,9 @@ def initialize(user)
8383

8484
after_create do
8585
changed_to_category(category)
86-
TopicUser.change(
87-
user_id, id,
88-
notification_level: TopicUser::NotificationLevel::WATCHING,
89-
notifications_reason_id: TopicUser::NotificationReasons::CREATED_TOPIC
90-
)
86+
TopicUser.change(user_id, id,
87+
notification_level: TopicUser.notification_levels[:watching],
88+
notifications_reason_id: TopicUser.notification_reasons[:created_topic])
9189
if archetype == Archetype.private_message
9290
DraftSequence.next!(user, Draft::NEW_PRIVATE_MESSAGE)
9391
else
@@ -206,8 +204,17 @@ def links_grouped
206204
end
207205

208206
def update_status(property, status, user)
207+
209208
Topic.transaction do
210-
update_column(property, status)
209+
210+
# Special case: if it's pinned, update that
211+
if property.to_sym == :pinned
212+
update_pinned(status)
213+
else
214+
# otherwise update the column
215+
update_column(property, status)
216+
end
217+
211218
key = "topic_statuses.#{property}_"
212219
key << (status ? 'enabled' : 'disabled')
213220

@@ -506,7 +513,7 @@ def toggle_star(user, starred)
506513

507514
# Enable/disable the mute on the topic
508515
def toggle_mute(user, muted)
509-
TopicUser.change(user, self.id, notification_level: muted?(user) ? TopicUser::NotificationLevel::REGULAR : TopicUser::NotificationLevel::MUTED )
516+
TopicUser.change(user, self.id, notification_level: muted?(user) ? TopicUser.notification_levels[:regular] : TopicUser.notification_levels[:muted] )
510517
end
511518

512519
def slug
@@ -526,7 +533,17 @@ def relative_url(post_number=nil)
526533
def muted?(user)
527534
return false unless user && user.id
528535
tu = topic_users.where(user_id: user.id).first
529-
tu && tu.notification_level == TopicUser::NotificationLevel::MUTED
536+
tu && tu.notification_level == TopicUser.notification_levels[:muted]
537+
end
538+
539+
def clear_pin_for(user)
540+
return unless user.present?
541+
542+
TopicUser.change(user.id, id, cleared_pinned_at: Time.now)
543+
end
544+
545+
def update_pinned(status)
546+
update_column(:pinned_at, status ? Time.now : nil)
530547
end
531548

532549
def draft_key
@@ -535,18 +552,18 @@ def draft_key
535552

536553
# notification stuff
537554
def notify_watch!(user)
538-
TopicUser.change(user, id, notification_level: TopicUser::NotificationLevel::WATCHING)
555+
TopicUser.change(user, id, notification_level: TopicUser.notification_levels[:watching])
539556
end
540557

541558
def notify_tracking!(user)
542-
TopicUser.change(user, id, notification_level: TopicUser::NotificationLevel::TRACKING)
559+
TopicUser.change(user, id, notification_level: TopicUser.notification_levels[:tracking])
543560
end
544561

545562
def notify_regular!(user)
546-
TopicUser.change(user, id, notification_level: TopicUser::NotificationLevel::REGULAR)
563+
TopicUser.change(user, id, notification_level: TopicUser.notification_levels[:regular])
547564
end
548565

549566
def notify_muted!(user)
550-
TopicUser.change(user, id, notification_level: TopicUser::NotificationLevel::MUTED)
567+
TopicUser.change(user, id, notification_level: TopicUser.notification_levels[:muted])
551568
end
552569
end

0 commit comments

Comments
 (0)