FIX: "Go Back" wasn't displaying properly most of the time

This commit is contained in:
Robin Ward 2016-05-24 16:21:23 -04:00
parent 3cebba5b1f
commit 3c30fa628b
No known key found for this signature in database
GPG key ID: 0E091E2B4ED1B83D
5 changed files with 33 additions and 16 deletions

View file

@ -849,14 +849,25 @@ export default Ember.Controller.extend(SelectedPostsCount, BufferedContent, {
if (topic.get('id') === topicId) { if (topic.get('id') === topicId) {
let highestReadPostId = 0;
// TODO identity map for postNumber // TODO identity map for postNumber
postStream.get('posts').forEach(post => { postStream.get('posts').forEach(post => {
if (!post.read && postNumbers.indexOf(post.post_number) !== -1) { if (!post.read && postNumbers.indexOf(post.post_number) !== -1) {
const id = post.get('id');
if (id > highestReadPostId) {
highestReadPostId = id;
}
post.set('read', true); post.set('read', true);
this.appEvents.trigger('post-stream:refresh', { id: post.id }); this.appEvents.trigger('post-stream:refresh', { id });
} }
}); });
if (highestReadPostId > 0) {
topic.set('last_read_post_id', highestReadPostId);
}
const max = _.max(postNumbers); const max = _.max(postNumbers);
if (max > topic.get("last_read_post_number")) { if (max > topic.get("last_read_post_number")) {
topic.set("last_read_post_number", max); topic.set("last_read_post_number", max);

View file

@ -320,11 +320,6 @@ const TopicTrackingState = Discourse.Model.extend({
.length; .length;
}, },
lastReadPostNumber(topicId) {
const state = this.states[`t${topicId}`];
return state ? state.last_read_post_number : null;
},
countCategory(category_id) { countCategory(category_id) {
let sum = 0; let sum = 0;
_.each(this.states, function(topic){ _.each(this.states, function(topic){

View file

@ -20,7 +20,7 @@ createWidget('timeline-last-read', {
this.attach('button', { this.attach('button', {
className: 'btn btn-primary btn-small', className: 'btn btn-primary btn-small',
icon: 'arrow-left', icon: 'arrow-left',
label: 'go_back', label: 'topic.timeline.back',
action: 'goBack' action: 'goBack'
}) })
]; ];
@ -92,7 +92,8 @@ createWidget('timeline-scrollarea', {
position() { position() {
const { attrs } = this; const { attrs } = this;
const percentage = this.state.percentage; const percentage = this.state.percentage;
const postStream = attrs.topic.get('postStream'); const topic = attrs.topic;
const postStream = topic.get('postStream');
const total = postStream.get('filteredPostsCount'); const total = postStream.get('filteredPostsCount');
let current = Math.round(total * percentage); let current = Math.round(total * percentage);
@ -111,12 +112,13 @@ createWidget('timeline-scrollarea', {
lastReadPercentage: null lastReadPercentage: null
}; };
if (attrs.topicTrackingState) { const lastReadId = topic.last_read_post_id;
const lastRead = attrs.topicTrackingState.lastReadPostNumber(attrs.topic.id); const lastReadNumber = topic.last_read_post_number;
if (lastRead) {
result.lastRead = lastRead; if (lastReadId && lastReadNumber) {
result.lastReadPercentage = lastRead === 1 ? 0.0 : parseFloat(lastRead) / total; const idx = postStream.get('stream').indexOf(lastReadId) + 1;
} result.lastRead = lastReadNumber;
result.lastReadPercentage = this._percentFor(topic, idx);
} }
return result; return result;
@ -173,7 +175,6 @@ createWidget('timeline-scrollarea', {
_percentFor(topic, postNumber) { _percentFor(topic, postNumber) {
const total = topic.get('postStream.filteredPostsCount'); const total = topic.get('postStream.filteredPostsCount');
console.log(postNumber, total);
let result = (postNumber === 1) ? 0.0 : parseFloat(postNumber) / total; let result = (postNumber === 1) ? 0.0 : parseFloat(postNumber) / total;
if (result < 0) { return 0.0; } if (result < 0) { return 0.0; }

View file

@ -47,6 +47,7 @@ class TopicViewSerializer < ApplicationSerializer
:details, :details,
:highest_post_number, :highest_post_number,
:last_read_post_number, :last_read_post_number,
:last_read_post_id,
:deleted_by, :deleted_by,
:has_deleted, :has_deleted,
:actions_summary, :actions_summary,
@ -164,8 +165,16 @@ class TopicViewSerializer < ApplicationSerializer
object.highest_post_number object.highest_post_number
end end
def last_read_post_id
return nil unless object.filtered_post_stream && last_read_post_number
object.filtered_post_stream.each do |ps|
return ps[0] if ps[1] === last_read_post_number
end
end
alias_method :include_last_read_post_id?, :has_topic_user?
def last_read_post_number def last_read_post_number
object.topic_user.last_read_post_number @last_read_post_number ||= object.topic_user.last_read_post_number
end end
alias_method :include_last_read_post_number?, :has_topic_user? alias_method :include_last_read_post_number?, :has_topic_user?

View file

@ -1294,6 +1294,7 @@ en:
auto_close_immediate: "The last post in the topic is already %{hours} hours old, so the topic will be closed immediately." auto_close_immediate: "The last post in the topic is already %{hours} hours old, so the topic will be closed immediately."
timeline: timeline:
back: "Back"
replies: "%{current} / %{total} replies" replies: "%{current} / %{total} replies"
replies_short: "%{current} / %{total}" replies_short: "%{current} / %{total}"