sending messages

This commit is contained in:
saparatayev 2021-06-29 17:03:18 +05:00
parent 1893258d97
commit 3c6557d9b8
16 changed files with 396 additions and 5 deletions

View File

@ -92,7 +92,7 @@ return [
|
*/
'timezone' => 'UTC',
'timezone' => 'Asia/Ashgabat',
/*
|--------------------------------------------------------------------------

View File

@ -41,7 +41,8 @@ class User extends UserBase
* @var array Relations
*/
public $belongsToMany = [
'groups' => [UserGroup::class, 'table' => 'users_groups']
'groups' => [UserGroup::class, 'table' => 'users_groups'],
'chatrooms' => ['TPS\Birzha\Models\Chatroom', 'table'=>'tps_birzha_chatrooms_users']
];
public $attachOne = [

View File

@ -169,7 +169,8 @@ class Plugin extends PluginBase
'TPS\Birzha\Components\Sliders' => 'sliders',
'TPS\Birzha\Components\Offers' => 'offers',
'TPS\Birzha\Components\Singleoffer' => 'singleoffer',
'TPS\Birzha\Components\OfferForm' => 'offerform'
'TPS\Birzha\Components\OfferForm' => 'offerform',
'TPS\Birzha\Components\Messages' => 'messages'
];
}

View File

@ -0,0 +1,62 @@
<?php namespace Tps\Birzha\Components;
use Cms\Classes\ComponentBase;
use RainLab\User\Models\User;
use Tps\Birzha\Models\Chatroom;
use Tps\Birzha\Models\Message;
use Auth;
use Input;
use Carbon\Carbon;
class Messages extends ComponentBase
{
public function componentDetails() {
return [
'name' => 'Messages',
'description' => 'Messages'
];
}
protected function loadMessages() {
$this->chatrooms = Auth::user()->chatrooms;
foreach($this->chatrooms as $room) {
$room->last_message = $room->messages()->latest('send_at')->first();
$room->message_partner = $room->users()->where('users.id','!=',Auth::user()->id)->first();
$room->count_unread_messages = $room->messages()->where('read_at',null)->where('reciver_id',Auth::user()->id)->count();
}
}
public $chatrooms;
public function onRun() {
$this->loadMessages();
}
public function onChatroom() {
$chatRoomId = Input::get('chatroom_id');
// Read unread messages
Chatroom::find($chatRoomId)->messages()->where('reciver_id',Auth::user()->id)->where('read_at',null)->update(['read_at'=>Carbon::now()]);
return [
'messages' => Chatroom::find($chatRoomId)->messages,
'currentUserId' => Auth::user()->id,
'chatRoomId' => $chatRoomId,
'chatRoomPartnerId' => Chatroom::find($chatRoomId)->users()->where('users.id','!=',Auth::user()->id)->first()->id
];
}
public function onMessageSend() {
$newMsg = new Message;
$newMsg->sender_id = Auth::user()->id;
$newMsg->reciver_id = Input::get('reciver_id');
$newMsg->send_at = Carbon::now();
$newMsg->message = Input::get('msg');
$newMsg->chatroom_id = Input::get('chatroom_id');
$newMsg->save();
return $this->onChatroom();
}
}

View File

