Skip to content

Commit

Permalink
refactor: implement Retort as a service
Browse files Browse the repository at this point in the history
  • Loading branch information
pangbo13 committed Jul 14, 2024
1 parent e7fb8a9 commit ad5a647
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Retort from "../../lib/retort";
import { getOwnerWithFallback } from "discourse-common/lib/get-owner";
export default {
setupComponent(args, component) {
Retort.setPicker(component);
const retort = getOwnerWithFallback(component).lookup("service:retort");
retort.setPicker(component);
},
};
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { withPluginApi } from "discourse/lib/plugin-api";
import Retort from "../lib/retort";

function initializePlugin(api) {
const { retort_enabled } = api._lookupContainer("site-settings:main");
const Retort = api.container.lookup("service:retort");

if (!retort_enabled) {
return;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,36 +1,37 @@
import Object from "@ember/object";
import Service, { inject as service } from '@ember/service';
import { ajax } from "discourse/lib/ajax";
import { popupAjaxError } from "discourse/lib/ajax-error";
import { getOwnerWithFallback } from "discourse-common/lib/get-owner";

export default Object.create({
export default class Retort extends Service {
@service appEvents;

createRetort({ id }, retort) {
return ajax(`/retorts/${id}.json`, {
type: "PUT",
data: { retort },
});
},
}

withdrawRetort({ id }, retort) {
return ajax(`/retorts/${id}.json`, {
type: "DELETE",
data: { retort },
});
},
}

removeRetort({ id }, retort) {
return ajax(`/retorts/${id}/all.json`, {
type: "DELETE",
data: { retort },
});
},
}

disabledCategories() {
const siteSettings =
getOwnerWithFallback(this).lookup("site-settings:main");
const categories = siteSettings.retort_disabled_categories.split("|");
// const siteSettings =
// getOwnerWithFallback(this).lookup("site-settings:main");
const categories = this.siteSettings.retort_disabled_categories.split("|");
return categories.map((cat) => parseInt(cat, 10)).filter(Boolean);
},
}

disableShowForTopic(topic) {
if (!topic) {
Expand All @@ -39,7 +40,7 @@ export default Object.create({
const categoryId = topic.get("category.id");
const disabledCategories = this.disabledCategories();
return categoryId && disabledCategories.includes(categoryId);
},
}

openPicker(post) {
const retortAnchor = document.querySelector(`
Expand All @@ -60,7 +61,7 @@ export default Object.create({
}
this.set("picker.isActive", false);
});
},
}

setPicker(picker) {
this.set("picker", picker);
Expand All @@ -73,10 +74,11 @@ export default Object.create({
console.error("Retort post id mismatch");
} else {
picker.post.set("retorts", data.retorts);
getOwnerWithFallback(this).lookup("service:app-events").trigger("post-stream:refresh", { id: data.id });
// getOwnerWithFallback(this).lookup("service:app-events")
this.appEvents.trigger("post-stream:refresh", { id: data.id });
}
})
.catch(popupAjaxError)
);
},
});
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { emojiUrlFor } from "discourse/lib/text";
import { createWidget } from "discourse/widgets/widget";
import Retort from "../lib/retort";

createWidget("post-retort-container", {
tagName: "div.post-retort-container",
services: ['retort'],

buildKey: (attrs) => `post-retort-container-${attrs.post.id}`,

html(attrs) {
const { post } = attrs;
if (Retort.disableShowForTopic(post.topic)) {
if (this.retort.disableShowForTopic(post.topic)) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import hbs from "discourse/widgets/hbs-compiler";
import { createWidget } from "discourse/widgets/widget";
import { getOwnerWithFallback } from "discourse-common/lib/get-owner";
import I18n from "discourse-i18n";
import Retort from "../lib/retort";

createWidget("retort-remove-emoji", {
tagName: "a.remove-retort",
template: hbs`{{d-icon "times"}}`,
services: ['retort'],

buildKey: (attrs) => `retort-remove-${attrs.post.id}-${attrs.emoji}`,

Expand All @@ -21,7 +21,7 @@ createWidget("retort-remove-emoji", {
dialog.confirm({
title: I18n.t("retort.confirm_remove.title"),
message: I18n.t("retort.confirm_remove.message", { emoji }),
didConfirm: () => Retort.removeRetort(post, emoji).catch(popupAjaxError),
didConfirm: () => this.retort.removeRetort(post, emoji).catch(popupAjaxError),
});
},
});
6 changes: 3 additions & 3 deletions assets/javascripts/discourse/widgets/retort-toggle.js.es6
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { h } from "virtual-dom";
import { popupAjaxError } from "discourse/lib/ajax-error";
import { createWidget } from "discourse/widgets/widget";
import I18n from "discourse-i18n";
import Retort from "../lib/retort";

export default createWidget("retort-toggle", {
tagName: "button.post-retort",
services: ['retort'],

buildClasses(attrs) {
const classList = [];
Expand All @@ -28,14 +28,14 @@ export default createWidget("retort-toggle", {
}
const { post, emoji } = this.attrs;
if (this.isMyRetort()) {
Retort.withdrawRetort(post, emoji)
this.retort.withdrawRetort(post, emoji)
.then((data) => {
post.set("retorts", data.retorts);
this.scheduleRerender();
})
.catch(popupAjaxError);
} else {
Retort.createRetort(post, emoji)
this.retort.createRetort(post, emoji)
.then((data) => {
post.set("retorts", data.retorts);
this.scheduleRerender();
Expand Down

0 comments on commit ad5a647

Please sign in to comment.