Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add homepage navigation and default homepage handling #105

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 17 additions & 19 deletions assets/javascripts/discourse/initializers/discourse-voting.js.es6
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import I18n from "I18n";
import { withPluginApi } from "discourse/lib/plugin-api";
import { setDefaultHomepage } from "discourse/lib/utilities";
import NavItem from "discourse/models/nav-item";

export default {
name: "discourse-voting",

after: "url-redirects",
initialize() {
withPluginApi("0.8.32", (api) => {
const siteSettings = api.container.lookup("site-settings:main");
Expand All @@ -18,30 +19,27 @@ export default {
term: "order:votes",
});

const topMenuItems = siteSettings.top_menu.split('|');
const votesBeforeNavItem = siteSettings.voting_show_votes_before;
if (topMenuItems.indexOf(votesBeforeNavItem) === 0) {
setDefaultHomepage('votes');
}

const showVotesOnHome = siteSettings.voting_show_votes_on_homepage;
api.addNavigationBarItem({
name: "votes",
before: "top",
customFilter: (category) => {
return category && category.can_vote;
},
customHref: (category, args) => {
const path = NavItem.pathFor("latest", args);
return `${path}?order=votes`;
},
forceActive: (category, args, router) => {
const queryParams = router.currentRoute.queryParams;
return (
queryParams &&
Object.keys(queryParams).length === 1 &&
queryParams["order"] === "votes"
);
},
before: votesBeforeNavItem,
customFilter: (category) => ((!category && showVotesOnHome) || (category && category.can_vote)),
customHref: (category, args) => (category ? `${category.url}/l/votes` : '/votes')
});

const myVotesBeforeNavItem = siteSettings.voting_show_my_votes_before;
const showMyVotesOnHome = siteSettings.voting_show_my_votes_on_homepage;
api.addNavigationBarItem({
name: "my_votes",
before: "top",
before: myVotesBeforeNavItem,
customFilter: (category) => {
return category && category.can_vote && api.getCurrentUser();
return api.getCurrentUser() && (category ? category.can_vote : showMyVotesOnHome);
},
customHref: (category, args) => {
const path = NavItem.pathFor("latest", args);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import I18n from "I18n";
import { withPluginApi } from "discourse/lib/plugin-api";
import discourseComputed from "discourse-common/utils/decorators";

function initialize(api) {
api.addPostClassesCallback((post) => {
Expand Down Expand Up @@ -44,6 +45,19 @@ export default {

initialize() {
withPluginApi("0.8.4", (api) => initialize(api));
withPluginApi("0.8.30", (api) => api.addCategorySortCriteria("votes"));
withPluginApi("0.8.30", (api) => {
api.addCategorySortCriteria("votes");
api.modifyClass("component:edit-category-settings", {
pluginId: "discourse-voting",

@discourseComputed
availableViews() {
return [...this._super(...arguments), {
name: I18n.t("filters.votes.title"),
value: "votes"
}];
}
})
});
},
};
4 changes: 4 additions & 0 deletions config/locales/server.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ en:
voting_tl4_vote_limit: 'How many active votes are TL4 users allowed?'
voting_show_who_voted: 'Allow users to see who voted?'
voting_show_votes_on_profile: 'Allow users to see their votes in their activity feed?'
voting_show_votes_on_homepage: 'Show vote list navigation item on homepage?'
voting_show_my_votes_on_homepage: 'Show my vote list navgiation item on homepage?'
voting_show_votes_before: 'Show vote list navgiation item before this navigation item'
voting_show_my_votes_before: 'Show my vote list navgiation item before this navigation item'
voting_alert_votes_left: 'Alert user when this many votes are left'
voting:
votes_moved:
Expand Down
34 changes: 34 additions & 0 deletions config/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,40 @@ plugins:
voting_show_votes_on_profile:
default: true
client: true
voting_show_votes_on_homepage:
default: false
client: true
voting_show_my_votes_on_homepage:
default: false
client: true
voting_show_votes_before:
default: top
client: true
type: enum
choices:
- latest
- new
- unread
- unseen
- top
- categories
- read
- posted
- bookmarks
voting_show_my_votes_before:
default: top
client: true
type: enum
choices:
- latest
- new
- unread
- unseen
- top
- categories
- read
- posted
- bookmarks
voting_tl0_vote_limit:
default: 2
voting_tl1_vote_limit:
Expand Down
2 changes: 1 addition & 1 deletion plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def user_voted
sort_dir = (options[:ascending] == "true") ? "ASC" : "DESC"
result = result
.joins("LEFT JOIN discourse_voting_topic_vote_count ON discourse_voting_topic_vote_count.topic_id = topics.id")
.reorder("COALESCE(discourse_voting_topic_vote_count.votes_count,'0')::integer #{sort_dir}")
.reorder("COALESCE(discourse_voting_topic_vote_count.votes_count,'0')::integer #{sort_dir}, topics.bumped_at DESC")
end

result
Expand Down