Add likes programmatically?

During import to discourse, a major post lost 100 likes in the transition (I failed to scrape it properly). I prefer not to re-run the import script in this case.

If I have the list of users who liked the post, what’s the best way of adding those? Is there a rake command?

What I’d do is look at the import script that imports the likes to see what it does to add the likes then do that in the rails console. I’m fairly certain it’s a PostAction.

I’m fairly certain that there is not such a rake command, as it’s a fairly unusual request (mostly, I’d just re-run the import script), but I haven’t looked.

3 Likes

Thanks for the response. I am nervous to re-run the import script since it’s been almost 6 months and I’m not sure what kind of side-effects it could have in edge cases where old data interacts with new data.

Here’s the helper function I used in my import script.

Here’s the relevant code

created_by = User.find_by(id: user_id_from_imported_user_id(params[:user_id]))
post = Post.find_by(id: post_id_from_imported_post_id(params[:post_id]))

if created_by && post
          PostActionCreator.create(created_by, post, :like, created_at: params[:created_at])
          created += 1
...

I will run something similar to this. Thanks!

3 Likes

I would be quite nervous as well!

Yippee! You never know when providing an answer as vague as “Well, what I would do. . .” whether it’ll be of any use.

If you’re going to paste stuff into the rails console, a handy trick to know is that if you terminate your lines with a ; you won’t get pushed into a less pager to see the results of the last statement. (If you write a proper loop, or run a script you don’t need this tip.)

Good luck!

2 Likes

If anyone wants to do this, here’s what worked for me. Open the rails console and run:

p = Post.find(31871)
u = User.find_by(username:"xxx")
PostActionCreator.create(u, p, :like, created_at: Time.now)

Obviously you will have to swap out the post id and the username

2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.