Skip to content

Commit

Permalink
Merge branch 'master' into azure
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelgira23 committed Sep 21, 2023
2 parents e0e136b + 5c246fe commit 0e444d2
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 22 deletions.
12 changes: 9 additions & 3 deletions src/libs/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { InputError, InternalError } from './errors';
import { NextFunction, Request, Response } from 'express';
import { TypeGuardError } from 'typescript-is';
import * as Sentry from '@sentry/node';
import config from './config';

declare global {
namespace Express {
Expand Down Expand Up @@ -81,12 +82,20 @@ function respondError(
action = null;
}

if (!config.production) {
console.error(error);
}

// Use proper status codes
if ([Action.LOGIN_EXPIRED, Action.UNAUTHORIZED, Action.NOT_LOGGED_IN].includes(action!)) {
res.status(401);
} else if (error instanceof TypeGuardError || error instanceof InputError) {
res.status(400);
} else {
if (!process.env.CI) {
Sentry.captureException(error);
}

if (error instanceof InternalError) {
err = error.message;
if (typeof error.source === 'object') {
Expand All @@ -96,9 +105,6 @@ function respondError(
}
}
res.status(500);
if (!process.env.CI) {
Sentry.captureException(error);
}
}

res.json({
Expand Down
22 changes: 3 additions & 19 deletions src/libs/feeds.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Db } from 'mongodb';
import { InternalError } from './errors';
import { recursivelyRemovePeriodKeys } from './utils';
import * as _ from 'lodash';
import * as canvas from './canvas';
import * as portal from './portal';
Expand Down Expand Up @@ -40,11 +41,8 @@ export async function updateCanvasCache(db: Db, user: string) {
ev.createdAt = creationDate;

// Iterate through all keys and remove any that contain periods
for (const key of Object.keys(ev)) {
if (key.includes('.')) {
delete ev[key];
}
}
// This is really a temporary fix and we should really address the wrong the ical parsing.
recursivelyRemovePeriodKeys(ev);
}

try {
Expand Down Expand Up @@ -101,13 +99,6 @@ export async function addPortalQueueClasses(db: Db, user: string) {

for (const ev of newEvents) {
ev.user = userDoc!._id;

// Iterate through all keys and remove any that contain periods
for (const key of Object.keys(ev)) {
if (key.includes('.')) {
delete ev[key];
}
}
}

try {
Expand Down Expand Up @@ -172,13 +163,6 @@ export async function addPortalQueueCalendar(db: Db, user: string) {

for (const ev of newEvents) {
ev.user = userDoc!._id;

// Iterate through all keys and remove any that contain periods
for (const key of Object.keys(ev)) {
if (key.includes('.')) {
delete ev[key];
}
}
}

try {
Expand Down
41 changes: 41 additions & 0 deletions src/libs/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { CanvasCacheEvent } from './canvas';

/**
* Pads a number to two digits with a leading zero.
* @param n An input number.
Expand Down Expand Up @@ -57,3 +59,42 @@ export function stringToColor(str: string) {
}
return colour;
}

/**
* Because our ical library parsees the description field incorrectly, we need to fix it.
* It currently will split the last colon in the description into the key, but we want the first colon.
* It changes something like:
* "description:This is a description: with a colon http": "//example.com"
* into
* "description": "This is a description: with a colon http://example.com"
*
* NOTE: This actually isn't being used anymore (in favor for the below function recursivelyRemovePeriodKeys())
* but I will keep in case we want to try doing things properly.
* Without using this function, the problematic description will just be deleted.
*/

export function icalDescriptionFix(event: CanvasCacheEvent): CanvasCacheEvent {
for (const key of Object.keys(event)) {
if (key.includes('.') && key.startsWith('description:')) {
const combinedLine = `${key}:${event[key] as string}`;
const parts = combinedLine.split(':');

const trueKey = parts.shift();
const trueValue = parts.join(':');

delete event[key as keyof CanvasCacheEvent];
event[trueKey as keyof CanvasCacheEvent] = trueValue;
}
}
return event;
}

export function recursivelyRemovePeriodKeys(obj: Record<string, any>) {
for (const key of Object.keys(obj)) {
if (key.includes('.')) {
delete obj[key];
} else if (typeof obj[key] === 'object') {
recursivelyRemovePeriodKeys(obj[key]);
}
}
}
18 changes: 18 additions & 0 deletions test/canvas.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { AliasType } from '@mymicds/sdk';
import { buildRequest, requireLoggedIn, validateParameters } from './helpers/shared';
import { deepStrictEqual } from 'assert';
import { expect, use } from 'chai';
import { generateJWT, saveTestUser, testUser } from './helpers/user';
import { icalDescriptionFix } from '../src/libs/utils';
import { initAPI } from '../src/init';
import { MongoMemoryServer } from 'mongodb-memory-server';
import { ObjectID } from 'mongodb';
Expand Down Expand Up @@ -224,6 +226,22 @@ describe('Canvas', () => {
requireLoggedIn();
});

describe('Canvas Fix Util', function () {
const before = {
'description:This is a. description: with a colon http': '//example.com'
};
const after = {
description: 'This is a. description: with a colon http://example.com'
};

const input = JSON.parse(JSON.stringify(before));
const ret = icalDescriptionFix(input);

// It both returns the new value and modifies in-place
deepStrictEqual(ret, after);
deepStrictEqual(input, after);
});

after(async function () {
await this.mongo.stop();
await calServer.stop();
Expand Down

0 comments on commit 0e444d2

Please sign in to comment.