mirror of
https://github.com/flarum/framework.git
synced 2025-01-19 16:23:16 +08:00
Add user activity system
This commit is contained in:
parent
9ceee041a9
commit
fe982aa587
|
@ -7,6 +7,13 @@ import AlertMessage from 'flarum/components/ui/alert-message';
|
|||
export default JsonApiAdapter.extend({
|
||||
host: config.apiURL,
|
||||
|
||||
pathForType: function(type) {
|
||||
if (type == 'activity') {
|
||||
return type;
|
||||
}
|
||||
return this._super(type);
|
||||
},
|
||||
|
||||
ajaxError: function(jqXHR) {
|
||||
var errors = this._super(jqXHR);
|
||||
|
||||
|
|
12
framework/core/ember/app/components/user/activity-item.js
Normal file
12
framework/core/ember/app/components/user/activity-item.js
Normal file
|
@ -0,0 +1,12 @@
|
|||
import Ember from 'ember';
|
||||
|
||||
import FadeIn from 'flarum/mixins/fade-in';
|
||||
|
||||
export default Ember.Component.extend(FadeIn, {
|
||||
layoutName: 'components/user/activity-item',
|
||||
tagName: 'li',
|
||||
|
||||
componentName: Ember.computed('activity.type', function() {
|
||||
return 'user/activity-'+this.get('activity.type');
|
||||
})
|
||||
});
|
|
@ -0,0 +1,9 @@
|
|||
import Ember from 'ember';
|
||||
|
||||
export default Ember.Component.extend({
|
||||
layoutName: 'components/user/activity-post',
|
||||
|
||||
isFirstPost: Ember.computed('activity.post.number', function() {
|
||||
return this.get('activity.post.number') === 1;
|
||||
})
|
||||
});
|
71
framework/core/ember/app/controllers/user/activity.js
Normal file
71
framework/core/ember/app/controllers/user/activity.js
Normal file
|
@ -0,0 +1,71 @@
|
|||
import Ember from 'ember';
|
||||
|
||||
export default Ember.Controller.extend({
|
||||
needs: ['user'],
|
||||
|
||||
queryParams: ['filter'],
|
||||
filter: '',
|
||||
|
||||
resultsLoading: false,
|
||||
|
||||
moreResults: true,
|
||||
|
||||
loadCount: 10,
|
||||
|
||||
getResults: function(start) {
|
||||
var type;
|
||||
switch (this.get('filter')) {
|
||||
case 'discussions':
|
||||
type = 'discussion';
|
||||
break;
|
||||
|
||||
case 'posts':
|
||||
type = 'post';
|
||||
break;
|
||||
}
|
||||
var controller = this;
|
||||
return this.store.find('activity', {
|
||||
users: this.get('controllers.user.model.id'),
|
||||
type: type,
|
||||
start: start,
|
||||
count: this.get('loadCount')
|
||||
}).then(function(results) {
|
||||
controller.set('moreResults', results.get('length') >= controller.get('loadCount'));
|
||||
return results;
|
||||
});
|
||||
},
|
||||
|
||||
paramsDidChange: Ember.observer('filter', function() {
|
||||
if (this.get('model') && !this.get('resultsLoading')) {
|
||||
Ember.run.once(this, this.loadResults);
|
||||
}
|
||||
}),
|
||||
|
||||
loadResults: function() {
|
||||
this.send('loadResults');
|
||||
},
|
||||
|
||||
actions: {
|
||||
loadResults: function() {
|
||||
var controller = this;
|
||||
controller.get('model').set('content', []);
|
||||
controller.set('resultsLoading', true);
|
||||
controller.getResults().then(function(results) {
|
||||
controller
|
||||
.set('resultsLoading', false)
|
||||
.set('meta', results.get('meta'))
|
||||
.set('model.content', results);
|
||||
});
|
||||
},
|
||||
|
||||
loadMore: function() {
|
||||
var controller = this;
|
||||
this.set('resultsLoading', true);
|
||||
this.getResults(this.get('model.length')).then(function(results) {
|
||||
controller.get('model.content').addObjects(results);
|
||||
controller.set('meta', results.get('meta'));
|
||||
controller.set('resultsLoading', false);
|
||||
});
|
||||
},
|
||||
}
|
||||
});
|
11
framework/core/ember/app/models/activity.js
Normal file
11
framework/core/ember/app/models/activity.js
Normal file
|
@ -0,0 +1,11 @@
|
|||
import DS from 'ember-data';
|
||||
|
||||
export default DS.Model.extend({
|
||||
type: DS.attr('string'),
|
||||
content: DS.attr('string'),
|
||||
time: DS.attr('date'),
|
||||
|
||||
user: DS.belongsTo('user'),
|
||||
sender: DS.belongsTo('user'),
|
||||
post: DS.belongsTo('post')
|
||||
});
|
|
@ -14,8 +14,6 @@ Router.map(function() {
|
|||
|
||||
this.resource('user', {path: '/u/:username'}, function() {
|
||||
this.route('activity', {path: '/'});
|
||||
this.route('discussions');
|
||||
this.route('posts');
|
||||
this.route('edit');
|
||||
});
|
||||
|
||||
|
|
12
framework/core/ember/app/routes/user/activity.js
Normal file
12
framework/core/ember/app/routes/user/activity.js
Normal file
|
@ -0,0 +1,12 @@
|
|||
import Ember from 'ember';
|
||||
|
||||
export default Ember.Route.extend({
|
||||
model: function() {
|
||||
return Ember.RSVP.resolve(Ember.ArrayProxy.create());
|
||||
},
|
||||
|
||||
setupController: function(controller, model) {
|
||||
controller.set('model', model);
|
||||
controller.send('loadResults');
|
||||
}
|
||||
});
|
|
@ -95,3 +95,76 @@
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
.user-content .loading-indicator {
|
||||
height: 46px;
|
||||
}
|
||||
.user-activity {
|
||||
border-left: 3px solid @fl-body-secondary-color;
|
||||
list-style: none;
|
||||
margin: 0 0 0 16px;
|
||||
padding: 0;
|
||||
|
||||
& > li {
|
||||
margin-bottom: 30px;
|
||||
padding-left: 32px;
|
||||
}
|
||||
& .activity-icon {
|
||||
.avatar-size(32px);
|
||||
float: left;
|
||||
margin-left: -49px;
|
||||
.box-shadow(0 0 0 3px #fff);
|
||||
margin-top: -5px;
|
||||
}
|
||||
}
|
||||
.activity-info {
|
||||
color: @fl-body-muted-color;
|
||||
margin-bottom: 10px;
|
||||
|
||||
& strong {
|
||||
margin-right: 5px;
|
||||
}
|
||||
}
|
||||
.activity-content {
|
||||
display: block;
|
||||
padding: 20px;
|
||||
background: @fl-body-secondary-color;
|
||||
border-radius: @border-radius-base;
|
||||
color: @fl-body-muted-color;
|
||||
|
||||
&, &:hover {
|
||||
text-decoration: none;
|
||||
}
|
||||
& .discussion-summary {
|
||||
margin: -20px 0;
|
||||
padding-left: 0;
|
||||
|
||||
& .author {
|
||||
display: none;
|
||||
}
|
||||
}
|
||||
}
|
||||
.activity-post {
|
||||
overflow: hidden;
|
||||
|
||||
& .title {
|
||||
margin: 0 0 10px;
|
||||
font-size: 14px;
|
||||
font-weight: bold;
|
||||
|
||||
&, & a {
|
||||
color: @fl-body-heading-color;
|
||||
}
|
||||
}
|
||||
&:hover .title {
|
||||
text-decoration: underline;
|
||||
}
|
||||
& .body {
|
||||
color: @fl-body-muted-color;
|
||||
line-height: 1.7em;
|
||||
|
||||
& :last-child {
|
||||
margin-bottom: 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
{{component componentName activity=activity}}
|
|
@ -0,0 +1,6 @@
|
|||
{{user-avatar activity.user class="activity-icon"}}
|
||||
|
||||
<div class="activity-info">
|
||||
<strong>Joined the forum</strong>
|
||||
{{human-time activity.time}}
|
||||
</div>
|
|
@ -0,0 +1,21 @@
|
|||
{{user-avatar activity.post.user class="activity-icon"}}
|
||||
|
||||
<div class="activity-info">
|
||||
<strong>{{if isFirstPost "Started a discussion" "Posted a reply"}}</strong>
|
||||
{{human-time activity.time}}
|
||||
</div>
|
||||
|
||||
{{#if isFirstPost}}
|
||||
<div class="activity-content activity-discussion">
|
||||
{{index/discussion-listing discussion=activity.post.discussion}}
|
||||
</div>
|
||||
{{else}}
|
||||
{{#link-to "discussion" activity.post.discussion (query-params start=activity.post.number) class="activity-content activity-post"}}
|
||||
<h3 class="title">
|
||||
{{activity.post.discussion.title}}
|
||||
</h3>
|
||||
<div class="body">
|
||||
{{{activity.post.contentHtml}}}
|
||||
</div>
|
||||
{{/link-to}}
|
||||
{{/if}}
|
|
@ -5,5 +5,7 @@
|
|||
{{ui/item-list items=view.sidebar}}
|
||||
</nav>
|
||||
|
||||
{{outlet}}
|
||||
<div class="offset-content user-content">
|
||||
{{outlet}}
|
||||
</div>
|
||||
</div>
|
||||
|
|
13
framework/core/ember/app/templates/user/activity.hbs
Normal file
13
framework/core/ember/app/templates/user/activity.hbs
Normal file
|
@ -0,0 +1,13 @@
|
|||
<ul class="user-activity">
|
||||
{{#each activity in model}}
|
||||
{{user/activity-item activity=activity}}
|
||||
{{/each}}
|
||||
</ul>
|
||||
|
||||
{{#if resultsLoading}}
|
||||
{{ui/loading-indicator size="small"}}
|
||||
{{else if moreResults}}
|
||||
<div class="load-more">
|
||||
{{ui/action-button class="control-loadMore btn btn-default" action="loadMore" label="Load More"}}
|
||||
</div>
|
||||
{{/if}}
|
|
@ -9,6 +9,22 @@ var precompileTemplate = Ember.Handlebars.compile;
|
|||
export default Ember.View.extend(HasItemLists, {
|
||||
itemLists: ['sidebar'],
|
||||
|
||||
didInsertElement: function() {
|
||||
// Affix the sidebar so that when the user scrolls down it will stick
|
||||
// to the top of their viewport.
|
||||
var $sidebar = this.$('.user-nav');
|
||||
$sidebar.find('> ul').affix({
|
||||
offset: {
|
||||
top: function () {
|
||||
return $sidebar.offset().top - $('#header').outerHeight(true) - parseInt($sidebar.css('margin-top'));
|
||||
},
|
||||
bottom: function () {
|
||||
return (this.bottom = $('#footer').outerHeight(true));
|
||||
}
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
populateSidebar: function(items) {
|
||||
var nav = this.populateItemList('nav');
|
||||
items.pushObjectWithTag(DropdownSelect.extend({items: nav, listItemClass: 'title-control'}), 'nav');
|
||||
|
@ -18,7 +34,7 @@ export default Ember.View.extend(HasItemLists, {
|
|||
items.pushObjectWithTag(NavItem.extend({
|
||||
label: 'Activity',
|
||||
icon: 'user',
|
||||
layout: precompileTemplate('{{#link-to "user.activity"}}{{fa-icon icon}} {{label}}{{/link-to}}')
|
||||
layout: precompileTemplate('{{#link-to "user.activity" (query-params filter="")}}{{fa-icon icon}} {{label}}{{/link-to}}')
|
||||
}), 'activity');
|
||||
|
||||
items.pushObjectWithTag(NavItem.extend({
|
||||
|
@ -26,7 +42,7 @@ export default Ember.View.extend(HasItemLists, {
|
|||
icon: 'reorder',
|
||||
badge: Ember.computed.alias('user.discussionsCount'),
|
||||
user: this.get('controller.model'),
|
||||
layout: precompileTemplate('{{#link-to "user.discussions"}}{{fa-icon icon}} {{label}} <span class="count">{{badge}}</span>{{/link-to}}')
|
||||
layout: precompileTemplate('{{#link-to "user.activity" (query-params filter="discussions")}}{{fa-icon icon}} {{label}} <span class="count">{{badge}}</span>{{/link-to}}')
|
||||
}), 'discussions');
|
||||
|
||||
items.pushObjectWithTag(NavItem.extend({
|
||||
|
@ -34,7 +50,7 @@ export default Ember.View.extend(HasItemLists, {
|
|||
icon: 'comment-o',
|
||||
badge: Ember.computed.alias('user.commentsCount'),
|
||||
user: this.get('controller.model'),
|
||||
layout: precompileTemplate('{{#link-to "user.posts"}}{{fa-icon icon}} {{label}} <span class="count">{{badge}}</span>{{/link-to}}')
|
||||
layout: precompileTemplate('{{#link-to "user.activity" (query-params filter="posts")}}{{fa-icon icon}} {{label}} <span class="count">{{badge}}</span>{{/link-to}}')
|
||||
}), 'posts');
|
||||
}
|
||||
});
|
||||
|
|
|
@ -16,12 +16,10 @@ class CreateActivityTable extends Migration {
|
|||
{
|
||||
$table->increments('id');
|
||||
$table->integer('user_id')->unsigned();
|
||||
$table->integer('from_user_id')->unsigned()->nullable();
|
||||
$table->string('subject');
|
||||
$table->integer('subject_id')->unsigned()->nullable();
|
||||
$table->integer('sender_id')->unsigned()->nullable();
|
||||
$table->string('type');
|
||||
$table->binary('data')->nullable();
|
||||
$table->dateTime('time');
|
||||
$table->boolean('is_read')->default(0);
|
||||
});
|
||||
}
|
||||
|
||||
|
|
47
framework/core/src/Api/Actions/Activity/IndexAction.php
Normal file
47
framework/core/src/Api/Actions/Activity/IndexAction.php
Normal file
|
@ -0,0 +1,47 @@
|
|||
<?php namespace Flarum\Api\Actions\Activity;
|
||||
|
||||
use Flarum\Core\Repositories\UserRepositoryInterface;
|
||||
use Flarum\Core\Repositories\ActivityRepositoryInterface;
|
||||
use Flarum\Core\Support\Actor;
|
||||
use Flarum\Api\Actions\BaseAction;
|
||||
use Flarum\Api\Actions\ApiParams;
|
||||
use Flarum\Api\Serializers\ActivitySerializer;
|
||||
|
||||
class IndexAction extends BaseAction
|
||||
{
|
||||
/**
|
||||
* Instantiate the action.
|
||||
*
|
||||
* @param \Flarum\Core\Search\Discussions\UserSearcher $searcher
|
||||
*/
|
||||
public function __construct(Actor $actor, UserRepositoryInterface $users, ActivityRepositoryInterface $activity)
|
||||
{
|
||||
$this->actor = $actor;
|
||||
$this->users = $users;
|
||||
$this->activity = $activity;
|
||||
}
|
||||
|
||||
/**
|
||||
* Show a user's activity feed.
|
||||
*
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
protected function run(ApiParams $params)
|
||||
{
|
||||
$start = $params->start();
|
||||
$count = $params->count(20, 50);
|
||||
$type = $params->get('type');
|
||||
$id = $params->get('users');
|
||||
|
||||
$user = $this->users->findOrFail($id, $this->actor->getUser());
|
||||
|
||||
$activity = $this->activity->findByUser($user->id, $this->actor->getUser(), $count, $start, $type);
|
||||
|
||||
// Finally, we can set up the activity serializer and use it to create
|
||||
// a collection of activity results.
|
||||
$serializer = new ActivitySerializer(['sender', 'post', 'post.discussion', 'post.user', 'post.discussion.startUser', 'post.discussion.lastUser'], ['user']);
|
||||
$document = $this->document()->setPrimaryElement($serializer->collection($activity));
|
||||
|
||||
return $this->respondWithDocument($document);
|
||||
}
|
||||
}
|
|
@ -5,12 +5,12 @@ use Flarum\Core\Repositories\DiscussionRepositoryInterface as DiscussionReposito
|
|||
use Flarum\Core\Repositories\PostRepositoryInterface as PostRepository;
|
||||
use Flarum\Api\Actions\BaseAction;
|
||||
use Flarum\Api\Actions\ApiParams;
|
||||
use Flarum\Api\Actions\Posts\GetsPostsForDiscussion;
|
||||
use Flarum\Api\Actions\Posts\GetsPosts;
|
||||
use Flarum\Api\Serializers\DiscussionSerializer;
|
||||
|
||||
class ShowAction extends BaseAction
|
||||
{
|
||||
use GetsPostsForDiscussion;
|
||||
use GetsPosts;
|
||||
|
||||
/**
|
||||
* The discussion repository.
|
||||
|
@ -51,7 +51,7 @@ class ShowAction extends BaseAction
|
|||
|
||||
if (in_array('posts', $include)) {
|
||||
$relations = ['user', 'user.groups', 'editUser', 'hideUser'];
|
||||
$discussion->posts = $this->getPostsForDiscussion($params, $discussion->id)->load($relations);
|
||||
$discussion->posts = $this->getPosts($params, ['discussion_id' => $discussion->id])->load($relations);
|
||||
|
||||
$include = array_merge($include, array_map(function ($relation) {
|
||||
return 'posts.'.$relation;
|
||||
|
|
|
@ -3,23 +3,23 @@
|
|||
use Flarum\Core\Models\User;
|
||||
use Flarum\Api\Actions\ApiParams;
|
||||
|
||||
trait GetsPostsForDiscussion
|
||||
trait GetsPosts
|
||||
{
|
||||
protected function getPostsForDiscussion(ApiParams $params, $discussionId)
|
||||
protected function getPosts(ApiParams $params, $where)
|
||||
{
|
||||
$sort = $params->sort(['time']);
|
||||
$count = $params->count(20, 50);
|
||||
$user = $this->actor->getUser();
|
||||
|
||||
if (($near = $params->get('near')) > 1) {
|
||||
$start = $this->posts->getIndexForNumber($discussionId, $near, $user);
|
||||
if (isset($where['discussion_id']) && ($near = $params->get('near')) > 1) {
|
||||
$start = $this->posts->getIndexForNumber($where['discussion_id'], $near, $user);
|
||||
$start = max(0, $start - $count / 2);
|
||||
} else {
|
||||
$start = 0;
|
||||
}
|
||||
|
||||
return $this->posts->findByDiscussion(
|
||||
$discussionId,
|
||||
return $this->posts->findWhere(
|
||||
$where,
|
||||
$user,
|
||||
$sort['field'],
|
||||
$sort['order'] ?: 'asc',
|
|
@ -9,7 +9,7 @@ use Flarum\Api\Serializers\PostSerializer;
|
|||
|
||||
class IndexAction extends BaseAction
|
||||
{
|
||||
use GetsPostsForDiscussion;
|
||||
use GetsPosts;
|
||||
|
||||
/**
|
||||
* The post repository.
|
||||
|
@ -37,14 +37,19 @@ class IndexAction extends BaseAction
|
|||
protected function run(ApiParams $params)
|
||||
{
|
||||
$postIds = (array) $params->get('ids');
|
||||
$include = ['user', 'user.groups', 'editUser', 'hideUser'];
|
||||
$include = ['user', 'user.groups', 'editUser', 'hideUser', 'discussion'];
|
||||
$user = $this->actor->getUser();
|
||||
|
||||
if (count($postIds)) {
|
||||
$posts = $this->posts->findByIds($postIds, $user);
|
||||
} else {
|
||||
$discussionId = $params->get('discussions');
|
||||
$posts = $this->getPostsForDiscussion($params, $discussionId, $user);
|
||||
if ($discussionId = $params->get('discussions')) {
|
||||
$where['discussion_id'] = $discussionId;
|
||||
}
|
||||
if ($userId = $params->get('users')) {
|
||||
$where['user_id'] = $userId;
|
||||
}
|
||||
$posts = $this->getPosts($params, $where, $user);
|
||||
}
|
||||
|
||||
if (! count($posts)) {
|
||||
|
|
|
@ -1,19 +1,65 @@
|
|||
<?php namespace Flarum\Api\Serializers;
|
||||
|
||||
use Flarum\Core\Models\Activity;
|
||||
use Event;
|
||||
|
||||
class ActivitySerializer extends BaseSerializer {
|
||||
|
||||
public function serialize(Activity $activity)
|
||||
{
|
||||
$serialized = [
|
||||
'id' => (int) $activity->id
|
||||
];
|
||||
class ActivitySerializer extends BaseSerializer
|
||||
{
|
||||
/**
|
||||
* The resource type.
|
||||
* @var string
|
||||
*/
|
||||
protected $type = 'activity';
|
||||
|
||||
Event::fire('flarum.api.serialize.activity', [&$serialized]);
|
||||
/**
|
||||
* Serialize attributes of an Activity model for JSON output.
|
||||
*
|
||||
* @param Activity $activity The Activity model to serialize.
|
||||
* @return array
|
||||
*/
|
||||
protected function attributes(Activity $activity)
|
||||
{
|
||||
$attributes = [
|
||||
'id' => ((int) $activity->id) ?: str_random(5),
|
||||
'type' => $activity->type,
|
||||
'content' => json_encode($activity->data),
|
||||
'time' => $activity->time->toRFC3339String()
|
||||
];
|
||||
|
||||
return $serialized;
|
||||
}
|
||||
return $this->attributesEvent($activity, $attributes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a resource containing an activity's sender.
|
||||
*
|
||||
* @param Activity $activity
|
||||
* @return Tobscure\JsonApi\Resource
|
||||
*/
|
||||
public function linkUser(Activity $activity)
|
||||
{
|
||||
return (new UserBasicSerializer)->resource($activity->user_id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a resource containing an activity's sender.
|
||||
*
|
||||
* @param Activity $activity
|
||||
* @param array $relations
|
||||
* @return Tobscure\JsonApi\Resource
|
||||
*/
|
||||
public function includeSender(Activity $activity, $relations)
|
||||
{
|
||||
return (new UserBasicSerializer($relations))->resource($activity->sender);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a resource containing an activity's sender.
|
||||
*
|
||||
* @param Activity $activity
|
||||
* @param array $relations
|
||||
* @return Tobscure\JsonApi\Resource
|
||||
*/
|
||||
public function includePost(Activity $activity, $relations)
|
||||
{
|
||||
return (new PostSerializer($relations))->resource($activity->post);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -79,7 +79,7 @@ class PostSerializer extends PostBasicSerializer
|
|||
*/
|
||||
public function includeDiscussion(Post $post, $relations = [])
|
||||
{
|
||||
return (new DiscussionBasicSerializer($relations))->resource($post->discussion);
|
||||
return (new DiscussionSerializer($relations))->resource($post->discussion);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -73,6 +73,14 @@ class CoreServiceProvider extends ServiceProvider
|
|||
'Flarum\Core\Repositories\UserRepositoryInterface',
|
||||
'Flarum\Core\Repositories\EloquentUserRepository'
|
||||
);
|
||||
$this->app->bind(
|
||||
'Flarum\Core\Repositories\ActivityRepositoryInterface',
|
||||
'Flarum\Core\Repositories\EloquentActivityRepository'
|
||||
);
|
||||
$this->app->bind(
|
||||
'Flarum\Core\Repositories\NotificationRepositoryInterface',
|
||||
'Flarum\Core\Repositories\EloquentNotificationRepository'
|
||||
);
|
||||
}
|
||||
|
||||
public function registerGambits()
|
||||
|
|
|
@ -1,36 +1,69 @@
|
|||
<?php namespace Flarum\Core\Activity;
|
||||
<?php namespace Flarum\Core\Models;
|
||||
|
||||
use Flarum\Core\Entity;
|
||||
use Illuminate\Support\Str;
|
||||
use Auth;
|
||||
class Activity extends Model
|
||||
{
|
||||
/**
|
||||
* The table associated with the model.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $table = 'activity';
|
||||
|
||||
class Activity extends Entity {
|
||||
/**
|
||||
* The attributes that should be mutated to dates.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $dates = ['time'];
|
||||
|
||||
protected $table = 'activity';
|
||||
/**
|
||||
* Unserialize the data attribute.
|
||||
*
|
||||
* @param string $value
|
||||
* @return string
|
||||
*/
|
||||
public function getDataAttribute($value)
|
||||
{
|
||||
return json_decode($value);
|
||||
}
|
||||
|
||||
public function getDates()
|
||||
{
|
||||
return ['time'];
|
||||
}
|
||||
/**
|
||||
* Serialize the data attribute.
|
||||
*
|
||||
* @param string $value
|
||||
*/
|
||||
public function setDataAttribute($value)
|
||||
{
|
||||
$this->attributes['data'] = json_encode($value);
|
||||
}
|
||||
|
||||
public function fromUser()
|
||||
{
|
||||
return $this->belongsTo('Flarum\Core\Models\User', 'from_user_id');
|
||||
}
|
||||
/**
|
||||
* Define the relationship with the activity's recipient.
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||||
*/
|
||||
public function user()
|
||||
{
|
||||
return $this->belongsTo('Flarum\Core\Models\User', 'user_id');
|
||||
}
|
||||
|
||||
public function permission($permission)
|
||||
{
|
||||
return User::current()->can($permission, 'activity', $this);
|
||||
}
|
||||
|
||||
public function editable()
|
||||
{
|
||||
return $this->permission('edit');
|
||||
}
|
||||
|
||||
public function deletable()
|
||||
{
|
||||
return $this->permission('delete');
|
||||
}
|
||||
/**
|
||||
* Define the relationship with the activity's sender.
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||||
*/
|
||||
public function sender()
|
||||
{
|
||||
return $this->belongsTo('Flarum\Core\Models\User', 'sender_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* Define the relationship with the activity's sender.
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||||
*/
|
||||
public function post()
|
||||
{
|
||||
return $this->belongsTo('Flarum\Core\Models\Post', 'post_id');
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,8 @@
|
|||
<?php namespace Flarum\Core\Repositories;
|
||||
|
||||
use Flarum\Core\Models\User;
|
||||
|
||||
interface ActivityRepositoryInterface
|
||||
{
|
||||
public function findByUser($userId, User $user, $count = null, $start = 0, $type = null);
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
<?php namespace Flarum\Core\Repositories;
|
||||
|
||||
use Flarum\Core\Models\Activity;
|
||||
use Flarum\Core\Models\Post;
|
||||
use Flarum\Core\Models\User;
|
||||
|
||||
class EloquentActivityRepository implements ActivityRepositoryInterface
|
||||
{
|
||||
public function findByUser($userId, User $viewer, $count = null, $start = 0, $type = null)
|
||||
{
|
||||
// This is all very rough and needs to be cleaned up
|
||||
|
||||
$null = \DB::raw('NULL');
|
||||
$query = Activity::with('sender')->select('id', 'user_id', 'sender_id', 'type', 'data', 'time', \DB::raw('NULL as post_id'))->where('user_id', $userId);
|
||||
|
||||
if ($type) {
|
||||
$query->where('type', $type);
|
||||
}
|
||||
|
||||
$posts = Post::whereCan($viewer, 'view')->with('post', 'post.discussion', 'post.user', 'post.discussion.startUser', 'post.discussion.lastUser')->select(\DB::raw("CONCAT('post', id)"), 'user_id', $null, \DB::raw("'post'"), $null, 'time', 'id')->where('user_id', $userId);
|
||||
|
||||
if ($type === 'post') {
|
||||
$posts->where('number', '>', 1);
|
||||
} elseif ($type === 'discussion') {
|
||||
$posts->where('number', 1);
|
||||
}
|
||||
|
||||
if (!$type) {
|
||||
$join = User::select(\DB::raw("CONCAT('join', id)"), 'id', 'id', \DB::raw("'join'"), $null, 'join_time', $null)->where('id', $userId);
|
||||
$query->union($join->getQuery());
|
||||
}
|
||||
|
||||
return $query->union($posts->getQuery())
|
||||
->orderBy('time', 'desc')
|
||||
->skip($start)
|
||||
->take($count)
|
||||
->get();
|
||||
}
|
||||
}
|
|
@ -24,10 +24,10 @@ class EloquentPostRepository implements PostRepositoryInterface
|
|||
}
|
||||
|
||||
/**
|
||||
* Find posts in a discussion, optionally making sure they are visible to
|
||||
* a certain user, and/or using other criteria.
|
||||
* Find posts that match certain conditions, optionally making sure they
|
||||
* are visible to a certain user, and/or using other criteria.
|
||||
*
|
||||
* @param integer $discussionId
|
||||
* @param array $where
|
||||
* @param \Flarum\Core\Models\User|null $user
|
||||
* @param string $sort
|
||||
* @param string $order
|
||||
|
@ -35,9 +35,9 @@ class EloquentPostRepository implements PostRepositoryInterface
|
|||
* @param integer $start
|
||||
* @return \Illuminate\Database\Eloquent\Collection
|
||||
*/
|
||||
public function findByDiscussion($discussionId, User $user = null, $sort = 'time', $order = 'asc', $count = null, $start = 0)
|
||||
public function findWhere($where = [], User $user = null, $sort = 'time', $order = 'asc', $count = null, $start = 0)
|
||||
{
|
||||
$query = Post::where('discussion_id', $discussionId)
|
||||
$query = Post::where($where)
|
||||
->orderBy($sort, $order)
|
||||
->skip($start)
|
||||
->take($count);
|
||||
|
|
|
@ -17,10 +17,10 @@ interface PostRepositoryInterface
|
|||
public function findOrFail($id, User $user = null);
|
||||
|
||||
/**
|
||||
* Find posts in a discussion, optionally making sure they are visible to
|
||||
* a certain user, and/or using other criteria.
|
||||
* Find posts that match certain conditions, optionally making sure they
|
||||
* are visible to a certain user, and/or using other criteria.
|
||||
*
|
||||
* @param integer $discussionId
|
||||
* @param array $where
|
||||
* @param \Flarum\Core\Models\User|null $user
|
||||
* @param string $sort
|
||||
* @param string $order
|
||||
|
@ -28,7 +28,7 @@ interface PostRepositoryInterface
|
|||
* @param integer $start
|
||||
* @return \Illuminate\Database\Eloquent\Collection
|
||||
*/
|
||||
public function findByDiscussion($discussionId, User $user = null, $sort = 'time', $order = 'asc', $count = null, $start = 0);
|
||||
public function findWhere($where = [], User $user = null, $sort = 'time', $order = 'asc', $count = null, $start = 0);
|
||||
|
||||
/**
|
||||
* Find posts by their IDs, optionally making sure they are visible to a
|
||||
|
|
Loading…
Reference in New Issue
Block a user