1
0
mirror of https://github.com/flarum/core.git synced 2025-08-12 11:24:30 +02:00
This commit is contained in:
Daniël Klabbers
2022-06-28 08:53:09 +02:00
parent bbf90e42ff
commit f034fdd9f2
7 changed files with 1451 additions and 2325 deletions

View File

@@ -7,14 +7,12 @@
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Likes;
use Flarum\Api\Controller;
use Flarum\Api\Serializer\BasicUserSerializer;
use Flarum\Api\Serializer\PostSerializer;
use Flarum\Extend;
use Flarum\Likes\Event\PostWasLiked;
use Flarum\Likes\Event\PostWasUnliked;
use Flarum\Likes\Listener;
use Flarum\Likes\Notification\PostLikedBlueprint;
use Flarum\Post\Event\Deleted;
use Flarum\Post\Event\Saving;
use Flarum\Post\Post;
@@ -29,34 +27,45 @@ return [
->js(__DIR__.'/js/dist/admin.js'),
(new Extend\Model(Post::class))
->relationship('recentLikes', Model\RecentLikesRelationship::class)
->belongsToMany('likes', User::class, 'post_likes', 'post_id', 'user_id'),
new Extend\Locales(__DIR__.'/locale'),
(new Extend\Notification())
->type(PostLikedBlueprint::class, PostSerializer::class, ['alert']),
->type(Notification\PostLikedBlueprint::class, PostSerializer::class, ['alert']),
(new Extend\ApiSerializer(PostSerializer::class))
->relationship('recentLikes', Api\RecentLikesRelationship::class)
->attributes(function (PostSerializer $serializer, $model, $attributes) {
if ($model->likes_count) $attributes['likesCount'] = $model->likes_count;
return $attributes;
})
->hasMany('likes', BasicUserSerializer::class)
->attribute('canLike', function (PostSerializer $serializer, $model) {
return (bool) $serializer->getActor()->can('like', $model);
return $serializer->getActor()->can('like', $model);
}),
(new Extend\ApiController(Controller\ShowDiscussionController::class))
->addInclude('posts.likes'),
->addOptionalInclude('posts.likes')
->addInclude('posts.recentLikes'),
(new Extend\ApiController(Controller\ListPostsController::class))
->addInclude('likes'),
->addOptionalInclude('likes')
->addInclude('recentLikes'),
(new Extend\ApiController(Controller\ShowPostController::class))
->addInclude('likes'),
->addOptionalInclude('likes')
->addInclude('recentLikes'),
(new Extend\ApiController(Controller\CreatePostController::class))
->addInclude('likes'),
->addOptionalInclude('likes')
->addInclude('recentLikes'),
(new Extend\ApiController(Controller\UpdatePostController::class))
->addInclude('likes'),
->addOptionalInclude('likes')
->addInclude('recentLikes'),
(new Extend\Event())
->listen(PostWasLiked::class, Listener\SendNotificationWhenPostIsLiked::class)
->listen(PostWasUnliked::class, Listener\SendNotificationWhenPostIsUnliked::class)
->listen(Event\PostWasLiked::class, Listener\SendNotificationWhenPostIsLiked::class)
->listen(Event\PostWasUnliked::class, Listener\SendNotificationWhenPostIsUnliked::class)
->listen(Deleted::class, [Listener\SaveLikesToDatabase::class, 'whenPostIsDeleted'])
->listen(Saving::class, [Listener\SaveLikesToDatabase::class, 'whenPostIsSaving']),
];

2
extensions/likes/js/dist/forum.js generated vendored
View File

