REFACTOR: Merge different templates from rendering user stream items

This commit is contained in:
Robin Ward 2017-06-20 14:41:41 -04:00
parent 9f8388c2eb
commit ae7734707e
12 changed files with 70 additions and 108 deletions

View File

@ -2,13 +2,7 @@ import { propertyEqual } from 'discourse/lib/computed';
import { actionDescription } from "discourse/components/small-action"; import { actionDescription } from "discourse/components/small-action";
export default Ember.Component.extend({ export default Ember.Component.extend({
classNameBindings: [":item", "item.hidden", "item.deleted", "moderatorAction"], classNameBindings: [":item", "item.hidden", "item.deleted:deleted", "moderatorAction"],
moderatorAction: propertyEqual("item.post_type", "site.post_types.moderator_action"), moderatorAction: propertyEqual("item.post_type", "site.post_types.moderator_action"),
actionDescription: actionDescription("item.action_code", "item.created_at", "item.username"), actionDescription: actionDescription("item.action_code", "item.created_at", "item.username"),
actions: {
removeBookmark(userAction) {
this.sendAction("removeBookmark", userAction);
}
}
}); });

View File

@ -1,6 +1,7 @@
import LoadMore from "discourse/mixins/load-more"; import LoadMore from "discourse/mixins/load-more";
import ClickTrack from 'discourse/lib/click-track'; import ClickTrack from 'discourse/lib/click-track';
import { selectedText } from 'discourse/lib/utilities'; import { selectedText } from 'discourse/lib/utilities';
import Post from 'discourse/models/post';
export default Ember.Component.extend(LoadMore, { export default Ember.Component.extend(LoadMore, {
loading: false, loading: false,
@ -44,6 +45,13 @@ export default Ember.Component.extend(LoadMore, {
}.on('willDestroyElement'), }.on('willDestroyElement'),
actions: { actions: {
removeBookmark(userAction) {
const stream = this.get('stream');
Post.updateBookmark(userAction.get("post_id"), false).then(() => {
stream.remove(userAction);
});
},
loadMore() { loadMore() {
if (this.get('loading')) { return; } if (this.get('loading')) { return; }

View File

@ -1,26 +0,0 @@
import Post from 'discourse/models/post';
export default Post.extend({
_attachCategory: function () {
const categoryId = this.get("category_id");
if (categoryId) {
this.set("category", Discourse.Category.findById(categoryId));
}
}.on("init"),
presentName: Ember.computed.or('name', 'username'),
sameUser: function() {
return this.get("username") === Discourse.User.currentProp("username");
}.property("username"),
descriptionKey: function () {
if (this.get("reply_to_post_number")) {
return this.get("sameUser") ? "you_replied_to_post" : "user_replied_to_post";
} else {
return this.get("sameUser") ? "you_replied_to_topic" : "user_replied_to_topic";
}
}.property("reply_to_post_number", "sameUser")
});

View File

@ -1,6 +1,6 @@
import { ajax } from 'discourse/lib/ajax'; import { ajax } from 'discourse/lib/ajax';
import { url } from 'discourse/lib/computed'; import { url } from 'discourse/lib/computed';
import AdminPost from 'discourse/models/admin-post'; import UserAction from 'discourse/models/user-action';
export default Discourse.Model.extend({ export default Discourse.Model.extend({
loaded: false, loaded: false,
@ -36,7 +36,7 @@ export default Discourse.Model.extend({
return ajax(this.get("url"), { cache: false }).then(function (result) { return ajax(this.get("url"), { cache: false }).then(function (result) {
if (result) { if (result) {
const posts = result.map(function (post) { return AdminPost.create(post); }); const posts = result.map(function (post) { return UserAction.create(post); });
self.get("content").pushObjects(posts); self.get("content").pushObjects(posts);
self.setProperties({ self.setProperties({
loaded: true, loaded: true,

View File

@ -19,26 +19,9 @@ export default Discourse.Route.extend(ViewingActionType, {
}, },
actions: { actions: {
didTransition() { didTransition() {
this.controllerFor("user-activity")._showFooter(); this.controllerFor("user-activity")._showFooter();
return true; return true;
}, }
removeBookmark(userAction) {
var user = this.modelFor("user");
Discourse.Post.updateBookmark(userAction.get("post_id"), false)
.then(function() {
// remove the user action from the stream
user.get("stream").remove(userAction);
// update the counts
user.get("stats").forEach(function (stat) {
if (stat.get("action_type") === userAction.action_type) {
stat.decrementProperty("count");
}
});
});
},
} }
}); });

View File

@ -6,6 +6,14 @@
<a href={{item.postUrl}}>{{{item.title}}}</a> <a href={{item.postUrl}}>{{{item.title}}}</a>
</span> </span>
<div class="category">{{category-link item.category}}</div> <div class="category">{{category-link item.category}}</div>
{{#if item.deleted_by}}
<span class="delete-info">
{{fa-icon "trash-o"}}
{{avatar item.deleted_by imageSize="tiny" extraClasses="actor" ignoreTitle="true"}}
{{format-date item.deleted_at leaveAgo="true"}}
</span>
{{/if}}
{{plugin-outlet name="user-stream-item-header" args=(hash item=item)}} {{plugin-outlet name="user-stream-item-header" args=(hash item=item)}}
</div> </div>
@ -20,7 +28,7 @@
<i class="icon {{child.icon}}"></i> <i class="icon {{child.icon}}"></i>
{{#each child.items as |grandChild|}} {{#each child.items as |grandChild|}}
{{#if grandChild.removableBookmark}} {{#if grandChild.removableBookmark}}
<button class="btn btn-default remove-bookmark" {{action "removeBookmark" grandChild}}> <button class="btn btn-default remove-bookmark" {{action removeBookmark grandChild}}>
{{fa-icon 'times'}} {{i18n "bookmarks.remove"}} {{fa-icon 'times'}} {{i18n "bookmarks.remove"}}
</button> </button>
{{else}} {{else}}

View File

@ -0,0 +1,3 @@
{{#each stream.content as |item|}}
{{stream-item item=item removeBookmark=(action "removeBookmark")}}
{{/each}}

View File

@ -1,30 +1 @@
{{#user-stream stream=model}} {{user-stream stream=model}}
{{#each model.content as |p|}}
<div class="item {{if p.hidden 'hidden'}} {{if p.deleted 'deleted'}} {{if p.moderator_action 'moderator-action'}}">
<div class="clearfix info">
<a href="{{unbound p.usernameUrl}}" class="avatar-link">
<div class="avatar-wrapper">
{{avatar p imageSize="large" extraClasses="actor" ignoreTitle="true"}}
</div>
</a>
<span class="time">
{{format-date p.created_at leaveAgo="true"}}
</span>
<span class="title">
<a href="{{unbound p.url}}">{{unbound p.topic_title}}</a>
</span>
<span class="category">
{{category-link p.category}}
</span>
{{#if p.deleted}}
<span class="delete-info">
<i class="fa fa-trash-o"></i> {{avatar p.deleted_by imageSize="tiny" extraClasses="actor" ignoreTitle="true"}} {{format-date p.deleted_at leaveAgo="true"}}
</span>
{{/if}}
</div>
<p class="excerpt">
{{{p.excerpt}}}
</p>
</div>
{{/each}}
{{/user-stream}}

View File

@ -3,8 +3,4 @@
{{{model.noContentHelp}}} {{{model.noContentHelp}}}
</div> </div>
{{/if}} {{/if}}
{{#user-stream stream=model}} {{user-stream stream=model}}
{{#each model.content as |item|}}
{{stream-item item=item removeBookmark="removeBookmark"}}
{{/each}}
{{/user-stream}}

View File

@ -445,7 +445,7 @@ class PostsController < ApplicationController
.where(disagreed_at: nil) .where(disagreed_at: nil)
.select(:post_id)) .select(:post_id))
render_serialized(posts, AdminPostSerializer) render_serialized(posts, AdminUserActionSerializer)
end end
def deleted_posts def deleted_posts
@ -458,7 +458,7 @@ class PostsController < ApplicationController
posts = user_posts(guardian, user.id, offset: offset, limit: limit).where.not(deleted_at: nil) posts = user_posts(guardian, user.id, offset: offset, limit: limit).where.not(deleted_at: nil)
render_serialized(posts, AdminPostSerializer) render_serialized(posts, AdminUserActionSerializer)
end end
protected protected

View File

@ -1,17 +1,29 @@
class AdminPostSerializer < ApplicationSerializer class AdminUserActionSerializer < ApplicationSerializer
attributes :id, attributes(
:created_at, :id,
:post_number, :created_at,
:name, :username, :avatar_template, :post_number,
:topic_id, :topic_slug, :topic_title, :name,
:category_id, :username,
:excerpt, :avatar_template,
:hidden, :topic_id,
:moderator_action, :slug,
:deleted_at, :deleted_by, :title,
:reply_to_post_number, :category_id,
:action_type :excerpt,
:hidden,
:moderator_action,
:deleted,
:deleted_at,
:deleted_by,
:reply_to_post_number,
:action_type
)
def deleted
deleted_at.present?
end
def name def name
object.user.name object.user.name
@ -29,11 +41,11 @@ class AdminPostSerializer < ApplicationSerializer
object.user.avatar_template object.user.avatar_template
end end
def topic_slug def slug
topic.slug topic.slug
end end
def topic_title def title
topic.title topic.title
end end

View File

@ -3,6 +3,7 @@ class UserActionSerializer < ApplicationSerializer
attributes :action_type, attributes :action_type,
:created_at, :created_at,
:excerpt, :excerpt,
:truncated,
:avatar_template, :avatar_template,
:acting_avatar_template, :acting_avatar_template,
:slug, :slug,
@ -29,9 +30,21 @@ class UserActionSerializer < ApplicationSerializer
:closed, :closed,
:archived :archived
def cooked
@cooked ||= object.cooked || PrettyText.cook(object.raw)
end
def excerpt def excerpt
cooked = object.cooked || PrettyText.cook(object.raw) return nil unless cooked
PrettyText.excerpt(cooked, 300, keep_emoji_images: true) if cooked @excerpt ||= PrettyText.excerpt(cooked, 300, keep_emoji_images: true)
end
def truncated
true
end
def include_truncated?
excerpt != cooked
end end
def avatar_template def avatar_template