index.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  1. // MODULE
  2. const { notes, pengguna, bookmark } = require('../db/schema')
  3. const bcrypt = require('bcrypt');
  4. //const { registerPartial } = require('handlebars');
  5. const fs = require('fs');
  6. const { send } = require('process');
  7. const { doesNotMatch } = require('assert');
  8. const url = require('url');
  9. const routes = app => {
  10. //PUBLIC
  11. // GET
  12. app.get('/', async(request, reply) => {
  13. try{
  14. const sessionData = request.session.get('data');
  15. if(sessionData === undefined){
  16. return reply.view('./html/index.hbs', {
  17. title: 'Login'
  18. })
  19. }
  20. origin = request.routerPath
  21. console.log("ORIGIN =>", origin)
  22. const page = request.query.page || 1;
  23. if(sessionData.role === 'superuser') {
  24. const data = await bookmark.paginate({}, {
  25. 'page': page,
  26. 'limit': 25,
  27. sort: {
  28. createdAt: -1
  29. },
  30. 'lean': true
  31. })
  32. return reply.view('./html/dashboard.hbs', {
  33. title: 'Bookmarknya Kus',
  34. bookmark: data,
  35. user: sessionData,
  36. origin: origin
  37. })
  38. }
  39. // if not superuser
  40. const data = await bookmark.paginate({
  41. tags: {
  42. $ne: 'nsfw'
  43. }
  44. }, {
  45. 'page': page,
  46. 'limit': 25,
  47. sort: {
  48. createdAt: -1
  49. },
  50. 'lean': true
  51. })
  52. return reply.view('./html/t.hbs', {
  53. title: 'Bookmarknya Kus',
  54. bookmark: data,
  55. user: sessionData,
  56. origin: origin
  57. })
  58. }catch(err){
  59. reply.send(err)
  60. }
  61. })
  62. // Profil
  63. app.get('/profil', async (request, reply) => {
  64. try{
  65. const sessionData = request.session.get('data');
  66. if(sessionData === undefined){
  67. return reply.view('./html/index.hbs', {
  68. title: 'Login'
  69. })
  70. }
  71. const user = sessionData.user
  72. const userProfile = await pengguna.findOne({username: user}).lean();
  73. const count = await bookmark.aggregate([{ $count: "url"}])
  74. const getTags = await bookmark.find();
  75. const aggrTags = await bookmark.aggregate([{
  76. $project: {
  77. _id: 0,
  78. tagslist: {
  79. $concatArrays: getTags.map(obj => obj.tags)
  80. }
  81. }
  82. }]);
  83. const arr = aggrTags[0].tagslist.sort().filter((urut, index) => {
  84. return aggrTags[0].tagslist.sort().indexOf(urut) === index
  85. })
  86. const simpan = await bookmark.find({}, {
  87. _id: 0,
  88. __v: 0,
  89. createdAt: 0,
  90. updatedAt: 0
  91. });
  92. const json = simpan.toString();
  93. fs.writeFile(`./assets/dl/ekspor-${sessionData.user}.json`, json, (err) => {
  94. if (err) throw err;
  95. });
  96. return reply.view('./html/profile.hbs', {
  97. title: 'Bookmarkus : profile',
  98. data: userProfile,
  99. user: sessionData,
  100. sum: count,
  101. ctags: arr.length
  102. })
  103. }catch(err){
  104. reply.send(err)
  105. }
  106. })
  107. // Notes
  108. app.get('/notes', async (request, reply) => {
  109. try{
  110. const url = request.url;
  111. const sessionData = request.session.get('data');
  112. if (sessionData === undefined) {
  113. return reply.view('./html/index.hbs', {
  114. title: 'Login'
  115. })
  116. }
  117. return reply.view('./html/notes.hbs', {
  118. title: 'Notekus',
  119. user: sessionData,
  120. url: url
  121. })
  122. }catch(err){
  123. return reply.send(err)
  124. }
  125. })
  126. app.get('/note', async (request, reply) => {
  127. try{
  128. const note = await notes.find({}).lean();
  129. return reply.send(note);
  130. }catch(err){
  131. reply.send(err)
  132. }
  133. })
  134. app.get('/login', async (request, reply) => {
  135. try{
  136. return reply.status('302').redirect('/')
  137. }catch(err){
  138. reply.send(err)
  139. }
  140. })
  141. // Hapus data
  142. app.get('/hapus/:id', async (request, reply) => {
  143. try{
  144. const sessionData = request.session.get('data');
  145. if(sessionData === undefined) { return 'Mau apa?'}
  146. let _id = request.params.id;
  147. const hapusData = await bookmark.findByIdAndRemove(_id);
  148. request.flash('Info', `Bookmark dengan ID : ${_id} berhasil dihapus`)
  149. const masalah = reply.flash('info')
  150. return reply.status(302).redirect('/', {
  151. error: masalah
  152. })
  153. }catch(err){
  154. reply.send(err)
  155. }
  156. })
  157. // Input page
  158. app.get('/input', async (request, reply) => {
  159. try{
  160. const sessionData = request.session.get('data');
  161. if (sessionData === undefined) {
  162. return reply.view('./html/index.hbs', {
  163. title: 'Login'
  164. })
  165. }
  166. return reply.view('./html/input.hbs', {
  167. title: 'Input Bookmarks',
  168. user: sessionData
  169. })
  170. }catch(err){
  171. reply.send(err)
  172. }
  173. })
  174. app.get('/baca', async(request, reply) => {
  175. try {
  176. const sessionData = request.session.get('data')
  177. if(sessionData === undefined){
  178. return {
  179. error: 'matamu!'
  180. }
  181. }
  182. const data = await bookmark.paginate({ 'nanti': 'on' }, { 'lean': true })
  183. origin = request.routerPath
  184. o = origin.split('/')
  185. console.log('ORIGIN =>', o[1])
  186. return reply.view('./html/dashboard.hbs', {
  187. title: 'yg belum terbaca',
  188. bookmark: data,
  189. user: sessionData,
  190. origin: o[1]
  191. })
  192. }catch(err){
  193. return (err)
  194. }
  195. })
  196. // Masih bermasalah
  197. // Menenentukan next page
  198. //
  199. app.get('/baca/:id', async (request, reply) => {
  200. try{
  201. const sessionData = request.session.get('data');
  202. if(sessionData === undefined) {
  203. return "matamu!"
  204. }
  205. const _id = request.params.id;
  206. const nanti = "off"
  207. refr = request.header
  208. console.log(refr)
  209. //const q = url.parse(request.url, true);
  210. //const pathawal = q.pathname.split('/')[1];
  211. //const arr = ['/',pathawal]
  212. //const originalUrl = arr.join('')
  213. //console.log(originalUrl)
  214. const sudahBaca = await bookmark.findByIdAndUpdate(_id, {
  215. nanti: nanti
  216. }, {new: true})
  217. return reply.status('302').redirect('/')
  218. }catch(err){
  219. reply.send(err)
  220. }
  221. })
  222. // Input - edit
  223. app.get('/edit/:id', async (request, reply) => {
  224. try{
  225. const sessionData = request.session.get('data');
  226. if (sessionData === undefined) {
  227. return reply.view('./html/index.hbs', {
  228. title: 'Login'
  229. })
  230. }
  231. let id = request.params.id;
  232. const data = await bookmark.findOne({_id: id}).lean()
  233. return reply.view('./html/input.hbs', {
  234. title: 'Edit Bookmarks',
  235. bookmark: data,
  236. id: id,
  237. user: sessionData
  238. })
  239. }catch(err){
  240. reply.send(err)
  241. }
  242. })
  243. app.get('/tags/:tag', async (request, reply) => {
  244. try{
  245. const sessionData = request.session.get('data');
  246. if (sessionData === undefined) {
  247. return reply.view('./html/index.hbs', {
  248. title: 'Login'
  249. })
  250. }
  251. const page = request.query.page || 1;
  252. const tag = request.params.tag;
  253. if(sessionData.role == 'superuser'){
  254. const data = await bookmark.paginate({
  255. tags: tag
  256. }, {
  257. "page": page,
  258. "limit": 25,
  259. "sort": {
  260. createdAt: -1
  261. },
  262. 'lean': true
  263. })
  264. return reply.view('./html/dashboard.hbs', {
  265. title: 'Bookmarknya Kus',
  266. bookmark: data,
  267. key: tag,
  268. user: sessionData
  269. })
  270. }
  271. if(tag == 'nsfw') {
  272. return reply.status(302).redirect('/')
  273. }
  274. const data = await bookmark.paginate({
  275. tags: tag
  276. }, {
  277. "page": page,
  278. "limit": 25,
  279. "sort": {
  280. createdAt: -1
  281. },
  282. 'lean': true
  283. })
  284. return reply.view('./html/dashboard.hbs', {
  285. title: 'Bookmarknya Kus',
  286. bookmark: data,
  287. user: sessionData,
  288. key: tag
  289. })
  290. }catch(err){
  291. reply.send(err)
  292. }
  293. })
  294. app.get('/tagslist', async (request, reply) => {
  295. try{
  296. const getTags = await bookmark.find();
  297. const aggrTags = await bookmark.aggregate([{
  298. $project: {
  299. _id: 0,
  300. tagslist: {
  301. $concatArrays: getTags.map(obj => obj.tags)
  302. }
  303. }
  304. }]);
  305. const arr = aggrTags[0].tagslist.sort().filter((urut, index) => {
  306. return aggrTags[0].tagslist.sort().indexOf(urut) === index
  307. })
  308. const t = arr => {
  309. const map = arr.reduce((a, v) => {
  310. let c = v.charAt(0).toUpperCase();
  311. a[c] = [].concat((a[c] || []), v);
  312. return a;
  313. }, {});
  314. const hasil = Object.keys(map).map(el => ({
  315. abjad: el,
  316. tags: map[el]
  317. }));
  318. return hasil;
  319. };
  320. return reply.send(t(arr))
  321. }catch(err){
  322. reply.send(err)
  323. }
  324. })
  325. app.get('/cari', async (request, reply) => {
  326. try{
  327. const sessionData = request.session.get('data')
  328. if (sessionData === undefined) {
  329. return reply.view('./html/index.hbs', {
  330. title: 'Login'
  331. })
  332. }
  333. const keyword = request.query.q;
  334. if(keyword == 'ernie'){
  335. return reply.status(301).redirect('/e')
  336. }
  337. const url = request.url;
  338. const page = request.query.page || 1;
  339. const data = await bookmark.paginate({
  340. $text: {
  341. $search: keyword
  342. }}, {
  343. "page": page,
  344. "limit": 25,
  345. sort: {
  346. createdAt: -1
  347. },
  348. lean: "true"
  349. });
  350. return reply.view('./html/dashboard.hbs', {
  351. title: 'Bookmarknya Kus',
  352. bookmark: data,
  353. user: sessionData,
  354. q: keyword,
  355. u: url
  356. })
  357. }catch(err){
  358. reply.send(err)
  359. }
  360. })
  361. app.get('/e', async(request, reply) => {
  362. try{
  363. const sessionData = request.session.get('data')
  364. if(sessionData === undefined) {
  365. return reply.send(401).send('Masih dibawah umur!. Minggat!!')
  366. }
  367. return reply.header('content-type', 'text/html; charset=utf-8').send(`
  368. <img src="https://i.ibb.co/d4c1ND8/ernie.jpg" alt="ernie" border="0">
  369. `)
  370. }catch(err){
  371. console.log(err)
  372. }
  373. })
  374. app.get('/logout', async(request, reply) => {
  375. try{
  376. request.session.delete()
  377. await reply.status('302').redirect('/')
  378. }catch(err){
  379. reply.send(err)
  380. }
  381. })
  382. // Testing
  383. app.get('/t', async(request, reply) => {
  384. try{
  385. r = request.query.ref
  386. if(r == 'baca') {
  387. return { 'ref': r }
  388. }
  389. return { 'ref': 'salah' }
  390. }catch(err){
  391. reply.send(err)
  392. }
  393. })
  394. app.get('/ekspor', async (request, reply) => {
  395. try{
  396. const sessionData = request.session.get('data');
  397. if(sessionData === undefined){ return 'Not Allowed'}
  398. reply.type('application/json').send(fs.readFileSync(`./assets/dl/ekspor-${sessionData.user}.json`))
  399. }catch(err){
  400. reply.send(err)
  401. }
  402. })
  403. // API
  404. // POST
  405. // Create User
  406. app.post('/buat-user', async(request, reply) => {
  407. try{
  408. const sessionData = request.session.get('data')
  409. if(sessionData === undefined) {
  410. return reply.send('Tidak diijinkan')
  411. }
  412. const username = request.body.username;
  413. const pass = request.body.password;
  414. const role = request.body.role;
  415. const h = 12;
  416. const hashPass = await bcrypt.hash(pass,h)
  417. const buatUser = new pengguna({
  418. username: username,
  419. password: hashPass,
  420. role: role
  421. })
  422. return buatUser.save();
  423. }catch(err){
  424. reply.send(err)
  425. }
  426. })
  427. // rubah password
  428. app.post('/edit-user', async(request, reply) => {
  429. try{
  430. const sessionData = request.session.get('data')
  431. if (sessionData === undefined) {
  432. return reply.send('Tidak diijinkan')
  433. }
  434. const id = request.body.id;
  435. const lama = request.body.passlama;
  436. const baru = request.body.passbaru;
  437. const cekId = await pengguna.findOne({_id: id})
  438. if(cekId) {
  439. const cekPassLama = await bcrypt.compare(lama, cekId.password)
  440. if(cekPassLama === true) {
  441. const cryptPassBaru = await bcrypt.hash(baru, 12)
  442. const updateData = await pengguna.findByIdAndUpdate(id, {
  443. 'password': cryptPassBaru
  444. }, {
  445. new: true
  446. })
  447. updateData.save();
  448. request.session.delete()
  449. return reply.view('./html/gp.hbs', { title: 'Sukses!'})
  450. }
  451. }
  452. reply.send('User tidak ditemukan')
  453. }catch(err){
  454. reply.send(err)
  455. }
  456. })
  457. // login
  458. app.post('/login', async(request, reply) => {
  459. try{
  460. let username = request.body.username;
  461. let password = request.body.password;
  462. request.flash('warning', 'Username atau Password salah!')
  463. const masalah = reply.flash('warning')
  464. const cekPengguna = await pengguna.findOne({username: username})
  465. if(cekPengguna){
  466. const decPass = await bcrypt.compare(password, cekPengguna.password)
  467. if(cekPengguna.username === username && decPass === true){
  468. request.session.set('data', {
  469. 'user': cekPengguna.username,
  470. 'role': cekPengguna.role
  471. })
  472. return reply.status('302').redirect('/')
  473. }}
  474. return reply.view('./html/index.hbs', {
  475. title: 'Login',
  476. warning: masalah
  477. })
  478. }catch(err){
  479. reply.send(err)
  480. }
  481. })
  482. // Input Data
  483. app.post('/input', async (request, reply) => {
  484. try{
  485. const sessionData = request.session.get('data')
  486. if (sessionData === undefined) {
  487. return reply.send('Tidak diijinkan')
  488. }
  489. let url = request.body.url;
  490. let judul = request.body.judul;
  491. let uraian = request.body.uraian;
  492. let rtags = request.body.tags || 'bookmark'
  493. let tags = rtags.split(' ')
  494. let nanti = request.body.nanti;
  495. const inputData = new bookmark({
  496. 'url': url,
  497. 'judul': judul,
  498. 'uraian': uraian,
  499. 'tags': tags,
  500. 'nanti': nanti
  501. })
  502. inputData.save()
  503. return reply.status('302').redirect('/')
  504. }catch(err){
  505. reply.send(err)
  506. }
  507. })
  508. // Edit data
  509. app.post('/edit', async (request, reply) => {
  510. try{
  511. const sessionData = request.session.get('data')
  512. if (sessionData === undefined) {
  513. return reply.send('Tidak diijinkan')
  514. }
  515. let id = request.body.id;
  516. let url = request.body.url;
  517. let judul = request.body.judul;
  518. let uraian = request.body.uraian;
  519. let rtags = request.body.tags;
  520. let tags = rtags.split(',')
  521. let nanti = request.body.nanti;
  522. const editData = await bookmark.findByIdAndUpdate(id, {
  523. 'url ': url,
  524. 'judul': judul,
  525. 'uraian': uraian,
  526. 'tags': tags,
  527. 'nanti': nanti
  528. }, {new: true})
  529. return reply.status('302').redirect('/')
  530. }catch(err){
  531. reply.send(err)
  532. }
  533. })
  534. // Import Data
  535. app.post('/impor', async (request, reply) => {
  536. try{
  537. const imporData = bookmark.insertMany(request.body)
  538. return imporData;
  539. }catch(err){
  540. reply.send(err)
  541. }
  542. })
  543. app.post('/notes', async (request, reply) => {
  544. try{
  545. let judul = request.body.judul;
  546. let isi = request.body.isi;
  547. let inputNotes = new notes({
  548. 'judul' : judul,
  549. 'isi' : isi
  550. })
  551. inputNotes.save();
  552. return inputNotes;
  553. }catch(err){
  554. return reply.send(err)
  555. }
  556. })
  557. app.get('/poes', async (request, reply) => {
  558. try{
  559. const sessionData = request.session.get('data')
  560. let data = sessionData.user;
  561. console.log(data)
  562. let insert = await bookmark.insertMany({user: data})
  563. return insert;
  564. }catch(err){
  565. console.log(err)
  566. }
  567. });
  568. }; // end of the road
  569. module.exports = { routes };