Merge branch 'master' of https://github.com/bloomsirenix/fosscord-api
This commit is contained in:
		
						commit
						b45e372b6e
					
				
							
								
								
									
										184389
									
								
								assets/passwords.txt
									
									
									
									
									
								
							
							
						
						
									
										184389
									
								
								assets/passwords.txt
									
									
									
									
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
							
								
								
									
										724
									
								
								package-lock.json
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										724
									
								
								package-lock.json
									
									
									
										generated
									
									
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
							
								
								
									
										10
									
								
								package.json
									
									
									
									
									
								
							
							
						
						
									
										10
									
								
								package.json
									
									
									
									
									
								
							| @ -9,7 +9,6 @@ | |||||||
| 		"test:watch": "jest --watch", | 		"test:watch": "jest --watch", | ||||||
| 		"start": "npm run build && node dist/start", | 		"start": "npm run build && node dist/start", | ||||||
| 		"build": "tsc -b .", | 		"build": "tsc -b .", | ||||||
| 		"postinstall": "patch-package", |  | ||||||
| 		"dev": "tsnd --respawn src/start.ts" | 		"dev": "tsnd --respawn src/start.ts" | ||||||
| 	}, | 	}, | ||||||
| 	"repository": { | 	"repository": { | ||||||
| @ -30,7 +29,7 @@ | |||||||
| 	}, | 	}, | ||||||
| 	"homepage": "https://github.com/fosscord/fosscord-api#readme", | 	"homepage": "https://github.com/fosscord/fosscord-api#readme", | ||||||
| 	"dependencies": { | 	"dependencies": { | ||||||
| 		"@fosscord/server-util": "^1.2.2", | 		"@fosscord/server-util": "^1.2.6", | ||||||
| 		"@types/jest": "^26.0.22", | 		"@types/jest": "^26.0.22", | ||||||
| 		"bcrypt": "^5.0.1", | 		"bcrypt": "^5.0.1", | ||||||
| 		"body-parser": "^1.19.0", | 		"body-parser": "^1.19.0", | ||||||
| @ -38,17 +37,16 @@ | |||||||
| 		"express": "^4.17.1", | 		"express": "^4.17.1", | ||||||
| 		"express-validator": "^6.9.2", | 		"express-validator": "^6.9.2", | ||||||
| 		"i18next": "^19.8.5", | 		"i18next": "^19.8.5", | ||||||
| 		"i18next-http-middleware": "^3.1.1", | 		"i18next-http-middleware": "^3.1.3", | ||||||
| 		"i18next-node-fs-backend": "^2.1.3", | 		"i18next-node-fs-backend": "^2.1.3", | ||||||
| 		"jsonwebtoken": "^8.5.1", | 		"jsonwebtoken": "^8.5.1", | ||||||
| 		"lambert-server": "^1.2.2", | 		"lambert-server": "^1.2.2", | ||||||
| 		"missing-native-js-functions": "^1.2.6", | 		"missing-native-js-functions": "^1.2.6", | ||||||
| 		"mongodb": "^3.6.4", | 		"mongodb": "^3.6.5", | ||||||
| 		"mongoose": "^5.12.3", | 		"mongoose": "^5.12.3", | ||||||
| 		"mongoose-autopopulate": "^0.12.3", | 		"mongoose-autopopulate": "^0.12.3", | ||||||
| 		"mongoose-long": "^0.3.2", | 		"mongoose-long": "^0.3.2", | ||||||
| 		"multer": "^1.4.2", | 		"multer": "^1.4.2" | ||||||
| 		"patch-package": "^6.2.2" |  | ||||||
| 	}, | 	}, | ||||||
| 	"devDependencies": { | 	"devDependencies": { | ||||||
| 		"@types/bcrypt": "^3.0.0", | 		"@types/bcrypt": "^3.0.0", | ||||||
|  | |||||||
| @ -1,274 +0,0 @@ | |||||||
| diff --git a/node_modules/i18next-http-middleware/cjs/httpFunctions.js b/node_modules/i18next-http-middleware/cjs/httpFunctions.js
 |  | ||||||
| index 47f0d61..c1ebebc 100644
 |  | ||||||
| --- a/node_modules/i18next-http-middleware/cjs/httpFunctions.js
 |  | ||||||
| +++ b/node_modules/i18next-http-middleware/cjs/httpFunctions.js
 |  | ||||||
| @@ -1,175 +1,181 @@
 |  | ||||||
|  "use strict"; |  | ||||||
|   |  | ||||||
|  Object.defineProperty(exports, "__esModule", { |  | ||||||
| -  value: true
 |  | ||||||
| +	value: true,
 |  | ||||||
|  }); |  | ||||||
|  exports.extendOptionsWithDefaults = exports.getSession = exports.send = exports.setStatus = exports.setContentType = exports.setHeader = exports.getHeader = exports.getBody = exports.getCookies = exports.getHeaders = exports.getParams = exports.getQuery = exports.getOriginalUrl = exports.setUrl = exports.getUrl = exports.getPath = void 0; |  | ||||||
|   |  | ||||||
|  var getPath = function getPath(req) { |  | ||||||
| -  if (req.path) return req.path;
 |  | ||||||
| -  if (req.raw && req.raw.path) return req.raw.path;
 |  | ||||||
| -  if (req.url) return req.url;
 |  | ||||||
| -  console.log('no possibility found to get path');
 |  | ||||||
| +	if (req.path) return req.path;
 |  | ||||||
| +	if (req.raw && req.raw.path) return req.raw.path;
 |  | ||||||
| +	if (req.url) return req.url;
 |  | ||||||
| +	console.log("no possibility found to get path");
 |  | ||||||
|  }; |  | ||||||
|   |  | ||||||
|  exports.getPath = getPath; |  | ||||||
|   |  | ||||||
|  var getUrl = function getUrl(req) { |  | ||||||
| -  if (req.url) return req.url;
 |  | ||||||
| -  if (req.raw && req.raw.url) return req.raw.url;
 |  | ||||||
| -  console.log('no possibility found to get url');
 |  | ||||||
| +	if (req.url) return req.url;
 |  | ||||||
| +	if (req.raw && req.raw.url) return req.raw.url;
 |  | ||||||
| +	console.log("no possibility found to get url");
 |  | ||||||
|  }; |  | ||||||
|   |  | ||||||
|  exports.getUrl = getUrl; |  | ||||||
|   |  | ||||||
|  var setUrl = function setUrl(req, url) { |  | ||||||
| -  if (req.url) {
 |  | ||||||
| -    req.url = url;
 |  | ||||||
| -    return;
 |  | ||||||
| -  }
 |  | ||||||
| +	if (req.url) {
 |  | ||||||
| +		req.url = url;
 |  | ||||||
| +		return;
 |  | ||||||
| +	}
 |  | ||||||
|   |  | ||||||
| -  console.log('no possibility found to get url');
 |  | ||||||
| +	console.log("no possibility found to get url");
 |  | ||||||
|  }; |  | ||||||
|   |  | ||||||
|  exports.setUrl = setUrl; |  | ||||||
|   |  | ||||||
|  var getOriginalUrl = function getOriginalUrl(req) { |  | ||||||
| -  if (req.originalUrl) return req.originalUrl;
 |  | ||||||
| -  if (req.raw && req.raw.originalUrl) return req.raw.originalUrl;
 |  | ||||||
| -  return getUrl(req);
 |  | ||||||
| +	if (req.originalUrl) return req.originalUrl;
 |  | ||||||
| +	if (req.raw && req.raw.originalUrl) return req.raw.originalUrl;
 |  | ||||||
| +	return getUrl(req);
 |  | ||||||
|  }; |  | ||||||
|   |  | ||||||
|  exports.getOriginalUrl = getOriginalUrl; |  | ||||||
|   |  | ||||||
|  var getQuery = function getQuery(req) { |  | ||||||
| -  if (req.query && typeof req.query.entries === 'function' && typeof Object.fromEntries === 'function') return Object.fromEntries(req.query);
 |  | ||||||
| -  if (req.query) return req.query;
 |  | ||||||
| -  if (req.raw && req.raw.query) return req.raw.query;
 |  | ||||||
| -  if (req.ctx && req.ctx.queryParams) return req.ctx.queryParams;
 |  | ||||||
| -  var url = req.url || req.raw && req.raw.url;
 |  | ||||||
| -  if (url && url.indexOf('?') < 0) return {};
 |  | ||||||
| -  console.log('no possibility found to get query');
 |  | ||||||
| -  return {};
 |  | ||||||
| +	try {
 |  | ||||||
| +		if (req.query && typeof req.query.entries === "function" && typeof Object.fromEntries === "function")
 |  | ||||||
| +			return Object.fromEntries(req.query);
 |  | ||||||
| +	} catch (e) {}
 |  | ||||||
| +
 |  | ||||||
| +	if (req.query) return req.query;
 |  | ||||||
| +	if (req.raw && req.raw.query) return req.raw.query;
 |  | ||||||
| +	if (req.ctx && req.ctx.queryParams) return req.ctx.queryParams;
 |  | ||||||
| +	var url = req.url || (req.raw && req.raw.url);
 |  | ||||||
| +	if (url && url.indexOf("?") < 0) return {};
 |  | ||||||
| +	console.log("no possibility found to get query");
 |  | ||||||
| +	return {};
 |  | ||||||
|  }; |  | ||||||
|   |  | ||||||
|  exports.getQuery = getQuery; |  | ||||||
|   |  | ||||||
|  var getParams = function getParams(req) { |  | ||||||
| -  if (req.params) return req.params;
 |  | ||||||
| -  if (req.raw && req.raw.params) return req.raw.params;
 |  | ||||||
| -  if (req.ctx && req.ctx.params) return req.ctx.params;
 |  | ||||||
| -  console.log('no possibility found to get params');
 |  | ||||||
| -  return {};
 |  | ||||||
| +	if (req.params) return req.params;
 |  | ||||||
| +	if (req.raw && req.raw.params) return req.raw.params;
 |  | ||||||
| +	if (req.ctx && req.ctx.params) return req.ctx.params;
 |  | ||||||
| +	console.log("no possibility found to get params");
 |  | ||||||
| +	return {};
 |  | ||||||
|  }; |  | ||||||
|   |  | ||||||
|  exports.getParams = getParams; |  | ||||||
|   |  | ||||||
|  var getHeaders = function getHeaders(req) { |  | ||||||
| -  if (req.headers) return req.headers;
 |  | ||||||
| -  console.log('no possibility found to get headers');
 |  | ||||||
| +	if (req.headers) return req.headers;
 |  | ||||||
| +	console.log("no possibility found to get headers");
 |  | ||||||
|  }; |  | ||||||
|   |  | ||||||
|  exports.getHeaders = getHeaders; |  | ||||||
|   |  | ||||||
|  var getCookies = function getCookies(req) { |  | ||||||
| -  if (req.cookies) return req.cookies;
 |  | ||||||
| +	if (req.cookies) return req.cookies;
 |  | ||||||
|   |  | ||||||
| -  if (getHeaders(req)) {
 |  | ||||||
| -    var list = {};
 |  | ||||||
| -    var rc = getHeaders(req).cookie;
 |  | ||||||
| -    rc && rc.split(';').forEach(function (cookie) {
 |  | ||||||
| -      var parts = cookie.split('=');
 |  | ||||||
| -      list[parts.shift().trim()] = decodeURI(encodeURI(parts.join('=')));
 |  | ||||||
| -    });
 |  | ||||||
| -    return list;
 |  | ||||||
| -  }
 |  | ||||||
| +	if (getHeaders(req)) {
 |  | ||||||
| +		var list = {};
 |  | ||||||
| +		var rc = getHeaders(req).cookie;
 |  | ||||||
| +		rc &&
 |  | ||||||
| +			rc.split(";").forEach(function (cookie) {
 |  | ||||||
| +				var parts = cookie.split("=");
 |  | ||||||
| +				list[parts.shift().trim()] = decodeURI(encodeURI(parts.join("=")));
 |  | ||||||
| +			});
 |  | ||||||
| +		return list;
 |  | ||||||
| +	}
 |  | ||||||
|   |  | ||||||
| -  console.log('no possibility found to get cookies');
 |  | ||||||
| +	console.log("no possibility found to get cookies");
 |  | ||||||
|  }; |  | ||||||
|   |  | ||||||
|  exports.getCookies = getCookies; |  | ||||||
|   |  | ||||||
|  var getBody = function getBody(req) { |  | ||||||
| -  if (req.ctx && typeof req.ctx.body === 'function') return req.ctx.body.bind(req.ctx);
 |  | ||||||
| -  if (req.ctx && req.ctx.body) return req.ctx.body;
 |  | ||||||
| -  if (req.json) return req.json;
 |  | ||||||
| -  if (req.body) return req.body;
 |  | ||||||
| -  console.log('no possibility found to get body');
 |  | ||||||
| -  return {};
 |  | ||||||
| +	if (req.ctx && typeof req.ctx.body === "function") return req.ctx.body.bind(req.ctx);
 |  | ||||||
| +	if (req.ctx && req.ctx.body) return req.ctx.body;
 |  | ||||||
| +	if (req.json) return req.json;
 |  | ||||||
| +	if (req.body) return req.body;
 |  | ||||||
| +	console.log("no possibility found to get body");
 |  | ||||||
| +	return {};
 |  | ||||||
|  }; |  | ||||||
|   |  | ||||||
|  exports.getBody = getBody; |  | ||||||
|   |  | ||||||
|  var getHeader = function getHeader(res, name) { |  | ||||||
| -  if (res.getHeader) return res.getHeader(name);
 |  | ||||||
| -  if (res.headers) return res.headers[name];
 |  | ||||||
| -  if (getHeaders(res) && getHeaders(res)[name]) return getHeaders(res)[name];
 |  | ||||||
| -  console.log('no possibility found to get header');
 |  | ||||||
| -  return undefined;
 |  | ||||||
| +	if (res.getHeader) return res.getHeader(name);
 |  | ||||||
| +	if (res.headers) return res.headers[name];
 |  | ||||||
| +	if (getHeaders(res) && getHeaders(res)[name]) return getHeaders(res)[name];
 |  | ||||||
| +	console.log("no possibility found to get header");
 |  | ||||||
| +	return undefined;
 |  | ||||||
|  }; |  | ||||||
|   |  | ||||||
|  exports.getHeader = getHeader; |  | ||||||
|   |  | ||||||
|  var setHeader = function setHeader(res, name, value) { |  | ||||||
| -  if (res._headerSent || res.headersSent) return;
 |  | ||||||
| -  if (typeof res.setHeader === 'function') return res.setHeader(name, value);
 |  | ||||||
| -  if (typeof res.header === 'function') return res.header(name, value);
 |  | ||||||
| -  if (res.responseHeaders && typeof res.responseHeaders.set === 'function') return res.responseHeaders.set(name, value);
 |  | ||||||
| -  if (res.headers && typeof res.headers.set === 'function') return res.headers.set(name, value);
 |  | ||||||
| -  console.log('no possibility found to set header');
 |  | ||||||
| +	if (res._headerSent || res.headersSent) return;
 |  | ||||||
| +	if (typeof res.setHeader === "function") return res.setHeader(name, value);
 |  | ||||||
| +	if (typeof res.header === "function") return res.header(name, value);
 |  | ||||||
| +	if (res.responseHeaders && typeof res.responseHeaders.set === "function")
 |  | ||||||
| +		return res.responseHeaders.set(name, value);
 |  | ||||||
| +	if (res.headers && typeof res.headers.set === "function") return res.headers.set(name, value);
 |  | ||||||
| +	console.log("no possibility found to set header");
 |  | ||||||
|  }; |  | ||||||
|   |  | ||||||
|  exports.setHeader = setHeader; |  | ||||||
|   |  | ||||||
|  var setContentType = function setContentType(res, type) { |  | ||||||
| -  if (typeof res.contentType === 'function') return res.contentType(type);
 |  | ||||||
| -  if (typeof res.type === 'function') return res.type(type);
 |  | ||||||
| -  setHeader(res, 'Content-Type', type);
 |  | ||||||
| +	if (typeof res.contentType === "function") return res.contentType(type);
 |  | ||||||
| +	if (typeof res.type === "function") return res.type(type);
 |  | ||||||
| +	setHeader(res, "Content-Type", type);
 |  | ||||||
|  }; |  | ||||||
|   |  | ||||||
|  exports.setContentType = setContentType; |  | ||||||
|   |  | ||||||
|  var setStatus = function setStatus(res, code) { |  | ||||||
| -  if (typeof res.status === 'function') return res.status(code);
 |  | ||||||
| -  if (res.status) return res.status = code;
 |  | ||||||
| -  console.log('no possibility found to set status');
 |  | ||||||
| +	if (typeof res.status === "function") return res.status(code);
 |  | ||||||
| +	if (res.status) return (res.status = code);
 |  | ||||||
| +	console.log("no possibility found to set status");
 |  | ||||||
|  }; |  | ||||||
|   |  | ||||||
|  exports.setStatus = setStatus; |  | ||||||
|   |  | ||||||
|  var send = function send(res, body) { |  | ||||||
| -  if (typeof res.send === 'function') return res.send(body);
 |  | ||||||
| -  return body;
 |  | ||||||
| +	if (typeof res.send === "function") return res.send(body);
 |  | ||||||
| +	return body;
 |  | ||||||
|  }; |  | ||||||
|   |  | ||||||
|  exports.send = send; |  | ||||||
|   |  | ||||||
|  var getSession = function getSession(req) { |  | ||||||
| -  if (req.session) return req.session;
 |  | ||||||
| -  if (req.raw && req.raw.session) return req.raw.session;
 |  | ||||||
| -  console.log('no possibility found to get session');
 |  | ||||||
| +	if (req.session) return req.session;
 |  | ||||||
| +	if (req.raw && req.raw.session) return req.raw.session;
 |  | ||||||
| +	console.log("no possibility found to get session");
 |  | ||||||
|  }; |  | ||||||
|   |  | ||||||
|  exports.getSession = getSession; |  | ||||||
|   |  | ||||||
|  var extendOptionsWithDefaults = function extendOptionsWithDefaults() { |  | ||||||
| -  var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
 |  | ||||||
| -  options.getPath = options.getPath || getPath;
 |  | ||||||
| -  options.getOriginalUrl = options.getOriginalUrl || getOriginalUrl;
 |  | ||||||
| -  options.getUrl = options.getUrl || getUrl;
 |  | ||||||
| -  options.setUrl = options.setUrl || setUrl;
 |  | ||||||
| -  options.getParams = options.getParams || getParams;
 |  | ||||||
| -  options.getSession = options.getSession || getSession;
 |  | ||||||
| -  options.getQuery = options.getQuery || getQuery;
 |  | ||||||
| -  options.getCookies = options.getCookies || getCookies;
 |  | ||||||
| -  options.getBody = options.getBody || getBody;
 |  | ||||||
| -  options.getHeaders = options.getHeaders || getHeaders;
 |  | ||||||
| -  options.getHeader = options.getHeader || getHeader;
 |  | ||||||
| -  options.setHeader = options.setHeader || setHeader;
 |  | ||||||
| -  options.setContentType = options.setContentType || setContentType;
 |  | ||||||
| -  options.setStatus = options.setStatus || setStatus;
 |  | ||||||
| -  options.send = options.send || send;
 |  | ||||||
| -  return options;
 |  | ||||||
| +	var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
 |  | ||||||
| +	options.getPath = options.getPath || getPath;
 |  | ||||||
| +	options.getOriginalUrl = options.getOriginalUrl || getOriginalUrl;
 |  | ||||||
| +	options.getUrl = options.getUrl || getUrl;
 |  | ||||||
| +	options.setUrl = options.setUrl || setUrl;
 |  | ||||||
| +	options.getParams = options.getParams || getParams;
 |  | ||||||
| +	options.getSession = options.getSession || getSession;
 |  | ||||||
| +	options.getQuery = options.getQuery || getQuery;
 |  | ||||||
| +	options.getCookies = options.getCookies || getCookies;
 |  | ||||||
| +	options.getBody = options.getBody || getBody;
 |  | ||||||
| +	options.getHeaders = options.getHeaders || getHeaders;
 |  | ||||||
| +	options.getHeader = options.getHeader || getHeader;
 |  | ||||||
| +	options.setHeader = options.setHeader || setHeader;
 |  | ||||||
| +	options.setContentType = options.setContentType || setContentType;
 |  | ||||||
| +	options.setStatus = options.setStatus || setStatus;
 |  | ||||||
| +	options.send = options.send || send;
 |  | ||||||
| +	return options;
 |  | ||||||
|  }; |  | ||||||
|   |  | ||||||
|  exports.extendOptionsWithDefaults = extendOptionsWithDefaults; |  | ||||||
| \ No newline at end of file |  | ||||||
| @ -37,13 +37,17 @@ export class FosscordServer extends Server { | |||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	async setupSchema() { | 	async setupSchema() { | ||||||
| 		await db.collection("users").createIndex({ id: 1 }, { unique: true }); | 		return Promise.all([ | ||||||
| 		await db.collection("messages").createIndex({ id: 1 }, { unique: true }); | 			db.collection("users").createIndex({ id: 1 }, { unique: true }), | ||||||
| 		await db.collection("channels").createIndex({ id: 1 }, { unique: true }); | 			db.collection("messages").createIndex({ id: 1 }, { unique: true }), | ||||||
| 		await db.collection("guilds").createIndex({ id: 1 }, { unique: true }); | 			db.collection("channels").createIndex({ id: 1 }, { unique: true }), | ||||||
| 		await db.collection("members").createIndex({ id: 1, guild_id: 1 }, { unique: true }); | 			db.collection("guilds").createIndex({ id: 1 }, { unique: true }), | ||||||
| 		await db.collection("roles").createIndex({ id: 1 }, { unique: true }); | 			db.collection("members").createIndex({ id: 1, guild_id: 1 }, { unique: true }), | ||||||
| 		await db.collection("emojis").createIndex({ id: 1 }, { unique: true }); | 			db.collection("roles").createIndex({ id: 1 }, { unique: true }), | ||||||
|  | 			db.collection("emojis").createIndex({ id: 1 }, { unique: true }), | ||||||
|  | 			db.collection("invites").createIndex({ code: 1 }, { unique: true }), | ||||||
|  | 			db.collection("invites").createIndex({ expires_at: 1 }, { expireAfterSeconds: 0 }) // after 0 seconds of expires_at the invite will get delete
 | ||||||
|  | 		]); | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	async start() { | 	async start() { | ||||||
| @ -70,9 +74,9 @@ export class FosscordServer extends Server { | |||||||
| 				fallbackLng: "en", | 				fallbackLng: "en", | ||||||
| 				ns, | 				ns, | ||||||
| 				backend: { | 				backend: { | ||||||
| 					loadPath: __dirname + "/../locales/{{lng}}/{{ns}}.json", | 					loadPath: __dirname + "/../locales/{{lng}}/{{ns}}.json" | ||||||
| 				}, | 				}, | ||||||
| 				load: "all", | 				load: "all" | ||||||
| 			}); | 			}); | ||||||
| 		this.app.use(i18nextMiddleware.handle(i18next, {})); | 		this.app.use(i18nextMiddleware.handle(i18next, {})); | ||||||
| 
 | 
 | ||||||
| @ -92,8 +96,8 @@ export class FosscordServer extends Server { | |||||||
| 			const response = await fetch(`https://discord.com/assets/${req.params.file}`, { | 			const response = await fetch(`https://discord.com/assets/${req.params.file}`, { | ||||||
| 				// @ts-ignore
 | 				// @ts-ignore
 | ||||||
| 				headers: { | 				headers: { | ||||||
| 					...req.headers, | 					...req.headers | ||||||
| 				}, | 				} | ||||||
| 			}); | 			}); | ||||||
| 			const buffer = await response.buffer(); | 			const buffer = await response.buffer(); | ||||||
| 
 | 
 | ||||||
| @ -107,7 +111,7 @@ export class FosscordServer extends Server { | |||||||
| 						"transfer-encoding", | 						"transfer-encoding", | ||||||
| 						"expect-ct", | 						"expect-ct", | ||||||
| 						"access-control-allow-origin", | 						"access-control-allow-origin", | ||||||
| 						"content-encoding", | 						"content-encoding" | ||||||
| 					].includes(name.toLowerCase()) | 					].includes(name.toLowerCase()) | ||||||
| 				) { | 				) { | ||||||
| 					return; | 					return; | ||||||
|  | |||||||
| @ -24,16 +24,19 @@ router.post("/", check(InviteCreateSchema), async (req: Request, res: Response) | |||||||
| 	const permission = await getPermission(user_id, guild_id); | 	const permission = await getPermission(user_id, guild_id); | ||||||
| 	permission.hasThrow("CREATE_INSTANT_INVITE"); | 	permission.hasThrow("CREATE_INSTANT_INVITE"); | ||||||
| 
 | 
 | ||||||
|  | 	const expires_at = new Date(req.body.max_age * 1000 + Date.now()); | ||||||
|  | 
 | ||||||
| 	const invite = { | 	const invite = { | ||||||
| 		code: random(), | 		code: random(), | ||||||
| 		temporary: req.body.temporary, | 		temporary: req.body.temporary, | ||||||
| 		uses: 0, | 		uses: 0, | ||||||
| 		max_uses: req.body.max_uses, | 		max_uses: req.body.max_uses, | ||||||
| 		max_age: req.body.max_age, | 		max_age: req.body.max_age, | ||||||
|  | 		expires_at, | ||||||
| 		created_at: new Date(), | 		created_at: new Date(), | ||||||
| 		guild_id, | 		guild_id, | ||||||
| 		channel_id: channel_id, | 		channel_id: channel_id, | ||||||
| 		inviter_id: user_id, | 		inviter_id: user_id | ||||||
| 	}; | 	}; | ||||||
| 
 | 
 | ||||||
| 	await new InviteModel(invite).save(); | 	await new InviteModel(invite).save(); | ||||||
|  | |||||||
| @ -1,11 +1,47 @@ | |||||||
| import { ChannelModel, getPermission, MessageDeleteEvent, MessageModel } from "@fosscord/server-util"; | import { ChannelModel, getPermission, MessageDeleteEvent, MessageModel, MessageUpdateEvent, toObject } from "@fosscord/server-util"; | ||||||
| import { Router } from "express"; | import { Router } from "express"; | ||||||
| import { HTTPError } from "lambert-server"; | import { HTTPError } from "lambert-server"; | ||||||
|  | import { MessageCreateSchema } from "../../../../../schema/Message"; | ||||||
| import { emitEvent } from "../../../../../util/Event"; | import { emitEvent } from "../../../../../util/Event"; | ||||||
| import { check } from "../../../../../util/instanceOf"; | import { check } from "../../../../../util/instanceOf"; | ||||||
|  | import { handleMessage } from "../../../../../util/Message"; | ||||||
| 
 | 
 | ||||||
| const router = Router(); | const router = Router(); | ||||||
| // TODO:
 | 
 | ||||||
|  | router.patch("/", check(MessageCreateSchema), async (req, res) => { | ||||||
|  | 	const { message_id, channel_id } = req.params; | ||||||
|  | 	var body = req.body as MessageCreateSchema; | ||||||
|  | 
 | ||||||
|  | 	var message = await MessageModel.findOne({ id: message_id, channel_id }, { author_id: true }).exec(); | ||||||
|  | 	if (!message) throw new HTTPError("Message not found", 404); | ||||||
|  | 
 | ||||||
|  | 	const permissions = await getPermission(req.user_id, undefined, channel_id); | ||||||
|  | 
 | ||||||
|  | 	if (req.user_id !== message.author_id) { | ||||||
|  | 		permissions.hasThrow("MANAGE_MESSAGES"); | ||||||
|  | 		body = { flags: body.flags }; | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	const opts = await handleMessage({ | ||||||
|  | 		...body, | ||||||
|  | 		author_id: message.author_id, | ||||||
|  | 		channel_id, | ||||||
|  | 		id: message_id, | ||||||
|  | 		edited_timestamp: new Date() | ||||||
|  | 	}); | ||||||
|  | 
 | ||||||
|  | 	message = await MessageModel.findOneAndUpdate({ id: message_id }, opts).populate("author").exec(); | ||||||
|  | 	if (!message) throw new HTTPError("Message not found", 404); | ||||||
|  | 
 | ||||||
|  | 	await emitEvent({ | ||||||
|  | 		event: "MESSAGE_UPDATE", | ||||||
|  | 		channel_id, | ||||||
|  | 		guild_id: message.guild_id, | ||||||
|  | 		data: { ...toObject(message), nonce: undefined } | ||||||
|  | 	} as MessageUpdateEvent); | ||||||
|  | 
 | ||||||
|  | 	return res.json(toObject(message)); | ||||||
|  | }); | ||||||
| 
 | 
 | ||||||
| router.delete("/", async (req, res) => { | router.delete("/", async (req, res) => { | ||||||
| 	const { message_id, channel_id } = req.params; | 	const { message_id, channel_id } = req.params; | ||||||
|  | |||||||
| @ -1,6 +1,206 @@ | |||||||
|  | import { | ||||||
|  | 	ChannelModel, | ||||||
|  | 	EmojiModel, | ||||||
|  | 	getPermission, | ||||||
|  | 	MemberModel, | ||||||
|  | 	MessageModel, | ||||||
|  | 	MessageReactionAddEvent, | ||||||
|  | 	MessageReactionRemoveAllEvent, | ||||||
|  | 	MessageReactionRemoveEmojiEvent, | ||||||
|  | 	MessageReactionRemoveEvent, | ||||||
|  | 	PartialEmoji, | ||||||
|  | 	PublicUserProjection, | ||||||
|  | 	toObject, | ||||||
|  | 	UserModel | ||||||
|  | } from "@fosscord/server-util"; | ||||||
| import { Router } from "express"; | import { Router } from "express"; | ||||||
|  | import { HTTPError } from "lambert-server"; | ||||||
|  | import { emitEvent } from "../../../../../util/Event"; | ||||||
| 
 | 
 | ||||||
| const router = Router(); | const router = Router(); | ||||||
| // TODO:
 | // TODO: check if emoji is really an unicode emoji or a prperly encoded external emoji
 | ||||||
|  | 
 | ||||||
|  | function getEmoji(emoji: string): PartialEmoji { | ||||||
|  | 	emoji = decodeURIComponent(emoji); | ||||||
|  | 	const parts = emoji.includes(":") && emoji.split(":"); | ||||||
|  | 	if (parts) | ||||||
|  | 		return { | ||||||
|  | 			name: parts[0], | ||||||
|  | 			id: parts[1] | ||||||
|  | 		}; | ||||||
|  | 
 | ||||||
|  | 	return { | ||||||
|  | 		id: undefined, | ||||||
|  | 		name: emoji | ||||||
|  | 	}; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | router.delete("/", async (req, res) => { | ||||||
|  | 	const { message_id, channel_id } = req.params; | ||||||
|  | 
 | ||||||
|  | 	const channel = await ChannelModel.findOne({ id: channel_id }, { guild_id: true }).exec(); | ||||||
|  | 	if (!channel) throw new HTTPError("Channel not found", 404); | ||||||
|  | 
 | ||||||
|  | 	const permissions = await getPermission(req.user_id, undefined, channel_id); | ||||||
|  | 	permissions.hasThrow("MANAGE_MESSAGES"); | ||||||
|  | 
 | ||||||
|  | 	const message = await MessageModel.findOneAndUpdate({ id: message_id, channel_id }, { reactions: [] }).exec(); | ||||||
|  | 	if (!message) throw new HTTPError("Message not found", 404); | ||||||
|  | 
 | ||||||
|  | 	await emitEvent({ | ||||||
|  | 		event: "MESSAGE_REACTION_REMOVE_ALL", | ||||||
|  | 		channel_id, | ||||||
|  | 		guild_id: channel.guild_id, | ||||||
|  | 		data: { | ||||||
|  | 			channel_id, | ||||||
|  | 			message_id, | ||||||
|  | 			guild_id: channel.guild_id | ||||||
|  | 		} | ||||||
|  | 	} as MessageReactionRemoveAllEvent); | ||||||
|  | 
 | ||||||
|  | 	res.sendStatus(204); | ||||||
|  | }); | ||||||
|  | 
 | ||||||
|  | router.delete("/:emoji", async (req, res) => { | ||||||
|  | 	const { message_id, channel_id } = req.params; | ||||||
|  | 	const emoji = getEmoji(req.params.emoji); | ||||||
|  | 
 | ||||||
|  | 	const channel = await ChannelModel.findOne({ id: channel_id }, { guild_id: true }).exec(); | ||||||
|  | 	if (!channel) throw new HTTPError("Channel not found", 404); | ||||||
|  | 
 | ||||||
|  | 	const permissions = await getPermission(req.user_id, undefined, channel_id); | ||||||
|  | 	permissions.hasThrow("MANAGE_MESSAGES"); | ||||||
|  | 
 | ||||||
|  | 	const message = await MessageModel.findOne({ id: message_id, channel_id }).exec(); | ||||||
|  | 	if (!message) throw new HTTPError("Message not found", 404); | ||||||
|  | 
 | ||||||
|  | 	const already_added = message.reactions.find((x) => (x.emoji.id === emoji.id && emoji.id) || x.emoji.name === emoji.name); | ||||||
|  | 	if (!already_added) throw new HTTPError("Reaction not found", 404); | ||||||
|  | 	message.reactions.remove(already_added); | ||||||
|  | 
 | ||||||
|  | 	await MessageModel.updateOne({ id: message_id, channel_id }, message).exec(); | ||||||
|  | 
 | ||||||
|  | 	await emitEvent({ | ||||||
|  | 		event: "MESSAGE_REACTION_REMOVE_EMOJI", | ||||||
|  | 		channel_id, | ||||||
|  | 		guild_id: channel.guild_id, | ||||||
|  | 		data: { | ||||||
|  | 			channel_id, | ||||||
|  | 			message_id, | ||||||
|  | 			guild_id: channel.guild_id, | ||||||
|  | 			emoji | ||||||
|  | 		} | ||||||
|  | 	} as MessageReactionRemoveEmojiEvent); | ||||||
|  | 
 | ||||||
|  | 	res.sendStatus(204); | ||||||
|  | }); | ||||||
|  | 
 | ||||||
|  | router.get("/:emoji", async (req, res) => { | ||||||
|  | 	const { message_id, channel_id } = req.params; | ||||||
|  | 	const emoji = getEmoji(req.params.emoji); | ||||||
|  | 
 | ||||||
|  | 	const message = await MessageModel.findOne({ id: message_id, channel_id }).exec(); | ||||||
|  | 	if (!message) throw new HTTPError("Message not found", 404); | ||||||
|  | 	const reaction = message.reactions.find((x) => (x.emoji.id === emoji.id && emoji.id) || x.emoji.name === emoji.name); | ||||||
|  | 	if (!reaction) throw new HTTPError("Reaction not found", 404); | ||||||
|  | 
 | ||||||
|  | 	const permissions = await getPermission(req.user_id, undefined, channel_id); | ||||||
|  | 	permissions.hasThrow("VIEW_CHANNEL"); | ||||||
|  | 
 | ||||||
|  | 	const users = await UserModel.find({ id: { $in: reaction.user_ids } }, PublicUserProjection).exec(); | ||||||
|  | 
 | ||||||
|  | 	res.json(toObject(users)); | ||||||
|  | }); | ||||||
|  | 
 | ||||||
|  | router.put("/:emoji/:user_id", async (req, res) => { | ||||||
|  | 	const { message_id, channel_id, user_id } = req.params; | ||||||
|  | 	if (user_id !== "@me") throw new HTTPError("Invalid user"); | ||||||
|  | 	const emoji = getEmoji(req.params.emoji); | ||||||
|  | 
 | ||||||
|  | 	const channel = await ChannelModel.findOne({ id: channel_id }, { guild_id: true }).exec(); | ||||||
|  | 	if (!channel) throw new HTTPError("Channel not found", 404); | ||||||
|  | 
 | ||||||
|  | 	const message = await MessageModel.findOne({ id: message_id, channel_id }).exec(); | ||||||
|  | 	if (!message) throw new HTTPError("Message not found", 404); | ||||||
|  | 	const already_added = message.reactions.find((x) => (x.emoji.id === emoji.id && emoji.id) || x.emoji.name === emoji.name); | ||||||
|  | 
 | ||||||
|  | 	const permissions = await getPermission(req.user_id, undefined, channel_id); | ||||||
|  | 	permissions.hasThrow("READ_MESSAGE_HISTORY"); | ||||||
|  | 	if (!already_added) permissions.hasThrow("ADD_REACTIONS"); | ||||||
|  | 
 | ||||||
|  | 	if (emoji.id) { | ||||||
|  | 		const external_emoji = await EmojiModel.findOne({ id: emoji.id }).exec(); | ||||||
|  | 		if (!external_emoji) throw new HTTPError("Emoji not found", 404); | ||||||
|  | 		if (!already_added) permissions.hasThrow("USE_EXTERNAL_EMOJIS"); | ||||||
|  | 		emoji.animated = external_emoji.animated; | ||||||
|  | 		emoji.name = external_emoji.name; | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	if (already_added) { | ||||||
|  | 		if (already_added.user_ids.includes(req.user_id)) return res.sendStatus(204); // Do not throw an error ¯\_(ツ)_/¯ as discord also doesn't throw any error
 | ||||||
|  | 		already_added.count++; | ||||||
|  | 	} else message.reactions.push({ count: 1, emoji, user_ids: [req.user_id] }); | ||||||
|  | 
 | ||||||
|  | 	await MessageModel.updateOne({ id: message_id, channel_id }, message).exec(); | ||||||
|  | 
 | ||||||
|  | 	const member = channel.guild_id && (await MemberModel.findOne({ id: req.user_id }).exec()); | ||||||
|  | 
 | ||||||
|  | 	await emitEvent({ | ||||||
|  | 		event: "MESSAGE_REACTION_ADD", | ||||||
|  | 		channel_id, | ||||||
|  | 		guild_id: channel.guild_id, | ||||||
|  | 		data: { | ||||||
|  | 			user_id: req.user_id, | ||||||
|  | 			channel_id, | ||||||
|  | 			message_id, | ||||||
|  | 			guild_id: channel.guild_id, | ||||||
|  | 			emoji, | ||||||
|  | 			member | ||||||
|  | 		} | ||||||
|  | 	} as MessageReactionAddEvent); | ||||||
|  | 
 | ||||||
|  | 	res.sendStatus(204); | ||||||
|  | }); | ||||||
|  | 
 | ||||||
|  | router.delete("/:emoji/:user_id", async (req, res) => { | ||||||
|  | 	var { message_id, channel_id, user_id } = req.params; | ||||||
|  | 
 | ||||||
|  | 	const emoji = getEmoji(req.params.emoji); | ||||||
|  | 
 | ||||||
|  | 	const channel = await ChannelModel.findOne({ id: channel_id }, { guild_id: true }).exec(); | ||||||
|  | 	if (!channel) throw new HTTPError("Channel not found", 404); | ||||||
|  | 
 | ||||||
|  | 	const message = await MessageModel.findOne({ id: message_id, channel_id }).exec(); | ||||||
|  | 	if (!message) throw new HTTPError("Message not found", 404); | ||||||
|  | 
 | ||||||
|  | 	const permissions = await getPermission(req.user_id, undefined, channel_id); | ||||||
|  | 
 | ||||||
|  | 	if (user_id === "@me") user_id = req.user_id; | ||||||
|  | 	else permissions.hasThrow("MANAGE_MESSAGES"); | ||||||
|  | 
 | ||||||
|  | 	const already_added = message.reactions.find((x) => (x.emoji.id === emoji.id && emoji.id) || x.emoji.name === emoji.name); | ||||||
|  | 	if (!already_added || !already_added.user_ids.includes(user_id)) throw new HTTPError("Reaction not found", 404); | ||||||
|  | 
 | ||||||
|  | 	already_added.count--; | ||||||
|  | 
 | ||||||
|  | 	if (already_added.count <= 0) message.reactions.remove(already_added); | ||||||
|  | 
 | ||||||
|  | 	await MessageModel.updateOne({ id: message_id, channel_id }, message).exec(); | ||||||
|  | 
 | ||||||
|  | 	await emitEvent({ | ||||||
|  | 		event: "MESSAGE_REACTION_REMOVE", | ||||||
|  | 		channel_id, | ||||||
|  | 		guild_id: channel.guild_id, | ||||||
|  | 		data: { | ||||||
|  | 			user_id: req.user_id, | ||||||
|  | 			channel_id, | ||||||
|  | 			message_id, | ||||||
|  | 			guild_id: channel.guild_id, | ||||||
|  | 			emoji | ||||||
|  | 		} | ||||||
|  | 	} as MessageReactionRemoveEvent); | ||||||
|  | 
 | ||||||
|  | 	res.sendStatus(204); | ||||||
|  | }); | ||||||
| 
 | 
 | ||||||
| export default router; | export default router; | ||||||
|  | |||||||
| @ -76,7 +76,18 @@ router.get("/", async (req, res) => { | |||||||
| 
 | 
 | ||||||
| 	const messages = await query.limit(limit).exec(); | 	const messages = await query.limit(limit).exec(); | ||||||
| 
 | 
 | ||||||
| 	return res.json(toObject(messages)); | 	return res.json( | ||||||
|  | 		toObject(messages).map((x) => { | ||||||
|  | 			(x.reactions || []).forEach((x) => { | ||||||
|  | 				// @ts-ignore
 | ||||||
|  | 				if ((x.user_ids || []).includes(req.user_id)) x.me = true; | ||||||
|  | 				// @ts-ignore
 | ||||||
|  | 				delete x.user_ids; | ||||||
|  | 			}); | ||||||
|  | 
 | ||||||
|  | 			return x; | ||||||
|  | 		}) | ||||||
|  | 	); | ||||||
| }); | }); | ||||||
| 
 | 
 | ||||||
| // TODO: config max upload size
 | // TODO: config max upload size
 | ||||||
|  | |||||||
| @ -13,6 +13,7 @@ import { HTTPError } from "lambert-server"; | |||||||
| import { ChannelModifySchema } from "../../../schema/Channel"; | import { ChannelModifySchema } from "../../../schema/Channel"; | ||||||
| import { emitEvent } from "../../../util/Event"; | import { emitEvent } from "../../../util/Event"; | ||||||
| import { check } from "../../../util/instanceOf"; | import { check } from "../../../util/instanceOf"; | ||||||
|  | import { createChannel } from "../../../util/Channel"; | ||||||
| const router = Router(); | const router = Router(); | ||||||
| 
 | 
 | ||||||
| router.get("/", async (req, res) => { | router.get("/", async (req, res) => { | ||||||
| @ -26,41 +27,7 @@ router.post("/", check(ChannelModifySchema), async (req, res) => { | |||||||
| 	const { guild_id } = req.params; | 	const { guild_id } = req.params; | ||||||
| 	const body = req.body as ChannelModifySchema; | 	const body = req.body as ChannelModifySchema; | ||||||
| 
 | 
 | ||||||
| 	if (!body.permission_overwrites) body.permission_overwrites = []; | 	const channel = await createChannel({ ...body, guild_id }, req.user_id); | ||||||
| 	if (!body.topic) body.topic = ""; |  | ||||||
| 	if (!body.rate_limit_per_user) body.rate_limit_per_user = 0; |  | ||||||
| 
 |  | ||||||
| 	switch (body.type) { |  | ||||||
| 		case ChannelType.DM: |  | ||||||
| 		case ChannelType.GROUP_DM: |  | ||||||
| 			throw new HTTPError("You can't create a dm channel in a guild"); |  | ||||||
| 		// TODO:
 |  | ||||||
| 		case ChannelType.GUILD_STORE: |  | ||||||
| 			throw new HTTPError("Not yet supported"); |  | ||||||
| 		case ChannelType.GUILD_NEWS: |  | ||||||
| 		// TODO: check if guild is community server
 |  | ||||||
| 	} |  | ||||||
| 
 |  | ||||||
| 	if (body.parent_id) { |  | ||||||
| 		const exists = await ChannelModel.findOne({ id: body.parent_id }, { guild_id: true }).exec(); |  | ||||||
| 		if (!exists) throw new HTTPError("Parent id channel doesn't exist", 400); |  | ||||||
| 		if (exists.guild_id !== guild_id) throw new HTTPError("The category channel needs to be in the guild"); |  | ||||||
| 	} |  | ||||||
| 
 |  | ||||||
| 	const guild = await GuildModel.findOne({ id: guild_id }, { id: true }).exec(); |  | ||||||
| 	if (!guild) throw new HTTPError("Guild not found", 404); |  | ||||||
| 
 |  | ||||||
| 	const channel = { |  | ||||||
| 		...body, |  | ||||||
| 		id: Snowflake.generate(), |  | ||||||
| 		created_at: new Date(), |  | ||||||
| 		guild_id, |  | ||||||
| 		recipients: null |  | ||||||
| 	}; |  | ||||||
| 
 |  | ||||||
| 	await new ChannelModel(channel).save(); |  | ||||||
| 
 |  | ||||||
| 	await emitEvent({ event: "CHANNEL_CREATE", data: channel, guild_id } as ChannelCreateEvent); |  | ||||||
| 
 | 
 | ||||||
| 	res.json(channel); | 	res.json(channel); | ||||||
| }); | }); | ||||||
|  | |||||||
| @ -54,15 +54,4 @@ router.patch("/", check(GuildUpdateSchema), async (req: Request, res: Response) | |||||||
| 	return res.send(data); | 	return res.send(data); | ||||||
| }); | }); | ||||||
| 
 | 
 | ||||||
| router.get("/vanity-url", async (req: Request, res: Response) => { |  | ||||||
| 	const { guild_id } = req.params; |  | ||||||
| 
 |  | ||||||
| 	const guild = await GuildModel.findOne({ id: guild_id }).exec(); |  | ||||||
| 	if (!guild) throw new HTTPError("Guild does not exist", 404); |  | ||||||
| 
 |  | ||||||
| 	if (!guild.vanity_url) throw new HTTPError("This guild has no vanity url", 204); |  | ||||||
| 
 |  | ||||||
| 	return res.json(guild.vanity_url); |  | ||||||
| }); |  | ||||||
| 
 |  | ||||||
| export default router; | export default router; | ||||||
|  | |||||||
| @ -1,114 +0,0 @@ | |||||||
| import { Request, Response, Router } from "express"; |  | ||||||
| import { |  | ||||||
| 	GuildModel, |  | ||||||
| 	MemberModel, |  | ||||||
| 	UserModel, |  | ||||||
| 	toObject, |  | ||||||
| 	GuildMemberAddEvent, |  | ||||||
| 	getPermission, |  | ||||||
| 	PermissionResolvable, |  | ||||||
| } from "@fosscord/server-util"; |  | ||||||
| import { HTTPError } from "lambert-server"; |  | ||||||
| import { instanceOf, Length, check } from "../../../util/instanceOf"; |  | ||||||
| import { PublicMemberProjection, addMember, removeMember, addRole, removeRole, changeNickname, isMember } from "../../../util/Member"; |  | ||||||
| import { emitEvent } from "../../../util/Event"; |  | ||||||
| import { MemberNickChangeSchema } from "../../../schema/Member"; |  | ||||||
| import { getPublicUser } from "../../../util/User"; |  | ||||||
| 
 |  | ||||||
| const router = Router(); |  | ||||||
| 
 |  | ||||||
| // TODO: not allowed for user -> only allowed for bots with privileged intents
 |  | ||||||
| // TODO: send over websocket
 |  | ||||||
| router.get("/", async (req: Request, res: Response) => { |  | ||||||
| 	const { guild_id } = req.params; |  | ||||||
| 	const guild = await GuildModel.findOne({ id: guild_id }).exec(); |  | ||||||
| 	if (!guild) throw new HTTPError("Guild not found", 404); |  | ||||||
| 	await isMember(req.user_id, guild_id); |  | ||||||
| 
 |  | ||||||
| 	try { |  | ||||||
| 		instanceOf({ $limit: new Length(Number, 1, 1000), $after: String }, req.query, { |  | ||||||
| 			path: "query", |  | ||||||
| 			req, |  | ||||||
| 			ref: { obj: null, key: "" }, |  | ||||||
| 		}); |  | ||||||
| 	} catch (error) { |  | ||||||
| 		return res.status(400).json({ code: 50035, message: "Invalid Query", success: false, errors: error }); |  | ||||||
| 	} |  | ||||||
| 
 |  | ||||||
| 	// @ts-ignore
 |  | ||||||
| 	if (!req.query.limit) req.query.limit = 1; |  | ||||||
| 	const { limit, after } = (<unknown>req.query) as { limit: number; after: string }; |  | ||||||
| 	const query = after ? { id: { $gt: after } } : {}; |  | ||||||
| 
 |  | ||||||
| 	var members = await MemberModel.find({ guild_id, ...query }, PublicMemberProjection) |  | ||||||
| 		.limit(limit) |  | ||||||
| 		.exec(); |  | ||||||
| 
 |  | ||||||
| 	return res.json(toObject(members)); |  | ||||||
| }); |  | ||||||
| 
 |  | ||||||
| router.get("/:member_id", async (req: Request, res: Response) => { |  | ||||||
| 	const { guild_id, member_id } = req.params; |  | ||||||
| 	await isMember(req.user_id, guild_id); |  | ||||||
| 
 |  | ||||||
| 	const member = await MemberModel.findOne({ id: member_id, guild_id }).exec(); |  | ||||||
| 	if (!member) throw new HTTPError("Member not found", 404); |  | ||||||
| 
 |  | ||||||
| 	return res.json(toObject(member)); |  | ||||||
| }); |  | ||||||
| 
 |  | ||||||
| router.put("/:member_id", async (req: Request, res: Response) => { |  | ||||||
| 	const { guild_id, member_id } = req.params; |  | ||||||
| 
 |  | ||||||
| 	throw new HTTPError("Maintenance: Currently you can't add a member", 403) |  | ||||||
| 	// TODO: only for oauth2 applications
 |  | ||||||
| 	await addMember(member_id, guild_id); |  | ||||||
| 	res.sendStatus(204) |  | ||||||
| }); |  | ||||||
| 
 |  | ||||||
| router.delete("/:member_id", async (req: Request, res: Response) => { |  | ||||||
| 	const { guild_id, member_id } = req.params; |  | ||||||
| 
 |  | ||||||
| 	const perms = await getPermission(req.user_id, guild_id); |  | ||||||
| 	perms.hasThrow("KICK_MEMBERS"); |  | ||||||
| 
 |  | ||||||
| 	await removeMember(member_id, guild_id); |  | ||||||
| 	res.sendStatus(204) |  | ||||||
| }); |  | ||||||
| 
 |  | ||||||
| router.delete("/:member_id/roles/:role_id", async (req: Request, res: Response) => { |  | ||||||
| 	const { guild_id, role_id, member_id } = req.params; |  | ||||||
| 
 |  | ||||||
| 	const perms = await getPermission(req.user_id, guild_id); |  | ||||||
| 	perms.hasThrow("MANAGE_ROLES"); |  | ||||||
| 
 |  | ||||||
| 	await removeRole(member_id, guild_id, role_id); |  | ||||||
| 	res.sendStatus(204); |  | ||||||
| }); |  | ||||||
| 
 |  | ||||||
| router.put("/:member_id/roles/:role_id", async (req: Request, res: Response) => { |  | ||||||
| 	const { guild_id, role_id, member_id } = req.params; |  | ||||||
| 
 |  | ||||||
| 	const perms = await getPermission(req.user_id, guild_id); |  | ||||||
| 	perms.hasThrow("MANAGE_ROLES"); |  | ||||||
| 
 |  | ||||||
| 	await addRole(member_id, guild_id, role_id); |  | ||||||
| 	res.sendStatus(204); |  | ||||||
| }); |  | ||||||
| 
 |  | ||||||
| router.patch("/:member_id/nick", check(MemberNickChangeSchema), async (req: Request, res: Response) => { |  | ||||||
| 	var { guild_id, member_id } = req.params; |  | ||||||
| 	var permissionString: PermissionResolvable = "MANAGE_NICKNAMES"; |  | ||||||
| 	if (member_id === "@me") { |  | ||||||
| 		member_id = req.user_id; |  | ||||||
| 		permissionString = "CHANGE_NICKNAME"; |  | ||||||
| 	} |  | ||||||
| 
 |  | ||||||
| 	const perms = await getPermission(req.user_id, guild_id); |  | ||||||
| 	perms.hasThrow(permissionString); |  | ||||||
| 
 |  | ||||||
| 	await changeNickname(member_id, guild_id, req.body.nickname); |  | ||||||
| 	res.status(204); |  | ||||||
| }); |  | ||||||
| 
 |  | ||||||
| export default router; |  | ||||||
							
								
								
									
										70
									
								
								src/routes/guilds/#guild_id/members/#member_id/index.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										70
									
								
								src/routes/guilds/#guild_id/members/#member_id/index.ts
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,70 @@ | |||||||
|  | import { Request, Response, Router } from "express"; | ||||||
|  | import { | ||||||
|  | 	GuildModel, | ||||||
|  | 	MemberModel, | ||||||
|  | 	UserModel, | ||||||
|  | 	toObject, | ||||||
|  | 	GuildMemberAddEvent, | ||||||
|  | 	getPermission, | ||||||
|  | 	PermissionResolvable, | ||||||
|  | 	RoleModel, | ||||||
|  | 	GuildMemberUpdateEvent | ||||||
|  | } from "@fosscord/server-util"; | ||||||
|  | import { HTTPError } from "lambert-server"; | ||||||
|  | import { addMember, isMember, removeMember } from "../../../../../util/Member"; | ||||||
|  | import { check } from "../../../../../util/instanceOf"; | ||||||
|  | import { MemberChangeSchema } from "../../../../../schema/Member"; | ||||||
|  | import { emitEvent } from "../../../../../util/Event"; | ||||||
|  | 
 | ||||||
|  | const router = Router(); | ||||||
|  | 
 | ||||||
|  | router.get("/", async (req: Request, res: Response) => { | ||||||
|  | 	const { guild_id, member_id } = req.params; | ||||||
|  | 	await isMember(req.user_id, guild_id); | ||||||
|  | 
 | ||||||
|  | 	const member = await MemberModel.findOne({ id: member_id, guild_id }).exec(); | ||||||
|  | 	if (!member) throw new HTTPError("Member not found", 404); | ||||||
|  | 
 | ||||||
|  | 	return res.json(toObject(member)); | ||||||
|  | }); | ||||||
|  | 
 | ||||||
|  | router.patch("/", check(MemberChangeSchema), async (req, res) => { | ||||||
|  | 	const { guild_id, member_id } = req.params; | ||||||
|  | 	const body = req.body as MemberChangeSchema; | ||||||
|  | 	if (body.roles) { | ||||||
|  | 		const roles = await RoleModel.find({ id: { $in: body.roles } }).exec(); | ||||||
|  | 		if (body.roles.length !== roles.length) throw new HTTPError("Roles not found", 404); | ||||||
|  | 		// TODO: check if user has permission to add role
 | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	const member = await MemberModel.findOneAndUpdate({ id: member_id, guild_id }, body).exec(); | ||||||
|  | 
 | ||||||
|  | 	await emitEvent({ | ||||||
|  | 		event: "GUILD_MEMBER_UPDATE", | ||||||
|  | 		guild_id, | ||||||
|  | 		data: toObject(member) | ||||||
|  | 	} as GuildMemberUpdateEvent); | ||||||
|  | 
 | ||||||
|  | 	res.json(toObject(member)); | ||||||
|  | }); | ||||||
|  | 
 | ||||||
|  | router.put("/", async (req: Request, res: Response) => { | ||||||
|  | 	const { guild_id, member_id } = req.params; | ||||||
|  | 
 | ||||||
|  | 	throw new HTTPError("Maintenance: Currently you can't add a member", 403); | ||||||
|  | 	// TODO: only for oauth2 applications
 | ||||||
|  | 	await addMember(member_id, guild_id); | ||||||
|  | 	res.sendStatus(204); | ||||||
|  | }); | ||||||
|  | 
 | ||||||
|  | router.delete("/", async (req: Request, res: Response) => { | ||||||
|  | 	const { guild_id, member_id } = req.params; | ||||||
|  | 
 | ||||||
|  | 	const perms = await getPermission(req.user_id, guild_id); | ||||||
|  | 	perms.hasThrow("KICK_MEMBERS"); | ||||||
|  | 
 | ||||||
|  | 	await removeMember(member_id, guild_id); | ||||||
|  | 	res.sendStatus(204); | ||||||
|  | }); | ||||||
|  | 
 | ||||||
|  | export default router; | ||||||
							
								
								
									
										24
									
								
								src/routes/guilds/#guild_id/members/#member_id/nick.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								src/routes/guilds/#guild_id/members/#member_id/nick.ts
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,24 @@ | |||||||
|  | import { getPermission, PermissionResolvable } from "@fosscord/server-util"; | ||||||
|  | import { Request, Response, Router } from "express"; | ||||||
|  | import { check } from "lambert-server"; | ||||||
|  | import { MemberNickChangeSchema } from "../../../../../schema/Member"; | ||||||
|  | import { changeNickname } from "../../../../../util/Member"; | ||||||
|  | 
 | ||||||
|  | const router = Router(); | ||||||
|  | 
 | ||||||
|  | router.patch("/", check(MemberNickChangeSchema), async (req: Request, res: Response) => { | ||||||
|  | 	var { guild_id, member_id } = req.params; | ||||||
|  | 	var permissionString: PermissionResolvable = "MANAGE_NICKNAMES"; | ||||||
|  | 	if (member_id === "@me") { | ||||||
|  | 		member_id = req.user_id; | ||||||
|  | 		permissionString = "CHANGE_NICKNAME"; | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	const perms = await getPermission(req.user_id, guild_id); | ||||||
|  | 	perms.hasThrow(permissionString); | ||||||
|  | 
 | ||||||
|  | 	await changeNickname(member_id, guild_id, req.body.nickname); | ||||||
|  | 	res.status(204); | ||||||
|  | }); | ||||||
|  | 
 | ||||||
|  | export default router; | ||||||
| @ -0,0 +1,27 @@ | |||||||
|  | import { getPermission } from "@fosscord/server-util"; | ||||||
|  | import { Request, Response, Router } from "express"; | ||||||
|  | import { addRole, removeRole } from "../../../../../../../util/Member"; | ||||||
|  | 
 | ||||||
|  | const router = Router(); | ||||||
|  | 
 | ||||||
|  | router.delete("/:member_id/roles/:role_id", async (req: Request, res: Response) => { | ||||||
|  | 	const { guild_id, role_id, member_id } = req.params; | ||||||
|  | 
 | ||||||
|  | 	const perms = await getPermission(req.user_id, guild_id); | ||||||
|  | 	perms.hasThrow("MANAGE_ROLES"); | ||||||
|  | 
 | ||||||
|  | 	await removeRole(member_id, guild_id, role_id); | ||||||
|  | 	res.sendStatus(204); | ||||||
|  | }); | ||||||
|  | 
 | ||||||
|  | router.put("/:member_id/roles/:role_id", async (req: Request, res: Response) => { | ||||||
|  | 	const { guild_id, role_id, member_id } = req.params; | ||||||
|  | 
 | ||||||
|  | 	const perms = await getPermission(req.user_id, guild_id); | ||||||
|  | 	perms.hasThrow("MANAGE_ROLES"); | ||||||
|  | 
 | ||||||
|  | 	await addRole(member_id, guild_id, role_id); | ||||||
|  | 	res.sendStatus(204); | ||||||
|  | }); | ||||||
|  | 
 | ||||||
|  | export default router; | ||||||
							
								
								
									
										39
									
								
								src/routes/guilds/#guild_id/members/index.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										39
									
								
								src/routes/guilds/#guild_id/members/index.ts
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,39 @@ | |||||||
|  | import { Request, Response, Router } from "express"; | ||||||
|  | import { GuildModel, MemberModel, toObject } from "@fosscord/server-util"; | ||||||
|  | import { HTTPError } from "lambert-server"; | ||||||
|  | import { instanceOf, Length } from "../../../../util/instanceOf"; | ||||||
|  | import { PublicMemberProjection, isMember } from "../../../../util/Member"; | ||||||
|  | 
 | ||||||
|  | const router = Router(); | ||||||
|  | 
 | ||||||
|  | // TODO: not allowed for user -> only allowed for bots with privileged intents
 | ||||||
|  | // TODO: send over websocket
 | ||||||
|  | router.get("/", async (req: Request, res: Response) => { | ||||||
|  | 	const { guild_id } = req.params; | ||||||
|  | 	const guild = await GuildModel.findOne({ id: guild_id }).exec(); | ||||||
|  | 	if (!guild) throw new HTTPError("Guild not found", 404); | ||||||
|  | 	await isMember(req.user_id, guild_id); | ||||||
|  | 
 | ||||||
|  | 	try { | ||||||
|  | 		instanceOf({ $limit: new Length(Number, 1, 1000), $after: String }, req.query, { | ||||||
|  | 			path: "query", | ||||||
|  | 			req, | ||||||
|  | 			ref: { obj: null, key: "" } | ||||||
|  | 		}); | ||||||
|  | 	} catch (error) { | ||||||
|  | 		return res.status(400).json({ code: 50035, message: "Invalid Query", success: false, errors: error }); | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	// @ts-ignore
 | ||||||
|  | 	if (!req.query.limit) req.query.limit = 1; | ||||||
|  | 	const { limit, after } = (<unknown>req.query) as { limit: number; after: string }; | ||||||
|  | 	const query = after ? { id: { $gt: after } } : {}; | ||||||
|  | 
 | ||||||
|  | 	var members = await MemberModel.find({ guild_id, ...query }, PublicMemberProjection) | ||||||
|  | 		.limit(limit) | ||||||
|  | 		.exec(); | ||||||
|  | 
 | ||||||
|  | 	return res.json(toObject(members)); | ||||||
|  | }); | ||||||
|  | 
 | ||||||
|  | export default router; | ||||||
| @ -1,27 +1,38 @@ | |||||||
| import { Request, Response, Router } from "express"; | import { Request, Response, Router } from "express"; | ||||||
| import { RoleModel, GuildModel, getPermission, toObject, UserModel, Snowflake, MemberModel } from "@fosscord/server-util"; | import { | ||||||
|  | 	RoleModel, | ||||||
|  | 	GuildModel, | ||||||
|  | 	getPermission, | ||||||
|  | 	toObject, | ||||||
|  | 	UserModel, | ||||||
|  | 	Snowflake, | ||||||
|  | 	MemberModel, | ||||||
|  | 	GuildRoleCreateEvent, | ||||||
|  | 	GuildRoleUpdateEvent, | ||||||
|  | 	GuildRoleDeleteEvent | ||||||
|  | } from "@fosscord/server-util"; | ||||||
| import { HTTPError } from "lambert-server"; | import { HTTPError } from "lambert-server"; | ||||||
| import { emitEvent } from "../../../util/Event"; | import { emitEvent } from "../../../util/Event"; | ||||||
| import { check } from "../../../util/instanceOf"; | import { check } from "../../../util/instanceOf"; | ||||||
| import { RoleCreateSchema, RoleModifySchema } from "../../../schema/Roles"; | import { RoleModifySchema } from "../../../schema/Roles"; | ||||||
| import { getPublicUser } from "../../../util/User"; | import { getPublicUser } from "../../../util/User"; | ||||||
|  | import { isMember } from "../../../util/Member"; | ||||||
| 
 | 
 | ||||||
| const router: Router = Router(); | const router: Router = Router(); | ||||||
| 
 | 
 | ||||||
| router.get("/", async (req: Request, res: Response) => { | router.get("/", async (req: Request, res: Response) => { | ||||||
| 	const guild_id = req.params.guild_id; | 	const guild_id = req.params.guild_id; | ||||||
| 
 | 
 | ||||||
| 	const guild = await GuildModel.exists({ id: guild_id }); | 	await isMember(req.user_id, guild_id); | ||||||
| 	if (!guild) throw new HTTPError("Guild not found", 404); | 
 | ||||||
|  | 	const roles = await RoleModel.find({ guild_id: guild_id }).exec(); | ||||||
| 
 | 
 | ||||||
| 	var roles = await RoleModel.find({ guild_id: guild_id }).exec(); |  | ||||||
| 	if (!roles) res.send("No roles"); |  | ||||||
| 	return res.json(toObject(roles)); | 	return res.json(toObject(roles)); | ||||||
| }); | }); | ||||||
| 
 | 
 | ||||||
| router.post("/", check(RoleCreateSchema), async (req: Request, res: Response) => { | router.post("/", check(RoleModifySchema), async (req: Request, res: Response) => { | ||||||
| 	const guild_id = req.params.guild_id; | 	const guild_id = req.params.guild_id; | ||||||
| 	const body = req.body as RoleCreateSchema; | 	const body = req.body as RoleModifySchema; | ||||||
| 
 | 
 | ||||||
| 	const guild = await GuildModel.findOne({ id: guild_id }, { id: true }).exec(); | 	const guild = await GuildModel.findOne({ id: guild_id }, { id: true }).exec(); | ||||||
| 	if (!guild) throw new HTTPError("Guild not found", 404); | 	if (!guild) throw new HTTPError("Guild not found", 404); | ||||||
| @ -30,22 +41,29 @@ router.post("/", check(RoleCreateSchema), async (req: Request, res: Response) => | |||||||
| 	if (!user) throw new HTTPError("User not found", 404); | 	if (!user) throw new HTTPError("User not found", 404); | ||||||
| 
 | 
 | ||||||
| 	const perms = await getPermission(req.user_id, guild_id); | 	const perms = await getPermission(req.user_id, guild_id); | ||||||
|  | 	perms.hasThrow("MANAGE_ROLES"); | ||||||
|  | 	if (!body.name) throw new HTTPError("You need to specify a name"); | ||||||
| 
 | 
 | ||||||
| 	if (!perms.has("MANAGE_ROLES")) throw new HTTPError("You missing the MANAGE_ROLES permission", 401); | 	const role = await new RoleModel({ | ||||||
| 
 |  | ||||||
| 	const role_id = Snowflake.generate(); |  | ||||||
| 
 |  | ||||||
| 	var role = { |  | ||||||
| 		...body, | 		...body, | ||||||
| 		id: role_id, | 		id: Snowflake.generate(), | ||||||
| 		guild_id: guild_id, | 		guild_id: guild_id, | ||||||
| 		managed: false, | 		managed: false, | ||||||
| 		position: 0, | 		position: 0, | ||||||
| 		tags: null | 		tags: null, | ||||||
| 	}; | 		permissions: body.permissions || 0n | ||||||
|  | 	}).save(); | ||||||
| 
 | 
 | ||||||
| 	const roleNew = await new RoleModel(role).save(); | 	await emitEvent({ | ||||||
| 	res.json(toObject(roleNew)).send(); | 		event: "GUILD_ROLE_CREATE", | ||||||
|  | 		guild_id, | ||||||
|  | 		data: { | ||||||
|  | 			guild_id, | ||||||
|  | 			role: toObject(role) | ||||||
|  | 		} | ||||||
|  | 	} as GuildRoleCreateEvent); | ||||||
|  | 
 | ||||||
|  | 	res.json(toObject(role)); | ||||||
| }); | }); | ||||||
| 
 | 
 | ||||||
| router.delete("/:role_id", async (req: Request, res: Response) => { | router.delete("/:role_id", async (req: Request, res: Response) => { | ||||||
| @ -68,7 +86,16 @@ router.delete("/:role_id", async (req: Request, res: Response) => { | |||||||
| 		guild_id: guild_id | 		guild_id: guild_id | ||||||
| 	}).exec(); | 	}).exec(); | ||||||
| 
 | 
 | ||||||
| 	res.send("Deleted"); | 	await emitEvent({ | ||||||
|  | 		event: "GUILD_ROLE_DELETE", | ||||||
|  | 		guild_id, | ||||||
|  | 		data: { | ||||||
|  | 			guild_id, | ||||||
|  | 			role_id | ||||||
|  | 		} | ||||||
|  | 	} as GuildRoleDeleteEvent); | ||||||
|  | 
 | ||||||
|  | 	res.sendStatus(204); | ||||||
| }); | }); | ||||||
| 
 | 
 | ||||||
| // TODO: check role hierarchy
 | // TODO: check role hierarchy
 | ||||||
| @ -76,6 +103,7 @@ router.delete("/:role_id", async (req: Request, res: Response) => { | |||||||
| router.patch("/:role_id", check(RoleModifySchema), async (req: Request, res: Response) => { | router.patch("/:role_id", check(RoleModifySchema), async (req: Request, res: Response) => { | ||||||
| 	const guild_id = req.params.guild_id; | 	const guild_id = req.params.guild_id; | ||||||
| 	const { role_id } = req.params; | 	const { role_id } = req.params; | ||||||
|  | 	const body = req.body as RoleModifySchema; | ||||||
| 
 | 
 | ||||||
| 	const guild = await GuildModel.findOne({ id: guild_id }, { id: true }).exec(); | 	const guild = await GuildModel.findOne({ id: guild_id }, { id: true }).exec(); | ||||||
| 	if (!guild) throw new HTTPError("Guild not found", 404); | 	if (!guild) throw new HTTPError("Guild not found", 404); | ||||||
| @ -92,9 +120,19 @@ router.patch("/:role_id", check(RoleModifySchema), async (req: Request, res: Res | |||||||
| 			id: role_id, | 			id: role_id, | ||||||
| 			guild_id: guild_id | 			guild_id: guild_id | ||||||
| 		}, | 		}, | ||||||
| 		...req.body | 		// @ts-ignore
 | ||||||
|  | 		body | ||||||
| 	).exec(); | 	).exec(); | ||||||
| 
 | 
 | ||||||
|  | 	await emitEvent({ | ||||||
|  | 		event: "GUILD_ROLE_UPDATE", | ||||||
|  | 		guild_id, | ||||||
|  | 		data: { | ||||||
|  | 			guild_id, | ||||||
|  | 			role | ||||||
|  | 		} | ||||||
|  | 	} as GuildRoleUpdateEvent); | ||||||
|  | 
 | ||||||
| 	res.json(toObject(role)); | 	res.json(toObject(role)); | ||||||
| }); | }); | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -6,6 +6,7 @@ import { GuildCreateSchema } from "../../schema/Guild"; | |||||||
| import Config from "../../util/Config"; | import Config from "../../util/Config"; | ||||||
| import { getPublicUser } from "../../util/User"; | import { getPublicUser } from "../../util/User"; | ||||||
| import { addMember } from "../../util/Member"; | import { addMember } from "../../util/Member"; | ||||||
|  | import { createChannel } from "../../util/Channel"; | ||||||
| 
 | 
 | ||||||
| const router: Router = Router(); | const router: Router = Router(); | ||||||
| 
 | 
 | ||||||
| @ -80,7 +81,8 @@ router.post("/", check(GuildCreateSchema), async (req: Request, res: Response) = | |||||||
| 		}).save() | 		}).save() | ||||||
| 	]); | 	]); | ||||||
| 
 | 
 | ||||||
| 	await addMember(req.user_id, guild_id, { guild: guild_doc }); | 	await createChannel({ name: "general", type: 0, guild_id, position: 0, permission_overwrites: [] }, req.user_id); | ||||||
|  | 	await addMember(req.user_id, guild_id); | ||||||
| 
 | 
 | ||||||
| 	res.status(201).json({ id: guild.id }); | 	res.status(201).json({ id: guild.id }); | ||||||
| }); | }); | ||||||
|  | |||||||
| @ -7,7 +7,7 @@ export const InviteCreateSchema = { | |||||||
| 	$temporary: Boolean, | 	$temporary: Boolean, | ||||||
| 	$unique: Boolean, | 	$unique: Boolean, | ||||||
| 	$target_user: String, | 	$target_user: String, | ||||||
| 	$target_user_type: Number, | 	$target_user_type: Number | ||||||
| }; | }; | ||||||
| export interface InviteCreateSchema { | export interface InviteCreateSchema { | ||||||
| 	target_user_id?: String; | 	target_user_id?: String; | ||||||
|  | |||||||
| @ -2,7 +2,7 @@ export const MemberCreateSchema = { | |||||||
| 	id: String, | 	id: String, | ||||||
| 	nick: String, | 	nick: String, | ||||||
| 	guild_id: String, | 	guild_id: String, | ||||||
| 	joined_at: Date, | 	joined_at: Date | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| export interface MemberCreateSchema { | export interface MemberCreateSchema { | ||||||
| @ -13,9 +13,17 @@ export interface MemberCreateSchema { | |||||||
| } | } | ||||||
| 
 | 
 | ||||||
| export const MemberNickChangeSchema = { | export const MemberNickChangeSchema = { | ||||||
| 	 nick: String, | 	nick: String | ||||||
| } | }; | ||||||
| 
 | 
 | ||||||
| export interface MemberNickChangeSchema { | export interface MemberNickChangeSchema { | ||||||
| 	 nick: string, | 	nick: string; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | export const MemberChangeSchema = { | ||||||
|  | 	$roles: [String] | ||||||
|  | }; | ||||||
|  | 
 | ||||||
|  | export interface MemberChangeSchema { | ||||||
|  | 	roles?: string[]; | ||||||
| } | } | ||||||
| @ -5,6 +5,7 @@ export const MessageCreateSchema = { | |||||||
| 	$content: new Length(String, 0, 2000), | 	$content: new Length(String, 0, 2000), | ||||||
| 	$nonce: String, | 	$nonce: String, | ||||||
| 	$tts: Boolean, | 	$tts: Boolean, | ||||||
|  | 	$flags: BigInt, | ||||||
| 	$embed: { | 	$embed: { | ||||||
| 		$title: new Length(String, 0, 256), //title of embed
 | 		$title: new Length(String, 0, 256), //title of embed
 | ||||||
| 		$type: String, // type of embed (always "rich" for webhook embeds)
 | 		$type: String, // type of embed (always "rich" for webhook embeds)
 | ||||||
| @ -15,48 +16,49 @@ export const MessageCreateSchema = { | |||||||
| 		$footer: { | 		$footer: { | ||||||
| 			text: new Length(String, 0, 2048), | 			text: new Length(String, 0, 2048), | ||||||
| 			icon_url: String, | 			icon_url: String, | ||||||
| 			proxy_icon_url: String, | 			proxy_icon_url: String | ||||||
| 		}, // footer object	footer information
 | 		}, // footer object	footer information
 | ||||||
| 		$image: EmbedImage, // image object	image information
 | 		$image: EmbedImage, // image object	image information
 | ||||||
| 		$thumbnail: EmbedImage, // thumbnail object	thumbnail information
 | 		$thumbnail: EmbedImage, // thumbnail object	thumbnail information
 | ||||||
| 		$video: EmbedImage, // video object	video information
 | 		$video: EmbedImage, // video object	video information
 | ||||||
| 		$provider: { | 		$provider: { | ||||||
| 			name: String, | 			name: String, | ||||||
| 			url: String, | 			url: String | ||||||
| 		}, // provider object	provider information
 | 		}, // provider object	provider information
 | ||||||
| 		$author: { | 		$author: { | ||||||
| 			name: new Length(String, 0, 256), | 			name: new Length(String, 0, 256), | ||||||
| 			url: String, | 			url: String, | ||||||
| 			icon_url: String, | 			icon_url: String, | ||||||
| 			proxy_icon_url: String, | 			proxy_icon_url: String | ||||||
| 		}, // author object	author information
 | 		}, // author object	author information
 | ||||||
| 		$fields: new Length( | 		$fields: new Length( | ||||||
| 			[ | 			[ | ||||||
| 				{ | 				{ | ||||||
| 					name: new Length(String, 0, 256), | 					name: new Length(String, 0, 256), | ||||||
| 					value: new Length(String, 0, 1024), | 					value: new Length(String, 0, 1024), | ||||||
| 					$inline: Boolean, | 					$inline: Boolean | ||||||
| 				}, | 				} | ||||||
| 			], | 			], | ||||||
| 			0, | 			0, | ||||||
| 			25 | 			25 | ||||||
| 		), | 		) | ||||||
| 	}, | 	}, | ||||||
| 	$allowed_mentions: [], | 	$allowed_mentions: [], | ||||||
| 	$message_reference: { | 	$message_reference: { | ||||||
| 		message_id: String, | 		message_id: String, | ||||||
| 		channel_id: String, | 		channel_id: String, | ||||||
| 		$guild_id: String, | 		$guild_id: String, | ||||||
| 		$fail_if_not_exists: Boolean, | 		$fail_if_not_exists: Boolean | ||||||
| 	}, | 	}, | ||||||
| 	$payload_json: String, | 	$payload_json: String, | ||||||
| 	$file: Object, | 	$file: Object | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| export interface MessageCreateSchema { | export interface MessageCreateSchema { | ||||||
| 	content?: string; | 	content?: string; | ||||||
| 	nonce?: string; | 	nonce?: string; | ||||||
| 	tts?: boolean; | 	tts?: boolean; | ||||||
|  | 	flags?: bigint; | ||||||
| 	embed?: Embed & { timestamp?: string }; | 	embed?: Embed & { timestamp?: string }; | ||||||
| 	allowed_mentions?: []; | 	allowed_mentions?: []; | ||||||
| 	message_reference?: { | 	message_reference?: { | ||||||
|  | |||||||
| @ -1,34 +1,17 @@ | |||||||
| export const RoleCreateSchema = { |  | ||||||
| 	name: String, |  | ||||||
| 	permissions: BigInt, |  | ||||||
| 	color: Number,  |  | ||||||
| 	hoist: Boolean, // whether the role should be displayed separately in the sidebar
 |  | ||||||
| 	mentionable: Boolean // whether the role should be mentionable
 |  | ||||||
| }; |  | ||||||
| 
 |  | ||||||
| export interface RoleCreateSchema { |  | ||||||
| 	name: string, |  | ||||||
| 	permissions: BigInt, |  | ||||||
| 	color: number, |  | ||||||
| 	hoist: boolean, // whether the role should be displayed separately in the sidebar
 |  | ||||||
| 	mentionable: boolean // whether the role should be mentionable
 |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| export const RoleModifySchema = { | export const RoleModifySchema = { | ||||||
| 	$name: String, | 	$name: String, | ||||||
| 	$permissions: BigInt, | 	$permissions: BigInt, | ||||||
| 	$color: Number, | 	$color: Number, | ||||||
| 	$hoist: Boolean, // whether the role should be displayed separately in the sidebar
 | 	$hoist: Boolean, // whether the role should be displayed separately in the sidebar
 | ||||||
| 	$mentionable: Boolean, // whether the role should be mentionable
 | 	$mentionable: Boolean, // whether the role should be mentionable
 | ||||||
| 	$position: Number, | 	$position: Number | ||||||
| 
 |  | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| export interface RoleModifySchema { | export interface RoleModifySchema { | ||||||
| 	name?: string, | 	name?: string; | ||||||
| 	permissions?: BigInt, | 	permissions?: BigInt; | ||||||
| 	color?: number, | 	color?: number; | ||||||
| 	hoist?: boolean, // whether the role should be displayed separately in the sidebar
 | 	hoist?: boolean; // whether the role should be displayed separately in the sidebar
 | ||||||
| 	mentionable?: boolean, // whether the role should be mentionable
 | 	mentionable?: boolean; // whether the role should be mentionable
 | ||||||
| 	position?: number, | 	position?: number; | ||||||
| } | } | ||||||
|  | |||||||
| @ -1,8 +1,3 @@ | |||||||
| import { getPermission } from "@fosscord/server-util"; | import { Snowflake } from "@fosscord/server-util"; | ||||||
| 
 | 
 | ||||||
| async function main() { | console.log(Snowflake.deconstruct("0")); | ||||||
| 	const t = await getPermission("811642917432066048", "812327318532915201"); |  | ||||||
| 	console.log(t); |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| main(); |  | ||||||
|  | |||||||
							
								
								
									
										54
									
								
								src/util/Channel.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										54
									
								
								src/util/Channel.ts
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,54 @@ | |||||||
|  | import { | ||||||
|  | 	ChannelCreateEvent, | ||||||
|  | 	ChannelModel, | ||||||
|  | 	ChannelType, | ||||||
|  | 	getPermission, | ||||||
|  | 	GuildModel, | ||||||
|  | 	Snowflake, | ||||||
|  | 	TextChannel, | ||||||
|  | 	VoiceChannel | ||||||
|  | } from "@fosscord/server-util"; | ||||||
|  | import { HTTPError } from "lambert-server"; | ||||||
|  | import { emitEvent } from "./Event"; | ||||||
|  | 
 | ||||||
|  | // TODO: DM channel
 | ||||||
|  | export async function createChannel(channel: Partial<TextChannel | VoiceChannel>, user_id: string = "0") { | ||||||
|  | 	if (!channel.permission_overwrites) channel.permission_overwrites = []; | ||||||
|  | 
 | ||||||
|  | 	switch (channel.type) { | ||||||
|  | 		case ChannelType.GUILD_TEXT: | ||||||
|  | 		case ChannelType.GUILD_VOICE: | ||||||
|  | 			break; | ||||||
|  | 		case ChannelType.DM: | ||||||
|  | 		case ChannelType.GROUP_DM: | ||||||
|  | 			throw new HTTPError("You can't create a dm channel in a guild"); | ||||||
|  | 		// TODO: check if guild is community server
 | ||||||
|  | 		case ChannelType.GUILD_STORE: | ||||||
|  | 		case ChannelType.GUILD_NEWS: | ||||||
|  | 		default: | ||||||
|  | 			throw new HTTPError("Not yet supported"); | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	const permissions = await getPermission(user_id, channel.guild_id); | ||||||
|  | 	permissions.hasThrow("MANAGE_CHANNELS"); | ||||||
|  | 
 | ||||||
|  | 	if (channel.parent_id) { | ||||||
|  | 		const exists = await ChannelModel.findOne({ id: channel.parent_id }, { guild_id: true }).exec(); | ||||||
|  | 		if (!exists) throw new HTTPError("Parent id channel doesn't exist", 400); | ||||||
|  | 		if (exists.guild_id !== channel.guild_id) throw new HTTPError("The category channel needs to be in the guild"); | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	// TODO: auto generate position
 | ||||||
|  | 
 | ||||||
|  | 	channel = await new ChannelModel({ | ||||||
|  | 		...channel, | ||||||
|  | 		id: Snowflake.generate(), | ||||||
|  | 		created_at: new Date(), | ||||||
|  | 		// @ts-ignore
 | ||||||
|  | 		recipients: null | ||||||
|  | 	}).save(); | ||||||
|  | 
 | ||||||
|  | 	await emitEvent({ event: "CHANNEL_CREATE", data: channel, guild_id: channel.guild_id } as ChannelCreateEvent); | ||||||
|  | 
 | ||||||
|  | 	return channel; | ||||||
|  | } | ||||||
| @ -9,7 +9,7 @@ import { HTTPError } from "lambert-server"; | |||||||
| import { emitEvent } from "./Event"; | import { emitEvent } from "./Event"; | ||||||
| // TODO: check webhook, application, system author
 | // TODO: check webhook, application, system author
 | ||||||
| 
 | 
 | ||||||
| export async function sendMessage(opts: Partial<Message>) { | export async function handleMessage(opts: Partial<Message>) { | ||||||
| 	const channel = await ChannelModel.findOne({ id: opts.channel_id }, { guild_id: true, type: true, permission_overwrites: true }).exec(); | 	const channel = await ChannelModel.findOne({ id: opts.channel_id }, { guild_id: true, type: true, permission_overwrites: true }).exec(); | ||||||
| 	if (!channel || !opts.channel_id) throw new HTTPError("Channel not found", 404); | 	if (!channel || !opts.channel_id) throw new HTTPError("Channel not found", 404); | ||||||
| 	// TODO: are tts messages allowed in dm channels? should permission be checked?
 | 	// TODO: are tts messages allowed in dm channels? should permission be checked?
 | ||||||
| @ -28,10 +28,8 @@ export async function sendMessage(opts: Partial<Message>) { | |||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	// TODO: check and put it all in the body
 | 	// TODO: check and put it all in the body
 | ||||||
| 	const message: Message = { | 	return { | ||||||
| 		...opts, | 		...opts, | ||||||
| 		id: Snowflake.generate(), |  | ||||||
| 		timestamp: new Date(), |  | ||||||
| 		guild_id: channel.guild_id, | 		guild_id: channel.guild_id, | ||||||
| 		channel_id: opts.channel_id, | 		channel_id: opts.channel_id, | ||||||
| 		// TODO: generate mentions and check permissions
 | 		// TODO: generate mentions and check permissions
 | ||||||
| @ -43,10 +41,14 @@ export async function sendMessage(opts: Partial<Message>) { | |||||||
| 		reactions: opts.reactions || [], | 		reactions: opts.reactions || [], | ||||||
| 		type: opts.type ?? 0 | 		type: opts.type ?? 0 | ||||||
| 	}; | 	}; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | export async function sendMessage(opts: Partial<Message>) { | ||||||
|  | 	const message = await handleMessage({ ...opts, id: Snowflake.generate(), timestamp: new Date() }); | ||||||
| 
 | 
 | ||||||
| 	const data = toObject(await new MessageModel(message).populate({ path: "member", select: PublicMemberProjection }).save()); | 	const data = toObject(await new MessageModel(message).populate({ path: "member", select: PublicMemberProjection }).save()); | ||||||
| 
 | 
 | ||||||
| 	await emitEvent({ event: "MESSAGE_CREATE", channel_id: opts.channel_id, data, guild_id: channel.guild_id } as MessageCreateEvent); | 	await emitEvent({ event: "MESSAGE_CREATE", channel_id: opts.channel_id, data, guild_id: message.guild_id } as MessageCreateEvent); | ||||||
| 
 | 
 | ||||||
| 	return data; | 	return data; | ||||||
| } | } | ||||||
|  | |||||||
| @ -5,7 +5,8 @@ import { Tuple } from "lambert-server"; | |||||||
| import "missing-native-js-functions"; | import "missing-native-js-functions"; | ||||||
| 
 | 
 | ||||||
| export const OPTIONAL_PREFIX = "$"; | export const OPTIONAL_PREFIX = "$"; | ||||||
| export const EMAIL_REGEX = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; | export const EMAIL_REGEX = | ||||||
|  | 	/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; | ||||||
| 
 | 
 | ||||||
| export function check(schema: any) { | export function check(schema: any) { | ||||||
| 	return (req: Request, res: Response, next: NextFunction) => { | 	return (req: Request, res: Response, next: NextFunction) => { | ||||||
| @ -27,9 +28,9 @@ export function FieldErrors(fields: Record<string, { code?: string; message: str | |||||||
| 			_errors: [ | 			_errors: [ | ||||||
| 				{ | 				{ | ||||||
| 					message, | 					message, | ||||||
| 					code: code || "BASE_TYPE_INVALID", | 					code: code || "BASE_TYPE_INVALID" | ||||||
| 				}, | 				} | ||||||
| 			], | 			] | ||||||
| 		})) | 		})) | ||||||
| 	); | 	); | ||||||
| } | } | ||||||
| @ -68,7 +69,7 @@ export function instanceOf( | |||||||
| 		optional = false, | 		optional = false, | ||||||
| 		errors = {}, | 		errors = {}, | ||||||
| 		req, | 		req, | ||||||
| 		ref, | 		ref | ||||||
| 	}: { path?: string; optional?: boolean; errors?: any; req: Request; ref?: { key: string | number; obj: any } } | 	}: { path?: string; optional?: boolean; errors?: any; req: Request; ref?: { key: string | number; obj: any } } | ||||||
| ): Boolean { | ): Boolean { | ||||||
| 	if (!ref) ref = { obj: null, key: "" }; | 	if (!ref) ref = { obj: null, key: "" }; | ||||||
| @ -131,7 +132,7 @@ export function instanceOf( | |||||||
| 								optional, | 								optional, | ||||||
| 								errors: errors[i], | 								errors: errors[i], | ||||||
| 								req, | 								req, | ||||||
| 								ref: { key: i, obj: value }, | 								ref: { key: i, obj: value } | ||||||
| 							}) === true | 							}) === true | ||||||
| 						) { | 						) { | ||||||
| 							delete errors[i]; | 							delete errors[i]; | ||||||
| @ -153,7 +154,7 @@ export function instanceOf( | |||||||
| 					throw new FieldError( | 					throw new FieldError( | ||||||
| 						"BASE_TYPE_BAD_LENGTH", | 						"BASE_TYPE_BAD_LENGTH", | ||||||
| 						req.t("common:field.BASE_TYPE_BAD_LENGTH", { | 						req.t("common:field.BASE_TYPE_BAD_LENGTH", { | ||||||
| 							length: `${type.min} - ${type.max}`, | 							length: `${type.min} - ${type.max}` | ||||||
| 						}) | 						}) | ||||||
| 					); | 					); | ||||||
| 				} | 				} | ||||||
| @ -185,7 +186,7 @@ export function instanceOf( | |||||||
| 							optional: OPTIONAL, | 							optional: OPTIONAL, | ||||||
| 							errors: errors[newKey], | 							errors: errors[newKey], | ||||||
| 							req, | 							req, | ||||||
| 							ref: { key: newKey, obj: value }, | 							ref: { key: newKey, obj: value } | ||||||
| 						}) === true | 						}) === true | ||||||
| 					) { | 					) { | ||||||
| 						delete errors[newKey]; | 						delete errors[newKey]; | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Bloom Sirenix
						Bloom Sirenix