@@ -1,2 +1,2 @@
(()=>{var t={n:o=>{var n=o&&o.__esModule?()=>o.default:()=>o;return t.d(n,{a:n}),n},d:(o,n)=>{for(var e in n)t.o(n,e)&&!t.o(o,e)&&Object.defineProperty(o,e,{enumerable:!0,get:n[e]})},o:(t,o)=>Object.prototype.hasOwnProperty.call(t,o),r:t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})}},o={};(()=>{"use strict";t.r(o);const n=flarum.core.compat["common/extend"],e=flarum.core.compat["forum/app"];var r=t.n(e);const s=flarum.core.compat["common/models/Post"];var a=t.n(s);const i=flarum.core.compat["common/Model"];var c=t.n(i);const u=flarum.core.compat["forum/components/NotificationGrid"];var l=t.n(u);const f=flarum.core.compat["common/components/Button"];var p=t.n(f);const d=flarum.core.compat["forum/components/CommentPost"];var k=t.n(d);const h=flarum.core.compat["common/components/Link"];var v=t.n(h);const y=flarum.core.compat["common/helpers/punctuateSeries"];var _=t.n(y);const b=flarum.core.compat["common/helpers/username"];var g=t.n(b);const P=flarum.core.compat["common/helpers/icon"];var L=t.n(P);function M(t,o){return M=Object.setPrototypeOf||function(t,o){return t.__proto__=o,t},M(t,o)}function x(t,o){t.prototype=Object.create(o.prototype),t.prototype.constructor=t,M(t,o)}const j=flarum.core.compat["common/components/Modal"];var O=t.n(j);const N=flarum.core.compat["common/helpers/avatar"];var S=t.n(N),B=function(t){function o(){return t.apply(this,arguments)||this}x(o,t);var n=o.prototype;return n.className=function(){return"PostLikesModal Modal--small"},n.title=function(){return r().translator.trans("flarum-likes.forum.post_likes.title")},n.content=function(){return m("div",{className:"Modal-body"},m("ul",{className:"PostLikesModal-list"},this.attrs.post.likes().map((function(t){return m("li",null,m(v(),{href:r().route.user(t)},S()(t)," ",g()(t)))}))))},o}(O());const T=flarum.core.compat["forum/components/Notification"];var w=t.n(T);const C=flarum.core.compat["common/utils/string"];var I=function(t){function o(){return t.apply(this,arguments)||this}x(o,t);var n=o.prototype;return n.icon=function(){return"far fa-thumbs-up"},n.href=function(){return r().route.post(this.attrs.notification.subject())},n.content=function(){var t=this.attrs.notification.fromUser();return r().translator.trans("flarum-likes.forum.notifications.post_liked_text",{user:t,count:1})},n.excerpt=function(){return(0,C.truncate)(this.attrs.notification.subject().contentPlain(),200)},o}(w());r().initializers.add("flarum-likes",(function(){r().notificationComponents.postLiked=I,a().prototype.canLike=c().attribute("canLike"),a().prototype.likes=c().hasMany("likes"),(0,n.extend)(k().prototype,"actionItems",(function(t){var o=this.attrs.post;if(!o.isHidden()&&o.canLike()){var n=o.likes(),e=r().session.user&&n&&n.some((function(t){return t===r().session.user}));t.add("like",p().component({className:"Button Button--link",onclick:function(){e=!e,o.save({isLiked:e});var t=o.data.relationships.likes.data;t.some((function(o,n){if(o.id===r().session.user.id())return t.splice(n,1),!0})),e&&t.unshift({type:"users",id:r().session.user.id()})}},r().translator.trans(e?"flarum-likes.forum.post.unlike_link":"flarum-likes.forum.post.like_link")))}})),(0,n.extend)(k().prototype,"footerItems",(function(t){var o=this.attrs.post,n=o.likes();if(n&&n.length){var e=n.length>4,s=n.sort((function(t){return t===r().session.user?-1:1})).slice(0,e?3:4).map((function(t){return m(v(),{href:r().route.user(t)},t===r().session.user?r().translator.trans("flarum-likes.forum.post.you_text"):g()(t))}));if(e){var a=n.length-s.length;s.push(m("a",{href:"#",onclick:function(t){t.preventDefault(),r().modal.show(B,{post:o})}},r().translator.trans("flarum-likes.forum.post.others_link",{count:a})))}t.add("liked",m("div",{className:"Post-likedBy"},L()("far fa-thumbs-up"),r().translator.trans("flarum-likes.forum.post.liked_by"+(n[0]===r().session.user?"_self":"")+"_text",{count:s.length,users:_()(s)})))}})),(0,n.extend)(l().prototype,"notificationTypes",(function(t){t.add("postLiked",{name:"postLiked",icon:"far fa-thumbs-up",label:r().translator.trans("flarum-likes.forum.settings.notify_post_liked_label")})}))}))})(),module.exports=o})();
(()=>{var t={n:o=>{var e=o&&o.__esModule?()=>o.default:()=>o;return t.d(e,{a:e}),e},d:(o,e)=>{for(var n in e)t.o(e,n)&&!t.o(o,n)&&Object.defineProperty(o,n,{enumerable:!0,get:e[n]})},o:(t,o)=>Object.prototype.hasOwnProperty.call(t,o),r:t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})}},o={};(()=>{"use strict";t.r(o);const e=flarum.core.compat["common/extend"],n=flarum.core.compat["forum/app"];var r=t.n(n);const s=flarum.core.compat["common/models/Post"];var a=t.n(s);const i=flarum.core.compat["common/Model"];var c=t.n(i);const u=flarum.core.compat["forum/components/NotificationGrid"];var l=t.n(u);const f=flarum.core.compat["common/components/Button"];var p=t.n(f);const d=flarum.core.compat["forum/components/CommentPost"];var k=t.n(d);const h=flarum.core.compat["common/components/Link"];var v=t.n(h);const y=flarum.core.compat["common/helpers/punctuateSeries"];var b=t.n(y);const _=flarum.core.compat["common/helpers/username"];var g=t.n(_);const L=flarum.core.compat["common/helpers/icon"];var M=t.n(L);function P(t,o){return P=Object.setPrototypeOf||function(t,o){return t.__proto__=o,t},P(t,o)}function x(t,o){t.prototype=Object.create(o.prototype),t.prototype.constructor=t,P(t,o)}const j=flarum.core.compat["common/components/Modal"];var O=t.n(j);const N=flarum.core.compat["common/helpers/avatar"];var S=t.n(N),B=function(t){function o(){return t.apply(this,arguments)||this}x(o,t);var e=o.prototype;return e.className=function(){return"PostLikesModal Modal--small"},e.title=function(){return r().translator.trans("flarum-likes.forum.post_likes.title")},e.content=function(){return m("div",{className:"Modal-body"},m("ul",{className:"PostLikesModal-list"},this.attrs.post.likes().map((function(t){return m("li",null,m(v(),{href:r().route.user(t)},S()(t)," ",g()(t)))}))))},o}(O());const C=flarum.core.compat["forum/components/Notification"];var T=t.n(C);const w=flarum.core.compat["common/utils/string"];var I=function(t){function o(){return t.apply(this,arguments)||this}x(o,t);var e=o.prototype;return e.icon=function(){return"far fa-thumbs-up"},e.href=function(){return r().route.post(this.attrs.notification.subject())},e.content=function(){var t=this.attrs.notification.fromUser();return r().translator.trans("flarum-likes.forum.notifications.post_liked_text",{user:t,count:1})},e.excerpt=function(){return(0,w.truncate)(this.attrs.notification.subject().contentPlain(),200)},o}(T());r().initializers.add("flarum-likes",(function(){r().notificationComponents.postLiked=I,a().prototype.canLike=c().attribute("canLike"),a().prototype.likes=c().hasMany("likes"),a().prototype.likesCount=c().attribute("likesCount"),a().prototype.recentLikes=c().hasMany("recentLikes"),(0,e.extend)(k().prototype,"actionItems",(function(t){var o=this.attrs.post;if(!o.isHidden()&&o.canLike()){var e=o.likes(),n=r().session.user&&e&&e.some((function(t){return t===r().session.user}));t.add("like",p().component({className:"Button Button--link",onclick:function(){n=!n,o.save({isLiked:n});var t=o.data.relationships.likes.data;t.some((function(o,e){if(o.id===r().session.user.id())return t.splice(e,1),!0})),n&&t.unshift({type:"users",id:r().session.user.id()})}},r().translator.trans(n?"flarum-likes.forum.post.unlike_link":"flarum-likes.forum.post.like_link")))}})),(0,e.extend)(k().prototype,"footerItems",(function(t){var o=this.attrs.post,e=o.likes();if(e&&e.length){var n=e.length>4,s=e.sort((function(t){return t===r().session.user?-1:1})).slice(0,n?3:4).map((function(t){return m(v(),{href:r().route.user(t)},t===r().session.user?r().translator.trans("flarum-likes.forum.post.you_text"):g()(t))}));if(n){var a=e.length-s.length;s.push(m("a",{href:"#",onclick:function(t){t.preventDefault(),r().modal.show(B,{post:o})}},r().translator.trans("flarum-likes.forum.post.others_link",{count:a})))}t.add("liked",m("div",{className:"Post-likedBy"},M()("far fa-thumbs-up"),r().translator.trans("flarum-likes.forum.post.liked_by"+(e[0]===r().session.user?"_self":"")+"_text",{count:s.length,users:b()(s)})))}})),(0,e.extend)(l().prototype,"notificationTypes",(function(t){t.add("postLiked",{name:"postLiked",icon:"far fa-thumbs-up",label:r().translator.trans("flarum-likes.forum.settings.notify_post_liked_label")})}))}))})(),module.exports=o})();
//# sourceMappingURL=forum.js.map

