forked from thesecretmaster/ips-comment-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.rb
150 lines (125 loc) · 3.34 KB
/
db.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
require "active_record"
require "htmlentities"
def setup_db(db_location)
ActiveRecord::Base.establish_connection(
adapter: "sqlite3",
database: db_location
)
end
def wipe_db
User.delete_all
Comment.delete_all
Regex.delete_all
Reason.delete_all
Room.delete_all
WhitelistedUser.delete_all
Feedback.delete_all
ChatUser.delete_all
end
class User < ActiveRecord::Base
has_many :comments
end
class Comment < ActiveRecord::Base
belongs_to :owner, class_name: "User"
# has_one :user, as: :reply_to_user
before_save :update_creation_date
def update_creation_date
self.creation_date = Time.at(self.se_creation_date.to_i).to_datetime
end
def self.record_comment(comment, logger, perspective_score:)
#return false unless comment.is_a? SE::API::Comment
c = Comment.new
%i[body body_markdown comment_id edited link post_id post_type score].each do |f|
value = comment.send(f)
value = HTMLEntities.new.decode(value) if %i[body body_markdown].include? f
c.send(:"#{f}=", value)
end
c.perspective_score = perspective_score
c.se_creation_date = comment.creation_date
#TODO: This looks like a bug...it'll think that any comment with tps/fps marked doesn't exist (so I believe)
# couldn't we just do a lookup by id??
if Comment.exists?(c.attributes.reject { |_k,v| v.nil? })
Comment.find_by(c.attributes.reject { |_k,v| v.nil? })
else
api_u = comment.owner
u = User.find_or_create_by(user_id: api_u.id)
u.update(display_name: api_u.name, reputation: api_u.reputation, link: api_u.link, user_type: api_u.type)
c.owner = u
logger.debug u.inspect
logger.debug c.inspect
if c.save
c
else
logger.error c.errors.full_messages
end
end
end
def add_remove_feedback(feedback_id, is_undo)
case feedback_id
when FeedbackTypedef.tp
self.tps += (is_undo ? -1 : 1)
when FeedbackTypedef.fp
self.fps += (is_undo ? -1 : 1)
when FeedbackTypedef.rude
self.rude += (is_undo ? -1 : 1)
self.tps += (is_undo ? -1 : 1)
end
self.save
end
def add_feedback(feedback_id)
add_remove_feedback(feedback_id, false)
end
def remove_feedback(feedback_id)
add_remove_feedback(feedback_id, true)
end
end
class Regex < ActiveRecord::Base
belongs_to :reason
end
class Reason < ActiveRecord::Base
has_many :regexes
end
class Room < ActiveRecord::Base
def self.reports
{
"regex" => :regex_match,
"hot" => :hot_post,
"inactive" => :inactive_post,
"custom" => :custom_report,
"animals" => :animals
}
end
def on?
self.on
end
def turn_on
self.update(on: true)
end
def turn_off
self.update(on: false)
end
end
class WhitelistedUser < ActiveRecord::Base
end
class ChatUser < ActiveRecord::Base
has_many :feedbacks
end
class FeedbackTypedef < ActiveRecord::Base
has_many :feedbacks
def self.tp
FeedbackTypedef.find_by(feedback: "tp").id
end
def self.fp
FeedbackTypedef.find_by(feedback: "fp").id
end
def self.rude
FeedbackTypedef.find_by(feedback: "rude").id
end
def self.feedback_name(feedback_id)
FeedbackTypedef.find_by(id: feedback_id).feedback
end
end
class Feedback < ActiveRecord::Base
belongs_to :chat_user, class_name: "ChatUser"
belongs_to :feedback_type, class_name: "FeedbackTypedef"
end