@ -0,0 +1,112 @@
{% set chatrooms = __SELF__.chatrooms %}
<!-- Chat ============================================================ -->
<section class="chat">
<div class="auto_container">
<div class="chat_wrap">
<div class="chat_box">
<div class="chat_people">
{% for chatroom in chatrooms %}
<a href="#" class="person" style="display: block;"
data-request="onChatroom"
data-request-data="chatroom_id: {{chatroom.id}}"
data-request-success="showMessages(data)"
data-id="{{ chatroom.id}}"
data-partner-id="{{ chatroom.message_partner.id }}"
>
<div class="person_name">
{% if chatroom.message_partner.name or chatroom.message_partner.surname %}
{{chatroom.message_partner.name}} {{chatroom.message_partner.surname}}
{% else %}
{{chatroom.message_partner.email}}
{% endif %}
&nbsp;
<span class="unread-messages-count">
{% if chatroom.count_unread_messages %}
({{chatroom.count_unread_messages}})
{% endif %}
</span>
</div>
<div class="person_message">
{{chatroom.last_message.message}}
</div>
</a>
{% endfor %}
</div>
<div class="chat_area">
<div class="chat_area-inner">
<form action="#" class="message_form"
data-request="onMessageSend"
data-request-success="showMessages(data)"
>
<input type="hidden" name="reciver_id">
<input type="hidden" name="chatroom_id">
<div class="message_input">
<textarea name="msg" required></textarea>
</div>
<button class="message_btn" type="submit" data-attach-loading>
<img src="{{'assets/images/svg/plane.svg'|theme}}" alt="send">
</button>
</form>
</div>
</div>
</div>
</div>
</div>
</section>
<!-- Chat end ======================================================== -->
{% put scripts %}
<script>
function showMessages(data) {
$('.unread-messages-count').empty();
$('.my_message,.friend_message').remove();
$.each(data.messages, function(index, element) {
$('.chat_area-inner').append(`
<div class="${ element.sender_id == data.currentUserId ? 'my_message' : 'friend_message'}">
<div class="message_time">
${
new Date(element.send_at).getHours() +
':' +
(new Date(element.send_at).getMinutes() < 10 ? '0' : '') +
new Date(element.send_at).getMinutes() +
' - ' +
new Date(element.send_at).getDate() +
'.' +
(+new Date(element.send_at).getMonth()+1 < 10 ? '0' : '') +
(+new Date(element.send_at).getMonth()+1) +
'.' +
new Date(element.send_at).getFullYear()
}
</div>
<div class="message_text">
${element.message}
</div>
</div>
`);
});
$('.person').removeClass('active');
$(`.person[data-id="${data.chatRoomId}"]`).addClass('active');
$('.chat_area form input[name="reciver_id"]').val(data.chatRoomPartnerId);
$('.chat_area form input[name="chatroom_id"]').val(data.chatRoomId);
$('.chat_area form textarea').val('');
var bottom= $('.chat_area-inner').height()+$('.chat_area-inner').prop('scrollHeight');
$('.chat_area-inner').scrollTop(bottom);
}
</script>
{% endput %}

View File

@ -0,0 +1,37 @@
<?php namespace TPS\Birzha\Models;
use Model;
/**
* Model
*/
class Chatroom extends Model
{
use \October\Rain\Database\Traits\Validation;
/*
* Disable timestamps by default.
* Remove this line if timestamps are defined in the database table.
*/
public $timestamps = false;
/**
* @var string The database table used by the model.
*/
public $table = 'tps_birzha_chatrooms';
/**
* @var array Validation rules
*/
public $rules = [
];
public $belongsToMany = [
'users' => ['RainLab\User\Models\User','table'=>'tps_birzha_chatrooms_users']
];
public $hasMany = [
'messages' => ['TPS\Birzha\Models\Message'],
];
}

View File

@ -30,4 +30,8 @@ class Message extends Model
*/
public $rules = [
];
public $hasOne = [
'chatroom' => ['TPS\Birzha\Models\Chatroom'],
];
}

View File

@ -0,0 +1,22 @@
<?php namespace TPS\Birzha\Updates;
use Schema;
use October\Rain\Database\Updates\Migration;
class BuilderTableCreateTpsBirzhaChatrooms extends Migration
{
public function up()
{
Schema::create('tps_birzha_chatrooms', function($table)
{
$table->engine = 'InnoDB';
$table->increments('id');
$table->integer('user_id');
});
}
public function down()
{
Schema::dropIfExists('tps_birzha_chatrooms');
}
}

View File

@ -0,0 +1,24 @@
<?php namespace TPS\Birzha\Updates;
use Schema;
use October\Rain\Database\Updates\Migration;
class BuilderTableCreateTpsBirzhaChatroomsUsers extends Migration
{
public function up()
{
Schema::create('tps_birzha_chatrooms_users', function($table)
{
$table->engine = 'InnoDB';
$table->integer('id');
$table->integer('user_id');
$table->integer('chatroom_id');
$table->primary(['id']);
});
}
public function down()
{
Schema::dropIfExists('tps_birzha_chatrooms_users');
}
}

