Implemented search function

- added graphql field search that searches for users/groups/events/posts
- marked searchUser as deprecated
pull/5/head
Trivernis 5 years ago
parent f103be95b3
commit 051f9206bb

@ -80,8 +80,9 @@ class App {
if (globals.config.server?.cors) { if (globals.config.server?.cors) {
this.app.use(cors()); this.app.use(cors());
} }
// handle authentification via bearer in the Authorization header // handle authentication via bearer in the Authorization header
this.app.use(async (req, res, next) => { this.app.use(async (req, res, next) => {
try {
if (!req.session.userId && req.headers.authorization) { if (!req.session.userId && req.headers.authorization) {
const bearer = req.headers.authorization.split("Bearer ")[1]; const bearer = req.headers.authorization.split("Bearer ")[1];
if (bearer) { if (bearer) {
@ -90,6 +91,10 @@ class App {
req.session.userId = user.id; req.session.userId = user.id;
} }
} }
} catch (err) {
logger.error(err.message);
logger.debug(err.stack);
}
next(); next();
}); });
this.app.use((req, res, next) => { this.app.use((req, res, next) => {

@ -9,6 +9,10 @@ import {InternalEvents} from "../lib/InternalEvents";
import * as models from "../lib/models"; import * as models from "../lib/models";
import {is} from "../lib/regex"; import {is} from "../lib/regex";
class Resolver {
}
/** /**
* Returns the resolvers for the graphql api. * Returns the resolvers for the graphql api.
* @param req - the request object * @param req - the request object
@ -16,8 +20,38 @@ import {is} from "../lib/regex";
*/ */
export function resolver(req: any, res: any): any { export function resolver(req: any, res: any): any {
return { return {
async search({first, offset, query}: { first: number, offset: number, query: string }) {
const limit = first;
const users = await models.User.findAll({
limit,
offset,
where: {
[Op.or]: [
{handle: {[Op.iRegexp]: query}},
{username: {[Op.iRegexp]: query}},
],
},
});
const groups = await models.Group.findAll({
limit,
offset,
where: {name: {[Op.iRegexp]: query}},
});
const posts = await models.Post.findAll({
limit,
offset,
where: {content: {[Op.iRegexp]: query}},
});
const events = await models.Event.findAll({
limit,
offset,
where: {name: {[Op.iRegexp]: query}},
});
return {users, posts, groups, events};
},
async findUser({first, offset, name, handle}: async findUser({first, offset, name, handle}:
{ first: number, offset: number, name: string, handle: string }) { { first: number, offset: number, name: string, handle: string }) {
res.status(status.MOVED_PERMANENTLY);
if (name) { if (name) {
return models.User.findAll({where: {username: {[Op.like]: `%${name}%`}}, offset, limit: first}); return models.User.findAll({where: {username: {[Op.like]: `%${name}%`}}, offset, limit: first});
} else if (handle) { } else if (handle) {
@ -392,7 +426,7 @@ export function resolver(req: any, res: any): any {
}, },
async createEvent({name, dueDate, groupId}: { name: string, dueDate: string, groupId: number }) { async createEvent({name, dueDate, groupId}: { name: string, dueDate: string, groupId: number }) {
if (req.session.userId) { if (req.session.userId) {
const date = new Date(dueDate); const date = new Date(Number(dueDate));
const group = await models.Group.findByPk(groupId); const group = await models.Group.findByPk(groupId);
return group.$create<models.Event>("rEvent", {name, dueDate: date}); return group.$create<models.Event>("rEvent", {name, dueDate: date});
} else { } else {

@ -17,9 +17,12 @@ type Query {
"returns the request object for its id" "returns the request object for its id"
getRequest(requestId: ID!): Request getRequest(requestId: ID!): Request
"find a user by user name or handle" "DEPRECATED! Find a user by user name or handle"
findUser(first: Int = 20, offset: Int = 0, name: String, handle: String): [User] findUser(first: Int = 20, offset: Int = 0, name: String, handle: String): [User]
"searches for users, groups, events, posts and returns a search result"
search(query: String!, first: Int = 20, offset: Int = 0): SearchResult!
"returns the post filtered by the sort type with pagination." "returns the post filtered by the sort type with pagination."
getPosts(first: Int=20, offset: Int=0, sort: SortType = NEW): [Post] getPosts(first: Int=20, offset: Int=0, sort: SortType = NEW): [Post]
@ -376,10 +379,28 @@ type Event {
"respresents an access token entry with the value as the acutal token and expires as the date the token expires." "respresents an access token entry with the value as the acutal token and expires as the date the token expires."
type Token { type Token {
"The token itself."
value: String! value: String!
"The timestamp when the token expires."
expires: String! expires: String!
} }
"The result of a search."
type SearchResult {
"The users that were found in the search."
users: [User!]!
"The posts that were found in the search."
posts: [Post!]!
"The groups that were found in the search."
groups: [Group!]!
"The events that were found in the search."
events: [Event!]!
}
"represents the type of vote performed on a post" "represents the type of vote performed on a post"
enum VoteType { enum VoteType {
UPVOTE UPVOTE

Loading…
Cancel
Save