-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
extend.php
93 lines (80 loc) · 4.41 KB
/
extend.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
use Flarum\Api\Controller\ListDiscussionsController;
use Flarum\Api\Controller\ShowDiscussionController;
use Flarum\Api\Serializer\DiscussionSerializer;
use Flarum\Database\AbstractModel;
use Flarum\Discussion\Discussion;
use Flarum\Discussion\Event\Saving;
use Flarum\Extend\ApiController;
use Flarum\Extend\ApiSerializer;
use Flarum\Extend\Conditional;
use Flarum\Extend\Event;
use FoF\MergeDiscussions\Events\MergingDiscussions;
use Michaelbelgium\Discussionviews\Listeners;
use Flarum\Extend\Locales;
use Flarum\Extend\Frontend;
use Flarum\Extend\Model;
use Flarum\Extend\Settings;
use Flarum\Settings\SettingsRepositoryInterface;
use Michaelbelgium\Discussionviews\Models\DiscussionView;
use Michaelbelgium\Discussionviews\Serializers\DiscussionViewSerializer;
$settings = resolve(SettingsRepositoryInterface::class);
const DV_RELATIONSHIP_UNIQUE = 'uniqueViews'; //discussion->uniqueViews()
const DV_RELATIONSHIP = 'views'; //$discussion->views()
const DV_RELATIONSHIP_LATEST = 'latestViews';
return [
(new Frontend('forum'))
->css(__DIR__ . '/less/extension.less')
->js(__DIR__. '/js/dist/forum.js'),
(new Frontend('admin'))
->js(__DIR__ . '/js/dist/admin.js'),
new Locales(__DIR__ . '/locale'),
(new Model(Discussion::class))
->relationship(DV_RELATIONSHIP, function (AbstractModel $model) {
return $model->hasMany(DiscussionView::class)->orderBy('visited_at', 'DESC');
})->relationship(DV_RELATIONSHIP_LATEST, function (AbstractModel $model) use ($settings) {
return $model->views()->limit($settings->get('michaelbelgium-discussionviews.max_listcount', 5));
})->relationship(DV_RELATIONSHIP_UNIQUE, function (AbstractModel $model) use ($settings) {
return $model->hasMany(DiscussionView::class)->groupBy('user_id')->havingRaw('user_id IS NOT NULL')->orderByRaw('MAX(visited_at) DESC')
->limit($settings->get('michaelbelgium-discussionviews.max_listcount', 5));
}),
(new Settings)
->default('michaelbelgium-discussionviews.show_footer_viewlist', false)
->default('michaelbelgium-discussionviews.track_guests', true)
->default('michaelbelgium-discussionviews.max_listcount', 5)
->default('michaelbelgium-discussionviews.show_viewlist', true)
->default('michaelbelgium-discussionviews.track_unique', false)
->default('michaelbelgium-discussionviews.show_filter', true)
->default('michaelbelgium-discussionviews.abbr_numbers', false)
->default('michaelbelgium-discussionviews.ignore_crawlers', false)
->serializeToForum('toggleFilter', 'michaelbelgium-discussionviews.show_filter', null, true)
->serializeToForum('abbrNumber', 'michaelbelgium-discussionviews.abbr_numbers', null, false)
->serializeToForum('showViewList', 'michaelbelgium-discussionviews.show_viewlist', null, true)
->serializeToForum('showFooterViewList', 'michaelbelgium-discussionviews.show_footer_viewlist', null, false),
(new ApiSerializer(DiscussionSerializer::class))
->attribute('canReset', function (DiscussionSerializer $serializer, $discussion) {
return (bool)$serializer->getActor()->can('resetViews', $discussion);
})->attribute('viewCount', function (DiscussionSerializer $serializer, $discussion) {
return $discussion->view_count;
})->attribute('canViewNumber', function (DiscussionSerializer $serializer, $discussion) {
return (bool)$serializer->getActor()->can('readViewnumber', $discussion);
})->hasMany(DV_RELATIONSHIP_LATEST, DiscussionViewSerializer::class)
->hasMany(DV_RELATIONSHIP_UNIQUE, DiscussionViewSerializer::class),
(new ApiController(ShowDiscussionController::class))
->prepareDataForSerialization(Listeners\AddDiscussionViewHandler::class)
->addInclude([
DV_RELATIONSHIP_UNIQUE, DV_RELATIONSHIP_UNIQUE . '.user',
DV_RELATIONSHIP_LATEST, DV_RELATIONSHIP_LATEST . '.user'
]),
(new ApiController(ListDiscussionsController::class))
->addSortField('view_count'),
(new Event())
->listen(Saving::class, Listeners\SaveDiscussionFromModal::class),
(new Conditional())
->whenExtensionEnabled('fof-merge-discussions', function() {
return [
(new Event())
->listen(MergingDiscussions::class, Listeners\MergeDiscussionHandler::class),
];
})
];