diff --git a/app/models/chat.rb b/app/models/chat.rb index 58e65eec9..1daef6bcc 100644 --- a/app/models/chat.rb +++ b/app/models/chat.rb @@ -115,4 +115,55 @@ class Chat < ApplicationModel def self.seads_available(diff = 2.minutes) seads_total(diff) - active_chat_count end + +=begin + +cleanup old chat messages + + Chat.cleanup + +optional you can parse the max oldest chat entries + + Chat.cleanup(3.months) + +=end + + def self.cleanup(diff = 3.months) + Chat::Session.where(state: 'closed').where('updated_at < ?', Time.zone.now - diff).each {|chat_session| + Chat::Message.where(chat_session_id: chat_session.id).delete_all + chat_session.destroy + } + + true + end + +=begin + +close chat sessions where participients are offline + + Chat.cleanup_close + +optional you can parse the max oldest chat sessions + + Chat.cleanup_close(5.minutes) + +=end + + def self.cleanup_close(diff = 5.minutes) + Chat::Session.where.not(state: 'closed').where('updated_at < ?', Time.zone.now - diff).each {|chat_session| + return if chat_session.recipients_active? + chat_session.state = 'closed' + chat_session.save + message = { + event: 'chat_session_closed', + data: { + session_id: chat_session.session_id, + realname: 'System', + }, + } + chat_session.send_to_recipients(message) + } + true + end + end diff --git a/db/migrate/20151210000001_update_chat.rb b/db/migrate/20151210000001_update_chat.rb new file mode 100644 index 000000000..1cdd924b4 --- /dev/null +++ b/db/migrate/20151210000001_update_chat.rb @@ -0,0 +1,25 @@ +class UpdateChat < ActiveRecord::Migration + def up + Scheduler.create_or_update( + name: 'Closed chat sessions where participients are offline.', + method: 'Chat.cleanup_close', + period: 60 * 15, + prio: 2, + active: true, + updated_by_id: 1, + created_by_id: 1, + ) + Scheduler.create_or_update( + name: 'Cleanup closed sessions.', + method: 'Chat.cleanup', + period: 5.days, + prio: 2, + active: true, + updated_by_id: 1, + created_by_id: 1, + ) + end + + def down + end +end diff --git a/db/seeds.rb b/db/seeds.rb index de600141b..13eace9bf 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -3269,6 +3269,24 @@ Scheduler.create_or_update( updated_by_id: 1, created_by_id: 1, ) +Scheduler.create_or_update( + name: 'Closed chat sessions where participients are offline.', + method: 'Chat.cleanup_close', + period: 60 * 15, + prio: 2, + active: true, + updated_by_id: 1, + created_by_id: 1, +) +Scheduler.create_or_update( + name: 'Cleanup closed sessions.', + method: 'Chat.cleanup', + period: 5.days, + prio: 2, + active: true, + updated_by_id: 1, + created_by_id: 1, +) # install locales and translations Locale.create_if_not_exists(