Skip to content

Commit 3dc827e

Browse files
committed
+ Chat frontend
1 parent 192564d commit 3dc827e

File tree

13 files changed

+175
-23
lines changed

13 files changed

+175
-23
lines changed

App/Controllers/Chat.php

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,17 @@ public function writeMessage ()
3636
$channelType = Input::getInteger("channelType");
3737
$message = Input::getString("message", true);
3838

39-
if ($channelId < 1 || !in_array($channelType, ChatModel::$validTypes) || strlen($message) < 10) {
39+
if (!in_array($channelType, ChatModel::$validTypes) || strlen($message) < 10) {
4040
throw new AppException(AppException::INVALID_DATA);
4141
}
4242

43-
$this->checkChannelPermissions($channelType);
43+
if ($channelType == ChatModel::CHANNEL_TYPE_REPLY) {
44+
if ($channelId < 1) {
45+
throw new AppException(AppException::INVALID_DATA);
46+
}
47+
} else {
48+
$channelId = $this->getChannelId($channelType);
49+
}
4450

4551
$success = ChatModel::create([
4652
"channel_id" => $channelId,
@@ -50,30 +56,38 @@ public function writeMessage ()
5056
]);
5157

5258
if ($success) {
53-
return $success->id;
59+
return $success->toArray();
5460
}
5561

5662
throw new AppException(AppException::ACTION_FAILED);
5763
}
5864

59-
private function checkChannelPermissions ($channelType)
65+
private function getChannelId ($channelType)
6066
{
6167
// ensure that he can see this channel
6268
switch ($channelType)
6369
{
70+
case ChatModel::CHANNEL_TYPE_COUNTRY:
71+
return App::user()->getLocation()["country"]["id"];
72+
case ChatModel::CHANNEL_TYPE_WORLD:
73+
return 1;
6474
case ChatModel::CHANNEL_TYPE_POLITICAL_PARTY:
6575
$myParty = App::user()->getPoliticalParty();
6676

6777
if (!$myParty) {
6878
throw new AppException(AppException::INVALID_DATA);
6979
}
70-
break;
80+
81+
return $myParty->id;
7182
case ChatModel::CHANNEL_TYPE_MILITIA:
7283
$myMilitia = App::user()->getMilitia();
7384

7485
if (!$myMilitia) {
7586
throw new AppException(AppException::INVALID_DATA);
7687
}
88+
return $myMilitia->id;
89+
default:
90+
throw new AppException(AppException::INVALID_DATA);
7791
break;
7892
}
7993
}
@@ -83,16 +97,22 @@ public function showMessages ()
8397
$channelId = Input::getInteger("channelId");
8498
$channelType = Input::getInteger("channelType");
8599

86-
if ($channelId < 1 || !in_array($channelType, ChatModel::$validTypes)) {
100+
if (!in_array($channelType, ChatModel::$validTypes)) {
87101
throw new AppException(AppException::INVALID_DATA);
88102
}
89103

90-
$this->checkChannelPermissions($channelType);
104+
if ($channelType == ChatModel::CHANNEL_TYPE_REPLY) {
105+
if ($channelId < 1) {
106+
throw new AppException(AppException::INVALID_DATA);
107+
}
108+
} else {
109+
$channelId = $this->getChannelId($channelType);
110+
}
91111

92-
$messages = ChatModel::where([
112+
$messages = ChatModel::with("sender")->where([
93113
"channel_id" => $channelId,
94114
"channel_type" => $channelType
95-
])->get(); //@todo order by
115+
])->orderBy('id', 'DESC')->get();
96116

97117
if (!$messages) {
98118
$messages = [];

App/Controllers/Home.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,17 @@ public function showHomepage ()
4444
"country" => App::user()->getLocation()["country"]["id"]
4545
])->limit(5)->get();
4646

47+
$hasPoliticalParty = false;
48+
49+
if (App::user()->getPoliticalParty()) {
50+
$hasPoliticalParty = true;
51+
}
52+
4753

4854
return $this->render('home.html.twig', [
4955
"job" => $jobData,
5056
"hasTrainedToday" => $userGyms->hasTrainedToday(1),
57+
"hasPoliticalParty" => $hasPoliticalParty,
5158
"latestArticles" => $latestArticles
5259
]);
5360
}

App/Controllers/Newspaper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ public function showArticle ($id)
203203
throw new AppException(AppException::INVALID_DATA);
204204
}
205205

206-
$article = NewspaperArticle::find($id);
206+
$article = NewspaperArticle::with("creator")->find($id);
207207

208208
if (!$article) {
209209
throw new AppException(AppException::INVALID_DATA);

App/Models/Chat.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,8 @@ class Chat extends Model
2424
];
2525

2626
protected $fillable = ["message", "sender", "channel_id", "channel_type", "likes"];
27+
28+
public function sender() {
29+
return $this->hasOne('App\Models\User', 'id', 'sender');
30+
}
2731
}

App/Models/NewspaperArticle.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,12 @@ class NewspaperArticle extends Model
1818
];
1919

