Browse Source

Add API new message broadcasted event handling to websocket server

master
l4p1n 1 year ago
parent
commit
5c2e94b043
Signed by: l4p1n GPG Key ID: 8A2C2F68B58DC35C
6 changed files with 85 additions and 13 deletions
  1. +18
    -1
      app/Http/Controllers/API/MessagesController.php
  2. +33
    -1
      app/Message.php
  3. +2
    -1
      composer.json
  4. +4
    -0
      routes/api.php
  5. +1
    -1
      socket_server/lib/ClientPayloadHandler.py
  6. +27
    -9
      socket_server/lib/RedisHandler.py

+ 18
- 1
app/Http/Controllers/API/MessagesController.php View File

@ -29,7 +29,7 @@ class MessagesController extends Controller
$message = new Message([
'type' => Message::DEFAULT,
'content' => Str::random(mt_rand(5, 10)),
'content' => '!help',
'tts' => false,
'mention_everyone' => false,
'embeds' => '{}',
@ -45,4 +45,21 @@ class MessagesController extends Controller
event(new NewMessageEvent($message));
return $message;
}
public function post_message(Request $request, Channel $channel){
$user = $request->user();
$message = new Message($request->all());
if(empty($message->nonce)){
$message->nonce = Str::uuid();
}
$message->author()->associate($user);
$channel->messages()->save($message);
event(new NewMessageEvent($message));
return $message->append('attachments', 'edited_timestamp', 'timestamp');
}
}

+ 33
- 1
app/Message.php View File

@ -21,13 +21,21 @@ class Message extends Model
const USER_PREMIUM_GUILD_SUBSCRIPTION_TIER_3 = 11;
const CHANNEL_FOLLOW_ADD = 12;
protected $guarded = ['id'];
protected $fillable = ['content', 'nonce', 'embeds', 'tts'];
protected $casts = [
'created_at' => 'datetime:c',
'updated_at' => 'datetime:c'
];
protected $attributes = [
'tts' => false,
'mention_everyone' => false,
'embeds' => '[]',
'pinned' => false,
'type' => self::DEFAULT,
];
use HasSnowflakePrimary;
public function channel(){
@ -37,4 +45,28 @@ class Message extends Model
public function author(){
return $this->belongsTo(User::class,'user_id');
}
public function getAttachmentsAttribute(){
return [];
}
public function getEditedTimestampAttribute(){
return $this->attributes['updated_at'];
}
public function getTimestampAttribute(){
return $this->attributes['created_at'];
}
public function getEmbedsAttribute(){
return json_decode($this->attributes['embeds']);
}
public function setEmbedsAttribute($value){
if(!is_string($value)){
$this->attributes['embeds'] = json_encode($value);
} else {
$this->attributes['embeds'] = $value;
}
}
}

+ 2
- 1
composer.json View File

@ -13,7 +13,8 @@
"fideloper/proxy": "^4.0",
"kra8/laravel-snowflake": "^1.2",
"laravel/framework": "^6.2",
"laravel/tinker": "^2.0"
"laravel/tinker": "^2.0",
"ext-json": "*"
},
"require-dev": {
"facade/ignition": "^1.4",


+ 4
- 0
routes/api.php View File

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

+ 1
- 1
socket_server/lib/ClientPayloadHandler.py View File

@ -196,7 +196,7 @@ class ClientPayloadHandler:
async def send(self, payload):
encoded_payload = json.dumps(payload)
#self.logger.info(f'Sending {encoded_payload}')
#self.logger.info(f'Sending\n {encoded_payload}')
await self.ws.send(encoded_payload)
def _prepare_member(self, member_row):


+ 27
- 9
socket_server/lib/RedisHandler.py View File

@ -74,18 +74,36 @@ class RedisHandler:
async def _process_new_message_event(self, event_data):
member = await Guild.get_member(event_data['user_id'], event_data['channel']['guild_id'])
# Complete what has been given by the API
event_data['member'] = dict(member)
event_data['timestamp'] = event_data['created_at']
event_data['edited_timestamp'] = event_data['updated_at'] if event_data['updated_at'] == event_data['created_at'] else None
event_data['attachments'] = []
event_data['mention_roles'] = []
event_data['mentions'] = []
event_data['embeds'] = json.loads(event_data['embeds'])
transmitted = {
'attachments': [],
'id': event_data['id'],
'channel_id': event_data['channel_id'],
'author': event_data['author'],
'member': {
"nick": member['nick'],
'roles': member['roles'],
'joined_at': member['joined_at'],
'deaf': True,
'mute': True
},
'content': event_data['content'],
'timestamp': event_data['created_at'],
'edited_timestamp': event_data['updated_at'] if event_data['created_at'] != event_data['updated_at'] else None,
'tts': event_data['tts'],
'mention_everyone': event_data['mention_everyone'],
'mentions': [],
'mention_roles': [],
'mention_channels': [],
'embeds': event_data['embeds'],
'pinned': event_data['pinned'],
'type': event_data['type']
}
print(repr(transmitted))
payload = {
'op': opcodes.DISPATCH,
'd': event_data,
'd': transmitted,
's': None,
't': 'MESSAGE_CREATE'
}


Loading…
Cancel
Save