Created http api endpoints for managing ignored users list
This commit is contained in:
parent
b80f82b9f2
commit
25a61b5809
4 changed files with 133 additions and 5 deletions
125
src/controllers/api/account/ignoredController.js
Normal file
125
src/controllers/api/account/ignoredController.js
Normal file
|
|
@ -0,0 +1,125 @@
|
|||
/*Canopy - The next generation of stoner streaming software
|
||||
Copyright (C) 2024-2026 Rainbownapkin and the TTN Community
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero General Public License as
|
||||
published by the Free Software Foundation, either version 3 of the
|
||||
License, or (at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.*/
|
||||
|
||||
//npm imports
|
||||
const {validationResult, matchedData} = require('express-validator');
|
||||
|
||||
//local imports
|
||||
const {userModel} = require('../../../schemas/user/userSchema');
|
||||
const accountUtils = require('../../../utils/sessionUtils');
|
||||
const {exceptionHandler, errorHandler} = require('../../../utils/loggerUtils');
|
||||
|
||||
//api account functions
|
||||
module.exports.get = async function(req, res){
|
||||
try{
|
||||
//syntactic sugar :P
|
||||
const user = req.session.user;
|
||||
|
||||
//make sure we're not bullshitting ourselves here.
|
||||
if(user == null || user.user == null){
|
||||
return errorHandler(res, 'You must be logged in to retrieve your ignored users!', 'unauthorized');
|
||||
}
|
||||
|
||||
//Grab user
|
||||
const userDB = await userModel.findOne({user: user.user});
|
||||
|
||||
//Bitch about non-existant users
|
||||
if(!userDB){
|
||||
return errorHandler(res, 'User not found!', 'unauthorized');
|
||||
}
|
||||
|
||||
//Set status and send ignored user list
|
||||
res.status(200);
|
||||
return res.send((await userDB.getIgnoredUsers(false)).user);
|
||||
|
||||
}catch(err){
|
||||
exceptionHandler(res, err);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports.post = async function(req, res){
|
||||
try{
|
||||
const validResult = validationResult(req);
|
||||
|
||||
if(validResult.isEmpty()){
|
||||
//syntactic sugar :P
|
||||
const user = req.session.user;
|
||||
const data = matchedData(req);
|
||||
|
||||
//make sure we're not bullshitting ourselves here.
|
||||
if(user == null || user.user == null){
|
||||
return errorHandler(res, 'You must be logged in to ignore a user!', 'unauthorized');
|
||||
}
|
||||
|
||||
//Grab user
|
||||
const userDB = await userModel.findOne({user: user.user});
|
||||
|
||||
//Bitch about non-existant users
|
||||
if(!userDB){
|
||||
return errorHandler(res, 'User not found!', 'unauthorized');
|
||||
}
|
||||
|
||||
//Ignore given user
|
||||
await userDB.ignoreByUser(data.user);
|
||||
|
||||
//Set status and send ignored user list
|
||||
res.status(200);
|
||||
return res.send((await userDB.getIgnoredUsers(false)).user);
|
||||
}else{
|
||||
res.status(400);
|
||||
res.send({errors: validResult.array()})
|
||||
}
|
||||
}catch(err){
|
||||
exceptionHandler(res, err);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports.delete = async function(req, res){
|
||||
try{
|
||||
const validResult = validationResult(req);
|
||||
|
||||
if(validResult.isEmpty()){
|
||||
//syntactic sugar :P
|
||||
const user = req.session.user;
|
||||
const data = matchedData(req);
|
||||
|
||||
//make sure we're not bullshitting ourselves here.
|
||||
if(user == null || user.user == null){
|
||||
return errorHandler(res, 'You must be logged in to ignore a user!', 'unauthorized');
|
||||
}
|
||||
|
||||
//Grab user
|
||||
const userDB = await userModel.findOne({user: user.user});
|
||||
|
||||
//Bitch about non-existant users
|
||||
if(!userDB){
|
||||
return errorHandler(res, 'User not found!', 'unauthorized');
|
||||
}
|
||||
|
||||
//Unignore given user
|
||||
await userDB.unignoreByUser(data.user);
|
||||
|
||||
//Set status and send ignored user list
|
||||
res.status(200);
|
||||
return res.send((await userDB.getIgnoredUsers(false)).user);
|
||||
}else{
|
||||
res.status(400);
|
||||
res.send({errors: validResult.array()})
|
||||
}
|
||||
}catch(err){
|
||||
exceptionHandler(res, err);
|
||||
}
|
||||
}
|
||||
|
|
@ -27,9 +27,11 @@ const validator = require('validator');//No express here, so regular validator i
|
|||
//profile functions
|
||||
module.exports.get = async function(req, res){
|
||||
try{
|
||||
//Get profile name from URL
|
||||
var profileName = req.url.slice(1) == '' ? (req.session.user ? req.session.user.user : null) : req.url.slice(1);
|
||||
|
||||
const profile = await userModel.findProfile({user: profileName}, true);
|
||||
//Pull profile including semi-sensitive information (this object will only be parsed server side)
|
||||
const profile = await userModel.findProfile({user: profileName}, true, true);
|
||||
|
||||
if(profile){
|
||||
//If we have a user, check if the is looking at their own profile
|
||||
|
|
|
|||
|
|
@ -28,6 +28,7 @@ const rankEnumController = require("../../controllers/api/account/rankEnumContro
|
|||
const passwordResetRequestController = require("../../controllers/api/account/passwordResetRequestController");
|
||||
const passwordResetController = require("../../controllers/api/account/passwordResetController");
|
||||
const emailChangeRequestController = require('../../controllers/api/account/emailChangeRequestController');
|
||||
const ignoredController = require("../../controllers/api/account/ignoredController");
|
||||
const deleteController = require("../../controllers/api/account/deleteController");
|
||||
|
||||
//globals
|
||||
|
|
@ -74,6 +75,10 @@ router.post('/passwordResetRequest', accountValidator.user(), passwordResetReque
|
|||
router.post('/passwordReset', accountValidator.securityToken(), accountValidator.securePass(), accountValidator.pass('confirmPass'), passwordResetController.post);
|
||||
//email change request
|
||||
router.post('/emailChangeRequest', accountValidator.email(), accountValidator.pass(), emailChangeRequestController.post);
|
||||
//ignored users list management
|
||||
router.get('/ignore', ignoredController.get);
|
||||
router.post('/ignore', accountValidator.user(), ignoredController.post);
|
||||
router.post('/unignore', accountValidator.user(), ignoredController.delete);
|
||||
//account deletion
|
||||
router.post('/delete', accountValidator.pass(), deleteController.post);
|
||||
|
||||
|
|
|
|||
|
|
@ -702,8 +702,6 @@ userSchema.methods.ignoreByUserDoc = async function(userDB){
|
|||
|
||||
//Save the DB document
|
||||
await this.save();
|
||||
|
||||
console.log(await this.getIgnoredUsers());
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -737,8 +735,6 @@ userSchema.methods.unignoreByUserDoc = async function(userDB){
|
|||
|
||||
//Save the DB document
|
||||
await this.save();
|
||||
|
||||
console.log(await this.getIgnoredUsers());
|
||||
}
|
||||
|
||||
userSchema.methods.getIgnoredUsers = async function(fetchAlts = true){
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue