changed post links to "topic/<topic_id>/<topic_slug>/<post_id>"
v1.18.x
barisusakli 11 years ago
parent 46028e49f7
commit 55dbc5c823

@ -40,6 +40,7 @@
"content-too-short": "Please enter a longer post. At least %1 characters.", "content-too-short": "Please enter a longer post. At least %1 characters.",
"title-too-short": "Please enter a longer title. At least %1 characters.", "title-too-short": "Please enter a longer title. At least %1 characters.",
"title-too-long": "Please enter a shorter title. Titles can't be longer than %1 characters.", "title-too-long": "Please enter a shorter title. Titles can't be longer than %1 characters.",
"invalid-title": "Invalid title!",
"too-many-posts": "You can only post every %1 seconds.", "too-many-posts": "You can only post every %1 seconds.",
"file-too-big": "Maximum allowed file size is %1 kbs", "file-too-big": "Maximum allowed file size is %1 kbs",

@ -47,8 +47,9 @@ define('forum/topic', ['forum/pagination', 'forum/infinitescroll', 'forum/topic/
addBlockquoteEllipses($('.topic .post-content > blockquote')); addBlockquoteEllipses($('.topic .post-content > blockquote'));
var bookmark = localStorage.getItem('topic:' + tid + ':bookmark'); var bookmark = localStorage.getItem('topic:' + tid + ':bookmark');
if (window.location.hash) { var postIndex = getPostIndex();
Topic.scrollToPost(window.location.hash.substr(1), true); if (postIndex) {
Topic.scrollToPost(postIndex, true);
} else if (bookmark && (!config.usePagination || (config.usePagination && pagination.currentPage === 1)) && postCount > 1) { } else if (bookmark && (!config.usePagination || (config.usePagination && pagination.currentPage === 1)) && postCount > 1) {
app.alert({ app.alert({
alert_id: 'bookmark', alert_id: 'bookmark',
@ -64,9 +65,7 @@ define('forum/topic', ['forum/pagination', 'forum/infinitescroll', 'forum/topic/
}); });
} }
if (!config.usePagination) {
navigator.init('.posts > .post-row', postCount, Topic.navigatorCallback); navigator.init('.posts > .post-row', postCount, Topic.navigatorCallback);
}
socket.on('event:new_post', onNewPost); socket.on('event:new_post', onNewPost);
@ -78,6 +77,11 @@ define('forum/topic', ['forum/pagination', 'forum/infinitescroll', 'forum/topic/
socket.emit('topics.increaseViewCount', tid); socket.emit('topics.increaseViewCount', tid);
}; };
function getPostIndex() {
var parts = window.location.pathname.split('/');
return parts[4] ? (parseInt(parts[4], 10) - 1) : '';
}
function showBottomPostBar() { function showBottomPostBar() {
if($('#post-container .post-row').length > 1 || !$('#post-container li[data-index="0"]').length) { if($('#post-container .post-row').length > 1 || !$('#post-container li[data-index="0"]').length) {
$('.bottom-post-bar').removeClass('hide'); $('.bottom-post-bar').removeClass('hide');
@ -142,32 +146,38 @@ define('forum/topic', ['forum/pagination', 'forum/infinitescroll', 'forum/topic/
} }
Topic.navigatorCallback = function(element) { Topic.navigatorCallback = function(element) {
var pid = element.attr('data-pid'); var postIndex = parseInt(element.attr('data-index'), 10);
var currentBookmark = localStorage.getItem('topic:' + ajaxify.variables.get('topic_id') + ':bookmark'); var currentBookmark = localStorage.getItem('topic:' + ajaxify.variables.get('topic_id') + ':bookmark');
if (!currentBookmark || parseInt(pid, 10) >= parseInt(currentBookmark, 10)) { if (!currentBookmark || parseInt(postIndex, 10) >= parseInt(currentBookmark, 10)) {
localStorage.setItem('topic:' + ajaxify.variables.get('topic_id') + ':bookmark', pid); localStorage.setItem('topic:' + ajaxify.variables.get('topic_id') + ':bookmark', postIndex);
app.removeAlert('bookmark'); app.removeAlert('bookmark');
} }
if (!scrollingToPost) { if (!scrollingToPost) {
var parts = window.location.pathname.split('/');
var newUrl = window.location.href.replace(window.location.hash, '') + '#' + pid; var topicId = parts[2],
slug = parts[3];
var newUrl = 'topic/' + topicId + '/' + (slug ? slug : '');
if (postIndex > 0) {
newUrl += '/' + (postIndex + 1);
}
if (newUrl !== currentUrl) { if (newUrl !== currentUrl) {
if (history.replaceState) { if (history.replaceState) {
var search = (window.location.search ? window.location.search : '');
history.replaceState({ history.replaceState({
url: window.location.pathname.slice(1) + (window.location.search ? window.location.search : '' ) + '#' + pid url: newUrl + search
}, null, newUrl); }, null, window.location.protocol + '//' + window.location.host + '/' + newUrl + search);
} }
currentUrl = newUrl; currentUrl = newUrl;
} }
} }
}; };
Topic.scrollToPost = function(pid, highlight, duration, offset) { Topic.scrollToPost = function(postIndex, highlight, duration, offset) {
if (!pid) { if (!postIndex) {
return; return;
} }
@ -177,43 +187,39 @@ define('forum/topic', ['forum/pagination', 'forum/infinitescroll', 'forum/topic/
scrollingToPost = true; scrollingToPost = true;
if($('#post_anchor_' + pid).length) { if($('#post_anchor_' + postIndex).length) {
return scrollToPid(pid); return scrollToPid(postIndex);
} }
if(config.usePagination) { if(config.usePagination) {
socket.emit('posts.getPidPage', pid, function(err, page) { if (window.location.search.indexOf('page') !== -1) {
if(err) { navigator.update();
scrollingToPost = false;
return; return;
} }
var page = Math.ceil((postIndex + 1) / config.postsPerPage)
if(parseInt(page, 10) !== pagination.currentPage) { if(parseInt(page, 10) !== pagination.currentPage) {
pagination.loadPage(page, function() { pagination.loadPage(page, function() {
scrollToPid(pid); scrollToPid(postIndex);
}); });
} else { } else {
scrollToPid(pid); scrollToPid(postIndex);
} }
});
} else { } else {
socket.emit('posts.getPidIndex', pid, function(err, index) {
if(err) {
return;
}
$('#post-container').empty(); $('#post-container').empty();
var after = index - config.postsPerPage + 1; var after = postIndex - config.postsPerPage + 1;
if(after < 0) { if(after < 0) {
after = 0; after = 0;
} }
loadPostsAfter(after, function() { loadPostsAfter(after, function() {
scrollToPid(pid); scrollToPid(postIndex);
});
}); });
} }
function scrollToPid(pid) { function scrollToPid(postIndex) {
var scrollTo = $('#post_anchor_' + pid), var scrollTo = $('#post_anchor_' + postIndex),
tid = $('#post-container').attr('data-tid'); tid = $('#post-container').attr('data-tid');
function animateScroll() { function animateScroll() {
@ -235,9 +241,8 @@ define('forum/topic', ['forum/pagination', 'forum/infinitescroll', 'forum/topic/
} }
} }
if (tid && scrollTo.length) { if (tid && scrollTo.length) {
if($('#post-container li.post-row[data-pid="' + pid + '"]').attr('data-index') !== '0') { if($('#post-container li.post-row[data-index="' + postIndex + '"]').attr('data-index') !== '0') {
animateScroll(); animateScroll();
} else { } else {
navigator.update(); navigator.update();
@ -362,14 +367,14 @@ define('forum/topic', ['forum/pagination', 'forum/infinitescroll', 'forum/topic/
} }
function loadMorePosts(direction) { function loadMorePosts(direction) {
if (!$('#post-container').length) { if (!$('#post-container').length || scrollingToPost) {
return; return;
} }
infinitescroll.calculateAfter(direction, '#post-container .post-row', config.postsPerPage, function(after, offset, el) { infinitescroll.calculateAfter(direction, '#post-container .post-row', config.postsPerPage, function(after, offset, el) {
loadPostsAfter(after, function() { loadPostsAfter(after, function() {
if (direction < 0 && el) { if (direction < 0 && el) {
Topic.scrollToPost(el.attr('data-pid'), false, 0, offset); Topic.scrollToPost(el.attr('data-index'), false, 0, offset);
} }
}); });
}); });

@ -453,6 +453,8 @@ define('composer', ['taskbar', 'composer/controls', 'composer/uploads', 'compose
return composerAlert('[[error:title-too-short, ' + config.minimumTitleLength + ']]'); return composerAlert('[[error:title-too-short, ' + config.minimumTitleLength + ']]');
} else if (checkTitle && titleEl.val().length > parseInt(config.maximumTitleLength, 10)) { } else if (checkTitle && titleEl.val().length > parseInt(config.maximumTitleLength, 10)) {
return composerAlert('[[error:title-too-long, ' + config.maximumTitleLength + ']]'); return composerAlert('[[error:title-too-long, ' + config.maximumTitleLength + ']]');
} else if (checkTitle && !utils.slugify(titleEl.val()).length) {
return composerAlert('[[error:invalid-title]]');
} else if (bodyEl.val().length < parseInt(config.minimumPostLength, 10)) { } else if (bodyEl.val().length < parseInt(config.minimumPostLength, 10)) {
return composerAlert('[[error:content-too-short, ' + config.minimumPostLength + ']]'); return composerAlert('[[error:content-too-short, ' + config.minimumPostLength + ']]');
} }

@ -10,8 +10,8 @@ define('share', function() {
var baseUrl = window.location.protocol + '//' + window.location.host; var baseUrl = window.location.protocol + '//' + window.location.host;
function openShare(url, hash, width, height) { function openShare(url, urlToPost, width, height) {
window.open(url + encodeURIComponent(baseUrl + window.location.pathname + hash), '_blank', 'width=' + width + ',height=' + height + ',scrollbars=no,status=no'); window.open(url + encodeURIComponent(baseUrl + urlToPost), '_blank', 'width=' + width + ',height=' + height + ',scrollbars=no,status=no');
return false; return false;
} }
@ -32,15 +32,15 @@ define('share', function() {
}); });
addHandler('.twitter-share', function () { addHandler('.twitter-share', function () {
return openShare('https://twitter.com/intent/tweet?text=' + name + '&url=', getPostHash($(this)), 550, 420); return openShare('https://twitter.com/intent/tweet?text=' + name + '&url=', getPostUrl($(this)), 550, 420);
}); });
addHandler('.facebook-share', function () { addHandler('.facebook-share', function () {
return openShare('https://www.facebook.com/sharer/sharer.php?u=', getPostHash($(this)), 626, 436); return openShare('https://www.facebook.com/sharer/sharer.php?u=', getPostUrl($(this)), 626, 436);
}); });
addHandler('.google-share', function () { addHandler('.google-share', function () {
return openShare('https://plus.google.com/share?url=', getPostHash($(this)), 500, 570); return openShare('https://plus.google.com/share?url=', getPostUrl($(this)), 500, 570);
}); });
}; };
@ -49,11 +49,9 @@ define('share', function() {
} }
function getPostHash(clickedElement) { function getPostHash(clickedElement) {
var pid = clickedElement.parents('.post-row').attr('data-pid'); var parts = window.location.pathname.split('/');
if (pid) { var postIndex = parseInt(clickedElement.parents('.post-row').attr('data-index'), 10);
return '#' + pid; return '/topic/' + parts[2] + (parts[3] ? '/' + parts[3] : '') + (postIndex ? '/' + (postIndex + 1) : '');
}
return '';
} }
return module; return module;

@ -40,8 +40,16 @@ topicsController.get = function(req, res, next) {
return next(err); return next(err);
} }
var start = (page - 1) * settings.postsPerPage, var postIndex = 0;
end = start + settings.postsPerPage - 1; if (!settings.usePagination) {
postIndex = Math.max((req.params.post_index || 1) - (settings.postsPerPage), 0);
} else if (!req.query.page) {
var index = Math.max(parseInt((req.params.post_index || 0), 10), 0);
page = Math.ceil((index + 1) / settings.postsPerPage);
}
var start = (page - 1) * settings.postsPerPage + postIndex,
end = start + settings.postsPerPage - 1 + postIndex;
topics.getTopicWithPosts(tid, uid, start, end, function (err, topicData) { topics.getTopicWithPosts(tid, uid, start, end, function (err, topicData) {
if (topicData) { if (topicData) {

@ -274,6 +274,9 @@ var db = require('./database'),
} }
postTools.parse(post.content, next); postTools.parse(post.content, next);
},
index: function(next) {
Posts.getPidIndex(post.pid, next);
} }
}, function(err, results) { }, function(err, results) {
if (err) { if (err) {
@ -283,6 +286,7 @@ var db = require('./database'),
post.user = results.user; post.user = results.user;
post.topic = results.topicCategory.topic; post.topic = results.topicCategory.topic;
post.category = results.topicCategory.category; post.category = results.topicCategory.category;
post.index = parseInt(results.index, 10) + 1;
if (stripTags) { if (stripTags) {
var s = S(results.content); var s = S(results.content);

@ -51,6 +51,9 @@ function staticRoutes(app, middleware, controllers) {
function topicRoutes(app, middleware, controllers) { function topicRoutes(app, middleware, controllers) {
app.get('/api/topic/teaser/:topic_id', controllers.topics.teaser); app.get('/api/topic/teaser/:topic_id', controllers.topics.teaser);
app.get('/topic/:topic_id/:slug/:post_index?', middleware.buildHeader, middleware.addSlug, controllers.topics.get);
app.get('/api/topic/:topic_id/:slug/:post_index?', controllers.topics.get);
app.get('/topic/:topic_id/:slug?', middleware.buildHeader, middleware.addSlug, controllers.topics.get); app.get('/topic/:topic_id/:slug?', middleware.buildHeader, middleware.addSlug, controllers.topics.get);
app.get('/api/topic/:topic_id/:slug?', controllers.topics.get); app.get('/api/topic/:topic_id/:slug?', controllers.topics.get);
} }

@ -312,29 +312,40 @@ var async = require('async'),
Topics.getTeaser = function(tid, callback) { Topics.getTeaser = function(tid, callback) {
Topics.getLatestUndeletedPid(tid, function(err, pid) { Topics.getLatestUndeletedPid(tid, function(err, pid) {
if (err) { if (err || !pid) {
return callback(err); return callback(err);
} }
if (!pid) { async.parallel({
return callback(null, null); postData: function(next) {
}
posts.getPostFields(pid, ['pid', 'uid', 'timestamp'], function(err, postData) { posts.getPostFields(pid, ['pid', 'uid', 'timestamp'], function(err, postData) {
if (err) { if (err) {
return callback(err); return next(err);
} else if(!postData || !utils.isNumber(postData.uid)) { } else if(!postData || !utils.isNumber(postData.uid)) {
return callback(new Error('[[error:no-teaser]]')); return next(new Error('[[error:no-teaser]]'));
} }
user.getUserFields(postData.uid, ['username', 'userslug', 'picture'], function(err, userData) { user.getUserFields(postData.uid, ['username', 'userslug', 'picture'], function(err, userData) {
if (err) { if (err) {
return callback(err); return next(err);
} }
postData.timestamp = utils.toISOString(postData.timestamp);
postData.user = userData; postData.user = userData;
callback(null, postData); next(null, postData);
});
}); });
},
postIndex: function(next) {
posts.getPidIndex(pid, next);
}
}, function(err, results) {
if (err) {
return callback(err);
}
results.postData.timestamp = utils.toISOString(results.postData.timestamp);
results.postData.index = parseInt(results.postIndex, 10) + 1;
callback(null, results.postData);
}); });
}); });
}; };

@ -24,9 +24,15 @@ module.exports = function(Topics) {
return callback(err); return callback(err);
} }
var slug = tid + '/' + utils.slugify(title), var slug = utils.slugify(title),
timestamp = Date.now(); timestamp = Date.now();
if (!slug.length) {
return callback(new Error('[[error:invalid-title]]'));
}
slug = tid + '/' + slug;
var topicData = { var topicData = {
'tid': tid, 'tid': tid,
'uid': uid, 'uid': uid,

@ -29,7 +29,7 @@ module.exports = function(Topics) {
if (Array.isArray(postData) && !postData.length) { if (Array.isArray(postData) && !postData.length) {
return callback(null, []); return callback(null, []);
} }
start = parseInt(start, 10);
for(var i=0; i<postData.length; ++i) { for(var i=0; i<postData.length; ++i) {
postData[i].index = start + i; postData[i].index = start + i;
} }

Loading…
Cancel
Save