-
Notifications
You must be signed in to change notification settings - Fork 13
feat: interaction activity tracking module #183
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
284a1fc
refactor(activity): restructure module into Message/Voice subdomains
danielhe4rt 3f14321
feat(activity): add Tracking subdomain with Interaction model
danielhe4rt 0dae364
feat(integration-devto): add new module for DevTo OAuth and article sync
danielhe4rt 621bea5
feat(identity): add DevTo to IdentityProvider enum
danielhe4rt 45f4bf3
feat(gamification): add HasInteractions trait to Character model
danielhe4rt 912a0ff
fix: linting
danielhe4rt c428109
fix: post-rebase cleanup for 4.x compatibility
danielhe4rt 6e1d0a1
fix: address PR #183 review feedback
danielhe4rt 8fa64fa
fix: PHPStan level 6 annotations and unused import
danielhe4rt caa4b9f
fix: rector
danielhe4rt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| return [ | ||
| 'classification' => [ | ||
| 'article' => ['tier' => 'high', 'coins_min' => 100, 'coins_max' => 300], | ||
| 'pr_merged' => ['tier' => 'high', 'coins_min' => 80, 'coins_max' => 250], | ||
| 'mentoring' => ['tier' => 'high', 'coins_min' => 50, 'coins_max' => 150], | ||
| 'squad_project' => ['tier' => 'high', 'coins_min' => 200, 'coins_max' => 500], | ||
| 'referral' => ['tier' => 'medium', 'coins_min' => 20, 'coins_max' => 30], | ||
| 'peer_review' => ['tier' => 'medium', 'coins_min' => 10, 'coins_max' => 25], | ||
| 'call_participation' => ['tier' => 'medium', 'coins_min' => 15, 'coins_max' => 30], | ||
| 'forum_debate' => ['tier' => 'medium', 'coins_min' => 10, 'coins_max' => 20], | ||
| 'content_share' => ['tier' => 'low', 'coins_min' => 5, 'coins_max' => 10], | ||
| 'engagement' => ['tier' => 'low', 'coins_min' => 1, 'coins_max' => 3], | ||
| 'repo_star' => ['tier' => 'low', 'coins_min' => 2, 'coins_max' => 2], | ||
| 'message' => ['tier' => 'low', 'coins_min' => 1, 'coins_max' => 2], | ||
| 'voice' => ['tier' => 'low', 'coins_min' => 1, 'coins_max' => 3], | ||
| ], | ||
|
|
||
| 'auto_approve_tiers' => ['low', 'medium'], | ||
|
|
||
| 'engagement_formula' => [ | ||
| 'reactions_multiplier' => 0.5, | ||
| 'reactions_cap' => 25, | ||
| 'bookmarks_multiplier' => 1.0, | ||
| 'bookmarks_cap' => 15, | ||
| 'comments_multiplier' => 2.0, | ||
| 'comments_cap' => 30, | ||
| ], | ||
|
|
||
| 'xp_multiplier' => 1, | ||
| ]; |
69 changes: 69 additions & 0 deletions
69
app-modules/activity/database/factories/InteractionFactory.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace He4rt\Activity\Database\Factories; | ||
|
|
||
| use He4rt\Activity\Tracking\Enums\ActivityStatus; | ||
| use He4rt\Activity\Tracking\Enums\ActivityType; | ||
| use He4rt\Activity\Tracking\Enums\ValueTier; | ||
| use He4rt\Activity\Tracking\Models\Interaction; | ||
| use He4rt\Gamification\Character\Models\Character; | ||
| use He4rt\Identity\ExternalIdentity\Enums\IdentityProvider; | ||
| use He4rt\Identity\Tenant\Models\Tenant; | ||
| use Illuminate\Database\Eloquent\Factories\Factory; | ||
|
|
||
| /** | ||
| * @extends Factory<Interaction> | ||
| */ | ||
| final class InteractionFactory extends Factory | ||
| { | ||
| protected $model = Interaction::class; | ||
|
|
||
| public function definition(): array | ||
| { | ||
| return [ | ||
| 'character_id' => Character::factory(), | ||
| 'tenant_id' => Tenant::factory(), | ||
| 'type' => ActivityType::Article, | ||
| 'provider' => IdentityProvider::DevTo, | ||
| 'value_tier' => ValueTier::High, | ||
| 'coins_min' => 100, | ||
| 'coins_max' => 300, | ||
| 'status' => ActivityStatus::Pending, | ||
| 'occurred_at' => now(), | ||
| ]; | ||
| } | ||
|
|
||
| public function autoApproved(): self | ||
| { | ||
| return $this->state([ | ||
| 'status' => ActivityStatus::AutoApproved, | ||
| 'type' => ActivityType::Engagement, | ||
| 'value_tier' => ValueTier::Low, | ||
| 'coins_min' => 1, | ||
| 'coins_max' => 3, | ||
| ]); | ||
| } | ||
|
|
||
| public function approved(): self | ||
| { | ||
| return $this->state([ | ||
| 'status' => ActivityStatus::Approved, | ||
| 'reviewed_at' => now(), | ||
| ]); | ||
| } | ||
|
|
||
| public function withEngagement(int $reactions = 0, int $comments = 0, int $bookmarks = 0): self | ||
| { | ||
| return $this->state([ | ||
| 'metadata' => [ | ||
| 'engagement_snapshot' => [ | ||
| 'reactions' => $reactions, | ||
| 'comments' => $comments, | ||
| 'bookmarks' => $bookmarks, | ||
| ], | ||
| ], | ||
| ]); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
app-modules/activity/database/migrations/2026_03_18_000000_create_interactions_table.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| use Illuminate\Database\Migrations\Migration; | ||
| use Illuminate\Database\Schema\Blueprint; | ||
| use Illuminate\Support\Facades\Schema; | ||
|
|
||
| return new class extends Migration | ||
| { | ||
| public function up(): void | ||
| { | ||
| Schema::create('interactions', function (Blueprint $table): void { | ||
| $table->uuid('id')->primary(); | ||
| $table->foreignUuid('character_id')->constrained('characters'); | ||
| $table->foreignId('tenant_id')->constrained('tenants'); | ||
| $table->string('type'); | ||
| $table->string('provider'); | ||
| $table->string('value_tier'); | ||
| $table->integer('coins_min'); | ||
| $table->integer('coins_max'); | ||
| $table->integer('coins_awarded')->nullable(); | ||
| $table->integer('xp_awarded')->nullable(); | ||
| $table->string('status')->default('pending'); | ||
| $table->nullableUuidMorphs('source'); | ||
| $table->string('external_ref')->nullable(); | ||
| $table->jsonb('metadata')->nullable(); | ||
| $table->timestamp('occurred_at'); | ||
| $table->timestamp('reviewed_at')->nullable(); | ||
| $table->timestamps(); | ||
|
|
||
| $table->index(['character_id', 'type', 'created_at'], 'idx_interactions_character_type'); | ||
| $table->index(['status', 'value_tier'], 'idx_interactions_status_tier'); | ||
| $table->index(['tenant_id', 'occurred_at'], 'idx_interactions_tenant'); | ||
| $table->unique(['tenant_id', 'provider', 'external_ref'], 'uniq_interactions_tenant_provider_ref'); | ||
| }); | ||
| } | ||
|
|
||
| public function down(): void | ||
| { | ||
| Schema::dropIfExists('interactions'); | ||
| } | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
app-modules/activity/src/Tracking/Actions/ApproveInteraction.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace He4rt\Activity\Tracking\Actions; | ||
|
|
||
| use He4rt\Activity\Tracking\Enums\ActivityStatus; | ||
| use He4rt\Activity\Tracking\Events\InteractionApproved; | ||
| use He4rt\Activity\Tracking\Models\Interaction; | ||
| use He4rt\Economy\Actions\Credit; | ||
| use He4rt\Economy\DTOs\CreditDTO; | ||
| use He4rt\Gamification\Character\Models\Character; | ||
| use Illuminate\Support\Facades\DB; | ||
|
|
||
| final readonly class ApproveInteraction | ||
| { | ||
| public function __construct( | ||
| private CalculateReward $calculateReward, | ||
| ) {} | ||
|
|
||
| public function handle(Interaction $interaction, ?int $peerReviewBase = null): Interaction | ||
| { | ||
| if ($interaction->status !== ActivityStatus::Pending) { | ||
| return $interaction; | ||
| } | ||
|
|
||
| return DB::transaction(function () use ($interaction, $peerReviewBase): Interaction { | ||
| $locked = Interaction::query() | ||
| ->where('id', $interaction->id) | ||
| ->where('status', ActivityStatus::Pending) | ||
| ->lockForUpdate() | ||
| ->first(); | ||
|
|
||
| if ($locked === null) { | ||
| return $interaction->fresh(); | ||
| } | ||
|
|
||
| $reward = $this->calculateReward->handle($locked, $peerReviewBase); | ||
|
|
||
| $character = Character::query()->findOrFail($locked->character_id); | ||
| $wallet = $character->getOrCreateWallet(); | ||
|
|
||
| resolve(Credit::class)->handle(new CreditDTO( | ||
| walletId: $wallet->id, | ||
| amount: $reward['coins_awarded'], | ||
| referenceType: Interaction::class, | ||
| referenceId: $locked->id, | ||
| description: 'Reward: '.$locked->type->value, | ||
| )); | ||
|
|
||
| $character->increment('experience', $reward['xp_awarded']); | ||
|
|
||
| $locked->update([ | ||
| 'status' => ActivityStatus::Approved, | ||
| 'reviewed_at' => now(), | ||
| ]); | ||
|
|
||
| event(new InteractionApproved($locked->fresh())); | ||
|
|
||
| return $locked->fresh(); | ||
| }); | ||
| } | ||
| } |
60 changes: 60 additions & 0 deletions
60
app-modules/activity/src/Tracking/Actions/CalculateReward.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace He4rt\Activity\Tracking\Actions; | ||
|
|
||
| use He4rt\Activity\Tracking\Models\Interaction; | ||
|
|
||
| final class CalculateReward | ||
| { | ||
| /** | ||
| * @return array{coins_awarded: int, xp_awarded: int} | ||
| */ | ||
| public function handle(Interaction $interaction, ?int $peerReviewBase = null): array | ||
| { | ||
| $engagementFormula = config('activity-tracking.engagement_formula'); | ||
| $xpMultiplier = config('activity-tracking.xp_multiplier', 1); | ||
|
|
||
| $metadata = $interaction->metadata ?? []; | ||
| $engagementSnapshot = $metadata['engagement_snapshot'] ?? null; | ||
|
|
||
| if ($engagementSnapshot !== null) { | ||
| $base = $peerReviewBase ?? (int) (($interaction->coins_min + $interaction->coins_max) / 2); | ||
|
|
||
| $reactionsBonus = min( | ||
| ($engagementSnapshot['reactions'] ?? 0) * $engagementFormula['reactions_multiplier'], | ||
| $engagementFormula['reactions_cap'] | ||
| ); | ||
|
|
||
| $bookmarksBonus = min( | ||
| ($engagementSnapshot['bookmarks'] ?? 0) * $engagementFormula['bookmarks_multiplier'], | ||
| $engagementFormula['bookmarks_cap'] | ||
| ); | ||
|
|
||
| $commentsBonus = min( | ||
| ($engagementSnapshot['comments'] ?? 0) * $engagementFormula['comments_multiplier'], | ||
| $engagementFormula['comments_cap'] | ||
| ); | ||
|
|
||
| $engagementBonus = (int) ($reactionsBonus + $bookmarksBonus + $commentsBonus); | ||
| $coinsAwarded = min($base + $engagementBonus, $interaction->coins_max); | ||
| } else { | ||
| $coinsAwarded = $peerReviewBase !== null | ||
| ? min($peerReviewBase, $interaction->coins_max) | ||
| : $interaction->coins_min; | ||
| } | ||
|
|
||
| $xpAwarded = (int) ($coinsAwarded * $xpMultiplier); | ||
|
|
||
| $interaction->update([ | ||
| 'coins_awarded' => $coinsAwarded, | ||
| 'xp_awarded' => $xpAwarded, | ||
| ]); | ||
|
|
||
| return [ | ||
| 'coins_awarded' => $coinsAwarded, | ||
| 'xp_awarded' => $xpAwarded, | ||
| ]; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.