Browse Source

Send the message with the broadcast

master
l4p1n 1 year ago
parent
commit
c70f80cb73
Signed by: l4p1n GPG Key ID: 8A2C2F68B58DC35C
6 changed files with 35 additions and 14 deletions
  1. +7
    -0
      app/Events/NewMessageEvent.php
  2. +12
    -7
      app/Http/Controllers/API/MessagesController.php
  3. +5
    -4
      app/Message.php
  4. +9
    -0
      database/seeds/GuildMemberSeeder.php
  5. +1
    -2
      database/seeds/MessagesTableSeeder.php
  6. +1
    -1
      routes/api.php

+ 7
- 0
app/Events/NewMessageEvent.php View File

@ -28,6 +28,7 @@ class NewMessageEvent implements ShouldBroadcastNow
public function __construct(Message $message)
{
$this->message = $message;
$this->message->load('channel', 'author');
}
/**
@ -39,4 +40,10 @@ class NewMessageEvent implements ShouldBroadcastNow
{
return new PrivateChannel('channel.' . $this->message->channel->id);
}
public function broadcastWith(){
return [
'message' => $this->message
];
}
}

+ 12
- 7
app/Http/Controllers/API/MessagesController.php View File

@ -13,15 +13,19 @@ use Illuminate\Support\Str;
class MessagesController extends Controller
{
public function send_message(Request $request){
public function send_message(Request $request, ?Channel $channel = null){
/** @var User $user */
$user = $request->user();
/** @var Guild $random_guild */
$random_guild = $user->guilds()->inRandomOrder()->take(1)->first();
if(is_null($channel)){
/** @var Guild $random_guild */
$guild = $user->guilds()->inRandomOrder()->take(1)->first();
/** @var Channel $random_channel */
$random_channel = $random_guild->channels()->inRandomOrder()->take(1)->first();
/** @var Channel $random_channel */
$channel = $guild->channels()->inRandomOrder()->take(1)->first();
} else {
$guild = $channel->guild;
}
$message = new Message([
'type' => Message::DEFAULT,
@ -34,10 +38,11 @@ class MessagesController extends Controller
]);
$message->author()->associate($user);
$random_channel->messages()->save($message);
$channel->messages()->save($message);
logger("Saved message for guild $random_guild->name ($random_guild->id) in channel $random_channel->name ($random_channel->id)", ["message" => $message]);
logger("Saved message for guild $guild->name ($guild->id) in channel $channel->name ($channel->id)", ["message" => $message]);
event(new NewMessageEvent($message));
return $message;
}
}

+ 5
- 4
app/Message.php View File

@ -23,16 +23,17 @@ class Message extends Model
protected $guarded = ['id'];
protected $casts = [
'created_at' => 'datetime:c',
'updated_at' => 'datetime:c'
];
use HasSnowflakePrimary;
public function channel(){
return $this->belongsTo(Channel::class);
}
public function guild(){
return $this->belongsTo(Guild::class);
}
public function author(){
return $this->belongsTo(User::class,'user_id');
}


+ 9
- 0
database/seeds/GuildMemberSeeder.php View File

@ -13,10 +13,16 @@ class GuildMemberSeeder extends Seeder
*/
public function run()
{
DB::beginTransaction();
$guilds = Guild::all();
$user_count = User::query()->count();
$guilds_count = count($guilds);
$i = 1;
foreach($guilds as $guild){
$this->command->info("($i/$guilds_count) Seeding for guild $guild->name");
$members = $guild->members->all();
$members_count = count($members);
$members_id = array_map(function(User $user){ return $user->id; }, $members);
@ -28,6 +34,9 @@ class GuildMemberSeeder extends Seeder
->get();
$guild->members()->attach($not_members);
$i++;
}
DB::commit();
}
}

+ 1
- 2
database/seeds/MessagesTableSeeder.php View File

@ -22,11 +22,10 @@ class MessagesTableSeeder extends Seeder
static $i = 1;
foreach($channels as $channel){
$nbre_messages = mt_rand(10, 20);
$nbre_messages = 5;
$this->command->info("($i/$max) Seeding channel {$channel->name} ($nbre_messages messages)");
factory(Message::class, $nbre_messages)
->create([
'guild_id' => $channel->guild->id,
'user_id' => $channel->guild->members->random()->id,
'channel_id' => $channel->id
]);


+ 1
- 1
routes/api.php View File

@ -19,7 +19,7 @@ Route::middleware('auth:api')->namespace("API")->group(function(){
});
Route::prefix('messages')->group(function(){
Route::get('send', 'MessagesController@send_message')->name('api.messages.send');
Route::get('send/{channel?}', 'MessagesController@send_message')->name('api.messages.send');
});
Route::get('gateway', 'GatewayController@gateway');


Loading…
Cancel
Save