|
| 1 | +import Component from "@glimmer/component"; |
| 2 | +import { withSilencedDeprecations } from "discourse/lib/deprecated"; |
| 3 | +import { iconNode } from "discourse/lib/icon-library"; |
| 4 | +import { withPluginApi } from "discourse/lib/plugin-api"; |
| 5 | +import { applyValueTransformer } from "discourse/lib/transformer"; |
| 6 | +import PostMetadataUserNotes from "../components/post-metadata-user-notes"; |
| 7 | +import { showUserNotes, updatePostUserNotesCount } from "../lib/user-notes"; |
| 8 | + |
| 9 | +/** |
| 10 | + * Plugin initializer for enabling user notes functionality |
| 11 | + */ |
| 12 | +export default { |
| 13 | + name: "enable-user-notes", |
| 14 | + initialize(container) { |
| 15 | + const siteSettings = container.lookup("service:site-settings"); |
| 16 | + const currentUser = container.lookup("service:current-user"); |
| 17 | + |
| 18 | + if (!siteSettings.user_notes_enabled || !currentUser?.staff) { |
| 19 | + return; |
| 20 | + } |
| 21 | + |
| 22 | + withPluginApi((api) => { |
| 23 | + customizePost(api, container); |
| 24 | + customizePostMenu(api, container); |
| 25 | + }); |
| 26 | + }, |
| 27 | +}; |
| 28 | + |
| 29 | +/** |
| 30 | + * Customizes how user notes are displayed in posts |
| 31 | + * |
| 32 | + * @param {Object} api - Plugin API instance |
| 33 | + * @param {Object} container - Container instance |
| 34 | + */ |
| 35 | +function customizePost(api, container) { |
| 36 | + const siteSettings = container.lookup("service:site-settings"); |
| 37 | + |
| 38 | + const placement = applyValueTransformer( |
| 39 | + "user-notes-icon-placement", |
| 40 | + siteSettings.user_notes_icon_placement |
| 41 | + ); |
| 42 | + |
| 43 | + // Component to display user notes flair icon |
| 44 | + class UserNotesPostMetadataFlairIcon extends Component { |
| 45 | + static shouldRender(args) { |
| 46 | + return args.post?.user_custom_fields?.user_notes_count > 0; |
| 47 | + } |
| 48 | + |
| 49 | + <template><PostMetadataUserNotes @post={{@post}} /></template> |
| 50 | + } |
| 51 | + |
| 52 | + // Handle placement next to avatar |
| 53 | + if (placement === "avatar") { |
| 54 | + api.renderAfterWrapperOutlet( |
| 55 | + "poster-avatar", |
| 56 | + UserNotesPostMetadataFlairIcon |
| 57 | + ); |
| 58 | + } |
| 59 | + // Handle placement next to username |
| 60 | + else if (placement === "name") { |
| 61 | + // Mobile-specific version |
| 62 | + class MobileUserNotesIcon extends UserNotesPostMetadataFlairIcon { |
| 63 | + static shouldRender(args, context) { |
| 64 | + return context.site.mobileView && super.shouldRender(args); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + // Desktop-specific version |
| 69 | + class DesktopUserNotesIcon extends UserNotesPostMetadataFlairIcon { |
| 70 | + static shouldRender(args, context) { |
| 71 | + return !context.site.mobileView && super.shouldRender(args); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + api.renderBeforeWrapperOutlet( |
| 76 | + "post-meta-data-poster-name", |
| 77 | + MobileUserNotesIcon |
| 78 | + ); |
| 79 | + api.renderAfterWrapperOutlet( |
| 80 | + "post-meta-data-poster-name", |
| 81 | + DesktopUserNotesIcon |
| 82 | + ); |
| 83 | + } |
| 84 | + |
| 85 | + withSilencedDeprecations("discourse.post-stream-widget-overrides", () => |
| 86 | + customizeWidgetPost(api) |
| 87 | + ); |
| 88 | +} |
| 89 | + |
| 90 | +/** |
| 91 | + * Customizes the post widget to display user notes |
| 92 | + * |
| 93 | + * @param {Object} api - Plugin API instance |
| 94 | + */ |
| 95 | +function customizeWidgetPost(api) { |
| 96 | + // Handler for showing user notes modal |
| 97 | + function widgetShowUserNotes() { |
| 98 | + showUserNotes( |
| 99 | + this.store, |
| 100 | + this.attrs.user_id, |
| 101 | + (count) => { |
| 102 | + this.sendWidgetAction("refreshUserNotes", count); |
| 103 | + }, |
| 104 | + { |
| 105 | + postId: this.attrs.id, |
| 106 | + } |
| 107 | + ); |
| 108 | + } |
| 109 | + |
| 110 | + // Update post when notes are changed |
| 111 | + api.attachWidgetAction("post", "refreshUserNotes", function (count) { |
| 112 | + updatePostUserNotesCount(this.model, count); |
| 113 | + }); |
| 114 | + |
| 115 | + const mobileView = api.container.lookup("service:site").mobileView; |
| 116 | + const loc = mobileView ? "before" : "after"; |
| 117 | + |
| 118 | + // Helper to attach notes icon if user has notes |
| 119 | + const attachUserNotesIconIfPresent = (dec) => { |
| 120 | + const post = dec.getModel(); |
| 121 | + if (post?.user_custom_fields?.user_notes_count > 0) { |
| 122 | + return dec.attach("user-notes-icon"); |
| 123 | + } |
| 124 | + }; |
| 125 | + |
| 126 | + // Add notes icon to poster name |
| 127 | + api.decorateWidget(`poster-name:${loc}`, (dec) => { |
| 128 | + if (dec.widget.settings.hideNotes) { |
| 129 | + return; |
| 130 | + } |
| 131 | + |
| 132 | + return attachUserNotesIconIfPresent(dec); |
| 133 | + }); |
| 134 | + |
| 135 | + // Add notes icon after avatar |
| 136 | + api.decorateWidget(`post-avatar:after`, (dec) => { |
| 137 | + if (!dec.widget.settings.showNotes) { |
| 138 | + return; |
| 139 | + } |
| 140 | + |
| 141 | + return attachUserNotesIconIfPresent(dec); |
| 142 | + }); |
| 143 | + |
| 144 | + api.attachWidgetAction("post", "showUserNotes", widgetShowUserNotes); |
| 145 | + |
| 146 | + // Create the user notes icon widget |
| 147 | + api.createWidget("user-notes-icon", { |
| 148 | + services: ["site-settings"], |
| 149 | + |
| 150 | + tagName: "span.user-notes-icon", |
| 151 | + click: widgetShowUserNotes, |
| 152 | + |
| 153 | + html() { |
| 154 | + if (this.siteSettings.enable_emoji) { |
| 155 | + return this.attach("emoji", { name: "memo" }); |
| 156 | + } else { |
| 157 | + return iconNode("pen-to-square"); |
| 158 | + } |
| 159 | + }, |
| 160 | + }); |
| 161 | +} |
| 162 | + |
| 163 | +/** |
| 164 | + * Adds user notes button to post admin menu |
| 165 | + * |
| 166 | + * @param {Object} api - Plugin API instance |
| 167 | + * @param {Object} container - Container instance |
| 168 | + */ |
| 169 | +function customizePostMenu(api, container) { |
| 170 | + const appEvents = container.lookup("service:app-events"); |
| 171 | + const store = container.lookup("service:store"); |
| 172 | + |
| 173 | + api.addPostAdminMenuButton((attrs) => { |
| 174 | + return { |
| 175 | + icon: "pen-to-square", |
| 176 | + label: "user_notes.attach", |
| 177 | + action: (post) => { |
| 178 | + showUserNotes( |
| 179 | + store, |
| 180 | + attrs.user_id, |
| 181 | + (count) => { |
| 182 | + updatePostUserNotesCount(post, count); |
| 183 | + appEvents.trigger("post-stream:refresh", { |
| 184 | + id: post.id, |
| 185 | + }); |
| 186 | + }, |
| 187 | + { postId: attrs.id } |
| 188 | + ); |
| 189 | + }, |
| 190 | + secondaryAction: "closeAdminMenu", |
| 191 | + className: "add-user-note", |
| 192 | + }; |
| 193 | + }); |
| 194 | +} |
0 commit comments