rcscript.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. readonly = true;
  62. var fields = [];
  63. let channel = $('<div>').append(fieldset.channel);
  64. channel.find('#wb-settings-channel').append(
  65. ...guildChannels.filter( guildChannel => {
  66. return ( hasPerm(guildChannel.permissions, 'VIEW_CHANNEL', 'MANAGE_WEBHOOKS') || settings.channel === guildChannel.id );
  67. } ).map( guildChannel => {
  68. var optionChannel = $(`<option id="wb-settings-channel-${guildChannel.id}">`).val(guildChannel.id);
  69. if ( settings.channel === guildChannel.id ) {
  70. optionChannel.attr('selected', '');
  71. if ( !hasPerm(guildChannel.permissions, 'VIEW_CHANNEL', 'MANAGE_WEBHOOKS') ) {
  72. optionChannel.addClass('wb-settings-error');
  73. readonly = true;
  74. }
  75. }
  76. return optionChannel.text(`${guildChannel.id} – #${guildChannel.name}`);
  77. } )
  78. );
  79. if ( !settings.channel ) channel.find('#wb-settings-channel').prepend(
  80. $(`<option id="wb-settings-channel-default" selected>`).val('').text('-- Select a Channel --')
  81. );
  82. fields.push(channel);
  83. let wiki = $('<div>').append(fieldset.wiki);
  84. wiki.find('#wb-settings-wiki').val(settings.wiki);
  85. fields.push(wiki);
  86. let lang = $('<div>').append(fieldset.lang);
  87. lang.find(`#wb-settings-lang-${settings.lang}`).attr('selected', '');
  88. fields.push(lang);
  89. let display = $('<div>').append(fieldset.display);
  90. display.find(`#wb-settings-display-${settings.display}`).attr('checked', '');
  91. if ( !settings.patreon ) display.find('.wb-settings-display').filter( (i, radioDisplay) => {
  92. return ( i >= rcgcdwLimit.display && !$(radioDisplay).has('input:checked').length );
  93. } ).remove();
  94. fields.push(display);
  95. let feeds = $('<div id="wb-settings-feeds-hide">').append(fieldset.feeds);
  96. if ( /\.(?:fandom\.com|wikia\.org)$/.test(new URL(settings.wiki).hostname) ) {
  97. if ( settings.wikiid ) {
  98. feeds.find('#wb-settings-feeds').attr('checked', '');
  99. if ( settings.rcid === -1 ) feeds.find('#wb-settings-feeds-only').attr('checked', '');
  100. }
  101. else feeds.find('#wb-settings-feeds-only-hide').attr('style', 'visibility: hidden;');
  102. }
  103. else {
  104. feeds.attr('style', 'display: none;');
  105. feeds.find('#wb-settings-feeds-only-hide').attr('style', 'visibility: hidden;');
  106. }
  107. fields.push(feeds);
  108. fields.push($(fieldset.save).val('Save'));
  109. if ( settings.channel ) {
  110. fields.push($(fieldset.delete).val('Delete').attr('onclick', `return confirm('Are you sure?');`));
  111. }
  112. var form = $('<fieldset>').append(...fields);
  113. if ( readonly ) {
  114. form.find('input').attr('readonly', '');
  115. form.find('input[type="checkbox"], input[type="radio"]:not(:checked), option').attr('disabled', '');
  116. form.find('input[type="submit"], button.addmore').remove();
  117. }
  118. return $('<form id="wb-settings" method="post" enctype="application/x-www-form-urlencoded">').append(
  119. $('<h2>').text(header),
  120. form
  121. );
  122. }
  123. /**
  124. * Let a user change recent changes scripts
  125. * @param {import('http').ServerResponse} res - The server response
  126. * @param {import('cheerio')} $ - The response body
  127. * @param {import('./util.js').Guild} guild - The current guild
  128. * @param {String[]} args - The url parts
  129. */
  130. function dashboard_rcscript(res, $, guild, args) {
  131. db.all( 'SELECT webhook, configid, wiki, lang, display, wikiid, rcid FROM rcgcdw WHERE guild = ? ORDER BY configid ASC', [guild.id], function(dberror, rows) {
  132. if ( dberror ) {
  133. console.log( '- Dashboard: Error while getting the RcGcDw: ' + dberror );
  134. $('#text .description').text('Failed to load the recent changes webhooks!');
  135. $('.channel#rcscript').addClass('selected');
  136. let body = $.html();
  137. res.writeHead(200, {'Content-Length': body.length});
  138. res.write( body );
  139. return res.end();
  140. }
  141. $('#text .description').text(`These are the recent changes webhooks for "${guild.name}":`);
  142. Promise.all(rows.map( row => {
  143. return got.get( 'https://discord.com/api/webhooks/' + row.webhook ).then( response => {
  144. row.channel = response.body.channel_id;
  145. }, error => {
  146. console.log( '- Dashboard: Error while getting the webhook: ' + error );
  147. row.channel = 'UNKNOWN';
  148. } );
  149. } )).finally( () => {
  150. $('#channellist #rcscript').after(
  151. ...rows.map( row => {
  152. return $('<a class="channel">').attr('id', `channel-${row.configid}`).append(
  153. $('<img>').attr('src', '/src/channel.svg'),
  154. $('<div>').text(`${row.configid} - ${( guild.channels.find( channel => {
  155. return channel.id === row.channel;
  156. } )?.name || row.channel )}`)
  157. ).attr('href', `/guild/${guild.id}/rcscript/${row.configid}`);
  158. } ),
  159. ( process.env.READONLY || rows.length >= rcgcdwLimit[( guild.patreon ? 'patreon' : 'default' )] ? '' :
  160. $('<a class="channel" id="channel-new">').append(
  161. $('<img>').attr('src', '/src/channel.svg'),
  162. $('<div>').text('New webhook')
  163. ).attr('href', `/guild/${guild.id}/rcscript/new`) )
  164. );
  165. if ( args[4] === 'new' ) {
  166. $('.channel#channel-new').addClass('selected');
  167. createForm($, 'New Recent Changes Webhook', {
  168. wiki: defaultSettings.wiki, lang: defaultSettings.lang,
  169. display: 1, patreon: guild.patreon
  170. }, guild.channels).attr('action', `/guild/${guild.id}/rcscript/new`).appendTo('#text');
  171. }
  172. else if ( rows.some( row => row.configid == args[4] ) ) {
  173. let row = rows.find( row => row.configid == args[4] );
  174. $(`.channel#channel-${row.configid}`).addClass('selected');
  175. createForm($, `Recent Changes Webhook #${row.configid}`, Object.assign({
  176. patreon: guild.patreon
  177. }, row), guild.channels).attr('action', `/guild/${guild.id}/rcscript/${row.configid}`).appendTo('#text');
  178. }
  179. else {
  180. $('.channel#rcscript').addClass('selected');
  181. $('#text .description').text(`*Insert explanation about recent changes webhooks here*`);
  182. }
  183. let body = $.html();
  184. res.writeHead(200, {'Content-Length': body.length});
  185. res.write( body );
  186. return res.end();
  187. } );
  188. } );
  189. }
  190. /**
  191. * Change recent changes scripts
  192. * @param {Function} res - The server response
  193. * @param {import('./util.js').Settings} userSettings - The settings of the user
  194. * @param {String} guild - The id of the guild
  195. * @param {String} type - The setting to change
  196. * @param {Object} settings - The new settings
  197. * @param {String} settings.channel
  198. * @param {String} settings.wiki
  199. * @param {String} settings.lang
  200. * @param {String} settings.display
  201. * @param {String} [settings.feeds]
  202. * @param {String} [settings.feeds_only]
  203. * @param {String} [settings.save_settings]
  204. * @param {String} [settings.delete_settings]
  205. */
  206. function update_rcscript(res, userSettings, guild, type, settings) {
  207. console.log( settings );
  208. return res(`/guild/${guild}/rcscript/${type}?save=failed`);
  209. }
  210. module.exports = {
  211. get: dashboard_rcscript,
  212. post: update_rcscript
  213. };