Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Converted /src/user/invite.js from JS to TS. Closes #11 #81

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
161 changes: 73 additions & 88 deletions src/controllers/composer.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
"use strict";
// This is one of the two example TypeScript files included with the NodeBB repository
// It is meant to serve as an example to assist you with your HW1 translation
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Expand All @@ -21,92 +12,86 @@ const plugins_1 = __importDefault(require("../plugins"));
const topics_1 = __importDefault(require("../topics"));
const posts_1 = __importDefault(require("../posts"));
const helpers_1 = __importDefault(require("./helpers"));
function get(req, res, callback) {
return __awaiter(this, void 0, void 0, function* () {
res.locals.metaTags = Object.assign(Object.assign({}, res.locals.metaTags), { name: 'robots', content: 'noindex' });
const data = yield plugins_1.default.hooks.fire('filter:composer.build', {
req: req,
res: res,
next: callback,
templateData: {},
async function get(req, res, callback) {
res.locals.metaTags = Object.assign(Object.assign({}, res.locals.metaTags), { name: 'robots', content: 'noindex' });
const data = await plugins_1.default.hooks.fire('filter:composer.build', {
req: req,
res: res,
next: callback,
templateData: {},
});
if (res.headersSent) {
return;
}
if (!data || !data.templateData) {
return callback(new Error('[[error:invalid-data]]'));
}
if (data.templateData.disabled) {
res.render('', {
title: '[[modules:composer.compose]]',
});
if (res.headersSent) {
return;
}
else {
data.templateData.title = '[[modules:composer.compose]]';
res.render('compose', data.templateData);
}
}
exports.get = get;
async function post(req, res) {
const { body } = req;
const data = {
uid: req.uid,
req: req,
timestamp: Date.now(),
content: body.content,
fromQueue: false,
};
req.body.noscript = 'true';
if (!data.content) {
return await helpers_1.default.noScriptErrors(req, res, '[[error:invalid-data]]', 400);
}
async function queueOrPost(postFn, data) {
// The next line calls a function in a module that has not been updated to TS yet
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
const shouldQueue = await posts_1.default.shouldQueue(req.uid, data);
if (shouldQueue) {
delete data.req;
// The next line calls a function in a module that has not been updated to TS yet
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
return await posts_1.default.addToQueue(data);
}
if (!data || !data.templateData) {
return callback(new Error('[[error:invalid-data]]'));
return await postFn(data);
}
try {
let result;
if (body.tid) {
data.tid = body.tid;
result = await queueOrPost(topics_1.default.reply, data);
}
if (data.templateData.disabled) {
res.render('', {
title: '[[modules:composer.compose]]',
});
else if (body.cid) {
data.cid = body.cid;
data.title = body.title;
data.tags = [];
data.thumb = '';
result = await queueOrPost(topics_1.default.post, data);
}
else {
data.templateData.title = '[[modules:composer.compose]]';
res.render('compose', data.templateData);
throw new Error('[[error:invalid-data]]');
}
});
}
exports.get = get;
function post(req, res) {
return __awaiter(this, void 0, void 0, function* () {
const { body } = req;
const data = {
uid: req.uid,
req: req,
timestamp: Date.now(),
content: body.content,
fromQueue: false,
};
req.body.noscript = 'true';
if (!data.content) {
return yield helpers_1.default.noScriptErrors(req, res, '[[error:invalid-data]]', 400);
if (result.queued) {
return res.redirect(`${nconf_1.default.get('relative_path') || '/'}?noScriptMessage=[[success:post-queued]]`);
}
function queueOrPost(postFn, data) {
return __awaiter(this, void 0, void 0, function* () {
// The next line calls a function in a module that has not been updated to TS yet
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
const shouldQueue = yield posts_1.default.shouldQueue(req.uid, data);
if (shouldQueue) {
delete data.req;
// The next line calls a function in a module that has not been updated to TS yet
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
return yield posts_1.default.addToQueue(data);
}
return yield postFn(data);
});
const uid = result.uid ? result.uid : result.topicData.uid;
// The next line calls a function in a module that has not been updated to TS yet
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
user_1.default.updateOnlineUsers(uid);
const path = result.pid ? `/post/${result.pid}` : `/topic/${result.topicData.slug}`;
res.redirect(nconf_1.default.get('relative_path') + path);
}
catch (err) {
if (err instanceof Error) {
await helpers_1.default.noScriptErrors(req, res, err.message, 400);
}
try {
let result;
if (body.tid) {
data.tid = body.tid;
result = yield queueOrPost(topics_1.default.reply, data);
}
else if (body.cid) {
data.cid = body.cid;
data.title = body.title;
data.tags = [];
data.thumb = '';
result = yield queueOrPost(topics_1.default.post, data);
}
else {
throw new Error('[[error:invalid-data]]');
}
if (result.queued) {
return res.redirect(`${nconf_1.default.get('relative_path') || '/'}?noScriptMessage=[[success:post-queued]]`);
}
const uid = result.uid ? result.uid : result.topicData.uid;
// The next line calls a function in a module that has not been updated to TS yet
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
user_1.default.updateOnlineUsers(uid);
const path = result.pid ? `/post/${result.pid}` : `/topic/${result.topicData.slug}`;
res.redirect(nconf_1.default.get('relative_path') + path);
}
catch (err) {
if (err instanceof Error) {
yield helpers_1.default.noScriptErrors(req, res, err.message, 400);
}
}
});
}
}
exports.post = post;
95 changes: 40 additions & 55 deletions src/social.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
"use strict";
// This is one of the two example TypeScript files included with the NodeBB repository
// It is meant to serve as an example to assist you with your HW1 translation
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Expand All @@ -19,56 +10,50 @@ const lodash_1 = __importDefault(require("lodash"));
const plugins_1 = __importDefault(require("./plugins"));
const database_1 = __importDefault(require("./database"));
let postSharing = null;
function getPostSharing() {
return __awaiter(this, void 0, void 0, function* () {
if (postSharing) {
return lodash_1.default.cloneDeep(postSharing);
}
let networks = [
{
id: 'facebook',
name: 'Facebook',
class: 'fa-facebook',
activated: null,
},
{
id: 'twitter',
name: 'Twitter',
class: 'fa-twitter',
activated: null,
},
];
networks = (yield plugins_1.default.hooks.fire('filter:social.posts', networks));
// The next line calls a function in a module that has not been updated to TS yet
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
const activated = yield database_1.default.getSetMembers('social:posts.activated');
networks.forEach((network) => {
network.activated = activated.includes(network.id);
});
postSharing = networks;
return lodash_1.default.cloneDeep(networks);
async function getPostSharing() {
if (postSharing) {
return lodash_1.default.cloneDeep(postSharing);
}
let networks = [
{
id: 'facebook',
name: 'Facebook',
class: 'fa-facebook',
activated: null,
},
{
id: 'twitter',
name: 'Twitter',
class: 'fa-twitter',
activated: null,
},
];
networks = await plugins_1.default.hooks.fire('filter:social.posts', networks);
// The next line calls a function in a module that has not been updated to TS yet
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
const activated = await database_1.default.getSetMembers('social:posts.activated');
networks.forEach((network) => {
network.activated = activated.includes(network.id);
});
postSharing = networks;
return lodash_1.default.cloneDeep(networks);
}
exports.getPostSharing = getPostSharing;
function getActivePostSharing() {
return __awaiter(this, void 0, void 0, function* () {
const networks = yield getPostSharing();
return networks.filter(network => network && network.activated);
});
async function getActivePostSharing() {
const networks = await getPostSharing();
return networks.filter(network => network && network.activated);
}
exports.getActivePostSharing = getActivePostSharing;
function setActivePostSharingNetworks(networkIDs) {
return __awaiter(this, void 0, void 0, function* () {
postSharing = null;
// The next line calls a function in a module that has not been updated to TS yet
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
yield database_1.default.delete('social:posts.activated');
if (!networkIDs.length) {
return;
}
// The next line calls a function in a module that has not been updated to TS yet
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
yield database_1.default.setAdd('social:posts.activated', networkIDs);
});
async function setActivePostSharingNetworks(networkIDs) {
postSharing = null;
// The next line calls a function in a module that has not been updated to TS yet
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
await database_1.default.delete('social:posts.activated');
if (!networkIDs.length) {
return;
}
// The next line calls a function in a module that has not been updated to TS yet
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
await database_1.default.setAdd('social:posts.activated', networkIDs);
}
exports.setActivePostSharingNetworks = setActivePostSharingNetworks;
Loading