Skip to content
This repository has been archived by the owner on May 26, 2023. It is now read-only.

WIP: optional: translate twitter to mastodon mentions #650

Closed

Conversation

rriemann
Copy link
Contributor

note that only twitter mentions of accounts registered on this
crossposter instance are translated

note that only twitter mentions of accounts registered on this 
crossposter instance are translated
Copy link
Contributor Author

@rriemann rriemann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't been working with Rails in the last 8 years and I am a bit overwhelmed to understand how I can setup my environment and use test-driven development. We should definitely cover this feature with tests.

if Rails.configuration.x.translate_twitter_accounts
mapping = User.twitter_mastodon_mapping
mapping.default_proc = proc {|hash, key| key }
text.gsub!(twitter_mention_regex, mapping)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not work currently. twitter_mention_regex also matches leading spaces. However, the mapping hash does not have this leading space and thus does not carry out a substitution.

@renatolond What is the best approach here? Add a different twitter regexp?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't remember the mention results exactly, but I think you have the leading spaces in \1, so you could something like:

gsub!(..., "\1#{mapping}")

since you're using a proc there, I'm not too sure on how you could do that, though 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi! mapping is a hash of the form {"@twitter_handle" => "@mastodon_handle@server.com", ...}. gsub can pick here the replacement text if the capture is found amongst the hash keys. The default_proc ensures that twitter mentions not found in the mapping are just replaced by themselves, i.e. not modified.

The expression "\1#{mapping}" would be converted into a string and gsub would then replace all captures by the same text.

If we can rewrite the twitter_mention_regex to not match a leading space/char, then we could use the regexp. I assume though that there was a specific reason to match a leading space…

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, and wouldn't it work if you do a transform_keys in your mapping hash to have \1@twitter_handle keys instead?

@@ -184,6 +184,9 @@ def self.get_authorization(provider, uid)
BLOCKED_UIDS = ["example@bad.bad.server"]

def self.from_omniauth(auth, current_user)
# reset twitter mastodon mapping cache upon human change of the user database
Rails.cache.delete('/twitter_mastodon_mapping')
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the cache should be devalidated (deleted), whenever a user is deleted or created. The position in the code is not obvious to me. I think it could fit here.

# https://developer.twitter.com/en/docs/twitter-api/users/lookup/api-reference/get-users-by-username-username
# rate limit: 900 requests per 15-min window shared among all users of your app
User.all.each do |user|
[user.twitter_handle, "@#{user.mastodon_id}"]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked the Twitter API and it seams the translation from Twitter IDs to twitter_handle does not allow for a batch request. With many accounts registered on a crossposer instance, this call to #twitter_handle in a loop can potentially be heavy.

I don't expect more than 200 accounts for the next months. So it's acceptable for the time being. However, I wonder if the twitter_handle should not be put in the local database alongside the twitter ID.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do think it should go in the database, it would make more sense. Then you don't have to deal with the cache, it should go in the Authorization table, so that it gets cleared automatically if the user changes the twitter account they linked :)

@renatolond
Copy link
Owner

You also need to commit the changes to the schema file, that's why the tests are failing here :)

@rriemann
Copy link
Contributor Author

I guess the schema file is updated upon rake db:migration. The problem is, that this command files, because I do not have a proper developing environment. I have neither postgres nor redis running on my PC yet. 🙈 I may spin up a cloud server for dev to keep my local machine clean from dev dependencies…

renatolond added a commit that referenced this pull request May 26, 2023
@renatolond renatolond closed this May 26, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants