annotate app/models/paste.rb @ 494:e508d3226214 default tip

Update gems
author nanaya <me@nanaya.net>
date Sun, 29 Oct 2023 23:24:13 +0900
parents 84ca55a0568e
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
433
b445318de858 Inheriting ApplicationRecord
nanaya <me@myconan.net>
parents: 431
diff changeset
1 class Paste < ApplicationRecord
210
d59731c3c7bf Add support for is_private flag
Edho Arief <edho@myconan.net>
parents: 197
diff changeset
2 attr_accessor :is_private
379
2ad092e60975 [mq]: new-design
nanaya <me@myconan.net>
parents: 377
diff changeset
3 after_initialize :set_privacy, :if => :new_record?
2ad092e60975 [mq]: new-design
nanaya <me@myconan.net>
parents: 377
diff changeset
4
70
8f0fb869e770 Limit pastes to 100/hour per IP address.
Edho Arief <edho@myconan.net>
parents: 51
diff changeset
5 before_validation :paste_limit
22
032686a0c995 Added newline converter, integrity fixups.
Edho Arief <edho@myconan.net>
parents: 2
diff changeset
6 before_validation :convert_newlines
2
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
7 before_validation :set_paste_hash
176
a9dba6a3008b Initial work to add paste deletion.
Edho Arief <edho@myconan.net>
parents: 71
diff changeset
8 before_validation :set_paste_key
210
d59731c3c7bf Add support for is_private flag
Edho Arief <edho@myconan.net>
parents: 197
diff changeset
9 before_validation :set_paste_secret
176
a9dba6a3008b Initial work to add paste deletion.
Edho Arief <edho@myconan.net>
parents: 71
diff changeset
10 validates :paste, :paste_hash, :key, :ip, :presence => true
267
0bf1d6f75baa Accidentally limited pastes to 0 characters.
edogawaconan <me@myconan.net>
parents: 265
diff changeset
11 validates :paste, :length => { :maximum => 1_000_000 }
2
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
12
210
d59731c3c7bf Add support for is_private flag
Edho Arief <edho@myconan.net>
parents: 197
diff changeset
13 def to_param
d59731c3c7bf Add support for is_private flag
Edho Arief <edho@myconan.net>
parents: 197
diff changeset
14 path
d59731c3c7bf Add support for is_private flag
Edho Arief <edho@myconan.net>
parents: 197
diff changeset
15 end
d59731c3c7bf Add support for is_private flag
Edho Arief <edho@myconan.net>
parents: 197
diff changeset
16
d59731c3c7bf Add support for is_private flag
Edho Arief <edho@myconan.net>
parents: 197
diff changeset
17 def self.safe_find(raw_id)
464
f11862e58af4 Canonicalize 0-prefixed ids as well
nanaya <me@nanaya.pro>
parents: 433
diff changeset
18 /\A(?<id>[0-9]+)(?:-(?<secret>[0-9a-f]+))?\z/i =~ raw_id.to_s
425
9369ad2f2ce8 Simplify logic
nanaya <me@myconan.net>
parents: 379
diff changeset
19
464
f11862e58af4 Canonicalize 0-prefixed ids as well
nanaya <me@nanaya.pro>
parents: 433
diff changeset
20 find_by(:secret => secret.try(:downcase), :id => id)
210
d59731c3c7bf Add support for is_private flag
Edho Arief <edho@myconan.net>
parents: 197
diff changeset
21 end
d59731c3c7bf Add support for is_private flag
Edho Arief <edho@myconan.net>
parents: 197
diff changeset
22
373
6e3e1e7b0212 Handle unique error in model instead of controller.
nanaya <me@myconan.net>
parents: 330
diff changeset
23 def self.graceful_create(params)
376
02903dc8214d Fix code styles.
nanaya <me@myconan.net>
parents: 373
diff changeset
24 paste = new(params)
373
6e3e1e7b0212 Handle unique error in model instead of controller.
nanaya <me@myconan.net>
parents: 330
diff changeset
25 fresh = true
6e3e1e7b0212 Handle unique error in model instead of controller.
nanaya <me@myconan.net>
parents: 330
diff changeset
26 created = true
6e3e1e7b0212 Handle unique error in model instead of controller.
nanaya <me@myconan.net>
parents: 330
diff changeset
27
6e3e1e7b0212 Handle unique error in model instead of controller.
nanaya <me@myconan.net>
parents: 330
diff changeset
28 begin
6e3e1e7b0212 Handle unique error in model instead of controller.
nanaya <me@myconan.net>
parents: 330
diff changeset
29 created = paste.save
6e3e1e7b0212 Handle unique error in model instead of controller.
nanaya <me@myconan.net>
parents: 330
diff changeset
30 rescue ActiveRecord::RecordNotUnique
376
02903dc8214d Fix code styles.
nanaya <me@myconan.net>
parents: 373
diff changeset
31 paste = find_by(:ip => paste.ip, :paste_hash => paste.paste_hash)
373
6e3e1e7b0212 Handle unique error in model instead of controller.
nanaya <me@myconan.net>
parents: 330
diff changeset
32 fresh = false
6e3e1e7b0212 Handle unique error in model instead of controller.
nanaya <me@myconan.net>
parents: 330
diff changeset
33 end
6e3e1e7b0212 Handle unique error in model instead of controller.
nanaya <me@myconan.net>
parents: 330
diff changeset
34
376
02903dc8214d Fix code styles.
nanaya <me@myconan.net>
parents: 373
diff changeset
35 [created, paste, fresh]
373
6e3e1e7b0212 Handle unique error in model instead of controller.
nanaya <me@myconan.net>
parents: 330
diff changeset
36 end
6e3e1e7b0212 Handle unique error in model instead of controller.
nanaya <me@myconan.net>
parents: 330
diff changeset
37
316
61f7f258a6fb Move from-gzip paste parsing to model.
edogawaconan <me@myconan.net>
parents: 290
diff changeset
38 def paste_gzip=(paste)
484
84ca55a0568e Fix pasting base64 gzip and fix tests
nanaya <me@nanaya.pro>
parents: 483
diff changeset
39 paste = paste.read if paste.is_a? ActionDispatch::Http::UploadedFile
84ca55a0568e Fix pasting base64 gzip and fix tests
nanaya <me@nanaya.pro>
parents: 483
diff changeset
40
84ca55a0568e Fix pasting base64 gzip and fix tests
nanaya <me@nanaya.pro>
parents: 483
diff changeset
41 self.paste = ActiveSupport::Gzip.decompress paste
316
61f7f258a6fb Move from-gzip paste parsing to model.
edogawaconan <me@myconan.net>
parents: 290
diff changeset
42 end
61f7f258a6fb Move from-gzip paste parsing to model.
edogawaconan <me@myconan.net>
parents: 290
diff changeset
43
61f7f258a6fb Move from-gzip paste parsing to model.
edogawaconan <me@myconan.net>
parents: 290
diff changeset
44 def paste_gzip_base64=(paste)
61f7f258a6fb Move from-gzip paste parsing to model.
edogawaconan <me@myconan.net>
parents: 290
diff changeset
45 self.paste_gzip = Base64.decode64(paste)
61f7f258a6fb Move from-gzip paste parsing to model.
edogawaconan <me@myconan.net>
parents: 290
diff changeset
46 end
61f7f258a6fb Move from-gzip paste parsing to model.
edogawaconan <me@myconan.net>
parents: 290
diff changeset
47
265
6cca1ab53337 Infinitely better error messages and notice.
edogawaconan <me@myconan.net>
parents: 255
diff changeset
48 def safe_destroy(param_key)
6cca1ab53337 Infinitely better error messages and notice.
edogawaconan <me@myconan.net>
parents: 255
diff changeset
49 if key == param_key
6cca1ab53337 Infinitely better error messages and notice.
edogawaconan <me@myconan.net>
parents: 255
diff changeset
50 destroy
6cca1ab53337 Infinitely better error messages and notice.
edogawaconan <me@myconan.net>
parents: 255
diff changeset
51 else
6cca1ab53337 Infinitely better error messages and notice.
edogawaconan <me@myconan.net>
parents: 255
diff changeset
52 errors.add(:key, "is invalid")
6cca1ab53337 Infinitely better error messages and notice.
edogawaconan <me@myconan.net>
parents: 255
diff changeset
53 false
6cca1ab53337 Infinitely better error messages and notice.
edogawaconan <me@myconan.net>
parents: 255
diff changeset
54 end
6cca1ab53337 Infinitely better error messages and notice.
edogawaconan <me@myconan.net>
parents: 255
diff changeset
55 end
6cca1ab53337 Infinitely better error messages and notice.
edogawaconan <me@myconan.net>
parents: 255
diff changeset
56
210
d59731c3c7bf Add support for is_private flag
Edho Arief <edho@myconan.net>
parents: 197
diff changeset
57 def path
d59731c3c7bf Add support for is_private flag
Edho Arief <edho@myconan.net>
parents: 197
diff changeset
58 [id, secret.presence].compact.join("-")
d59731c3c7bf Add support for is_private flag
Edho Arief <edho@myconan.net>
parents: 197
diff changeset
59 end
d59731c3c7bf Add support for is_private flag
Edho Arief <edho@myconan.net>
parents: 197
diff changeset
60
2
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
61 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
62 self.paste_hash = Digest::SHA512.hexdigest("#{paste}\n")
2
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
63 end
22
032686a0c995 Added newline converter, integrity fixups.
Edho Arief <edho@myconan.net>
parents: 2
diff changeset
64
176
a9dba6a3008b Initial work to add paste deletion.
Edho Arief <edho@myconan.net>
parents: 71
diff changeset
65 def set_paste_key
a9dba6a3008b Initial work to add paste deletion.
Edho Arief <edho@myconan.net>
parents: 71
diff changeset
66 self.key ||= SecureRandom.hex(4)
a9dba6a3008b Initial work to add paste deletion.
Edho Arief <edho@myconan.net>
parents: 71
diff changeset
67 end
a9dba6a3008b Initial work to add paste deletion.
Edho Arief <edho@myconan.net>
parents: 71
diff changeset
68
210
d59731c3c7bf Add support for is_private flag
Edho Arief <edho@myconan.net>
parents: 197
diff changeset
69 def set_paste_secret
330
96631607785f Even more coding style.
edogawaconan <me@myconan.net>
parents: 316
diff changeset
70 self.secret = SecureRandom.hex(4) if is_private?
230
1c750d3cde1b Correct way to test is_private flag.
Edho Arief <edho@myconan.net>
parents: 229
diff changeset
71 end
1c750d3cde1b Correct way to test is_private flag.
Edho Arief <edho@myconan.net>
parents: 229
diff changeset
72
1c750d3cde1b Correct way to test is_private flag.
Edho Arief <edho@myconan.net>
parents: 229
diff changeset
73 def is_private?
330
96631607785f Even more coding style.
edogawaconan <me@myconan.net>
parents: 316
diff changeset
74 is_private == "1"
210
d59731c3c7bf Add support for is_private flag
Edho Arief <edho@myconan.net>
parents: 197
diff changeset
75 end
d59731c3c7bf Add support for is_private flag
Edho Arief <edho@myconan.net>
parents: 197
diff changeset
76
22
032686a0c995 Added newline converter, integrity fixups.
Edho Arief <edho@myconan.net>
parents: 2
diff changeset
77 def convert_newlines
431
68eb23b52864 Rails 5 \o/
nanaya <me@myconan.net>
parents: 428
diff changeset
78 self.paste = paste.to_s.gsub("\r\n", "\n").tr("\r", "\n")
22
032686a0c995 Added newline converter, integrity fixups.
Edho Arief <edho@myconan.net>
parents: 2
diff changeset
79 end
032686a0c995 Added newline converter, integrity fixups.
Edho Arief <edho@myconan.net>
parents: 2
diff changeset
80
70
8f0fb869e770 Limit pastes to 100/hour per IP address.
Edho Arief <edho@myconan.net>
parents: 51
diff changeset
81 def paste_limit
377
4e6afc8140fb Use Time.zone as per rubocop recommendation.
nanaya <me@myconan.net>
parents: 376
diff changeset
82 ip_post_recent_count = self.class.where(:ip => ip).where("created_at > ?", Time.zone.now - 1.hour).count
70
8f0fb869e770 Limit pastes to 100/hour per IP address.
Edho Arief <edho@myconan.net>
parents: 51
diff changeset
83 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
84 end
8f0fb869e770 Limit pastes to 100/hour per IP address.
Edho Arief <edho@myconan.net>
parents: 51
diff changeset
85
379
2ad092e60975 [mq]: new-design
nanaya <me@myconan.net>
parents: 377
diff changeset
86 def set_privacy
2ad092e60975 [mq]: new-design
nanaya <me@myconan.net>
parents: 377
diff changeset
87 self.is_private ||= "0"
2ad092e60975 [mq]: new-design
nanaya <me@myconan.net>
parents: 377
diff changeset
88 end
2ad092e60975 [mq]: new-design
nanaya <me@myconan.net>
parents: 377
diff changeset
89
22
032686a0c995 Added newline converter, integrity fixups.
Edho Arief <edho@myconan.net>
parents: 2
diff changeset
90 def self.fix_all
255
a894d7696b7e More useful return of Paste.fix_all.
edogawaconan <me@myconan.net>
parents: 247
diff changeset
91 stats = Hash.new(0)
376
02903dc8214d Fix code styles.
nanaya <me@myconan.net>
parents: 373
diff changeset
92 all.find_each do |p|
22
032686a0c995 Added newline converter, integrity fixups.
Edho Arief <edho@myconan.net>
parents: 2
diff changeset
93 p.save
255
a894d7696b7e More useful return of Paste.fix_all.
edogawaconan <me@myconan.net>
parents: 247
diff changeset
94 stats[:count] += 1
a894d7696b7e More useful return of Paste.fix_all.
edogawaconan <me@myconan.net>
parents: 247
diff changeset
95 stats[:private] += 1 if p.secret
22
032686a0c995 Added newline converter, integrity fixups.
Edho Arief <edho@myconan.net>
parents: 2
diff changeset
96 end
330
96631607785f Even more coding style.
edogawaconan <me@myconan.net>
parents: 316
diff changeset
97 stats
22
032686a0c995 Added newline converter, integrity fixups.
Edho Arief <edho@myconan.net>
parents: 2
diff changeset
98 end
2
42de15334db1 Added the pastes.
Edho Arief <edho@myconan.net>
parents:
diff changeset
99 end