marshmallow/nova-activity 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

marshmallow/nova-activity

Composer 安装命令:

composer require marshmallow/nova-activity

包简介

A Laravel Nova field.

关键字:

README 文档

README

alt text

Nova Activity

Latest Version on Packagist Total Downloads

A Laravel Nova field that adds a timeline of activities, comments and quick replies to a resource. Activities are stored as a polymorphic relation on your own models, support mentions, file uploads, custom actions and emit events for every interaction.

Installation

Install the package via Composer:

composer require marshmallow/nova-activity

Publish and run the migrations:

php artisan vendor:publish --tag="nova-activity-migrations"
php artisan migrate

Publish the config file:

php artisan vendor:publish --tag="nova-activity-config"

Optionally publish the translations:

php artisan vendor:publish --tag="nova-activity-translations"

Setup

Add the NovaActivities trait to any model that should record activities:

use Marshmallow\NovaActivity\Traits\NovaActivities;

class Order extends Model
{
    use NovaActivities;
}

Add the Activity field to the corresponding Nova resource:

use Marshmallow\NovaActivity\Activity;

public function fields(NovaRequest $request)
{
    return [
        Activity::make(__('Activity'))
            ->types(function () {
                return [
                    1 => 'Called the customer and spoke to them.',
                    2 => 'Customer will send additional information.',
                ];
            })
            ->showOnPreview(),
    ];
}

When the Activity field is shown on the index view, add the following to the resource so the row click action does not interfere with the field:

public static $clickAction = 'ignore';

Configuration

The published config/nova-activity.php file exposes the following options:

Key Default Description
table_name nova_activity Table used to store activities.
use_quick_replies true Enable the quick-reply (reaction) buttons.
use_comments true Allow writing comments on activities.
use_file_uploads false Allow attaching file uploads to activities.
dates.format d M, Y \a\t H:i PHP date format.
dates.js_format Do MMM, YYYY moment.js date format used in the field UI.
dates.use_human_readable true Render dates as human-readable, relative strings.
comment_validation.type required Validation rule applied to the comment input.
quick_replies array of reactions The available quick replies (name, background, icon, solid_icon).

Each quick reply is defined as:

'quick_replies' => [
    'exited' => [
        'name' => 'Exited',
        'background' => '#ef4444',
        'icon' => 'fire',
        'solid_icon' => false,
    ],
    // ...
],

Usage

Adding activities from code

The NovaActivities trait adds an addActivity() method to your model:

use Carbon\Carbon;

$model->addActivity(
    user_id: $request->user()->id,
    type: $request->type,
    label: $request->type_label,
    comment: $request->comment,
    created_at: Carbon::parse($request->date),
    quick_replies: [
        'user_1' => 'sad',
    ],
    mentions: [
        [
            'key' => 1,
            'class' => User::class,
        ],
    ],
);

All arguments are optional; only the ones you pass are stored.

Field options

The Activity field exposes a fluent API. All of the methods below return the field instance so they can be chained:

Activity::make(__('Activity'))
    ->activityTitle('Activity title')   // pass null to hide the title
    ->useComments(true)
    ->useQuickReplies(true)
    ->useFileUploads(true)
    ->useHumanReadableDates(true)
    ->alwaysShowComments()
    ->truncateComments(100)
    ->jsDateFormat('Do MMM, YYYY')       // moment.js format
    ->setLocale('nl');

setLocale() also accepts a closure:

->setLocale(function () {
    return auth()->user()->locale;
})

Limiting the number of activities shown

->limit(3)            // limit on index, detail and forms
->limitOnDetail(null) // null shows all
->limitOnIndex(3)
->limitOnForms(10)

Types

types() accepts an array or a closure mapping a value to a label:

->types(function () {
    return [
        2 => 'Called the customer and spoke to them.',
    ];
})

Mentions

Provide the list of mentionable users with mentions():

->mentions(function (): array {
    return User::get()->map(function ($user) {
        return [
            'model' => $user,
            'value' => str_slug($user->name),
            'avatar_url' => Activity::getUserAvatar($user),
            'key' => $user->name,
        ];
    })->toArray();
})

On a stored activity you can inspect the mentions:

/** Check if this activity has any mentions. */
$activity->hasMentions();

/** Get a collection of all the classes that were mentioned. */
$activity->getMentions();

Avatars

Activity::getUserAvatar($user) falls back to a Gravatar based on the user's email. To use a custom avatar, add an avatarPath() method to your user model:

// App\Models\User
public function avatarPath()
{
    return 'https://example.com/avatars/me.jpg';
}

Custom actions

Attach actions to the field with actions():

use Marshmallow\NovaActivity\Actions\ToggleTagAction;

->actions([
    ToggleTagAction::make(__('Important'), 'important')
        ->color('red')
        ->backgroundColor('#ffffff')
        ->borderColor('red')
        ->icon('fire'),
])

Events

Every interaction dispatches an event. Listen for them in your EventServiceProvider:

use App\Listeners\DoWhatEver;
use Marshmallow\NovaActivity\Events\ActivityCreated;

protected $listen = [
    ActivityCreated::class => [
        DoWhatEver::class,
    ],
];

The available events are:

Event Public properties
ActivityCreated $activity
ActivityDeleted $activity, $user
ActivityPinned $activity, $user
ActivityUnpinned $activity, $user
ActivityCommentHidden $activity, $user
ActivityCommentShow $activity, $user
QuickReplyChanged $activity, $user

Inside a listener you can reach the activity and the model it belongs to:

class DoWhatEver
{
    public function handle($event)
    {
        /** The model on which the activity was created. */
        $model = $event->activity->nova_activity;

        /** The activity itself. */
        $activity = $event->activity;

        /** Mentions. */
        $activity->hasMentions();
        $activity->getMentions();
    }
}

Credits

Security Vulnerabilities

Please report security vulnerabilities by email rather than via the public issue tracker.

License

The MIT License (MIT). Please see the License File for more information.

marshmallow/nova-activity 适用场景与选型建议

marshmallow/nova-activity 是一款 基于 Vue 开发的 Composer 扩展包,目前已累计 2.04k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 12 月 07 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「nova」 「laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 marshmallow/nova-activity 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 marshmallow/nova-activity 我们能提供哪些服务?
定制开发 / 二次开发

基于 marshmallow/nova-activity 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 2.04k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 12
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 2
  • Forks: 0
  • 开发语言: Vue

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-12-07