Erster Aufbau Forum

This commit is contained in:
Torsten Schulz
2024-10-15 16:28:42 +02:00
parent c31be3f879
commit 663564aa96
163 changed files with 9449 additions and 116 deletions

View File

@@ -22,6 +22,7 @@ import GuestbookEntry from '../models/community/guestbook.js';
import { JSDOM } from 'jsdom';
import DOMPurify from 'dompurify';
import sharp from 'sharp';
import Diary from '../models/community/diary.js';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
@@ -42,9 +43,7 @@ class SocialNetworkService extends BaseService {
async createFolder(hashedUserId, data, folderId) {
await this.checkUserAccess(hashedUserId);
const user = await User.findOne({
where: { hashedId: hashedUserId }
});
const user = await this.loadUserByHash(hashedUserId);
if (!user) {
throw new Error('User not found');
}
@@ -266,10 +265,12 @@ class SocialNetworkService extends BaseService {
return image;
}
async checkUserAccess(hashedId) {
const user = await User.findOne({ hashedId: hashedId });
if (!user || !user.active) throw new Error('Access denied: User not found or inactive');
return user.id;
async loadUserByHash(hashedId) {
return await User.findOne({ hashedId });
}
async loadUserByName(userName) {
return await User.findOne({ username: userName});
}
validateFolderData(data) {
@@ -354,9 +355,7 @@ class SocialNetworkService extends BaseService {
async constructUserProfile(user, hashedUserId) {
const userParams = {};
const requestingUser = await User.findOne({
where: { hashedId: hashedUserId },
});
const requestingUser = await this.loadUserByHash(hashedUserId);
if (!requestingUser) {
throw new Error('User not found');
}
@@ -461,7 +460,7 @@ class SocialNetworkService extends BaseService {
}
async getFoldersByUsername(username, hashedUserId) {
const user = await User.findOne({ where: { username } });
const user = await this.loadUserByName(username);
if (!user) {
throw new Error('User not found');
}
@@ -528,11 +527,11 @@ class SocialNetworkService extends BaseService {
}
async createGuestbookEntry(hashedSenderId, recipientName, htmlContent, image) {
const sender = await User.findOne({ where: { hashedId: hashedSenderId } });
const sender = await this.loadUserByHash(hashedSenderId);
if (!sender) {
throw new Error('Sender not found');
}
const recipient = await User.findOne({ where: { username: recipientName } });
const recipient = await this.loadUserByName(recipientName);
if (!recipient) {
throw new Error('Recipient not found');
}
@@ -594,7 +593,7 @@ class SocialNetworkService extends BaseService {
const pageSize = 20;
const offset = (page - 1) * pageSize;
this.checkUserAccess(hashedUserId);
const user = await User.findOne({ where: { username: username } });
const user = await this.loadUserByName(username);
if (!user) {
throw new Error('User not found');
}
@@ -624,9 +623,7 @@ class SocialNetworkService extends BaseService {
async getGuestbookImageFilePath(hashedUserId, guestbookOwnerName, entryId) {
await this.checkUserAccess(hashedUserId);
const guestbookOwner = await User.findOne({
where: { username: guestbookOwnerName },
});
const guestbookOwner = await this.loadUserByName(guestbookOwnerName);
if (!guestbookOwner) {
throw new Error('usernotfound');
}
@@ -649,7 +646,7 @@ class SocialNetworkService extends BaseService {
}
async deleteGuestbookEntry(hashedUserId, entryId) {
const user = await User.findOne({ where: { hashedId: hashedUserId } });
const user = await this.loadUserByHash(hashedUserId);
if (!user) {
throw new Error('User not found');
}
@@ -663,7 +660,8 @@ class SocialNetworkService extends BaseService {
await entry.destroy();
}
async createDiaryEntry(userId, text) {
async createDiaryEntry(hashedUserId, text) {
const userId = await this.checkUserAccess(hashedUserId);
const newEntry = await Diary.create({
userId: userId,
text: text,
@@ -673,29 +671,24 @@ class SocialNetworkService extends BaseService {
return newEntry;
}
async updateDiaryEntry(diaryId, userId, newText) {
async updateDiaryEntry(diaryEntryId, hashedUserId, newText) {
const userId = await this.checkUserAccess(hashedUserId);
const existingEntry = await Diary.findOne({
where: { id: diaryId, userId: userId }
where: { id: diaryEntryId, userId: userId }
});
if (!existingEntry) {
throw new Error('Diary entry not found or unauthorized access');
}
await DiaryHistory.create({
diaryId: existingEntry.id,
userId: existingEntry.userId,
oldText: existingEntry.text,
oldCreatedAt: existingEntry.createdAt,
oldUpdatedAt: existingEntry.updatedAt,
});
existingEntry.text = newText;
existingEntry.updatedAt = new Date();
await existingEntry.save();
return existingEntry;
}
async deleteDiaryEntry(diaryId, userId) {
async deleteDiaryEntry(diaryEntryId, hashedUserId) {
const userId = await this.checkUserAccess(hashedUserId);
const entryToDelete = await Diary.findOne({
where: { id: diaryId, userId: userId }
where: { id: diaryEntryId, userId: userId }
});
if (!entryToDelete) {
throw new Error('Diary entry not found or unauthorized access');
@@ -704,13 +697,15 @@ class SocialNetworkService extends BaseService {
return true;
}
async getDiaryEntries(userId) {
const entries = await Diary.findAll({
async getDiaryEntries(hashedUserId, page) {
const userId = await this.checkUserAccess(hashedUserId);
const entries = await Diary.findAndCountAll({
where: { userId: userId },
order: [['createdAt', 'DESC']]
order: [['createdAt', 'DESC']],
offset: (page - 1) * 20,
limit: 20,
});
return entries;
return { entries: entries.rows, totalPages: Math.ceil(entries.count / 20) };
}
}
export default SocialNetworkService;
export default SocialNetworkService;