This repository has been archived on 2023-03-18. You can view files and clone it, but cannot push or open issues or pull requests.
osr-discourse-src/app/assets/javascripts/discourse/routes/topic-from-params.js.es6
Sam 6ddd8d9166 FIX: when entering topics "tracking" would not be set
There was a timing issue when subscribing to messages for topics.

Old flow:

- We generate JSON for topic
- We subscribe to messages for topic

New flow:

- We keep track of last id in the topic message bus channel
- We generate JSON
- We subscribe to messages for topic starting at saved message id

This ensures that there is complete overlap for message consumption
and that there are no cases where an update may go missing due to timing
2017-05-16 15:04:21 -04:00

74 lines
2.3 KiB
JavaScript

import DiscourseURL from 'discourse/lib/url';
import Draft from 'discourse/models/draft';
// This route is used for retrieving a topic based on params
export default Discourse.Route.extend({
// Avoid default model hook
model(params) { return params; },
deactivate() {
this._super();
this.controllerFor('topic').unsubscribe();
},
setupController(controller, params) {
params = params || {};
params.track_visit = true;
const self = this,
topic = this.modelFor('topic'),
postStream = topic.get('postStream'),
topicController = this.controllerFor('topic'),
composerController = this.controllerFor('composer');
// I sincerely hope no topic gets this many posts
if (params.nearPost === "last") { params.nearPost = 999999999; }
params.forceLoad = true;
postStream.refresh(params).then(function () {
// TODO we are seeing errors where closest post is null and this is exploding
// we need better handling and logging for this condition.
// The post we requested might not exist. Let's find the closest post
const closestPost = postStream.closestPostForPostNumber(params.nearPost || 1);
const closest = closestPost.get('post_number');
topicController.setProperties({
'model.currentPost': closest,
enteredIndex: postStream.get('stream').indexOf(closestPost.get('id')),
enteredAt: new Date().getTime().toString(),
});
topicController.subscribe();
// Highlight our post after the next render
Ember.run.scheduleOnce('afterRender', function() {
self.appEvents.trigger('post:highlight', closest);
});
const opts = {};
if (document.location.hash && document.location.hash.length) {
opts.anchor = document.location.hash;
}
DiscourseURL.jumpToPost(closest, opts);
if (!Ember.isEmpty(topic.get('draft'))) {
composerController.open({
draft: Draft.getLocal(topic.get('draft_key'), topic.get('draft')),
draftKey: topic.get('draft_key'),
draftSequence: topic.get('draft_sequence'),
topic: topic,
ignoreIfChanged: true
});
}
}).catch(e => {
if (!Ember.testing) {
console.log('Could not view topic', e);
}
});
}
});