我的 Script framework to rearrange topics and categories 有了一个新功能,它不仅仅是重新排列主题和分类!
我通常会提醒大家,我不会 Ruby 或 Ruby on Rails,所以我的代码是特有的,而不是惯用的。但到目前为止,在我的测试中它似乎是有效的!
def migrateRetortToReactions(allowed:, likes: nil, emojimap: nil)
# 尽可能迁移,不覆盖任何现有的点赞
# 这是一个必然会丢失信息的转换,并且仅通过 PostDetail 的顺序保持一致
# 不会尝试优先选择一个 PostDetail 记录而不是另一个
emojimap = {} if emojimap.nil?
allowed.each do |a|
emojimap[a] = a
end
retort = "retort".freeze
emojiType = "emoji".freeze
usermap = Hash.new { |hash, username| hash[username] = User.find_by_username(username) }
postmap = Hash.new { |hash, post_id| hash[post_id] = Post.find(post_id) }
likeType = PostActionType.where(name_key: "like").pluck(:id).first
PostDetail.where(extra: retort).each do |pd|
begin
p = postmap[pd.post_id]
rescue
# PostDetail 与删除不一致
$stderr.puts sprintf("Could not find post for %d: %s / %s", pd.post_id, pd.key, pd.value)
next
end
emoji = pd.key.split('|').first
users = JSON.parse(pd.value)
users.each do |user|
u = usermap[user]
next if u.nil? # 用户名已更改或已删除用户会留下孤立的 Retorts
if likes.include?(emoji)
pa = PostAction.where(post_id: p.id, user_id: u.id, post_action_type_id: likeType).first
next unless pa.nil?
$stderr.puts sprintf("Adding like for Retort %s for user %s in %s", emoji, user, p.url)
PostActionCreator.create(u, p, :like, created_at: pd.created_at, silent: true)
elsif emojimap.has_key?(emoji)
e = emojimap[emoji]
r = DiscourseReactions::Reaction.where(post_id: p.id, reaction_type: emojiType, reaction_value: e).first_or_create
ru = DiscourseReactions::ReactionUser.where(user_id: u.id, post_id: p.id).first
next unless ru.nil?
$stderr.puts sprintf("Converting Retort %s to Reaction %s for user %s in %s", emoji, e, user, p.url)
DiscourseReactions::ReactionUser.create(reaction_id: r.id, user_id: u.id, post_id: p.id, created_at: pd.created_at)
else
$stderr.puts sprintf("Ignoring unmapped Retort %s for user %s in %s", emoji, user, p.url)
end
end
end
end
我使用我构建的框架提供了一个看起来像这样的 YAML 配置:
- migrateRetortToReactions:
allowed:
- rofl
- astonished
- crossed_fingers
- sob
- thinking
- grimacing
- frowning_face
- drum
likes:
- dart
- +1
- joy
- "100"
- brain
- heart
- heart_eyes
- hearts
emojimap:
rage: sob
four_leaf_clover: crossed_fingers
cry: sob
open_mouth: astonished
scream: frowning_face
不过,你也可以将它包装在一个 Ruby 脚本中,包括将那些参数变成字面上的 Ruby 代码,将其放入 script/ 目录,然后运行它。