Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions app/Events/TagFrequencyChanged.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace App\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class TagFrequencyChanged
{
use Dispatchable, InteractsWithSockets, SerializesModels;

/**
* Create a new event instance.
*/
public function __construct(public array|null $oldTags, public array|null $newTags) { }

/**
* Get the channels the event should broadcast on.
*
* @return array<int, \Illuminate\Broadcasting\Channel>
*/
public function broadcastOn(): array
{
return [
new PrivateChannel('channel-name'),
];
}
}
4 changes: 4 additions & 0 deletions app/Http/Controllers/ComputerScienceResourceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Controllers;

use App\Events\TagFrequencyChanged;
use App\Http\Requests\ComputerScienceResource\StoreResourceRequest;
use App\Models\ComputerScienceResource;
use App\Models\ResourceEdits;
Expand Down Expand Up @@ -77,6 +78,9 @@ public function store(StoreResourceRequest $request)
$resource->general_tags = $validatedData['general_tags'];
}

// Change tag frequency
TagFrequencyChanged::dispatch(null, $resource->tagCounter());

Log::debug("Created resource " . json_encode($resource));

return redirect(route('resources.show', ['computerScienceResource' => $resource->id]))
Expand Down
9 changes: 8 additions & 1 deletion app/Http/Controllers/ResourceEditsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Controllers;

use App\Events\TagFrequencyChanged;
use App\Models\ComputerScienceResource;
use App\Models\ResourceEdits;
use App\Services\ResourceEditsService;
Expand All @@ -26,7 +27,7 @@ public function create(ComputerScienceResource $computerScienceResource)
}


// TODO: Make an array facade
// TODO: Make an array facade or something
function normalize($array) {
ksort($array);
foreach ($array as &$value) {
Expand Down Expand Up @@ -93,6 +94,7 @@ public function merge(ResourceEditsService $editsService, ResourceEdits $resourc
}

$resource = ComputerScienceResource::findOrFail($resourceEdits->computer_science_resource_id);
$old_tag_counter = $resource->tagCounter();

$resource->name = $resourceEdits->name;
$resource->description = $resourceEdits->description;
Expand All @@ -108,6 +110,11 @@ public function merge(ResourceEditsService $editsService, ResourceEdits $resourc
$resource->programming_language_tags = $resourceEdits->programming_language_tags;
$resource->general_tags = $resourceEdits->general_tags;

// Get the new tag counter
$new_tags = collect([$resourceEdits->topic_tags, $resourceEdits->programming_language_tags, $resourceEdits->general_tags])->flatten()->unique()->countBy()->toArray();
// Change tag frequency
TagFrequencyChanged::dispatch($old_tag_counter, $new_tags);

// Delete the edit since we successfully merged the changes
$resourceEdits->delete();

Expand Down
3 changes: 1 addition & 2 deletions app/Http/Controllers/ResourceReviewController.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ public function store(StoreResourceReview $request, ComputerScienceResource $com

ResourceReviewProcessed::dispatch($computerScienceResource->id, null, $review->attributesToArray());

return to_route('resources.show', ['computerScienceResource' => $review->computer_science_resource_id])
->with('success', 'Review created successfully!');
return response()->json();
}

public function update(StoreResourceReview $request, ComputerScienceResource $computerScienceResource)
Expand Down
26 changes: 26 additions & 0 deletions app/Http/Controllers/TagFrequencyController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace App\Http\Controllers;

use App\Models\TagFrequency;
use Illuminate\Http\Request;

class TagFrequencyController extends Controller
{
public function search(string $query = "")
{
if (strlen($query) > 50)
{
return response(422)->json();
}

$prefixed_tags = TagFrequency::where('tag', 'like', $query.'%')
->orderByDesc('count')
->limit(20)
->get();

return response()->json([
'tags' => $prefixed_tags
]);
}
}
64 changes: 64 additions & 0 deletions app/Listeners/ModifyTagFrequency.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace App\Listeners;

use App\Events\TagFrequencyChanged;
use App\Models\TagFrequency;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Facades\DB;

class ModifyTagFrequency
{
/**
* Create the event listener.
*/
public function __construct()
{
//
}

/**
* Handle the event.
*/
public function handle(TagFrequencyChanged $event): void
{
$old = $event->oldTags ?? [];
$new = $event->newTags ?? [];

// 1. Build diffs for every tag
$diffs = [];
foreach (array_unique(array_merge(array_keys($old), array_keys($new))) as $tag) {
$diff = ($new[$tag] ?? 0) - ($old[$tag] ?? 0);
if ($diff !== 0) {
$diffs[$tag] = $diff;
}
}

if (empty($diffs)) {
return;
}

// 2. One upsert: increment (or decrement) existing, insert new
$upserts = [];
foreach ($diffs as $tag => $count) {
$upserts[] = [
'tag' => $tag,
'count' => $count,
];
}

DB::table('tag_frequencies')->upsert(
$upserts,
['tag'],
[
'count' => DB::raw('tag_frequencies.count + VALUES(count)'),
]
);

// 3. Clean out any zero-or-negative counts
DB::table('tag_frequencies')
->where('count', '<=', 0)
->delete();
}
}
6 changes: 6 additions & 0 deletions app/Models/ComputerScienceResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,10 @@ protected function generalTags(): Attribute
set: fn(array $value) => $this->syncTagsWithType($value, 'general_tags')
);
}

public function tagCounter(): array
{
$tag_collection = collect([$this->topic_tags, $this->programming_language_tags, $this->general_tags]);
return $tag_collection->flatten()->unique()->countBy()->toArray();
}
}
10 changes: 10 additions & 0 deletions app/Models/TagFrequency.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class TagFrequency extends Model
{
public $timestamps = false;
}
12 changes: 7 additions & 5 deletions app/Services/ResourceEditsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,11 @@ class ResourceEditsService
*/
public function requiredVotes(int $totalVotes): int
{
// So little votes, so we only need 1 vote to approve
if ($totalVotes <= 1) return 1;

// Dropoff
return min($totalVotes,
// Either the current votes, or the log equation
$votes = min($totalVotes,
floor(log($totalVotes, 1.25)) + 1
);
return max(3, $votes); // Need to be 3 votes minimum
}

/**
Expand All @@ -31,6 +29,10 @@ public function requiredVotes(int $totalVotes): int
*/
public function canMergeEdits(ResourceEdits $edits) : bool
{
if (app()->isLocal()) {
return true;
}

$totalVotes = $edits->resource->votes_count;
$neededApprovals = $this->requiredVotes($totalVotes);

Expand Down
3 changes: 3 additions & 0 deletions database/factories/ComputerScienceResourceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Database\Factories;

use App\Events\TagFrequencyChanged;
use App\Models\ComputerScienceResource;
use App\Models\User;
use Illuminate\Support\Facades\Log;
Expand Down Expand Up @@ -51,6 +52,8 @@ public function configure(): Factory
$resource->topic_tags = fake()->randomElements($tags, fake()->numberBetween(3, count($tags)));
$resource->programming_language_tags = fake()->randomElements($tags);
$resource->general_tags = fake()->randomElements($tags);

TagFrequencyChanged::dispatch(null, $resource->tagCounter());
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('tag_frequencies', function (Blueprint $table) {
$table->char('tag', 100)->primary();
$table->bigInteger('count')->default(0);
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('tag_frequencies');
}
};
Loading
Loading