From 0d326dfb45734950159030ea02b02dec4fba55b7 Mon Sep 17 00:00:00 2001 From: friendica Date: Wed, 29 Jan 2014 18:02:51 -0800 Subject: mod/chatsvc - ajax backend for chat --- mod/chatsvc.php | 104 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 mod/chatsvc.php (limited to 'mod/chatsvc.php') diff --git a/mod/chatsvc.php b/mod/chatsvc.php new file mode 100644 index 000000000..b8d2a9f69 --- /dev/null +++ b/mod/chatsvc.php @@ -0,0 +1,104 @@ + false); + + $a->data['chat']['room_id'] = intval($_REQUEST['room_id']); + $x = q("select cr_uid from chatroom where cr_id = %d and cr_id != 0 limit 1", + intval($a->data['chat']['room_id']) + ); + if(! $x) + json_return_and_die($ret); + + $a->data['chat']['uid'] = $x[0]['cr_uid']; + + if(! perm_is_allowed($a->data['chat']['uid'],get_observer_hash(),'chat')) { + json_return_and_die($ret); + } + +} + +function chatsvc_post(&$a) { + + $ret = array('success' => false); + + $room_id = $a->data['chat']['room_id']; + $text = escape_tags($_REQUEST['chat_text']); + + + $sql_extra = permissions_sql($a->data['chat']['uid']); + + $r = q("select * from chatroom where cr_uid = %d and cr_id = %d $sql_extra", + intval($a->data['chat']['uid']), + intval($a->data['chat']['room_id']) + ); + if(! $r) + json_return_and_die($ret); + + $x = q("insert into chat ( chat_room, chat_xchan, created, chat_text ) + values( %d, '%s', '%s', '%s' )", + intval($a->data['chat']['room_id']), + dbesc(get_observer_hash()), + dbesc(datetime_convert()), + dbesc($text) + ); + +} + +function chatsvc_content(&$a) { + + $lastseen = intval($_REQUEST['last']); + + $ret = array('success' => false); + + $sql_extra = permissions_sql($a->data['chat']['uid']); + + $r = q("select * from chatroom where cr_uid = %d and cr_id = %d $sql_extra", + intval($a->data['chat']['uid']), + intval($a->data['chat']['room_id']) + ); + if(! $r) + json_return_and_die($ret); + + $inroom = array(); + + $r = q("select * from chatpresence left join xchan on xchan_hash = cp_xchan where cp_room = %d order by xchan_name", + intval($a->data['chat']['room_id']) + ); + if($r) { + foreach($r as $rr) { + $inroom[] = array('img' => zid($rr['xchan_photo_m']), 'img_type' => $rr['xchan_photo_mimetype'],'name' => $rr['xchan_name']); + } + } + + $chats = array(); + + $r = q("select * from chat left join xchan on chat_xchan = xchan_hash where chat_room = %d and chat_id > %d", + intval($a->data['chat']['room_id']), + intval($lastseen) + ); + if($r) { + foreach($r as $rr) { + $chats[] = array( + 'id' => $rr['chat_id'], + 'img' => zid($rr['xchan_photo_m']), + 'img_type' => $rr['xchan_photo_mimetype'], + 'name' => $rr['xchan_name'], + 'isotime' => datetime_convert('UTC', date_default_timezone_get(), $rr['created'], 'c'), + 'localtime' => datetime_convert('UTC', date_default_timezone_get(), $rr['created'], 'r'), + 'text' => smilies(bbcode($rr['chat_text'])) + ); + } + } + + $ret['success'] = true; + $ret['inroom'] = $inroom; + $ret['chats'] = $chats; + + json_return_and_die($ret); + +} + \ No newline at end of file -- cgit v1.2.3 From 9fdee53c9a35d584de8cacd071c6608de88a18b6 Mon Sep 17 00:00:00 2001 From: friendica Date: Wed, 29 Jan 2014 20:14:18 -0800 Subject: start on the ajax bits --- mod/chatsvc.php | 2 ++ 1 file changed, 2 insertions(+) (limited to 'mod/chatsvc.php') diff --git a/mod/chatsvc.php b/mod/chatsvc.php index b8d2a9f69..23b95cd1c 100644 --- a/mod/chatsvc.php +++ b/mod/chatsvc.php @@ -4,6 +4,8 @@ require_once('include/security.php'); function chatsvc_init(&$a) { +//logger('chatsvc'); + $ret = array('success' => false); $a->data['chat']['room_id'] = intval($_REQUEST['room_id']); -- cgit v1.2.3 From 7b609782623d91c2bdf81c04cd801aaae927e9fa Mon Sep 17 00:00:00 2001 From: friendica Date: Wed, 29 Jan 2014 20:40:37 -0800 Subject: back-end for changing online status and keeping presence refreshed --- mod/chatsvc.php | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) (limited to 'mod/chatsvc.php') diff --git a/mod/chatsvc.php b/mod/chatsvc.php index 23b95cd1c..39161b5a7 100644 --- a/mod/chatsvc.php +++ b/mod/chatsvc.php @@ -29,7 +29,19 @@ function chatsvc_post(&$a) { $room_id = $a->data['chat']['room_id']; $text = escape_tags($_REQUEST['chat_text']); - + $status = strip_tags($_REQUEST['status']); + + if($status && $room_id) { + $r = q("update chatpresence set cp_status = '%s', cp_last = '%s' where cp_room = %d and cp_xchan = '%s' and cp_client = '%s' limit 1", + dbesc($status), + dbesc(datetime_convert()), + intval($room_id), + dbesc(get_observer_hash()), + dbesc($_SERVER['REMOTE_ADDR']) + ); + } + if(! $text) + return; $sql_extra = permissions_sql($a->data['chat']['uid']); @@ -96,6 +108,13 @@ function chatsvc_content(&$a) { } } + $r = q("update chatpresence set cp_last = '%s' where cp_room = %d and cp_xchan = '%s' and cp_client = '%s' limit 1", + dbesc(datetime_convert()), + intval($room_id), + dbesc(get_observer_hash()), + dbesc($_SERVER['REMOTE_ADDR']) + ); + $ret['success'] = true; $ret['inroom'] = $inroom; $ret['chats'] = $chats; -- cgit v1.2.3 From 6c6a9b963a925d33b2cc436d877a4edc5f0d59b1 Mon Sep 17 00:00:00 2001 From: friendica Date: Thu, 30 Jan 2014 01:00:46 -0800 Subject: a bit more ajax work on chat and chatsvc and some fiddling with layouts --- mod/chatsvc.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'mod/chatsvc.php') diff --git a/mod/chatsvc.php b/mod/chatsvc.php index 39161b5a7..d76a87462 100644 --- a/mod/chatsvc.php +++ b/mod/chatsvc.php @@ -59,7 +59,8 @@ function chatsvc_post(&$a) { dbesc(datetime_convert()), dbesc($text) ); - + $ret['success'] = true; + json_return_and_die($ret); } function chatsvc_content(&$a) { -- cgit v1.2.3