File diff suppressed because one or more lines are too long

View File

@@ -14,6 +14,9 @@ app.initializers.add('flarum-likes', () => {
Post.prototype.canLike = Model.attribute('canLike');
Post.prototype.likes = Model.hasMany('likes');
Post.prototype.likesCount = Model.attribute('likesCount');
Post.prototype.recentLikes = Model.hasMany('recentLikes');
addLikeAction();
addLikesList();

View File

@@ -0,0 +1,27 @@
<?php
namespace Flarum\Likes\Api;
use Flarum\Api\Serializer\BasicUserSerializer;
use Flarum\Api\Serializer\PostSerializer;
use Flarum\Post\Post;
use Tobscure\JsonApi\Collection;
use Tobscure\JsonApi\Relationship;
class RecentLikesRelationship
{
private BasicUserSerializer $serializer;
public function __construct(BasicUserSerializer $serializer)
{
$this->serializer = $serializer;
}
public function __invoke(PostSerializer $serializer, Post $post): Relationship
{
return new Relationship(new Collection(
$post->recentLikes,
$this->serializer
));
}
}

View File

@@ -0,0 +1,24 @@
<?php
namespace Flarum\Likes\Model;
use Flarum\Post\Post;
use Flarum\User\User;
class RecentLikesRelationship
{
public function __invoke(Post $post)
{
return $post->belongsToMany(
User::class,
'post_likes',
'post_id',
'user_id'
)
->withCount([
'likes'
])
->limit(3)
->orderBy('post_likes.created_at');
}
}

3683
yarn.lock

File diff suppressed because it is too large Load Diff