@@ -33,74 +33,80 @@ def self.accept_answer!(post, acting_user, topic: nil)
33
33
DistributedMutex . synchronize ( "discourse_solved_toggle_answer_#{ topic . id } " ) do
34
34
solved = topic . solved
35
35
36
- if previous_accepted_post_id = solved &.answer_post_id
37
- UserAction . where (
38
- action_type : UserAction ::SOLVED ,
39
- target_post_id : previous_accepted_post_id ,
40
- ) . destroy_all
41
- else
42
- UserAction . log_action! (
43
- action_type : UserAction ::SOLVED ,
44
- user_id : post . user_id ,
45
- acting_user_id : acting_user . id ,
46
- target_post_id : post . id ,
47
- target_topic_id : post . topic_id ,
48
- )
49
- end
50
-
51
- solved ||=
52
- DiscourseSolved ::SolvedTopic . new ( topic :, answer_post : post , accepter : acting_user )
53
-
54
- notification_data = {
55
- message : "solved.accepted_notification" ,
56
- display_username : acting_user . username ,
57
- topic_title : topic . title ,
58
- title : "solved.notification.title" ,
59
- } . to_json
60
-
61
- unless acting_user . id == post . user_id
62
- Notification . create! (
63
- notification_type : Notification . types [ :custom ] ,
64
- user_id : post . user_id ,
65
- topic_id : post . topic_id ,
66
- post_number : post . post_number ,
67
- data : notification_data ,
68
- )
69
- end
70
-
71
- if SiteSetting . notify_on_staff_accept_solved && acting_user . id != topic . user_id
72
- Notification . create! (
73
- notification_type : Notification . types [ :custom ] ,
74
- user_id : topic . user_id ,
75
- topic_id : post . topic_id ,
76
- post_number : post . post_number ,
77
- data : notification_data ,
78
- )
79
- end
80
-
81
- auto_close_hours = 0
82
- if topic &.category . present?
83
- auto_close_hours = topic . category . custom_fields [ "solved_topics_auto_close_hours" ] . to_i
84
- auto_close_hours = 175_200 if auto_close_hours > 175_200 # 20 years
85
- end
86
-
87
- auto_close_hours = SiteSetting . solved_topics_auto_close_hours if auto_close_hours == 0
88
-
89
- if ( auto_close_hours > 0 ) && !topic . closed
90
- topic_timer =
91
- topic . set_or_create_timer (
92
- TopicTimer . types [ :silent_close ] ,
93
- nil ,
94
- based_on_last_post : true ,
95
- duration_minutes : auto_close_hours * 60 ,
36
+ ActiveRecord ::Base . transaction do
37
+ if previous_accepted_post_id = solved &.answer_post_id
38
+ UserAction . where (
39
+ action_type : UserAction ::SOLVED ,
40
+ target_post_id : previous_accepted_post_id ,
41
+ ) . destroy_all
42
+ solved . destroy!
43
+ else
44
+ UserAction . log_action! (
45
+ action_type : UserAction ::SOLVED ,
46
+ user_id : post . user_id ,
47
+ acting_user_id : acting_user . id ,
48
+ target_post_id : post . id ,
49
+ target_topic_id : post . topic_id ,
96
50
)
97
- solved . topic_timer = topic_timer
98
-
99
- MessageBus . publish ( "/topic/#{ topic . id } " , reload_topic : true )
51
+ end
52
+
53
+ solved =
54
+ DiscourseSolved ::SolvedTopic . new ( topic :, answer_post : post , accepter : acting_user )
55
+
56
+ unless acting_user . id == post . user_id
57
+ Notification . create! (
58
+ notification_type : Notification . types [ :custom ] ,
59
+ user_id : post . user_id ,
60
+ topic_id : post . topic_id ,
61
+ post_number : post . post_number ,
62
+ data : {
63
+ message : "solved.accepted_notification" ,
64
+ display_username : acting_user . username ,
65
+ topic_title : topic . title ,
66
+ title : "solved.notification.title" ,
67
+ } . to_json ,
68
+ )
69
+ end
70
+
71
+ if SiteSetting . notify_on_staff_accept_solved && acting_user . id != topic . user_id
72
+ Notification . create! (
73
+ notification_type : Notification . types [ :custom ] ,
74
+ user_id : topic . user_id ,
75
+ topic_id : post . topic_id ,
76
+ post_number : post . post_number ,
77
+ data : {
78
+ message : "solved.accepted_notification" ,
79
+ display_username : acting_user . username ,
80
+ topic_title : topic . title ,
81
+ title : "solved.notification.title" ,
82
+ } . to_json ,
83
+ )
84
+ end
85
+
86
+ auto_close_hours = 0
87
+ if topic &.category . present?
88
+ auto_close_hours = topic . category . custom_fields [ "solved_topics_auto_close_hours" ] . to_i
89
+ auto_close_hours = 175_200 if auto_close_hours > 175_200 # 20 years
90
+ end
91
+
92
+ auto_close_hours = SiteSetting . solved_topics_auto_close_hours if auto_close_hours == 0
93
+
94
+ if ( auto_close_hours > 0 ) && !topic . closed
95
+ topic_timer =
96
+ topic . set_or_create_timer (
97
+ TopicTimer . types [ :silent_close ] ,
98
+ nil ,
99
+ based_on_last_post : true ,
100
+ duration_minutes : auto_close_hours * 60 ,
101
+ )
102
+ solved . topic_timer = topic_timer
103
+
104
+ MessageBus . publish ( "/topic/#{ topic . id } " , reload_topic : true )
105
+ end
106
+
107
+ solved . save!
100
108
end
101
109
102
- solved . save!
103
-
104
110
if WebHook . active_web_hooks ( :accepted_solution ) . exists?
105
111
payload = WebHook . generate_payload ( :post , post )
106
112
WebHook . enqueue_solved_hooks ( :accepted_solution , post , payload )
@@ -127,7 +133,6 @@ def self.unaccept_answer!(post, topic: nil)
127
133
topic_id : post . topic_id ,
128
134
post_number : post . post_number ,
129
135
) &.destroy!
130
- solved . topic_timer . destroy! if solved . topic_timer
131
136
solved . destroy!
132
137
end
133
138
0 commit comments