/ server / auride / getNoteData.js
getNoteData.js
  1  const express = require("express");
  2  const router = express.Router();
  3  const admin = require("firebase-admin");
  4  const db = admin.database();
  5  
  6  router.get("/api/auride/getNoteData", async (req, res) => {
  7      if (req.method !== "GET")
  8          return res.status(403).json({ error: "This method can only be accessed via GET." });
  9  
 10      try {
 11          const { endBefore, limit, path } = req.query;
 12          let notesRef = null;
 13  
 14          // if there's a path, use that for the db. else, use notes
 15          if (path)
 16              notesRef = db.ref(path);
 17          else
 18              notesRef = db.ref("notes");
 19  
 20          let query = notesRef.orderByKey();
 21  
 22          // if we have an endBefore, we want our db to, well, endBefore the ID
 23          if (endBefore)
 24              query = query.endBefore(endBefore);
 25  
 26          // if limit, make sure we only return that amount of notes
 27          if (limit && limit > 1)
 28              query = query.limitToLast(parseInt(limit, 10));
 29          else
 30              return res.status(403).json({ error: "Please load 2 or more notes." });
 31  
 32          // then, get the snapshot
 33          const snapshot = await query.once("value");
 34  
 35          // parse the notesArray to return
 36          // and promises, if necessary!
 37          const notesArray = [];
 38          const promises = [];
 39          snapshot.forEach(childSnapshot => {
 40              const note = childSnapshot.val();
 41  
 42              // function, since this code is repeated more than once
 43              function fetchFullNote(childNote) {
 44                  promises.push((async () => {
 45                      const snap = await db.ref(`notes/${childNote.key}`).once("value");
 46                      const fullNote = snap.val();
 47  
 48                      if (!fullNote)
 49                          return;
 50                      fullNote.key = childNote.key;
 51                      
 52                      // ignore reply and deletion.. details below
 53                      if (fullNote.replyingTo && !path.startsWith("/notes/-"))
 54                          return;
 55                      if (fullNote.isDeleted)
 56                          return;
 57                      const userData = (await db.ref(`users/${fullNote.whoSentIt}`).once("value")).val();
 58                      if (!userData || userData.username === undefined || userData.display === undefined)
 59                          return;
 60  
 61                      notesArray.push(fullNote);
 62                  })());
 63                  return;
 64              }
 65  
 66              // check... is it notes from a user profile or favorite?
 67              if (note.isRenote !== undefined && note.isRenote !== null) // if so, we have to get these separately...
 68                  fetchFullNote(childSnapshot);
 69              else if (note.favorited !== null && note.favorited !== undefined)
 70                  fetchFullNote(childSnapshot);
 71  
 72              // is it a reply? if so, ignore it
 73              // note: replyingTo IS depreciated, however, older forks of auride may still have notes 
 74              // that contain replyingTo.
 75              // if you do, please go to /upgrading/upgradeReplies.js, we end support for this soon!
 76              // TODO: end support for replyingTo
 77              if (note.replyingTo && !path.startsWith("/notes/-"))
 78                  return;
 79  
 80              // is it deleted? if so, ignore it
 81              if (note.isDeleted)
 82                  return;
 83  
 84              promises.push((async () => {
 85                  const userData = (await db.ref(`users/${note.whoSentIt}`).once("value")).val();
 86                  if (!userData || userData.username === undefined || userData.display === undefined)
 87                      return; // they dont exist, or we shouldnt render their note anyways.
 88  
 89                  // else, continue
 90                  note.key = childSnapshot.key;
 91                  notesArray.push(note);
 92              })());
 93          });
 94          await Promise.all(promises);
 95  
 96          // return :)
 97          res.json(notesArray);
 98      } catch (err) {
 99          console.error(err);
100          res.status(500).json({ error: "Failed to fetch notes." });
101      }
102  });
103  
104  module.exports = router;