2020
protected $fillable = ["title", "text", "category", "uid", "country", "views", "votes"];
21+
22+
public function creator() {
23+
return $this->hasOne('App\Models\User', 'id', 'uid');
24+
}
25+
26+
public function country() {
27+
return $this->hasOne('App\Models\Country', 'id', 'country');
28+
}
2129
}

htdocs/css/src/_home.scss

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#chat-container {
2+
float: right;
3+
width: 30%;
4+
background: #03a9f4;
5+
padding: 10px;
6+
7+
.textarea-container {
8+
margin-top: 5px;
9+
}
10+
11+
textarea {
12+
width: 85%;
13+
}
14+
}
15+
#chat-channels li {
16+
display: inline-block;
17+
padding: 5px;
18+
background: rgba(219, 221, 223, 0.458824);
19+
color: white;
20+
border-top-right-radius: 5px;
21+
border-top-left-radius: 5px;
22+
cursor: pointer;
23+
}
24+
.chat-message {
25+
background-color: #f5f5f5;
26+
27+
.message-body, .avatar-container {
28+
display: inline-block;
29+
}
30+
.message-body {
31+
width: 85%;
32+
}
33+
}

htdocs/css/src/main.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@
1010
@import 'market';
1111
@import 'party';
1212
@import 'news';
13+
@import 'home';

htdocs/img/default_avatar.jpg

59.1 KB
Loading

htdocs/js/src/chat.js

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,26 @@ peque.chat = function ()
22
{
33
'use strict';
44

5+
var $chatContainer;
6+
57
var init = function (defaultChannel)
68
{
9+
$chatContainer = $('#chat-container');
10+
711
$('#chat-channels li').on("click", changeChannel);
812

913
$('#chat-channels li[data-channel=' + defaultChannel + ']').click();
1014
};
1115

1216
var initMessageListeners = function ()
1317
{
14-
$('[data-action=post]').on("submit", post);
15-
$('[data-action=vote]').on("click", vote);
16-
$('[data-action=show-comments]').on("click", showComments);
18+
$chatContainer.find('[data-action=post]').on("click", post);
19+
$chatContainer.find('[data-action=vote]').on("click", vote);
20+
$chatContainer.find('[data-action=show-comments]').on("click", showComments);
21+
};
22+
23+
var showComments = function () {
24+
1725
};
1826

1927
var changeChannel = function ()
@@ -23,24 +31,50 @@ peque.chat = function ()
2331
$('#chat-channels li').removeClass("selected");
2432
$('#chat-channels li[data-channel=' + channel + ']').addClass("selected");
2533

26-
peque.api("chat/list", {channel: channel}, function (data) {
34+
peque.api("chat/list", {channelType: channel}, function (data) {
2735
if (data.error > 0) {
2836
return false;
2937
}
3038

31-
$('#chat-messages-container').html(data.result);
39+
var k, html = '';
40+
41+
for (k in data.result)
42+
{
43+
if (!data.result.hasOwnProperty(k)) {
44+
continue;
45+
}
46+
47+
html += renderMessage(data.result[k]);
48+
}
49+
50+
$('#chat-messages-container').html(html);
3251
initMessageListeners();
3352
});
3453
};
3554

55+
var renderMessage = function (message)
56+
{
57+
var $template = peque.utils.getTemplate("chat-message");
58+
59+
$template.find('[data-id="message"]').text(message.message);
60+
$template.find('.message-body strong').text(message.sender.nick);
61+
$template.find('[data-id="likes-count"]').text(message.likes);
62+
63+
return $template.parent().html();
64+
};
65+
3666
var post = function (e) {
3767
e.preventDefault();
3868

3969
var id = $(this).data("id"),
40-
type = $(this).data("type"),
41-
message = $(this).data("message");
70+
type = parseInt($(this).data("type")),
71+
message = $chatContainer.find("textarea").val();
72+
73+
if (type < 1 || isNaN(type)) {
74+
type = parseInt($('#chat-channels li.selected').data("channel"));
75+
}
4276

43-
if (id < 1 || type < 1 || message.legth < 4) {
77+
if (id < 1 || type < 1 || message.length < 10) {
4478
return false;
4579
}
4680

@@ -49,9 +83,8 @@ peque.chat = function ()
4983
if (data.error > 0) {
5084
return false;
5185
}
52-
var $template = peque.utils.getTemplate("chat-message");
5386

54-
$template.find("p").html(message);
87+
$('#chat-messages-container').prepend(renderMessage(data.result));
5588
});
5689
};
5790

routes.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,14 +342,14 @@
342342

343343
$app->group('/chat', function () use ($app)
344344
{
345-
$app->get('/get', function($request, $response, $args) use ($app) {
345+
$app->post('/list', function($request, $response, $args) use ($app) {
346346
$ct = new Chat($app, $response);
347347
$ct->json('showMessages');
348348
});
349349

350350
$app->post('/post', function($request, $response, $args) use ($app) {
351351
$ct = new Chat($app, $response);
352-
$ct->json('postMessage');
352+
$ct->json('writeMessage');
353353
});
354354

355355
$app->post('/vote', function($request, $response, $args) use ($app) {

0 commit comments

Comments
 (0)