Skip to content

Commit

Permalink
Implement hard deletion and rename soft delete to hide
Browse files Browse the repository at this point in the history
  • Loading branch information
tobyzerner committed Feb 12, 2015
1 parent c336976 commit 36787bc
Show file tree
Hide file tree
Showing 18 changed files with 90 additions and 50 deletions.
20 changes: 13 additions & 7 deletions ember/app/components/discussion/post-comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ export default Ember.Component.extend(FadeIn, HasItemLists, UseComposer, {
tagName: 'article',
classNames: ['post', 'post-comment'],
classNameBindings: [
'post.isHidden:deleted',
'post.isEdited:edited',
'post.isHidden:is-hidden',
'post.isEdited:is-edited',
'revealContent:reveal-content'
],
itemLists: ['controls', 'header', 'footer'],
Expand All @@ -35,7 +35,7 @@ export default Ember.Component.extend(FadeIn, HasItemLists, UseComposer, {
populateControls: function(items) {
if (this.get('post.isHidden')) {
this.addActionItem(items, 'restore', 'Restore', 'reply', 'post.canEdit');
this.addActionItem(items, 'delete', 'Delete', 'times', 'post.canDelete');
this.addActionItem(items, 'delete', 'Delete Forever', 'times', 'post.canDelete');
} else {
this.addActionItem(items, 'edit', 'Edit', 'pencil', 'post.canEdit');
this.addActionItem(items, 'hide', 'Delete', 'times', 'post.canEdit');
Expand Down Expand Up @@ -88,8 +88,8 @@ export default Ember.Component.extend(FadeIn, HasItemLists, UseComposer, {
var post = this.get('post');
post.setProperties({
isHidden: true,
deleteTime: new Date,
deleteUser: this.get('session.user')
hideTime: new Date,
hideUser: this.get('session.user')
});
post.save();
},
Expand All @@ -98,10 +98,16 @@ export default Ember.Component.extend(FadeIn, HasItemLists, UseComposer, {
var post = this.get('post');
post.setProperties({
isHidden: false,
deleteTime: null,
deleteUser: null
hideTime: null,
hideUser: null
});
post.save();
},

delete: function() {
var post = this.get('post');
post.destroyRecord();
this.sendAction('postRemoved', post);
}
}
});
4 changes: 4 additions & 0 deletions ember/app/components/discussion/stream-content.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,10 @@ export default Ember.Component.extend({

loadRange: function(start, end, backwards) {
this.get('stream').loadRange(start, end, backwards);
},

postRemoved: function(post) {
this.sendAction('postRemoved', post);
}
}
});
4 changes: 4 additions & 0 deletions ember/app/controllers/discussion.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ export default Ember.Controller.extend(Ember.Evented, UseComposerMixin, {
discussion.set('readNumber', endNumber);
discussion.save();
}
},

postRemoved: function(post) {
this.get('stream').removePost(post);
}
}
});
6 changes: 6 additions & 0 deletions ember/app/models/post-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,12 @@ export default Ember.ArrayProxy.extend(Ember.Evented, {
this.get('content').pushObject(this.makeItem(index, index, post));
},

removePost: function(post) {
this.get('ids').removeObject(post.get('id'));
var content = this.get('content');
content.removeObject(content.findBy('content', post));
},

makeItem: function(indexStart, indexEnd, post) {
var item = Ember.Object.create({
indexStart: indexStart,
Expand Down
4 changes: 2 additions & 2 deletions ember/app/models/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ export default DS.Model.extend({
editUser: DS.belongsTo('user'),
isEdited: Ember.computed.notEmpty('editTime'),

hideTime: DS.attr('date'),
hideUser: DS.belongsTo('user'),
isHidden: DS.attr('boolean'),
deleteTime: DS.attr('date'),
deleteUser: DS.belongsTo('user'),

canEdit: DS.attr('boolean'),
canDelete: DS.attr('boolean')
Expand Down
16 changes: 16 additions & 0 deletions ember/app/serializers/post.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import ApplicationSerializer from 'flarum/serializers/application';

export default ApplicationSerializer.extend({
attrs: {
number: {serialize: false},
time: {serialize: false},
type: {serialize: false},
contentHtml: {serialize: false},
editTime: {serialize: false},
editUser: {serialize: false},
hideTime: {serialize: false},
hideUser: {serialize: false},
canEdit: {serialize: false},
canDelete: {serialize: false}
}
});
7 changes: 5 additions & 2 deletions ember/app/styles/flarum/discussion.less
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@
float: right;
margin: -2px 0 0 10px;
visibility: hidden;
z-index: 1;
}
&:hover .contextual-controls, & .contextual-controls.open {
visibility: visible;
Expand Down Expand Up @@ -159,7 +158,7 @@
text-align: center;
font-size: 22px;
}
.post.deleted {
.post.is-hidden {
& .post-user, & .post-header > ul, & .post-header > ul a:not(.btn) {
color: @fl-body-muted-more-color;
}
Expand All @@ -173,6 +172,10 @@
opacity: 0.5;
}
}
& .btn-more {
background: #eee;
color: @fl-body-muted-more-color;
}
}
.post-meta {
width: 400px;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{{#each item in stream}}
{{#discussion/stream-item item=item stream=stream loadRange="loadRange"}}
{{#if item.content}}
{{component item.component content=item.content}}
{{component item.component content=item.content postRemoved="postRemoved"}}
{{/if}}
{{/discussion/stream-item}}
{{/each}}
3 changes: 2 additions & 1 deletion ember/app/templates/discussion.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@
viewName="streamContent"
stream=stream
class="discussion-posts posts"
positionChanged="positionChanged"}}
positionChanged="positionChanged"
postRemoved="postRemoved"}}
</div>
4 changes: 2 additions & 2 deletions src/Flarum/Api/Actions/Discussions/Show.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Show extends Base

/**
* The post repository.
*
*
* @var PostRepository
*/
protected $posts;
Expand All @@ -39,7 +39,7 @@ protected function run()
$discussion = Discussion::whereCanView()->findOrFail($this->param('id'));

if (in_array('posts', $include)) {
$relations = ['user', 'user.groups', 'editUser', 'deleteUser'];
$relations = ['user', 'user.groups', 'editUser', 'hideUser'];
$discussion->posts = $this->getPostsForDiscussion($this->posts, $discussion->id, $relations);

$include = array_merge($include, array_map(function ($relation) {
Expand Down
4 changes: 2 additions & 2 deletions src/Flarum/Api/Actions/Posts/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Index extends Base

/**
* The post repository.
*
*
* @var PostRepository
*/
protected $posts;
Expand All @@ -35,7 +35,7 @@ public function __construct(PostRepository $posts)
protected function run()
{
$postIds = (array) $this->input('ids');
$include = ['user', 'user.groups', 'editUser', 'deleteUser'];
$include = ['user', 'user.groups', 'editUser', 'hideUser'];

if (count($postIds)) {
$posts = $this->posts->findMany($postIds, $include);
Expand Down
2 changes: 1 addition & 1 deletion src/Flarum/Api/Actions/Posts/Show.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ protected function run()
}

$include = $this->included(['discussion', 'replyTo']);
$relations = array_merge(['user', 'editUser', 'deleteUser'], $include);
$relations = array_merge(['user', 'editUser', 'hideUser'], $include);
$posts->load($relations);

// Finally, we can set up the post serializer and use it to create
Expand Down
20 changes: 10 additions & 10 deletions src/Flarum/Api/Serializers/PostSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class PostSerializer extends PostBasicSerializer
* Default relations to include.
* @var array
*/
protected $include = ['user', 'editUser', 'deleteUser'];
protected $include = ['user', 'editUser', 'hideUser'];

/**
* Serialize attributes of a Post model for JSON output.
Expand Down Expand Up @@ -51,9 +51,9 @@ protected function attributes(Post $post)
$attributes['editTime'] = $post->edit_time->toRFC3339String();
}

if ($post->delete_time) {
if ($post->hide_time) {
$attributes['isHidden'] = true;
$attributes['deleteTime'] = $post->delete_time->toRFC3339String();
$attributes['hideTime'] = $post->hide_time->toRFC3339String();
}

$attributes += [
Expand All @@ -66,7 +66,7 @@ protected function attributes(Post $post)

/**
* Get a resource containing a post's user.
*
*
* @param Post $post
* @param array $relations
* @return Tobscure\JsonApi\Resource
Expand All @@ -78,7 +78,7 @@ public function includeUser(Post $post, $relations = [])

/**
* Get a resource containing a post's discussion.
*
*
* @param Post $post
* @param array $relations
* @return Tobscure\JsonApi\Resource
Expand All @@ -90,7 +90,7 @@ public function includeDiscussion(Post $post, $relations = [])

/**
* Get a resource containing a post's edit user.
*
*
* @param Post $post
* @param array $relations
* @return Tobscure\JsonApi\Resource
Expand All @@ -101,14 +101,14 @@ public function includeEditUser(Post $post, $relations = [])
}

/**
* Get a resource containing a post's delete user.
*
* Get a resource containing a post's hide user.
*
* @param Post $post
* @param array $relations
* @return Tobscure\JsonApi\Resource
*/
public function includeDeleteUser(Post $post, $relations = [])
public function includeHideUser(Post $post, $relations = [])
{
return (new UserBasicSerializer($relations))->resource($post->deleteUser);
return (new UserBasicSerializer($relations))->resource($post->hideUser);
}
}
2 changes: 1 addition & 1 deletion src/Flarum/Core/Discussions/Discussion.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public function posts()

public function comments()
{
return $this->posts()->where('type', 'comment')->whereNull('delete_time');
return $this->posts()->where('type', 'comment')->whereNull('hide_time');
}

public function startPost()
Expand Down
12 changes: 6 additions & 6 deletions src/Flarum/Core/Posts/CommentPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,24 +54,24 @@ public function revise($content, $user)

public function hide($user)
{
if ($this->delete_time) {
if ($this->hide_time) {
return;
}

$this->delete_time = time();
$this->delete_user_id = $user->id;
$this->hide_time = time();
$this->hide_user_id = $user->id;

$this->raise(new Events\PostWasHidden($this));
}

public function restore($user)
{
if ($this->delete_time === null) {
if ($this->hide_time === null) {
return;
}

$this->delete_time = null;
$this->delete_user_id = null;
$this->hide_time = null;
$this->hide_user_id = null;

$this->raise(new Events\PostWasRestored($this));
}
Expand Down
18 changes: 9 additions & 9 deletions src/Flarum/Core/Posts/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ class Post extends Entity
'user_id' => 'integer',
'edit_time' => 'date',
'edit_user_id' => 'integer',
'delete_time' => 'date',
'delete_user_id' => 'integer',
'hide_time' => 'date',
'hide_user_id' => 'integer',
];

public static function boot()
Expand All @@ -43,7 +43,7 @@ public static function boot()
});

static::check('view', function ($check, $user) {
$check->whereNull('delete_user_id')
$check->whereNull('hide_user_id')
->orWhereCan('edit');
});

Expand All @@ -55,8 +55,8 @@ public static function boot()
});

static::check('editOwn', function ($check, $user) {
$check->whereNull('delete_user_id')
->orWhere('delete_user_id', $user->id);
$check->whereNull('hide_user_id')
->orWhere('hide_user_id', $user->id);
});

static::deleted(function ($post) {
Expand All @@ -79,14 +79,14 @@ public function editUser()
return $this->belongsTo('Flarum\Core\Users\User', 'edit_user_id');
}

public function deleteUser()
public function hideUser()
{
return $this->belongsTo('Flarum\Core\Users\User', 'delete_user_id');
return $this->belongsTo('Flarum\Core\Users\User', 'hide_user_id');
}

public function getDates()
{
return ['time', 'edit_time', 'delete_time'];
return ['time', 'edit_time', 'hide_time'];
}

// Terminates the query and returns an array of matching IDs.
Expand Down Expand Up @@ -122,7 +122,7 @@ public function newFromBuilder($attributes = [])
return $instance;
}
}

return parent::newFromBuilder($attributes);
}
}
8 changes: 4 additions & 4 deletions src/Flarum/Core/Support/Seeders/DiscussionTableSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ public function run()
]);
} else {
$edited = rand(1, 20) == 1;
$deleted = rand(1, 100) == 1;
$hidden = rand(1, 100) == 1;

if ($deleted) {
if ($hidden) {
$discussion->comments_count--;
}

Expand All @@ -82,8 +82,8 @@ public function run()
'content' => $faker->realText(rand(50, 500)),
'edit_time' => $edited ? $startTime = date_add($startTime, date_interval_create_from_date_string('1 second')) : null,
'edit_user_id' => $edited ? rand(1, $users) : null,
'delete_time' => $deleted ? $startTime = date_add($startTime, date_interval_create_from_date_string('1 second')) : null,
'delete_user_id' => $deleted ? rand(1, $users) : null,
'hide_time' => $hidden ? $startTime = date_add($startTime, date_interval_create_from_date_string('1 second')) : null,
'hide_user_id' => $hidden ? rand(1, $users) : null,
]);

$posts[] = $post;
Expand Down
Loading

0 comments on commit 36787bc

Please sign in to comment.