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.
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.
Left one kudos and got “Thank you for leaving kudos!”
Tried to leave second kudos and got “You have already left kudos here. :)”
ETA: The issue for the “User ^” is
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.