public and private mode, add customizable user link path

This commit is contained in:
Sam 2017-02-15 12:35:07 -05:00
parent 13d070c823
commit a6861ba65a
7 changed files with 57 additions and 21 deletions

View File

@ -1,3 +1,3 @@
{{#if context.topic.owner}}
<a class='assigned-to discourse-tag simple' href="{{context.topic.owner.path}}">@{{context.topic.owner.username}}</a>
{{#if context.topic.assigned_to_user}}
<a class='assigned-to discourse-tag simple' href="{{context.topic.assignedToUserPath}}">@{{context.topic.assigned_to_user.username}}</a>
{{/if}}

View File

@ -1,7 +1,8 @@
{{#unless context.topic.tags}}
{{#if context.topic.owner}}
{{#if context.topic.assigned_to_user}}
<div class='discourse-tags'>
<a class='assigned-to discourse-tag simple' href="{{context.topic.owner.path}}">@{{context.topic.owner.username}}</a>
<a class='assigned-to discourse-tag simple' href="{{context.topic.assignedToUserPath}}">@{{context.topic.assigned_to_user.username}}</a>
</div>
{{/if}}
{{/unless}}

View File

@ -1,6 +1,18 @@
import { withPluginApi } from 'discourse/lib/plugin-api';
function initialize(api) {
// should this be in API ?
import Topic from 'discourse/models/topic';
function initialize(api, container) {
const siteSettings = container.lookup('site-settings:main');
Topic.reopen({
assignedToUserPath: function(){
return siteSettings.assigns_user_url_path.replace("{username}", this.get("assigned_to_user.username"));
}.property('owner')
});
api.addPostSmallActionIcon('assigned','user-plus');
api.decorateWidget('post-contents:after-cooked', dec => {
@ -9,18 +21,22 @@ function initialize(api) {
if (postModel) {
const assignedToUser = postModel.get('topic.assigned_to_user');
if (assignedToUser) {
const html = I18n.t('discourse_assign.assign_html', assignedToUser);
//const topic = postModel.get('topic');
const path = postModel.get('topic.assignedToUserPath');
const userLink = `<a href='${path}'>${assignedToUser.username}</a>`;
const html = I18n.t('discourse_assign.assign_html', {userLink});
return dec.rawHtml(html);
}
}
}
});
};
export default {
name: 'extend-for-assign',
initialize() {
withPluginApi('0.8', initialize);
initialize(container) {
withPluginApi('0.8', api => {
initialize(api, container);
});
}
};

View File

@ -3,7 +3,7 @@ en:
action_codes:
assigned: "assigned"
discourse_assign:
assign_html: "<p class='assigned-to'>Assigned to: @{{username}}</p>"
assign_html: "<p class='assigned-to'><i class='fa-user-plus fa'></i> Assigned to {{userLink}}</p>"
assign_notification: "<i title='assigned' class='fa fa-user-plus'></i><p><span>{{username}}</span> {{description}}</p>"
assign:
title: "Assign"

View File

@ -1,3 +1,6 @@
en:
site_settings:
assigns_public: "Allow general public to see topic assignments"
assigns_user_url_path: "Path to link all user links for assigned (use: {username} to subtitue username)"
discourse_assign:
assigned_to: "Topic assigned to @%{username}"

5
config/settings.yml Normal file
View File

@ -0,0 +1,5 @@
plugins:
assigns_public: false
assigns_user_url_path:
client: true
default: "/latest?assigned={username}"

View File

@ -40,11 +40,13 @@ after_initialize do
target_post_id: topic.posts.find_by(post_number: 1).id,
target_topic_id: topic.id)
post_type = SiteSetting.assigns_public ? Post.types[:small_action] : Post.types[:whisper]
topic.add_moderator_post(current_user,
I18n.t('discourse_assign.assigned_to',
username: assign_to.username),
{ bump: false,
post_type: Post.types[:small_action],
post_type: post_type,
action_code: "assigned"})
unless false && current_user.id == assign_to.id
@ -67,22 +69,25 @@ after_initialize do
end
class ::Topic
def owner
@owner ||
def assigned_to_user
@assigned_to_user ||
if user_id = custom_fields["assigned_to_id"]
@owner = User.find_by(id: user_id)
@assigned_to_user = User.find_by(id: user_id)
end
end
def preload_owner(owner)
@owner = owner
def preload_assigned_to_user(assigned_to_user)
@assigned_to_user = assigned_to_user
end
end
TopicList.preloaded_custom_fields << "assigned_to_id"
TopicList.on_preload do |topics|
if topics.length > 0
TopicList.on_preload do |topics, topic_list|
is_staff = topic_list.current_user && topic_list.current_user.staff?
allowed_access = SiteSetting.assigns_public || is_staff
if allowed_access && topics.length > 0
users = User.where("id in (
SELECT value::int
FROM topic_custom_fields
@ -95,7 +100,7 @@ after_initialize do
topics.each do |t|
if id = t.custom_fields['assigned_to_id']
t.preload_owner(map[id.to_i])
t.preload_assigned_to_user(map[id.to_i])
end
end
end
@ -103,7 +108,11 @@ after_initialize do
require_dependency 'listable_topic_serializer'
class ::ListableTopicSerializer
has_one :owner, serializer: BasicUserSerializer, embed: :objects
has_one :assigned_to_user, serializer: BasicUserSerializer, embed: :objects
def include_assigned_to_user?
(SiteSetting.assigns_public || scope.is_staff?) && object.assigned_to_user
end
end
require_dependency 'topic_view_serializer'
@ -128,7 +137,9 @@ after_initialize do
end
def include_assigned_to_user?
assigned_to_user_id
if SiteSetting.assigns_public || scope.is_staff?
assigned_to_user_id
end
end
def assigned_to_user_id