Change kudo uniqueness validation to use users instead of pseuds

Description

Update the Rails uniqueness validator on kudos to use user_id instead of pseud_id.

We won't clean up the duplicates in the database just yet, and as before people will still be able to get past this check on a dodgy connection, but it will be harder to leave duplicates from the same user (even when changing default pseuds).

When we do add the uniqueness constraint at the database level, we'll have the error handling ready at the application level (people will see "left kudos here " as usual).

For the rare cases where users can get past the Rails validation and run into the (future) database constraint, we should catch ActiveRecord::RecordNotUnique exceptions, similar to what we do on user creations: https://github.com/otwcode/otwarchive/blob/57e7cab589abc7cc666ddf4d3abe768d801b3493/app/controllers/users_controller.rb#L9-L24.

How to test:

  • Check that leaving logged in kudos still works.

  • Check that leaving logged in kudos a second time on the same work will get you the "left kudos here " message.

  • Check that you can no longer leave logged in kudos after changing default pseuds.

Activity

Show:
redsummernight
February 22, 2020, 5:54 AM

That Rails can throw ActiveRecord::RecordNotUnique exceptions because of database constraints and in spite of application-level validations, as well as the workaround where we parse such exceptions and ask users to retry, are documented here: https://api.rubyonrails.org/v5.1/classes/ActiveRecord/Validations/ClassMethods.html#method-i-validates_uniqueness_of-label-Concurrency+and+integrity.

james_
March 7, 2020, 6:54 AM

Before migration

https://test.archiveofourown.org/works/33973

Switch psueds and

 

Sarken
March 7, 2020, 6:58 AM
Edited

Left one kudos and got “Thank you for leaving kudos!”

Tried to leave second kudos and got “You have already left kudos here. :)”

Disabled JavaScript and the initial kudos worked the same. The second kudos gave me, “User ^You have already left kudos here. :)” which I think is expected.

I changed my default pseud and tried to leave a second kudos. Same results: “You have already left kudos here. :)” with JavaScript enabled and “User ^You have already left kudos here. :)” with it disabled.

Looks good!

ETA: The issue for the “User ^” is

redsummernight
March 7, 2020, 7:13 AM

Note that when this issue is live, but before the migration in is run:

  • When creating a kudos, unique validation on Rails for ip_address and user_id will not be backed by database indices; that's not new for ip_address, but new for user_id.

  • When loading kudos for a work, we are still hitting the existing database indices involving pseud_id.

So queries to create new kudos for registered users will be slower; other kudos-related queries stay the same.

DeployedToBeta

Assignee

redsummernight

Reporter

redsummernight

Roadmap

Misc

Priority

Medium

Affects versions

Fix versions

Components

BackEnd

Difficulty

Medium

Milestone

Internal 0.9
Configure