rcscript.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. const {defaultSettings, limit: {rcgcdw: rcgcdwLimit}} = require('../util/default.json');
  2. const {RcGcDw: allLangs} = require('../i18n/allLangs.json');
  3. const {got, db, sendMsg, hasPerm} = require('./util.js');
  4. const fieldset = {
  5. channel: '<label for="wb-settings-channel">Channel:</label>'
  6. + '<select id="wb-settings-channel" name="channel" required></select>',
  7. wiki: '<label for="wb-settings-wiki">Wiki:</label>'
  8. + '<input type="url" id="wb-settings-wiki" name="wiki" required>',
  9. //+ '<button type="button" id="wb-settings-wiki-search" class="collapsible">Search wiki</button>'
  10. //+ '<fieldset style="display: none;">'
  11. //+ '<legend>Wiki search</legend>'
  12. //+ '</fieldset>',
  13. lang: '<label for="wb-settings-lang">Language:</label>'
  14. + '<select id="wb-settings-lang" name="lang" required>'
  15. + Object.keys(allLangs.names).map( lang => {
  16. return `<option id="wb-settings-lang-${lang}" value="${lang}">${allLangs.names[lang]}</option>`
  17. } ).join('\n')
  18. + '</select>',
  19. display: '<span>Display mode:</span>'
  20. + '<div class="wb-settings-display">'
  21. + '<input type="radio" id="wb-settings-display-0" name="display" value="0" required>'
  22. + '<label for="wb-settings-display-0">Compact text messages with inline links.</label>'
  23. + '</div><div class="wb-settings-display">'
  24. + '<input type="radio" id="wb-settings-display-1" name="display" value="1" required>'
  25. + '<label for="wb-settings-display-1">Embed messages with edit tags and category changes.</label>'
  26. + '</div><div class="wb-settings-display">'
  27. + '<input type="radio" id="wb-settings-display-2" name="display" value="2" required>'
  28. + '<label for="wb-settings-display-2">Embed messages with image previews.</label>'
  29. + '</div><div class="wb-settings-display">'
  30. + '<input type="radio" id="wb-settings-display-3" name="display" value="3" required>'
  31. + '<label for="wb-settings-display-3">Embed messages with image previews and edit differences.</label>'
  32. + '</div>',
  33. feeds: '<label for="wb-settings-feeds">Feeds based changes:</label>'
  34. + '<input type="checkbox" id="wb-settings-feeds" name="feeds">'
  35. + '<div id="wb-settings-feeds-only-hide">'
  36. + '<label for="wb-settings-feeds-only">Only feeds based changes:</label>'
  37. + '<input type="checkbox" id="wb-settings-feeds-only" name="feeds_only">'
  38. + '</div>',
  39. save: '<input type="submit" id="wb-settings-save" name="save_settings">',
  40. delete: '<input type="submit" id="wb-settings-delete" name="delete_settings">'
  41. };
  42. /**
  43. * Create a settings form
  44. * @param {import('cheerio')} $ - The response body
  45. * @param {String} header - The form header
  46. * @param {Object} settings - The current settings
  47. * @param {Boolean} settings.patreon
  48. * @param {String} settings.channel
  49. * @param {String} settings.wiki
  50. * @param {String} settings.lang
  51. * @param {Number} settings.display
  52. * @param {Number} settings.wikiid
  53. * @param {Number} settings.rcid
  54. * @param {Object[]} guildChannels - The guild channels
  55. * @param {String} guildChannels.id
  56. * @param {String} guildChannels.name
  57. * @param {Number} guildChannels.permissions
  58. */
  59. function createForm($, header, settings, guildChannels) {
  60. var readonly = ( process.env.READONLY ? true : false );
  61. var fields = [];
  62. let channel = $('<div>').append(fieldset.channel);
  63. channel.find('#wb-settings-channel').append(
  64. ...guildChannels.filter( guildChannel => {
  65. return ( hasPerm(guildChannel.permissions, 'VIEW_CHANNEL', 'MANAGE_WEBHOOKS') || settings.channel === guildChannel.id );
  66. } ).map( guildChannel => {
  67. var optionChannel = $(`<option id="wb-settings-channel-${guildChannel.id}">`).val(guildChannel.id);
  68. if ( settings.channel === guildChannel.id ) {
  69. optionChannel.attr('selected', '');
  70. if ( !hasPerm(guildChannel.permissions, 'VIEW_CHANNEL', 'MANAGE_WEBHOOKS') ) {
  71. optionChannel.addClass('wb-settings-error');
  72. readonly = true;
  73. }
  74. }
  75. return optionChannel.text(`${guildChannel.id} – #${guildChannel.name}`);
  76. } )
  77. );
  78. if ( !settings.channel ) channel.find('#wb-settings-channel').prepend(
  79. $(`<option id="wb-settings-channel-default" selected>`).val('').text('-- Select a Channel --')
  80. );
  81. fields.push(channel);
  82. let wiki = $('<div>').append(fieldset.wiki);
  83. wiki.find('#wb-settings-wiki').val(settings.wiki);
  84. fields.push(wiki);
  85. let lang = $('<div>').append(fieldset.lang);
  86. lang.find(`#wb-settings-lang-${settings.lang}`).attr('selected', '');
  87. fields.push(lang);
  88. let display = $('<div>').append(fieldset.display);
  89. display.find(`#wb-settings-display-${settings.display}`).attr('checked', '');
  90. if ( !settings.patreon ) display.find('.wb-settings-display').filter( (i, radioDisplay) => {
  91. return ( i >= rcgcdwLimit.display && !$(radioDisplay).has('input:checked').length );
  92. } ).remove();
  93. fields.push(display);
  94. let feeds = $('<div id="wb-settings-feeds-hide">').append(fieldset.feeds);
  95. if ( /\.(?:fandom\.com|wikia\.org)$/.test(new URL(settings.wiki).hostname) ) {
  96. if ( settings.wikiid ) {
  97. feeds.find('#wb-settings-feeds').attr('checked', '');
  98. if ( settings.rcid === -1 ) feeds.find('#wb-settings-feeds-only').attr('checked', '');
  99. }
  100. else feeds.find('#wb-settings-feeds-only-hide').attr('style', 'visibility: hidden;');
  101. }
  102. else {
  103. feeds.attr('style', 'display: none;');
  104. feeds.find('#wb-settings-feeds-only-hide').attr('style', 'visibility: hidden;');
  105. }
  106. fields.push(feeds);
  107. fields.push($(fieldset.save).val('Save'));
  108. if ( settings.channel ) {
  109. fields.push($(fieldset.delete).val('Delete').attr('onclick', `return confirm('Are you sure?');`));
  110. }
  111. var form = $('<fieldset>').append(...fields);
  112. if ( readonly ) {
  113. form.find('input').attr('readonly', '');
  114. form.find('input[type="checkbox"], input[type="radio"]:not(:checked), option').attr('disabled', '');
  115. form.find('input[type="submit"], button.addmore').remove();
  116. }
  117. return $('<form id="wb-settings" method="post" enctype="application/x-www-form-urlencoded">').append(
  118. $('<h2>').text(header),
  119. form
  120. );
  121. }
  122. /**
  123. * Let a user change recent changes scripts
  124. * @param {import('http').ServerResponse} res - The server response
  125. * @param {import('cheerio')} $ - The response body
  126. * @param {import('./util.js').Guild} guild - The current guild
  127. * @param {String[]} args - The url parts
  128. */
  129. function dashboard_rcscript(res, $, guild, args) {
  130. db.all( 'SELECT webhook, configid, wiki, lang, display, wikiid, rcid FROM rcgcdw WHERE guild = ? ORDER BY configid ASC', [guild.id], function(dberror, rows) {
  131. if ( dberror ) {
  132. console.log( '- Dashboard: Error while getting the RcGcDw: ' + dberror );
  133. $('#text .description').text('Failed to load the recent changes webhooks!');
  134. $('.channel#rcscript').addClass('selected');
  135. let body = $.html();
  136. res.writeHead(200, {'Content-Length': body.length});
  137. res.write( body );
  138. return res.end();
  139. }
  140. $('#text .description').text(`These are the recent changes webhooks for "${guild.name}":`);
  141. Promise.all(rows.map( row => {
  142. return got.get( 'https://discord.com/api/webhooks/' + row.webhook ).then( response => {
  143. row.channel = response.body.channel_id;
  144. }, error => {
  145. console.log( '- Dashboard: Error while getting the webhook: ' + error );
  146. row.channel = 'UNKNOWN';
  147. } );
  148. } )).finally( () => {
  149. $('#channellist #rcscript').after(
  150. ...rows.map( row => {
  151. return $('<a class="channel">').attr('id', `channel-${row.configid}`).append(
  152. $('<img>').attr('src', '/src/channel.svg'),
  153. $('<div>').text(`${row.configid} - ${( guild.channels.find( channel => {
  154. return channel.id === row.channel;
  155. } )?.name || row.channel )}`)
  156. ).attr('href', `/guild/${guild.id}/rcscript/${row.configid}`);
  157. } ),
  158. ( process.env.READONLY || rows.length >= rcgcdwLimit[( guild.patreon ? 'patreon' : 'default' )] ? '' :
  159. $('<a class="channel" id="channel-new">').append(
  160. $('<img>').attr('src', '/src/channel.svg'),
  161. $('<div>').text('New webhook')
  162. ).attr('href', `/guild/${guild.id}/rcscript/new`) )
  163. );
  164. if ( args[4] === 'new' ) {
  165. $('.channel#channel-new').addClass('selected');
  166. createForm($, 'New Recent Changes Webhook', {
  167. wiki: defaultSettings.wiki, lang: defaultSettings.lang,
  168. display: 1, patreon: guild.patreon
  169. }, guild.channels).attr('action', `/guild/${guild.id}/rcscript/new`).appendTo('#text');
  170. }
  171. else if ( rows.some( row => row.configid == args[4] ) ) {
  172. let row = rows.find( row => row.configid == args[4] );
  173. $(`.channel#channel-${row.configid}`).addClass('selected');
  174. createForm($, `Recent Changes Webhook #${row.configid}`, Object.assign({
  175. patreon: guild.patreon
  176. }, row), guild.channels).attr('action', `/guild/${guild.id}/rcscript/${row.configid}`).appendTo('#text');
  177. }
  178. else {
  179. $('.channel#rcscript').addClass('selected');
  180. $('#text .description').text(`*Insert explanation about recent changes webhooks here*`);
  181. }
  182. let body = $.html();
  183. res.writeHead(200, {'Content-Length': body.length});
  184. res.write( body );
  185. return res.end();
  186. } );
  187. } );
  188. }
  189. /**
  190. * Change recent changes scripts
  191. * @param {Function} res - The server response
  192. * @param {import('./util.js').Settings} userSettings - The settings of the user
  193. * @param {String} guild - The id of the guild
  194. * @param {String} type - The setting to change
  195. * @param {Object} settings - The new settings
  196. * @param {String} settings.channel
  197. * @param {String} settings.wiki
  198. * @param {String} settings.lang
  199. * @param {String} settings.display
  200. * @param {String} [settings.feeds]
  201. * @param {String} [settings.feeds_only]
  202. * @param {String} [settings.save_settings]
  203. * @param {String} [settings.delete_settings]
  204. */
  205. function update_rcscript(res, userSettings, guild, type, settings) {
  206. console.log( settings );
  207. return res(`/guild/${guild}/rcscript/${type}?save=failed`);
  208. }
  209. module.exports = {
  210. get: dashboard_rcscript,
  211. post: update_rcscript
  212. };