import and sync chatrooms

This commit is contained in:
redmatrix 2015-09-03 18:44:40 -07:00
parent d93e1c84ac
commit d79e81a069
6 changed files with 114 additions and 4 deletions

View File

@ -91,6 +91,8 @@ function chatroom_destroy($channel,$arr) {
return $ret; return $ret;
} }
create_sync_packet($channel['channel_id'],array('chatroom' => $r));
q("delete from chatroom where cr_id = %d", q("delete from chatroom where cr_id = %d",
intval($r[0]['cr_id']) intval($r[0]['cr_id'])
); );

View File

@ -566,13 +566,18 @@ function identity_basic_export($channel_id, $items = false) {
if($r) if($r)
$ret['obj'] = $r; $ret['obj'] = $r;
$r = q("select * from app where app_channel = %d", $r = q("select * from app where app_channel = %d",
intval($channel_id) intval($channel_id)
); );
if($r) if($r)
$ret['app'] = $r; $ret['app'] = $r;
$r = q("select * from chatroom where cr_uid = %d",
intval($channel_id)
);
if($r)
$ret['chatroom'] = $r;
if(! $items) if(! $items)
return $ret; return $ret;

View File

@ -349,7 +349,7 @@ function sync_apps($channel,$apps) {
intval($channel['channel_id']) intval($channel['channel_id'])
); );
if($x) { if($x) {
if($x[0]['app_edited'] >= $obj['app_edited']) if($x[0]['app_edited'] >= $app['app_edited'])
continue; continue;
$exists = true; $exists = true;
} }
@ -378,3 +378,98 @@ function sync_apps($channel,$apps) {
} }
} }
} }
function import_chatrooms($channel,$chatrooms) {
if($channel && $chatrooms) {
foreach($chatrooms as $chatroom) {
if(! $chatroom['cr_name'])
continue;
unset($chatroom['cr_id']);
unset($chatroom['cr_aid']);
unset($chatroom['cr_uid']);
$chatroom['cr_aid'] = $channel['channel_account_id'];
$chatroom['cr_uid'] = $channel['channel_id'];
dbesc_array($chatroom);
$r = dbq("INSERT INTO chatroom (`"
. implode("`, `", array_keys($chatroom))
. "`) VALUES ('"
. implode("', '", array_values($chatroom))
. "')"
);
}
}
}
function sync_chatrooms($channel,$chatrooms) {
if($channel && $chatrooms) {
foreach($chatrooms as $chatroom) {
if(! $chatroom['cr_name'])
continue;
if(array_key_exists('cr_deleted',$chatroom) && $chatroom['cr_deleted']) {
q("delete from chatroom where cr_name = '%s' and cr_uid = %d limit 1",
dbesc($chatroom['cr_name']),
intval($channel['channel_id'])
);
continue;
}
unset($chatroom['cr_id']);
unset($chatroom['cr_aid']);
unset($chatroom['cr_uid']);
if(! $chatroom['cr_created'] || $chatroom['cr_created'] === NULL_DATE)
$chatroom['cr_created'] = datetime_convert();
if(! $chatroom['cr_edited'] || $chatroom['cr_edited'] === NULL_DATE)
$chatroom['cr_edited'] = datetime_convert();
$chatroom['cr_aid'] = $channel['channel_account_id'];
$chatroom['cr_uid'] = $channel['channel_id'];
$exists = false;
$x = q("select * from chatroom where cr_name = '%s' and cr_uid = %d limit 1",
dbesc($chatroom['cr_name']),
intval($channel['channel_id'])
);
if($x) {
if($x[0]['cr_edited'] >= $chatroom['cr_edited'])
continue;
$exists = true;
}
$name = $chatroom['cr_name'];
if($exists) {
foreach($chatroom as $k => $v) {
$r = q("UPDATE chatroom SET `%s` = '%s' WHERE cr_name = '%s' AND cr_uid = %d",
dbesc($k),
dbesc($v),
dbesc($name),
intval($channel['channel_id'])
);
}
}
else {
dbesc_array($chatroom);
$r = dbq("INSERT INTO chatroom (`"
. implode("`, `", array_keys($chatroom))
. "`) VALUES ('"
. implode("', '", array_values($chatroom))
. "')"
);
}
}
}
}

View File

@ -2881,6 +2881,9 @@ function process_channel_sync_delivery($sender, $arr, $deliveries) {
if(array_key_exists('app',$arr) && $arr['app']) if(array_key_exists('app',$arr) && $arr['app'])
sync_apps($channel,$arr['app']); sync_apps($channel,$arr['app']);
if(array_key_exists('chatroom',$arr) && $arr['chatroom'])
sync_apps($channel,$arr['chatroom']);
if(array_key_exists('channel',$arr) && is_array($arr['channel']) && count($arr['channel'])) { if(array_key_exists('channel',$arr) && is_array($arr['channel']) && count($arr['channel'])) {
if(array_key_exists('channel_page_flags',$arr['channel']) && intval($arr['channel']['channel_pageflags'])) { if(array_key_exists('channel_page_flags',$arr['channel']) && intval($arr['channel']['channel_pageflags'])) {
$arr['channel']['channel_removed'] = (($arr['channel']['channel_pageflags'] & 0x8000) ? 1 : 0); $arr['channel']['channel_removed'] = (($arr['channel']['channel_pageflags'] & 0x8000) ? 1 : 0);

View File

@ -62,11 +62,13 @@ function chat_post(&$a) {
chatroom_create($channel,$arr); chatroom_create($channel,$arr);
$x = q("select cr_id from chatroom where cr_name = '%s' and cr_uid = %d limit 1", $x = q("select * from chatroom where cr_name = '%s' and cr_uid = %d limit 1",
dbesc($room), dbesc($room),
intval(local_channel()) intval(local_channel())
); );
create_sync_packet(0, array['chatroom'] => $x);
if($x) if($x)
goaway(z_root() . '/chat/' . $channel['channel_address'] . '/' . $x[0]['cr_id']); goaway(z_root() . '/chat/' . $channel['channel_address'] . '/' . $x[0]['cr_id']);

View File

@ -435,6 +435,9 @@ function import_post(&$a) {
if(is_array($data['app'])) if(is_array($data['app']))
import_apps($channel,$data['app']); import_apps($channel,$data['app']);
if(is_array($data['chatroom']))
import_chatrooms($channel,$data['chatroom']);
$saved_notification_flags = notifications_off($channel['channel_id']); $saved_notification_flags = notifications_off($channel['channel_id']);
if($import_posts && array_key_exists('item',$data) && $data['item']) { if($import_posts && array_key_exists('item',$data) && $data['item']) {