自定义主页以根据用户兴趣显示话题

我想听听你对这个插件的看法。我正在开发一个插件,它将允许访客根据他们的兴趣选择一个新主题显示在主页上。我写下了这个想法并绘制了这张图,但我还没有实际操作过。

plugin.rb

# plugins/custom_homepage/plugin.rb
# frozen_string_literal: true

# name: custom-homepage
# about: Customizes the homepage to show topics based on user interests
# version: 0.1
# authors: MHAT
# url: https://domain.com

enabled_site_setting :custom_homepage_enabled

after_initialize do
  # code to be executed after initialization
end
// plugins/custom_homepage/assets/javascripts/discourse/initializers/custom-homepage.js.es6

import { withPluginApi } from 'discourse/lib/plugin-api';

export default {
  name: 'custom-homepage',
  initialize(container) {
    withPluginApi('0.8', api => {
      api.modifyClass('component:topic-list', {
        didInsertElement() {
          this._super(...arguments);

          if (this.currentUser) {
            this.loadUserInterests();
          }
        },

        loadUserInterests() {
          const userId = this.currentUser.id;

          fetch(`/u/${userId}/preferences/interests`)
            .then(response => response.json())
            .then(data => {
              this.set('topics', this.sortTopicsByInterests(this.topics, data.interests));
            });
        },

        sortTopicsByInterests(topics, interests) {
          return topics.sort((a, b) => {
            const aScore = interests.includes(a.category_id) ? 1 : 0;
            const bScore = interests.includes(b.category_id) ? 1 : 0;

            return bScore - aScore;
          });
        }
      });
    });
  }
};
# plugins/custom_homepage/config/locales/server.en.yml
en:
  site_settings:
    custom_homepage_enabled: "Enable Custom Homepage"

  user:
    preferences:
      interests: "Interests"

# plugins/custom_homepage/config/discourse_plugin.yml
enabled_site_setting: custom_homepage_enabled

设置的 HTML

<script type="text/x-handlebars" data-template-name="/connectors/user-preferences/interests">
  <div class="control-group">
    <label class="control-label">{{i18n 'user.preferences.interests'}}</label>
    <div class="controls">
      {{input value=currentUser.custom_fields.interests class="form-control"}}
    </div>
  </div>
</script>
# plugins/custom_homepage/plugin.rb

after_initialize do
  User.register_custom_field_type('interests', :string)
  
  add_to_serializer(:current_user, :interests) do
    object.custom_fields['interests']
  end
end

感谢您的反馈和改进建议。

我是泰国人。我的英语可能不太好。我在开发 discourse 的同时努力学习。

/preferences/interests 在哪里实现?

我认为,为了处理用户兴趣并将其集成到用户偏好设置中,必须创建所需的路由、控制器操作和模板。

不过,这只是我的看法,而且只是初步设计。我还没有开始着手构建。如果我尝试,可能会不顺利。

为用户偏好设置添加路由
- 添加一个路由来处理对 /u/:username/preferences/interests 的请求。

```ruby
# plugins/custom_homepage/config/routes.rb
Discourse::Application.routes.append do
  get '/u/:username/preferences/interests' => 'custom_homepage#interests', constraints: { username: /[^\\/]+/ }
end
```

创建控制器操作
- 定义一个控制器来处理请求并获取用户的兴趣。

```ruby
# plugins/custom_homepage/app/controllers/custom_homepage_controller.rb
class CustomHomepageController < ApplicationController
  requires_login
  
  def interests
    username = params[:username]
    user = User.find_by(username: username)
    
    if user
      render json: {
        interests: user.custom_fields['interests']
      }
    else
      render json: { error: 'User not found' }, status: 404
    end
  end
end
```

在用户偏好设置中添加兴趣字段
- 修改用户偏好设置模板以包含一个兴趣输入框。

```html
<!-- plugins/custom_homepage/assets/javascripts/discourse/connectors/user-preferences/interests.hbs -->
<div class="control-group">
  <label class="control-label">{{i18n 'user.preferences.interests'}}</label>
  <div class="controls">
    {{input value=model.custom_fields.interests class="form-control"}}
  </div>
</div>
```

扩展序列化器
- 扩展用户序列化器以包含兴趣字段。

```ruby
# plugins/custom_homepage/plugin.rb

after_initialize do
  User.register_custom_field_type('interests', :string)
  
  add_to_serializer(:current_user, :interests) do
    object.custom_fields['interests']
  end

  add_to_serializer(:admin_detailed_user, :interests) do
    object.custom_fields['interests']
  end
end
```

修改用于处理兴趣的 JavaScript
- 使用 JavaScript 来获取兴趣并将其保存到用户配置文件中。

```javascript
// plugins/custom_homepage/assets/javascripts/discourse/initializers/custom-homepage.js.es6

import { withPluginApi } from 'discourse/lib/plugin-api';

export default {
  name: 'custom-homepage',
  initialize(container) {
    withPluginApi('0.8', api => {
      api.modifyClass('controller:preferences/account', {
        save() {
          const interests = this.model.custom_fields.interests;
          this.model.save().then(() => {
            fetch(`/u/${this.model.username}/preferences/interests`, {
              method: 'PUT',
              headers: {
                'Content-Type': 'application/json',
                'X-CSRF-Token': Discourse.CSRFToken
              },
              body: JSON.stringify({ interests })
            }).then(response => response.json()).then(() => {
              this.flash('Your preferences have been saved.');
            });
          });
        }
      });
    });
  }
};
```
  1. 处理 PUT 请求

    • 在控制器中实现处理 PUT 请求并保存兴趣的逻辑。
    # plugins/custom_homepage/app/controllers/custom_homepage_controller.rb
    class CustomHomepageController < ApplicationController
      requires_login
    
      def interests
        if request.get?
          username = params[:username]
          user = User.find_by(username: username)
          
          if user
            render json: {
              interests: user.custom_fields['interests']
            }
          else
            render json: { error: 'User not found' }, status: 404
          end
        elsif request.put?
          user = current_user
          interests = params[:interests]
          user.custom_fields['interests'] = interests
          user.save_custom_fields
          render json: success_json
        end
      end
    end
    
2 个赞

看起来很有趣。你也可以从这个插件中找到一些想法。