|
1 | | -const { h, when } = require('mutant') |
2 | | -const nest = require('depnest') |
3 | | -const getRoot = require('../../../message/sync/root') |
4 | | -const electron = require("electron") |
| 1 | +const { h, when } = require("mutant"); |
| 2 | +const nest = require("depnest"); |
| 3 | +const getRoot = require("../../../message/sync/root"); |
| 4 | +const customScripts = require("../../scripts/lua/custom-scripts.js") |
| 5 | +const electron = require("electron"); |
5 | 6 |
|
6 | 7 | exports.needs = nest({ |
7 | | - 'intl.sync.i18n': 'first', |
8 | | - 'app.navigate': 'first', |
9 | | - 'message.obs.doesLike': 'first', |
10 | | - 'sbot.async.publish': 'first', |
11 | | - 'sheet.editTags': 'first' |
12 | | -}) |
| 8 | + "intl.sync.i18n": "first", |
| 9 | + "app.navigate": "first", |
| 10 | + "message.obs.doesLike": "first", |
| 11 | + "sbot.async.publish": "first", |
| 12 | + "sheet.editTags": "first", |
| 13 | + "scripts.lua.environment.init": "first", |
| 14 | + "scripts.lua.environment.call": "first", |
| 15 | +}); |
13 | 16 |
|
14 | | -exports.gives = nest('message.html.actions') |
| 17 | +exports.gives = nest("message.html.actions"); |
15 | 18 |
|
16 | 19 | exports.create = (api) => { |
17 | | - const i18n = api.intl.sync.i18n |
| 20 | + const i18n = api.intl.sync.i18n; |
18 | 21 |
|
19 | | - return nest('message.html.actions', function like (msg) { |
20 | | - const liked = api.message.obs.doesLike(msg.key) |
| 22 | + return nest("message.html.actions", function like(msg) { |
| 23 | + const liked = api.message.obs.doesLike(msg.key); |
| 24 | + |
| 25 | + const customActions = [] |
| 26 | + |
| 27 | + const activeScripts = customScripts.getActiveScriptsList() |
| 28 | + |
| 29 | + for (const script of activeScripts) { |
| 30 | + const L = api.scripts.lua.environment.init(script.name) |
| 31 | + const [r, label] = api.scripts.lua.environment.call(L, "buttonAction",[msg],2) |
| 32 | + if (r) { |
| 33 | + const button = h("a.lua -right", { |
| 34 | + href: "#", |
| 35 | + title: label, |
| 36 | + "ev-click": (ev) => { |
| 37 | + const [r, label] = api.scripts.lua.environment.call(L, "action",[msg],2) |
| 38 | + ev.preventDefault(); |
| 39 | + }, |
| 40 | + }, label) |
| 41 | + customActions.push(button) |
| 42 | + } |
| 43 | + } |
21 | 44 |
|
22 | 45 | return [ |
23 | | - when(liked, |
24 | | - h('a.like -liked', { |
25 | | - href: '#', |
26 | | - title: i18n('Click to unlike'), |
27 | | - 'ev-click': () => publishLike(msg, false) |
28 | | - }, i18n('Liked')), |
29 | | - h('a.like', { |
30 | | - href: '#', |
31 | | - 'ev-click': () => publishLike(msg, true) |
32 | | - }, i18n('Like')) |
| 46 | + when( |
| 47 | + liked, |
| 48 | + h("a.like -liked", { |
| 49 | + href: "#", |
| 50 | + title: i18n("Click to unlike"), |
| 51 | + "ev-click": () => publishLike(msg, false), |
| 52 | + }, i18n("Liked")), |
| 53 | + h("a.like", { |
| 54 | + href: "#", |
| 55 | + "ev-click": () => publishLike(msg, true), |
| 56 | + }, i18n("Like")), |
33 | 57 | ), |
34 | | - h('a.reply', { |
| 58 | + h("a.reply", { |
35 | 59 | href: msg.key, |
36 | | - anchor: 'reply', |
37 | | - 'ev-click': { handleEvent, api, msg } |
38 | | - }, i18n('Reply')), |
39 | | - h('a.tag -right', { |
40 | | - href: '#', |
41 | | - title: i18n('Add / Edit Tags'), |
42 | | - 'ev-click': () => api.sheet.editTags({ msgId: msg.key }, console.log) |
43 | | - }, i18n('Tags')), |
44 | | - when(messageHasAudio(msg),h('a.audio -right', { |
45 | | - href: '#', |
46 | | - title: i18n('Open in Audio Player'), |
47 | | - 'ev-click': (ev) => { |
48 | | - openInPlayer(msg) |
49 | | - ev.preventDefault() |
50 | | - } |
51 | | - }, i18n('Open in Audio Player'))) |
52 | | - ] |
53 | | - }) |
54 | | - |
55 | | - function publishLike (msg, status = true) { |
| 60 | + anchor: "reply", |
| 61 | + "ev-click": { handleEvent, api, msg }, |
| 62 | + }, i18n("Reply")), |
| 63 | + h("a.tag -right", { |
| 64 | + href: "#", |
| 65 | + title: i18n("Add / Edit Tags"), |
| 66 | + "ev-click": () => api.sheet.editTags({ msgId: msg.key }, console.log), |
| 67 | + }, i18n("Tags")), |
| 68 | + when( |
| 69 | + messageHasAudio(msg), |
| 70 | + h("a.audio -right", { |
| 71 | + href: "#", |
| 72 | + title: i18n("Open in Audio Player"), |
| 73 | + "ev-click": (ev) => { |
| 74 | + openInPlayer(msg); |
| 75 | + ev.preventDefault(); |
| 76 | + }, |
| 77 | + }, i18n("Open in Audio Player")), |
| 78 | + ), |
| 79 | + ...customActions |
| 80 | + ]; |
| 81 | + }); |
| 82 | + |
| 83 | + function publishLike(msg, status = true) { |
56 | 84 | const like = status |
57 | 85 | ? { |
58 | | - type: 'vote', |
59 | | - channel: msg.value.content.channel, |
60 | | - vote: { link: msg.key, value: 1, expression: 'Like' } |
61 | | - } |
| 86 | + type: "vote", |
| 87 | + channel: msg.value.content.channel, |
| 88 | + vote: { link: msg.key, value: 1, expression: "Like" }, |
| 89 | + } |
62 | 90 | : { |
63 | | - type: 'vote', |
64 | | - channel: msg.value.content.channel, |
65 | | - vote: { link: msg.key, value: 0, expression: 'Unlike' } |
66 | | - } |
| 91 | + type: "vote", |
| 92 | + channel: msg.value.content.channel, |
| 93 | + vote: { link: msg.key, value: 0, expression: "Unlike" }, |
| 94 | + }; |
67 | 95 | if (msg.value.content.recps) { |
68 | 96 | like.recps = msg.value.content.recps.map(function (e) { |
69 | | - return e && typeof e !== 'string' ? e.link : e |
70 | | - }) |
71 | | - like.private = true |
| 97 | + return e && typeof e !== "string" ? e.link : e; |
| 98 | + }); |
| 99 | + like.private = true; |
72 | 100 | } |
73 | | - api.sbot.async.publish(like) |
| 101 | + api.sbot.async.publish(like); |
74 | 102 | } |
75 | | -} |
| 103 | +}; |
76 | 104 |
|
77 | | -function handleEvent (ev) { |
78 | | - const { api, msg } = this |
79 | | - const el = getMessageElement(ev.target) |
| 105 | +function handleEvent(ev) { |
| 106 | + const { api, msg } = this; |
| 107 | + const el = getMessageElement(ev.target); |
80 | 108 |
|
81 | 109 | // HACK: if this is the last message in the list, reply to the root message |
82 | 110 | if (el && !el.nextElementSibling) { |
83 | | - api.app.navigate(getRoot(msg), 'reply') |
84 | | - ev.preventDefault() |
| 111 | + api.app.navigate(getRoot(msg), "reply"); |
| 112 | + ev.preventDefault(); |
85 | 113 | } |
86 | 114 | } |
87 | 115 |
|
88 | | -function openInPlayer (msg) { |
89 | | - console.log("open in player", msg) |
90 | | - electron.ipcRenderer.send("open-in-audio-player", msg) |
| 116 | +function openInPlayer(msg) { |
| 117 | + console.log("open in player", msg); |
| 118 | + electron.ipcRenderer.send("open-in-audio-player", msg); |
91 | 119 | } |
92 | 120 |
|
93 | | -function messageHasAudio (msg) { |
| 121 | +function messageHasAudio(msg) { |
94 | 122 | if (!msg?.value?.content?.mentions) { |
95 | | - return false |
| 123 | + return false; |
96 | 124 | } |
97 | 125 |
|
98 | 126 | if (!Array.isArray(msg.value.content.mentions)) { |
99 | | - return false |
| 127 | + return false; |
100 | 128 | } |
101 | 129 |
|
102 | 130 | for (const mention of msg.value.content.mentions) { |
103 | 131 | if (mention?.type && mention.type.startsWith("audio/")) { |
104 | | - return true |
| 132 | + return true; |
105 | 133 | } |
106 | 134 |
|
107 | 135 | if (mention?.name && mention.name.endsWith("mp3")) { |
108 | | - return true |
| 136 | + return true; |
109 | 137 | } |
110 | 138 | } |
111 | | - return false |
| 139 | + return false; |
112 | 140 | } |
113 | 141 |
|
114 | | -function getMessageElement (el) { |
| 142 | +function getMessageElement(el) { |
115 | 143 | while (el && el.classList) { |
116 | | - if (el.classList.contains('Message') && el.parentNode && el.parentNode.classList.contains('replies')) { |
117 | | - return el |
| 144 | + if ( |
| 145 | + el.classList.contains("Message") && el.parentNode && |
| 146 | + el.parentNode.classList.contains("replies") |
| 147 | + ) { |
| 148 | + return el; |
118 | 149 | } |
119 | | - el = el.parentNode |
| 150 | + el = el.parentNode; |
120 | 151 | } |
121 | 152 | } |
0 commit comments