Import scripts fail to relate topics and posts

AI 生成的摘要

The discussion revolves around issues with import scripts in a custom forum. Matias_Hernandez is having trouble relating topics and posts using the create_posts method. gerhard suggests rewriting the function, pointing out that the topic_lookup_from_imported_post_id() method returns a hash, and provides a revised code snippet.

paultaylor is experiencing a separate issue, trying to create topics owned by a particular user rather than the admin user running the script. They reference a related discussion, and ask for clarification on how to pass the user ID to the API, specifically whether to use the id column or the username column of the users table.

I’m writing an import script for a custom forum.
The importing of categories and users works good, but the script (using the create_posts method) is not able to build the relation between the topic and the replies.

The relevant code

//Topics
create_posts(threads, total: total) do |t|
  source_user_id = @usernamesMap[t["username"]]
  {
    id: t["id"],
    user_id: user_id_from_imported_user_id(source_user_id) || Discourse::SYSTEM_USER_ID,
    raw: t["Thread_Description"],
    created_at: t["created_at"],
    posts_count: t["count"],
    category: category_id_from_imported_category_id(t["Topic_ID"]),
    title: t["title"]
  }
end

// Replies
create_posts(posts, total: total) do |t|
  next unless topic = topic_lookup_from_imported_post_id(t["Thread_ID"])
  source_user_id = @usernamesMap[t["username"]]
  opts = {
     id: t['id'],
     user_id: user_id_from_imported_user_id(source_user_id) || Discourse::SYSTEM_USER_ID,
     raw: t["Post_Description"],
     created_at: t["created_at"],
     title: t["title"],
     topic_id: topic_lookup_from_imported_post_id(t["Thread_ID"]), //This is not working correctly
     reply_to_post_number: topic_lookup_from_imported_post_id(t["Thread_ID"])
   }
end

Any ideas?

The topic_lookup_from_imported_post_id() method returns a hash.

So, I’d rewrite your function to something like this:

create_posts(posts, total: total) do |t|
  parent = topic_lookup_from_imported_post_id(t["Thread_ID"])
  next unless parent
  source_user_id = @usernamesMap[t["username"]]
  opts = {
     id: t['id'],
     user_id: user_id_from_imported_user_id(source_user_id) || Discourse::SYSTEM_USER_ID,
     raw: t["Post_Description"],
     created_at: t["created_at"],
     title: t["title"],
     topic_id: parent[:topic_id],
     reply_to_post_number: parent[:post_number]
   }
end

正在研究如何创建由特定用户而非管理员用户拥有的主题,而不是运行脚本。

从您的帖子来看,似乎只需向 API 传递 user_id 即可,尽管这并未在文档中说明,但对我来说似乎不起作用。情况确实如此吗?如果是的话,user_id 是 users 表中的 id 列还是 username 列?