Skip to content
Permalink
Browse files

add room support

  • Loading branch information...
devnewton committed Jan 7, 2018
1 parent df0165d commit 95ee3bbc1f5838a0f7aac7ef59232948d43628e1
Showing with 66 additions and 32 deletions.
  1. +2 −1 README.md
  2. +6 −2 common.php
  3. +1 −0 config.php
  4. BIN data/backend.db
  5. +2 −1 get.php
  6. +2 −1 index.php
  7. +48 −23 js/taab.js
  8. +5 −4 post.php
@@ -6,4 +6,5 @@ taab is a KISS tribune powered by vue.js, pegjs, blazecss, sqlite and php (sorry

- TSV backend
- lastId
- X-Post-Id
- X-Post-Id
- rooms
@@ -2,19 +2,23 @@
include_once "config.php";
function taab_echo_backend($lastId, $newPostId = NULL) {
function taab_echo_backend($lastId, $room = '', $newPostId = NULL) {
global $pdo;
$posts = $pdo->prepare(
"SELECT id, strftime('%Y%m%d%H%M%S', time) as time, login, info, message
FROM posts
WHERE id > :lastId
and room = :room
ORDER BY id DESC
LIMIT :maxPosts");
$posts->execute(array("lastId" => $lastId, "maxPosts" => TAAB_BACKEND_MAX_POSTS));
$posts->execute(array("lastId" => $lastId, "room" => $room, "maxPosts" => TAAB_BACKEND_MAX_POSTS));
header("Content-Type: text/tab-separated-values");
if ($newPostId !== NULL) {
header('X-Post-Id: ' . $newPostId);
}
if($room) {
header('X-Room: ' . $room);
}
$outstream = fopen("php://output", 'w');
while($post = $posts->fetch(PDO::FETCH_OBJ)) {
fputs($outstream, $post->id);
@@ -5,6 +5,7 @@
$pdo->query("CREATE TABLE IF NOT EXISTS posts (
id INTEGER PRIMARY KEY AUTOINCREMENT,
time TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
room TEXT DEFAULT '' NOT NULL,
info TEXT DEFAULT '' NOT NULL,
login TEXT DEFAULT '' NOT NULL,
message TEXT DEFAULT '' NOT NULL
BIN +0 Bytes (100%) data/backend.db
Binary file not shown.
@@ -2,7 +2,8 @@
include_once 'common.php';
$room = filter_input(INPUT_GET, 'room', FILTER_VALIDATE_REGEXP, array('options' => array('default' => '', 'regexp' => "/\w+/")));
$lastId = filter_input(INPUT_GET, 'lastId', FILTER_VALIDATE_INT, array('options' => array('default' => 0)));
taab_echo_backend($lastId);
taab_echo_backend($lastId, $room);
?>
@@ -9,8 +9,9 @@
<body class="c-text">
<main id="taab-coincoin" class="o-container o-container--super u-window-box--tiny" v-on:click="clicked" v-on:mouseover="mouseEntered" v-on:mouseout="mouseLeaved">
<form class="c-input-group" v-on:submit.prevent="post" accept-charset="UTF-8" enctype="application/x-www-form-urlencoded" autofocus>
<div v-if="room" class="c-button">#{{ room }}</div>
<div class="o-field">
<input v-model="message" ref="message" name="message" placeholder="message or command (/nick)" class="c-field" spellcheck="true">
<input v-model="message" ref="message" name="message" placeholder="message or command (/nick, /join)" class="c-field" spellcheck="true">
</div>
<button type="submit" class="c-button c-button--info">Post</button>
</form>
@@ -5,6 +5,7 @@ document.addEventListener('DOMContentLoaded', function () {
new Vue({
el: '#taab-coincoin',
data: {
room: "",
message: "",
posts: []
},
@@ -18,7 +19,7 @@ document.addEventListener('DOMContentLoaded', function () {
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
self.parseBackendResponse(xhr.responseText);
self.parseBackendResponse(xhr);
self.message = "";
}
}
@@ -30,6 +31,10 @@ document.addEventListener('DOMContentLoaded', function () {
if (login) {
params += "&login=" + encodeURIComponent(login).replace(/%20/g, '+');
}
var room = this.room;
if (room) {
params += "&room=" + encodeURIComponent(room).replace(/%20/g, '+');
}
params += "&lastId=" + self.getLastId();
xhr.send(params);
}
@@ -40,15 +45,20 @@ document.addEventListener('DOMContentLoaded', function () {
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
self.parseBackendResponse(xhr.responseText);
self.parseBackendResponse(xhr);
}
}
};
xhr.open("GET", "get.php?lastId=" + encodeURIComponent(self.getLastId()));
var params = "?lastId=" + encodeURIComponent(self.getLastId());
var room = self.room;
if (room) {
params += "&room=" + encodeURIComponent(room).replace(/%20/g, '+');
}
xhr.open("GET", "get.php" + params);
xhr.send();
},
handleCommand: function () {
return this.handleCommandNick();
return this.handleCommandNick() || this.handleCommandJoin();
},
handleCommandNick: function () {
var result = /\/nick (.*)/.exec(this.message);
@@ -59,6 +69,17 @@ document.addEventListener('DOMContentLoaded', function () {
return false;
}
},
handleCommandJoin: function () {
var result = /\/join ?#?(\w+)?/.exec(this.message);
if (result && result.length === 2) {
this.posts = [];
this.room = result[1] || '';
this.get();
return true;
} else {
return false;
}
},
clicked: function (e) {
switch (e.target.tagName) {
case 'TIME':
@@ -106,25 +127,29 @@ document.addEventListener('DOMContentLoaded', function () {
break;
}
},
parseBackendResponse: function (responseText) {
var newPosts = responseText.split(/\r\n|\n/).map(function (line) {
var post = line.split(/\t/);
if (post.length >= 5) {
var time = post[1];
var formattedTime = time.substr(0, 4) + "-" + time.substr(4, 2) + "-" + time.substr(6, 2) + "T" + time.substr(8, 2) + ":" + time.substr(10, 2) + ":" + time.substr(12, 2);
var htmlMessage = taab_backend2html.parse(post[4]);
return {id: post[0], time: formattedTime, info: post[2], login: post[3], message: htmlMessage};
} else {
return false;
}
}).filter(function (post) {
return post && post.id && post.time && post.message;
}).concat(this.posts);
this.posts = newPosts.sort(function (a, b) {
return b.id - a.id;
}).filter(function (elem, pos) {
return newPosts.indexOf(elem) === pos;
});
parseBackendResponse: function (xhr) {
var room = this.room || '';
var responseRoom = xhr.getResponseHeader("X-Room") || '';
if (room === responseRoom) {
var newPosts = xhr.responseText.split(/\r\n|\n/).map(function (line) {
var post = line.split(/\t/);
if (post.length >= 5) {
var time = post[1];
var formattedTime = time.substr(0, 4) + "-" + time.substr(4, 2) + "-" + time.substr(6, 2) + "T" + time.substr(8, 2) + ":" + time.substr(10, 2) + ":" + time.substr(12, 2);
var htmlMessage = taab_backend2html.parse(post[4]);
return {id: post[0], time: formattedTime, info: post[2], login: post[3], message: htmlMessage};
} else {
return false;
}
}).filter(function (post) {
return post && post.id && post.time && post.message;
}).concat(this.posts);
this.posts = newPosts.sort(function (a, b) {
return b.id - a.id;
}).filter(function (elem, pos) {
return newPosts.indexOf(elem) === pos;
});
}
},
getLastId: function () {
return this.posts.reduce(function (acc, val) {
@@ -2,6 +2,7 @@
include_once 'common.php';
$room = filter_input(INPUT_POST, 'room', FILTER_VALIDATE_REGEXP, array('options' => array('default' => '', 'regexp' => "/\w+/")));
$lastId = filter_input(INPUT_POST, 'lastId', FILTER_VALIDATE_INT, array('options' => array('default' => 0)));
$message = mb_substr(filter_input(INPUT_POST, 'message', FILTER_UNSAFE_RAW, FILTER_FLAG_STRIP_LOW), 0, TAAB_MAX_POST_LENGTH);
$login = trim(mb_substr(filter_input(INPUT_POST, 'login', FILTER_SANITIZE_EMAIL), 0, TAAB_MAX_LOGIN_LENGTH));
@@ -22,9 +23,9 @@
if (mb_strlen(trim($message)) > 0 && mb_detect_encoding($message, 'UTF-8', true)) {
$pdo->prepare(
"INSERT INTO posts
(login, info, message)
VALUES (:login, :info, :message)
")->execute(array("login" => $login, "info" => $info, "message" => $message));
(room, login, info, message)
VALUES (:room, :login, :info, :message)
")->execute(array("room" => $room, "login" => $login, "info" => $info, "message" => $message));
}
taab_echo_backend($lastId, $pdo->lastInsertId());
taab_echo_backend($lastId, $room, $pdo->lastInsertId());
?>

0 comments on commit 95ee3bb

Please sign in to comment.
You can’t perform that action at this time.