View File

@ -0,0 +1,27 @@
<?php namespace TPS\Birzha\Updates;
use Schema;
use October\Rain\Database\Updates\Migration;
class BuilderTableUpdateTpsBirzhaChatrooms extends Migration
{
public function up()
{
Schema::table('tps_birzha_chatrooms', function($table)
{
$table->timestamp('created_at')->nullable();
$table->timestamp('updated_at')->nullable();
$table->dropColumn('user_id');
});
}
public function down()
{
Schema::table('tps_birzha_chatrooms', function($table)
{
$table->dropColumn('created_at');
$table->dropColumn('updated_at');
$table->integer('user_id');
});
}
}

View File

@ -0,0 +1,23 @@
<?php namespace TPS\Birzha\Updates;
use Schema;
use October\Rain\Database\Updates\Migration;
class BuilderTableUpdateTpsBirzhaChatroomsUsers extends Migration
{
public function up()
{
Schema::table('tps_birzha_chatrooms_users', function($table)
{
$table->increments('id')->change();
});
}
public function down()
{
Schema::table('tps_birzha_chatrooms_users', function($table)
{
$table->integer('id')->change();
});
}
}

View File

@ -0,0 +1,23 @@
<?php namespace TPS\Birzha\Updates;
use Schema;
use October\Rain\Database\Updates\Migration;
class BuilderTableUpdateTpsBirzhaMessages extends Migration
{
public function up()
{
Schema::table('tps_birzha_messages', function($table)
{
$table->integer('chat_id');
});
}
public function down()
{
Schema::table('tps_birzha_messages', function($table)
{
$table->dropColumn('chat_id');
});
}
}

View File

@ -0,0 +1,23 @@
<?php namespace TPS\Birzha\Updates;
use Schema;
use October\Rain\Database\Updates\Migration;
class BuilderTableUpdateTpsBirzhaMessages2 extends Migration
{
public function up()
{
Schema::table('tps_birzha_messages', function($table)
{
$table->renameColumn('chat_id', 'chatroom_id');
});
}
public function down()
{
Schema::table('tps_birzha_messages', function($table)
{
$table->renameColumn('chatroom_id', 'chat_id');
});
}
}

View File

@ -134,3 +134,21 @@
1.0.46:
- 'Updated table tps_birzha_category'
- builder_table_update_tps_birzha_categories.php
1.0.47:
- 'Created table tps_birzha_chatrooms'
- builder_table_create_tps_birzha_chatrooms.php
1.0.48:
- 'Updated table tps_birzha_chatrooms'
- builder_table_update_tps_birzha_chatrooms.php
1.0.49:
- 'Created table tps_birzha_chatrooms_users'
- builder_table_create_tps_birzha_chatrooms_users.php
1.0.50:
- 'Updated table tps_birzha_chatrooms_users'
- builder_table_update_tps_birzha_chatrooms_users.php
1.0.51:
- 'Updated table tps_birzha_messages'
- builder_table_update_tps_birzha_messages.php
1.0.52:
- 'Updated table tps_birzha_messages'
- builder_table_update_tps_birzha_messages_2.php

View File

@ -1,2 +1,16 @@
title = "messages"
title = "Messages"
url = "/messages"
layout = "default"
is_hidden = 0
[messages]
[session]
security = "user"
redirect = "index"
[viewBag]
localeTitle[ru] = "Сообщения"
localeUrl[ru] = "/soobshcheniya"
==
{% component 'messages' %}

View File

@ -134,7 +134,7 @@ code = "main-top"
</div>
</a>
<a href="#" class="profile_item">
<a href="{{ 'kabinet/messages'|page }}" class="profile_item">
<div class="profile_icon">
<img src="{{'assets/images/svg/inbox.svg'|theme}}" alt="">
</div>