Files
NodeBB/src/controllers/accounts/profile.js

155 lines
4.2 KiB
JavaScript
Raw Normal View History

2015-09-25 01:52:41 -04:00
'use strict';
const _ = require('lodash');
2015-09-25 01:52:41 -04:00
const db = require('../../database');
const user = require('../../user');
const posts = require('../../posts');
const categories = require('../../categories');
const plugins = require('../../plugins');
const privileges = require('../../privileges');
const helpers = require('../helpers');
const accountHelpers = require('./helpers');
const utils = require('../../utils');
2015-09-25 01:52:41 -04:00
const profileController = module.exports;
2015-09-25 01:52:41 -04:00
profileController.get = async function (req, res, next) {
const { userData } = res.locals;
if (!userData) {
return next();
}
2015-09-25 01:52:41 -04:00
await incrementProfileViews(req, userData);
2017-05-26 16:11:43 -04:00
const [latestPosts, bestPosts, customUserFields] = await Promise.all([
getLatestPosts(req.uid, userData),
getBestPosts(req.uid, userData),
accountHelpers.getCustomUserFields(userData),
posts.parseSignature(userData, req.uid),
]);
userData.customUserFields = customUserFields;
userData.posts = latestPosts; // for backwards compat.
userData.latestPosts = latestPosts;
userData.bestPosts = bestPosts;
userData.breadcrumbs = helpers.buildBreadcrumbs([{ text: userData.username }]);
userData.title = userData.username;
2015-10-29 15:11:52 -04:00
// Show email changed modal on first access after said change
userData.emailChanged = req.session.emailChanged;
delete req.session.emailChanged;
if (!userData.profileviews) {
userData.profileviews = 1;
}
2015-09-25 01:52:41 -04:00
addMetaTags(res, userData);
2020-07-24 10:39:51 -04:00
res.render('account/profile', userData);
2015-09-25 01:52:41 -04:00
};
async function incrementProfileViews(req, userData) {
if (req.uid >= 1) {
req.session.uids_viewed = req.session.uids_viewed || {};
2021-02-04 02:07:29 -07:00
if (
req.uid !== userData.uid &&
(!req.session.uids_viewed[userData.uid] || req.session.uids_viewed[userData.uid] < Date.now() - 3600000)
) {
await user.incrementUserFieldBy(userData.uid, 'profileviews', 1);
req.session.uids_viewed[userData.uid] = Date.now();
}
}
}
async function getLatestPosts(callerUid, userData) {
return await getPosts(callerUid, userData, 'pids');
}
async function getBestPosts(callerUid, userData) {
return await getPosts(callerUid, userData, 'pids:votes');
}
async function getPosts(callerUid, userData, setSuffix) {
const cids = await categories.getCidsByPrivilege('categories:cid', callerUid, 'topics:read');
2021-02-03 23:59:08 -07:00
const keys = cids.map(c => `cid:${c}:uid:${userData.uid}:${setSuffix}`);
2020-10-13 23:19:07 -04:00
let hasMorePosts = true;
let start = 0;
const count = 10;
const postData = [];
const [isAdmin, isModOfCids, canSchedule] = await Promise.all([
user.isAdministrator(callerUid),
user.isModerator(callerUid, cids),
privileges.categories.isUserAllowedTo('topics:schedule', cids, callerUid),
]);
Chat refactor (#11779) * first part of chat refactor remove per user chat zsets & store all mids in chat:room:<roomId>:mids reverse uids in getUidsInRoom * feat: create room button public groups wip * feat: public rooms create chats:room zset chat room deletion * join socket.io room * get rid of some calls that load all users in room * dont load all users when loadRoom is called * mange room users infinitescroll dont load all members in api call * IS for user list ability to change groups field for public rooms update groups field if group is renamed * test: test fixes * wip * keep 150 messages * fix extra awaits fix dupe code in chat toggleReadState * unread state for public rooms * feat: faster push unread * test: spec * change base to harmony * test: lint fixes * fix language of chat with message * add 2 methods for perf messaging.getTeasers and getUsers(roomIds) instead of loading one by one * refactor: cleaner conditional * test fix upgrade script fix save timestamp of room creation in room object * set progress.total * don't check for guests/spiders * public room unread fix * add public unread counts * mark read on send * ignore instead of throwing * doggy.gif * fix: restore delete * prevent entering chat rooms with meta.enter * fix self message causing mark unread * ability to sort public rooms * dont init sortable on mobile * move chat-loaded class to core * test: fix spec * add missing keys * use ajaxify * refactor: store some refs * fix: when user is deleted remove from public rooms as well * feat: change how unread count is calculated * get rid of cleaned content get rid of mid * add help text * test: fix tests, add back mid to prevent breaking change * ability to search members of chat rooms * remove * derp * perf: switch with partial data fix tests * more fixes if user leaves a group leave public rooms is he is no longer part of any of the groups that have access fix the cache key used to get all public room ids dont allow joining chat socket.io room if user is no longer part of group * fix: lint * fix: js error when trying to delete room after switching * add isRoomPublic
2023-07-12 13:03:54 -04:00
const isModOfCid = _.zipObject(cids, isModOfCids);
const cidToCanSchedule = _.zipObject(cids, canSchedule);
2020-10-13 23:19:07 -04:00
do {
/* eslint-disable no-await-in-loop */
let pids = await db.getSortedSetRevRange(keys, start, start + count - 1);
2020-10-13 23:19:07 -04:00
if (!pids.length || pids.length < count) {
hasMorePosts = false;
}
if (pids.length) {
({ pids } = await plugins.hooks.fire('filter:account.profile.getPids', {
uid: callerUid,
userData,
setSuffix,
pids,
}));
2020-10-13 23:19:07 -04:00
const p = await posts.getPostSummaryByPids(pids, callerUid, { stripTags: false });
2021-02-04 02:07:29 -07:00
postData.push(...p.filter(
Chat refactor (#11779) * first part of chat refactor remove per user chat zsets & store all mids in chat:room:<roomId>:mids reverse uids in getUidsInRoom * feat: create room button public groups wip * feat: public rooms create chats:room zset chat room deletion * join socket.io room * get rid of some calls that load all users in room * dont load all users when loadRoom is called * mange room users infinitescroll dont load all members in api call * IS for user list ability to change groups field for public rooms update groups field if group is renamed * test: test fixes * wip * keep 150 messages * fix extra awaits fix dupe code in chat toggleReadState * unread state for public rooms * feat: faster push unread * test: spec * change base to harmony * test: lint fixes * fix language of chat with message * add 2 methods for perf messaging.getTeasers and getUsers(roomIds) instead of loading one by one * refactor: cleaner conditional * test fix upgrade script fix save timestamp of room creation in room object * set progress.total * don't check for guests/spiders * public room unread fix * add public unread counts * mark read on send * ignore instead of throwing * doggy.gif * fix: restore delete * prevent entering chat rooms with meta.enter * fix self message causing mark unread * ability to sort public rooms * dont init sortable on mobile * move chat-loaded class to core * test: fix spec * add missing keys * use ajaxify * refactor: store some refs * fix: when user is deleted remove from public rooms as well * feat: change how unread count is calculated * get rid of cleaned content get rid of mid * add help text * test: fix tests, add back mid to prevent breaking change * ability to search members of chat rooms * remove * derp * perf: switch with partial data fix tests * more fixes if user leaves a group leave public rooms is he is no longer part of any of the groups that have access fix the cache key used to get all public room ids dont allow joining chat socket.io room if user is no longer part of group * fix: lint * fix: js error when trying to delete room after switching * add isRoomPublic
2023-07-12 13:03:54 -04:00
p => p && p.topic && (
isAdmin ||
isModOfCid[p.topic.cid] ||
(p.topic.scheduled && cidToCanSchedule[p.topic.cid]) ||
(!p.deleted && !p.topic.deleted)
)
2021-02-04 02:07:29 -07:00
));
2020-10-13 23:19:07 -04:00
}
start += count;
} while (postData.length < count && hasMorePosts);
return postData.slice(0, count);
}
function addMetaTags(res, userData) {
2021-02-04 00:06:15 -07:00
const plainAboutMe = userData.aboutme ? utils.stripHTMLTags(utils.decodeHTMLEntities(userData.aboutme)) : '';
res.locals.metaTags = [
{
name: 'title',
content: userData.fullname || userData.username,
noEscape: true,
},
{
name: 'description',
content: plainAboutMe,
},
{
property: 'og:title',
content: userData.fullname || userData.username,
noEscape: true,
},
{
property: 'og:description',
content: plainAboutMe,
},
];
if (userData.picture) {
res.locals.metaTags.push(
{
property: 'og:image',
content: userData.picture,
noEscape: true,
},
{
property: 'og:image:url',
content: userData.picture,
noEscape: true,
}
);
}
}