Mercurial > zeropaste
annotate app/models/paste.rb @ 190:d4682cea8e58
Fun with alerts.
author | Edho Arief <edho@myconan.net> |
---|---|
date | Sat, 23 Feb 2013 14:31:34 +0900 |
parents | a9dba6a3008b |
children | af80541dc915 |
rev | line source |
---|---|
2 | 1 class Paste < ActiveRecord::Base |
2 attr_accessible :paste | |
70
8f0fb869e770
Limit pastes to 100/hour per IP address.
Edho Arief <edho@myconan.net>
parents:
51
diff
changeset
|
3 before_validation :paste_limit |
22
032686a0c995
Added newline converter, integrity fixups.
Edho Arief <edho@myconan.net>
parents:
2
diff
changeset
|
4 before_validation :convert_newlines |
2 | 5 before_validation :set_paste_hash |
176
a9dba6a3008b
Initial work to add paste deletion.
Edho Arief <edho@myconan.net>
parents:
71
diff
changeset
|
6 before_validation :set_paste_key |
a9dba6a3008b
Initial work to add paste deletion.
Edho Arief <edho@myconan.net>
parents:
71
diff
changeset
|
7 validates :paste, :paste_hash, :key, :ip, :presence => true |
2 | 8 validates :paste, :length => { :maximum => 1_000_000 } |
9 | |
10 def set_paste_hash | |
51
36d07f047ec2
Or maybe not. Still too long with b62. Backed out changeset ba29d6394863
Edho Arief <edho@myconan.net>
parents:
46
diff
changeset
|
11 self.paste_hash = Digest::SHA512.hexdigest("#{paste}\n") |
2 | 12 end |
22
032686a0c995
Added newline converter, integrity fixups.
Edho Arief <edho@myconan.net>
parents:
2
diff
changeset
|
13 |
176
a9dba6a3008b
Initial work to add paste deletion.
Edho Arief <edho@myconan.net>
parents:
71
diff
changeset
|
14 def set_paste_key |
a9dba6a3008b
Initial work to add paste deletion.
Edho Arief <edho@myconan.net>
parents:
71
diff
changeset
|
15 self.key ||= SecureRandom.hex(4) |
a9dba6a3008b
Initial work to add paste deletion.
Edho Arief <edho@myconan.net>
parents:
71
diff
changeset
|
16 end |
a9dba6a3008b
Initial work to add paste deletion.
Edho Arief <edho@myconan.net>
parents:
71
diff
changeset
|
17 |
22
032686a0c995
Added newline converter, integrity fixups.
Edho Arief <edho@myconan.net>
parents:
2
diff
changeset
|
18 def convert_newlines |
71
7bb46d9febad
Ensure we're gsubbing a string.
Edho Arief <edho@myconan.net>
parents:
70
diff
changeset
|
19 self.paste = self.paste.to_s.gsub("\r\n", "\n").gsub("\r", "\n") |
22
032686a0c995
Added newline converter, integrity fixups.
Edho Arief <edho@myconan.net>
parents:
2
diff
changeset
|
20 end |
032686a0c995
Added newline converter, integrity fixups.
Edho Arief <edho@myconan.net>
parents:
2
diff
changeset
|
21 |
70
8f0fb869e770
Limit pastes to 100/hour per IP address.
Edho Arief <edho@myconan.net>
parents:
51
diff
changeset
|
22 def paste_limit |
8f0fb869e770
Limit pastes to 100/hour per IP address.
Edho Arief <edho@myconan.net>
parents:
51
diff
changeset
|
23 ip_post_recent_count = self.class.where(:ip => self.ip).where('created_at > ?', Time.now - 1.hour).count |
8f0fb869e770
Limit pastes to 100/hour per IP address.
Edho Arief <edho@myconan.net>
parents:
51
diff
changeset
|
24 errors.add :base, :limit if ip_post_recent_count > 100 |
8f0fb869e770
Limit pastes to 100/hour per IP address.
Edho Arief <edho@myconan.net>
parents:
51
diff
changeset
|
25 end |
8f0fb869e770
Limit pastes to 100/hour per IP address.
Edho Arief <edho@myconan.net>
parents:
51
diff
changeset
|
26 |
22
032686a0c995
Added newline converter, integrity fixups.
Edho Arief <edho@myconan.net>
parents:
2
diff
changeset
|
27 def self.fix_all |
032686a0c995
Added newline converter, integrity fixups.
Edho Arief <edho@myconan.net>
parents:
2
diff
changeset
|
28 self.all.each do |p| |
032686a0c995
Added newline converter, integrity fixups.
Edho Arief <edho@myconan.net>
parents:
2
diff
changeset
|
29 p.save |
032686a0c995
Added newline converter, integrity fixups.
Edho Arief <edho@myconan.net>
parents:
2
diff
changeset
|
30 end |
032686a0c995
Added newline converter, integrity fixups.
Edho Arief <edho@myconan.net>
parents:
2
diff
changeset
|
31 end |
2 | 32 end |