Skip to content

Commit 74e0f3d

Browse files
Filter deactivated comments (#401)
1 parent 75b0362 commit 74e0f3d

File tree

3 files changed

+147
-0
lines changed

3 files changed

+147
-0
lines changed

api/v1_track_comment_count.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ func (app *ApiServer) v1TrackCommentCount(c *fiber.Ctx) error {
3636
WHERE score < 0
3737
),
3838
39+
-- Deactivated users
40+
deactivated_users AS (
41+
SELECT user_id
42+
FROM users
43+
WHERE is_deactivated = true
44+
),
45+
3946
-- Comments reported by high-karma users
4047
high_karma_reporters AS (
4148
SELECT comment_id
@@ -63,6 +70,11 @@ func (app *ApiServer) v1TrackCommentCount(c *fiber.Ctx) error {
6370
low_abuse_score.user_id = comments.user_id
6471
AND @myId != comments.user_id -- always show comments to their poster
6572
AND track.owner_id != comments.user_id -- always show comments from the track owner
73+
)
74+
LEFT JOIN deactivated_users ON (
75+
deactivated_users.user_id = comments.user_id
76+
AND @myId != comments.user_id -- always show comments to their poster
77+
AND track.owner_id != comments.user_id -- always show comments from the track owner
6678
)
6779
WHERE comments.entity_id = @trackId
6880
AND comments.entity_type = 'Track'
@@ -87,6 +99,8 @@ func (app *ApiServer) v1TrackCommentCount(c *fiber.Ctx) error {
8799
)
88100
-- Filter out comments from users with low abuse score
89101
AND low_abuse_score.user_id IS NULL
102+
-- Filter out comments from deactivated users
103+
AND deactivated_users.user_id IS NULL
90104
`
91105

92106
rows, err := app.pool.Query(c.Context(), sql, pgx.NamedArgs{

api/v1_track_comments.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ func (app *ApiServer) v1TrackComments(c *fiber.Ctx) error {
3434
WHERE score < 0
3535
),
3636
37+
-- Deactivated users
38+
deactivated_users AS (
39+
SELECT user_id
40+
FROM users
41+
WHERE is_deactivated = true
42+
),
43+
3744
-- Comments reported by high-karma users
3845
high_karma_reporters AS (
3946
SELECT comment_reports.comment_id
@@ -63,6 +70,11 @@ func (app *ApiServer) v1TrackComments(c *fiber.Ctx) error {
6370
AND @myId != comments.user_id -- always show comments to their poster
6471
AND track.owner_id != comments.user_id -- always show comments from the track owner
6572
)
73+
LEFT JOIN deactivated_users ON (
74+
deactivated_users.user_id = comments.user_id
75+
AND @myId != comments.user_id -- always show comments to their poster
76+
AND track.owner_id != comments.user_id -- always show comments from the track owner
77+
)
6678
WHERE comments.entity_id = @track_id
6779
AND parent_comment_id IS NULL
6880
AND entity_type = 'Track'
@@ -87,6 +99,8 @@ func (app *ApiServer) v1TrackComments(c *fiber.Ctx) error {
8799
)
88100
-- Filter out comments from users with low abuse score
89101
AND low_abuse_score.user_id IS NULL
102+
-- Filter out comments from deactivated users
103+
AND deactivated_users.user_id IS NULL
90104
`
91105

92106
args := pgx.NamedArgs{

api/v1_track_comments_test.go

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,122 @@ func TestTrackComments_UnlistedTrack(t *testing.T) {
121121
"data.1.id": "",
122122
})
123123
}
124+
125+
func TestTrackComments_LowScoreUser(t *testing.T) {
126+
app := emptyTestApp(t)
127+
fixtures := database.FixtureMap{
128+
"users": {
129+
{
130+
"user_id": 1,
131+
"handle": "testuser",
132+
"handle_lc": "testuser",
133+
"name": "Test User",
134+
"wallet": "0x7d273271690538cf855e5b3002a0dd8c154bb060",
135+
},
136+
{
137+
"user_id": 2,
138+
"handle": "lowscoreuser",
139+
"handle_lc": "lowscoreuser",
140+
"name": "Low Score User",
141+
"wallet": "0xc3d1d41e6872ffbd15c473d14fc3a9250be5b5e0",
142+
},
143+
},
144+
"aggregate_user": {
145+
{
146+
"user_id": 2,
147+
"score": -1,
148+
},
149+
},
150+
"tracks": {
151+
{
152+
"track_id": 101,
153+
"owner_id": 10,
154+
"title": "Test Track",
155+
},
156+
},
157+
"comments": {
158+
{
159+
"comment_id": 1,
160+
"user_id": 1,
161+
"entity_id": 101,
162+
"text": "cool",
163+
},
164+
{
165+
"comment_id": 2,
166+
"user_id": 2,
167+
"entity_id": 101,
168+
"text": "i am spam loser",
169+
},
170+
},
171+
}
172+
database.Seed(app.pool.Replicas[0], fixtures)
173+
status, body := testGet(t, app, "/v1/tracks/ePWJD/comments")
174+
assert.Equal(t, 200, status)
175+
jsonAssert(t, body, map[string]any{
176+
"data.0.message": "cool",
177+
"data.0.id": "7eP5n",
178+
"data.0.user_id": "7eP5n",
179+
"data.0.entity_id": "ePWJD",
180+
181+
// there is no second comment
182+
"data.#": 1,
183+
"data.1.id": "",
184+
})
185+
}
186+
187+
func TestTrackComments_DeactivatedUser(t *testing.T) {
188+
app := emptyTestApp(t)
189+
fixtures := database.FixtureMap{
190+
"users": {
191+
{
192+
"user_id": 1,
193+
"handle": "testuser",
194+
"handle_lc": "testuser",
195+
"name": "Test User",
196+
"wallet": "0x7d273271690538cf855e5b3002a0dd8c154bb060",
197+
},
198+
{
199+
"user_id": 2,
200+
"handle": "deactivateduser",
201+
"handle_lc": "deactivateduser",
202+
"name": "Deactivated User",
203+
"wallet": "0xc3d1d41e6872ffbd15c473d14fc3a9250be5b5e0",
204+
"is_deactivated": true,
205+
},
206+
},
207+
"tracks": {
208+
{
209+
"track_id": 101,
210+
"owner_id": 10,
211+
"title": "Test Track",
212+
},
213+
},
214+
"comments": {
215+
{
216+
"comment_id": 1,
217+
"user_id": 1,
218+
"entity_id": 101,
219+
"text": "cool",
220+
},
221+
{
222+
"comment_id": 2,
223+
"user_id": 2,
224+
"entity_id": 101,
225+
"text": "i am deactivated",
226+
},
227+
},
228+
}
229+
database.Seed(app.pool.Replicas[0], fixtures)
230+
status, body := testGet(t, app, "/v1/tracks/ePWJD/comments")
231+
assert.Equal(t, 200, status)
232+
jsonAssert(t, body, map[string]any{
233+
"data.0.message": "cool",
234+
"data.0.id": "7eP5n",
235+
"data.0.user_id": "7eP5n",
236+
"data.0.entity_id": "ePWJD",
237+
238+
// there is no second comment
239+
"data.#": 1,
240+
"data.1.id": "",
241+
})
242+
}

0 commit comments

Comments
 (0)