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,15 +80,20 @@ class App {
if (globals.config.server?.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) => {
if (!req.session.userId && req.headers.authorization) {
const bearer = req.headers.authorization.split("Bearer ")[1];
if (bearer) {
const user = await dataaccess.getUserByToken(bearer);
// @ts-ignore
req.session.userId = user.id;
try {
if (!req.session.userId && req.headers.authorization) {
const bearer = req.headers.authorization.split("Bearer ")[1];
if (bearer) {
const user = await dataaccess.getUserByToken(bearer);
// @ts-ignore
req.session.userId = user.id;
}
}
} catch (err) {
logger.error(err.message);
logger.debug(err.stack);
}
next();
});

@ -9,6 +9,10 @@ import {InternalEvents} from "../lib/InternalEvents";
import * as models from "../lib/models";
import {is} from "../lib/regex";
class Resolver {
}
/**
* Returns the resolvers for the graphql api.
* @param req - the request object
@ -16,8 +20,38 @@ import {is} from "../lib/regex";
*/
export function resolver(req: any, res: any): any {
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}:
{first: number, offset: number, name: string, handle: string}) {
{ first: number, offset: number, name: string, handle: string }) {
res.status(status.MOVED_PERMANENTLY);
if (name) {
return models.User.findAll({where: {username: {[Op.like]: `%${name}%`}}, offset, limit: first});
} else if (handle) {
@ -113,7 +147,7 @@ export function resolver(req: any, res: any): any {
return new NotLoggedInGqlError();
}
},
async getToken({email, passwordHash}: {email: string, passwordHash: string}) {
async getToken({email, passwordHash}: { email: string, passwordHash: string }) {
if (email && passwordHash) {
try {
const user = await dataaccess.getUserByLogin(email, passwordHash);
@ -151,7 +185,7 @@ export function resolver(req: any, res: any): any {
return new GraphQLError("No username, email or password given.");
}
},
async setUserSettings({settings}: {settings: string}) {
async setUserSettings({settings}: { settings: string }) {
if (req.session.userId) {
const user = await models.User.findByPk(req.session.userId);
try {
@ -392,7 +426,7 @@ export function resolver(req: any, res: any): any {
},
async createEvent({name, dueDate, groupId}: { name: string, dueDate: string, groupId: number }) {
if (req.session.userId) {
const date = new Date(dueDate);
const date = new Date(Number(dueDate));
const group = await models.Group.findByPk(groupId);
return group.$create<models.Event>("rEvent", {name, dueDate: date});
} else {

@ -17,9 +17,12 @@ type Query {
"returns the request object for its id"
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]
"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."
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."
type Token {
"The token itself."
value: String!
"The timestamp when the token expires."
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"
enum VoteType {
UPVOTE

Loading…
Cancel
Save