Skip to content

Commit

Permalink
initial announcement system - closes #752
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentauger committed Jan 3, 2025
1 parent 1251b26 commit 34b7627
Show file tree
Hide file tree
Showing 18 changed files with 349 additions and 209 deletions.
3 changes: 0 additions & 3 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,6 @@ USE_OLLAMA=false
OLLAMA_MODEL=llama3.2
OLLAMA_URL=

OHDEAR_ENABLED=false
OHDEAR_URL=""

ORCID_USE_SANDBOX=false
ORCID_REDIRECT_URI="${FRONTEND_URL}api/orcid/callback"
ORCID_CLIENT_ID=""
Expand Down
30 changes: 30 additions & 0 deletions app/Http/Controllers/Announcement/CheckAnnouncementController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace App\Http\Controllers\Announcement;

use App\Http\Controllers\Controller;
use App\Http\Resources\AnnouncementResource;
use App\Models\Announcement;
use App\Traits\LocaleTrait;
use Illuminate\Http\Request;

class CheckAnnouncementController extends Controller
{
use LocaleTrait;

/**
* Display active announcements.
*/
public function __invoke(Request $request)
{

$announcment = Announcement::active()->orderBy('start_at', 'desc')->get();

if ($announcment->isEmpty()) {
return response()->json([], 204);
}

return AnnouncementResource::collection($announcment);

}
}
38 changes: 0 additions & 38 deletions app/Http/Controllers/OhDear/CheckStatusPageController.php

This file was deleted.

36 changes: 36 additions & 0 deletions app/Http/Resources/AnnouncementResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace App\Http\Resources;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

/**
* @mixin \App\Models\Announcement
*/
class AnnouncementResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @return array<string, mixed>
*/
public function toArray(Request $request): array
{
return [
'data' => [
'id' => $this->id,
'title_en' => $this->title_en,
'title_fr' => $this->title_fr,
'text_en' => $this->text_en,
'text_fr' => $this->text_fr,
'start_at' => $this->start_at,
'end_at' => $this->end_at,
],
'can' => [
'update' => false,
'delete' => false,
],
];
}
}
35 changes: 35 additions & 0 deletions app/Models/Announcement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Announcement extends Model
{
/** @use HasFactory<\Database\Factories\AnnouncementFactory> */
use HasFactory;

/**
* Get the attributes that should be cast.
*
* @return array<string, string>
*/
public function casts(): array
{
return [
'start_at' => 'datetime',
'end_at' => 'datetime',
];
}

/**
* Return active announcements
*/
public function scopeActive(Builder $query)
{
return $query->where('start_at', '<=', now())
->where('end_at', '>=', now());
}
}
50 changes: 50 additions & 0 deletions app/Policies/AnnouncementPolicy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace App\Policies;

use App\Enums\Permissions\UserRole;
use App\Models\Announcement;
use App\Models\User;

class AnnouncementPolicy
{
/**
* Determine whether the user can view any models.
*/
public function viewAny(User $user): bool
{
return $user->hasRole(UserRole::ADMIN);
}

/**
* Determine whether the user can view the model.
*/
public function view(User $user, Announcement $announcement): bool
{
return $user->hasRole(UserRole::ADMIN);
}

/**
* Determine whether the user can create models.
*/
public function create(User $user): bool
{
return $user->hasRole(UserRole::ADMIN);
}

/**
* Determine whether the user can update the model.
*/
public function update(User $user, Announcement $announcement): bool
{
return $user->hasRole(UserRole::ADMIN);
}

/**
* Determine whether the user can delete the model.
*/
public function delete(User $user, Announcement $announcement): bool
{
return $user->hasRole(UserRole::ADMIN);
}
}
5 changes: 0 additions & 5 deletions config/osp.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,6 @@
'redirect_uri' => env('ORCID_REDIRECT_URI'),
],

'ohdear' => [
'enabled' => env('OHDEAR_ENABLED', false),
'url' => env('OHDEAR_URL'),
],

'ollama' => [
'enabled' => env('USE_OLLAMA', false),
'url' => env('OLLAMA_URL'),
Expand Down
28 changes: 28 additions & 0 deletions database/factories/AnnouncementFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;

/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Announcement>
*/
class AnnouncementFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
return [
'title_en' => $this->faker->sentence,
'title_fr' => $this->faker->sentence,
'text_en' => $this->faker->paragraph,
'text_fr' => $this->faker->paragraph,
'start_at' => $this->faker->dateTimeBetween('-2 week', '-1 week'),
'end_at' => $this->faker->dateTimeBetween('+1 week', '+2 week'),
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?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('announcements', function (Blueprint $table) {
$table->id();
$table->timestamps();
$table->string('title_en', 255);
$table->string('title_fr', 255);
$table->string('text_en', 500);
$table->string('text_fr', 500);
$table->dateTime('start_at');
$table->dateTime('end_at');
});
}
};
81 changes: 0 additions & 81 deletions resources/src/api/OhDearStatus.ts

This file was deleted.

2 changes: 2 additions & 0 deletions resources/src/auto-imports.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ declare global {
const onBeforeUpdate: typeof import('vue')['onBeforeUpdate']
const onClickOutside: typeof import('@vueuse/core')['onClickOutside']
const onDeactivated: typeof import('vue')['onDeactivated']
const onElementRemoval: typeof import('@vueuse/core')['onElementRemoval']
const onErrorCaptured: typeof import('vue')['onErrorCaptured']
const onKeyStroke: typeof import('@vueuse/core')['onKeyStroke']
const onLongPress: typeof import('@vueuse/core')['onLongPress']
Expand Down Expand Up @@ -409,6 +410,7 @@ declare module 'vue' {
readonly onBeforeUpdate: UnwrapRef<typeof import('vue')['onBeforeUpdate']>
readonly onClickOutside: UnwrapRef<typeof import('@vueuse/core')['onClickOutside']>
readonly onDeactivated: UnwrapRef<typeof import('vue')['onDeactivated']>
readonly onElementRemoval: UnwrapRef<typeof import('@vueuse/core')['onElementRemoval']>
readonly onErrorCaptured: UnwrapRef<typeof import('vue')['onErrorCaptured']>
readonly onKeyStroke: UnwrapRef<typeof import('@vueuse/core')['onKeyStroke']>
readonly onLongPress: UnwrapRef<typeof import('@vueuse/core')['onLongPress']>
Expand Down
Loading

0 comments on commit 34b7627

Please sign in to comment.