Хочу услышать ваше мнение об этом плагине. Я работаю над плагином, который позволит посетителям выбирать новую тему для отображения на главной странице в зависимости от их интересов. Я записал идею и нарисовал схему этого процесса, но ещё не тестировал её.
plugin.rb
# plugins/custom_homepage/plugin.rb
# frozen_string_literal: true
# name: custom-homepage
# about: Настраивает главную страницу для отображения тем на основе интересов пользователя
# version: 0.1
# authors: MHAT
# url: https://domain.com
enabled_site_setting :custom_homepage_enabled
after_initialize do
# код, который будет выполнен после инициализации
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: "Включить пользовательскую главную страницу"
user:
preferences:
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.