2013-02-05 14:16:51 -05:00
|
|
|
class Draft < ActiveRecord::Base
|
|
|
|
NEW_TOPIC = 'new_topic'
|
|
|
|
NEW_PRIVATE_MESSAGE = 'new_private_message'
|
|
|
|
EXISTING_TOPIC = 'topic_'
|
|
|
|
|
|
|
|
def self.set(user, key, sequence, data)
|
2017-07-27 21:20:09 -04:00
|
|
|
d = find_draft(user, key)
|
2013-02-05 14:16:51 -05:00
|
|
|
if d
|
|
|
|
return if d.sequence > sequence
|
2015-08-03 00:29:04 -04:00
|
|
|
exec_sql("UPDATE drafts
|
|
|
|
SET data = :data,
|
|
|
|
sequence = :sequence,
|
|
|
|
revisions = revisions + 1
|
|
|
|
WHERE id = :id", id: d.id, sequence: sequence, data: data)
|
2013-02-05 14:16:51 -05:00
|
|
|
else
|
2013-10-01 22:17:27 -04:00
|
|
|
Draft.create!(user_id: user.id, draft_key: key, data: data, sequence: sequence)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.get(user, key, sequence)
|
2017-07-27 21:20:09 -04:00
|
|
|
d = find_draft(user, key)
|
2013-02-07 10:45:24 -05:00
|
|
|
if d && d.sequence == sequence
|
2013-02-05 14:16:51 -05:00
|
|
|
d.data
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.clear(user, key, sequence)
|
2017-07-27 21:20:09 -04:00
|
|
|
d = find_draft(user, key)
|
2013-02-05 14:16:51 -05:00
|
|
|
if d && d.sequence <= sequence
|
|
|
|
d.destroy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-03-25 18:56:21 -04:00
|
|
|
def self.find_draft(user, key)
|
|
|
|
if user.is_a?(User)
|
|
|
|
find_by(user_id: user.id, draft_key: key)
|
|
|
|
else
|
|
|
|
find_by(user_id: user, draft_key: key)
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2015-06-01 23:45:47 -04:00
|
|
|
|
|
|
|
def self.cleanup!
|
|
|
|
exec_sql("DELETE FROM drafts where sequence < (
|
|
|
|
SELECT max(s.sequence) from draft_sequences s
|
|
|
|
WHERE s.draft_key = drafts.draft_key AND
|
|
|
|
s.user_id = drafts.user_id
|
|
|
|
)")
|
2015-06-03 04:52:41 -04:00
|
|
|
|
|
|
|
# remove old drafts
|
|
|
|
delete_drafts_older_than_n_days = SiteSetting.delete_drafts_older_than_n_days.days.ago
|
|
|
|
Draft.where("updated_at < ?", delete_drafts_older_than_n_days).destroy_all
|
2015-06-01 23:45:47 -04:00
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2013-05-23 22:48:32 -04:00
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: drafts
|
|
|
|
#
|
|
|
|
# id :integer not null, primary key
|
|
|
|
# user_id :integer not null
|
2016-02-22 18:33:53 -05:00
|
|
|
# draft_key :string not null
|
2013-05-23 22:48:32 -04:00
|
|
|
# data :text not null
|
2014-08-27 01:19:25 -04:00
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
2013-05-23 22:48:32 -04:00
|
|
|
# sequence :integer default(0), not null
|
2015-09-17 20:41:10 -04:00
|
|
|
# revisions :integer default(1), not null
|
2013-05-23 22:48:32 -04:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
|
|
|
# index_drafts_on_user_id_and_draft_key (user_id,draft_key)
|
|
|
|
#
|