diff --git a/README.md b/README.md index 1973807..a6e2f91 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,11 @@ This project is the main Comunic RestAPI. It assures data backend support. (c) Pierre HUBERT since 2017 # Crons required for Comunic -Currently, this server does not make use of crons. + +## Calls cron +There is a cron to automatically cleanup old conversation. Ideally this cron should be executed every 30 seconds. The file to execute is `bin/clean_calls` + + # Use calls in Comunic To use calls in Comunic, you need a WebRTCSignalExchangerServer, a small signal exchanging server written using NodeJS. You also need to modify your configuration file located at `config/overwrite.php` by copying and pasting commented configuration located at `config/calls.php` and make it fit your needs. diff --git a/bin/clean_calls b/bin/clean_calls new file mode 100755 index 0000000..ea0a4c0 --- /dev/null +++ b/bin/clean_calls @@ -0,0 +1,18 @@ +#!/usr/bin/env php + +############################ +# ComunicAPI calls cleaner # +# # +# @author Pierre HUBERT # +############################ + +Automatically remove old calls + +calls->cleanCalls()) + echo "Could not clean calls!"; + +echo "Calls successfully cleaned."; \ No newline at end of file diff --git a/classes/components/CallsComponent.php b/classes/components/CallsComponent.php index 084ac8f..b78b9eb 100644 --- a/classes/components/CallsComponent.php +++ b/classes/components/CallsComponent.php @@ -304,6 +304,47 @@ class CallsComponents { return true; } + /** + * Delete all old call + * + * @return bool TRUE for a success / FALSE else + */ + public function cleanCalls() : bool { + + //Compute timeout time + $old_time = time() - cs()->config->get("calls_expiration"); + + //Get the list of old calls + $calls = db()->select( + self::CALLS_LIST_TABLE, + "WHERE last_active < ?", + array($old_time), + array("id") + ); + + //Process each result + foreach($calls as $call){ + + //Delete all the members of the call + db()->deleteEntry( + self::CALLS_MEMBERS_TABLE, + "call_id = ?", + array($call["id"]) + ); + + } + + //Delete calls entries + db()->deleteEntry( + self::CALLS_LIST_TABLE, + "last_active < ?", + array($old_time) + ); + + //Success + return true; + } + /** * Turn a database entry into a CallInformation object * diff --git a/config/calls.php b/config/calls.php index 6cf065b..1c4726c 100644 --- a/config/calls.php +++ b/config/calls.php @@ -26,4 +26,12 @@ $config->set("calls", array( "turn_username" => "anonymous", "turn_password" => "anonymous" )); -*/ \ No newline at end of file +*/ + +/** + * Calls expiration time + * + * The amount of time of inactivity for what the call get + * automatically deleted + */ +$config->set("calls_expiration", 30); \ No newline at end of file