mirror of
https://github.com/aljazceru/landscape-template.git
synced 2026-01-26 09:44:21 +01:00
Merge branch 'dev' into feature/list-your-product-ui
This commit is contained in:
@@ -18,6 +18,7 @@ const { marked } = require('marked');
|
||||
const { resolveImgObjectToUrl } = require('../../../utils/resolveImageUrl');
|
||||
const { ImageInput } = require('./misc');
|
||||
const { deleteImage } = require('../../../services/imageUpload.service');
|
||||
const { logError } = require('../../../utils/logger');
|
||||
|
||||
|
||||
const POST_TYPE = enumType({
|
||||
@@ -435,97 +436,129 @@ const createStory = extendType({
|
||||
let coverImage = null
|
||||
let bodyImageIds = []
|
||||
|
||||
// Edit story
|
||||
if (id) {
|
||||
const oldPost = await prisma.story.findFirst({
|
||||
where: { id },
|
||||
select: {
|
||||
user_id: true,
|
||||
is_published: true,
|
||||
cover_image_id: true,
|
||||
body_image_ids: true
|
||||
}
|
||||
})
|
||||
was_published = oldPost.is_published;
|
||||
if (user.id !== oldPost.user_id) throw new ApolloError("Not post author")
|
||||
|
||||
// Body images
|
||||
bodyImageIds = await getHostedImageIdsFromBody(body, oldPost.body_image_ids)
|
||||
|
||||
// Old cover image is found
|
||||
if (oldPost.cover_image_id) {
|
||||
const oldCoverImage = await prisma.hostedImage.findFirst({
|
||||
where: {
|
||||
id: oldPost.cover_image_id
|
||||
try {
|
||||
if (id) {
|
||||
const oldPost = await prisma.story.findFirst({
|
||||
where: { id },
|
||||
select: {
|
||||
user_id: true,
|
||||
is_published: true,
|
||||
cover_image_id: true,
|
||||
body_image_ids: true
|
||||
}
|
||||
})
|
||||
was_published = oldPost.is_published;
|
||||
if (user.id !== oldPost.user_id) throw new ApolloError("Not post author")
|
||||
|
||||
// New cover image
|
||||
if (cover_image?.id && cover_image.id !== oldCoverImage?.provider_image_id) {
|
||||
await deleteImage(oldCoverImage.id)
|
||||
coverImage = await addCoverImage(cover_image.id)
|
||||
// Body images
|
||||
bodyImageIds = await getHostedImageIdsFromBody(body, oldPost.body_image_ids)
|
||||
|
||||
// Old cover image is found
|
||||
if (oldPost.cover_image_id) {
|
||||
const oldCoverImage = await prisma.hostedImage.findFirst({
|
||||
where: {
|
||||
id: oldPost.cover_image_id
|
||||
}
|
||||
})
|
||||
|
||||
// New cover image
|
||||
if (cover_image?.id && cover_image.id !== oldCoverImage?.provider_image_id) {
|
||||
await deleteImage(oldCoverImage.id)
|
||||
coverImage = await addCoverImage(cover_image.id)
|
||||
} else {
|
||||
coverImage = oldCoverImage
|
||||
}
|
||||
} else {
|
||||
coverImage = oldCoverImage
|
||||
// No old image found and new cover image
|
||||
if (cover_image?.id) {
|
||||
coverImage = await addCoverImage(cover_image.id)
|
||||
}
|
||||
}
|
||||
|
||||
// Remove unused body images
|
||||
const unusedImagesIds = oldPost.body_image_ids.filter(x => !bodyImageIds.includes(x));
|
||||
unusedImagesIds.map(async i => await deleteImage(i))
|
||||
|
||||
} else {
|
||||
// No old image found and new cover image
|
||||
// Body images
|
||||
bodyImageIds = await getHostedImageIdsFromBody(body)
|
||||
|
||||
// New story and new cover image
|
||||
if (cover_image?.id) {
|
||||
coverImage = await addCoverImage(cover_image.id)
|
||||
}
|
||||
}
|
||||
|
||||
// Remove unused body images
|
||||
const unusedImagesIds = oldPost.body_image_ids.filter(x => !bodyImageIds.includes(x));
|
||||
unusedImagesIds.map(async i => await deleteImage(i))
|
||||
|
||||
} else {
|
||||
// Body images
|
||||
bodyImageIds = await getHostedImageIdsFromBody(body)
|
||||
|
||||
// New story and new cover image
|
||||
if (cover_image?.id) {
|
||||
coverImage = await addCoverImage(cover_image.id)
|
||||
}
|
||||
}
|
||||
|
||||
// Preprocess & insert
|
||||
const htmlBody = marked.parse(body);
|
||||
const excerpt = htmlBody
|
||||
.replace(/<[^>]+>/g, '')
|
||||
.slice(0, 120)
|
||||
.replace(/&/g, "&")
|
||||
.replace(/'/g, "'")
|
||||
.replace(/"/g, '"')
|
||||
;
|
||||
// Preprocess & insert
|
||||
const htmlBody = marked.parse(body);
|
||||
const excerpt = htmlBody
|
||||
.replace(/<[^>]+>/g, '')
|
||||
.slice(0, 120)
|
||||
.replace(/&/g, "&")
|
||||
.replace(/'/g, "'")
|
||||
.replace(/"/g, '"')
|
||||
;
|
||||
|
||||
|
||||
const coverImageRel = coverImage ? {
|
||||
cover_image_rel: {
|
||||
connect:
|
||||
{
|
||||
id: coverImage ? coverImage.id : null
|
||||
const coverImageRel = coverImage ? {
|
||||
cover_image_rel: {
|
||||
connect:
|
||||
{
|
||||
id: coverImage ? coverImage.id : null
|
||||
}
|
||||
}
|
||||
} : {}
|
||||
|
||||
if (id) {
|
||||
await prisma.story.update({
|
||||
where: { id },
|
||||
data: {
|
||||
tags: {
|
||||
set: []
|
||||
},
|
||||
}
|
||||
});
|
||||
|
||||
return prisma.story.update({
|
||||
where: { id },
|
||||
data: {
|
||||
title,
|
||||
body,
|
||||
cover_image: '',
|
||||
excerpt,
|
||||
is_published: was_published || is_published,
|
||||
tags: {
|
||||
connectOrCreate:
|
||||
tags.map(tag => {
|
||||
tag = tag.toLowerCase().trim();
|
||||
return {
|
||||
where: {
|
||||
title: tag,
|
||||
},
|
||||
create: {
|
||||
title: tag
|
||||
}
|
||||
}
|
||||
})
|
||||
},
|
||||
body_image_ids: bodyImageIds,
|
||||
...coverImageRel
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
logError(error)
|
||||
throw new ApolloError("Unexpected error happened...")
|
||||
})
|
||||
}
|
||||
} : {}
|
||||
|
||||
if (id) {
|
||||
await prisma.story.update({
|
||||
where: { id },
|
||||
data: {
|
||||
tags: {
|
||||
set: []
|
||||
},
|
||||
}
|
||||
});
|
||||
|
||||
return prisma.story.update({
|
||||
where: { id },
|
||||
return prisma.story.create({
|
||||
data: {
|
||||
title,
|
||||
body,
|
||||
cover_image: '',
|
||||
excerpt,
|
||||
is_published: was_published || is_published,
|
||||
is_published,
|
||||
tags: {
|
||||
connectOrCreate:
|
||||
tags.map(tag => {
|
||||
@@ -540,42 +573,24 @@ const createStory = extendType({
|
||||
}
|
||||
})
|
||||
},
|
||||
user: {
|
||||
connect: {
|
||||
id: user.id,
|
||||
}
|
||||
},
|
||||
body_image_ids: bodyImageIds,
|
||||
...coverImageRel
|
||||
}
|
||||
}).catch(error => {
|
||||
logError(error)
|
||||
throw new ApolloError("Unexpected error happened...")
|
||||
})
|
||||
}
|
||||
|
||||
return await prisma.story.create({
|
||||
data: {
|
||||
title,
|
||||
body,
|
||||
cover_image: '',
|
||||
excerpt,
|
||||
is_published,
|
||||
tags: {
|
||||
connectOrCreate:
|
||||
tags.map(tag => {
|
||||
tag = tag.toLowerCase().trim();
|
||||
return {
|
||||
where: {
|
||||
title: tag,
|
||||
},
|
||||
create: {
|
||||
title: tag
|
||||
}
|
||||
}
|
||||
})
|
||||
},
|
||||
user: {
|
||||
connect: {
|
||||
id: user.id,
|
||||
}
|
||||
},
|
||||
body_image_ids: bodyImageIds,
|
||||
...coverImageRel
|
||||
}
|
||||
})
|
||||
|
||||
} catch (error) {
|
||||
logError(error)
|
||||
throw new ApolloError("Unexpected error happened...")
|
||||
}
|
||||
}
|
||||
})
|
||||
},
|
||||
|
||||
@@ -9,6 +9,7 @@ const jose = require('jose');
|
||||
const { JWT_SECRET } = require('../../utils/consts');
|
||||
const { generatePrivateKey, getPublicKey } = require('../../utils/nostr-tools');
|
||||
const { getUserByPubKey } = require('../../auth/utils/helperFuncs');
|
||||
const { logError } = require('../../utils/logger');
|
||||
|
||||
|
||||
|
||||
@@ -28,10 +29,18 @@ const loginHandler = async (req, res) => {
|
||||
|
||||
if (action === 'link' && user_token) {
|
||||
try {
|
||||
const { payload } = await jose.jwtVerify(user_token, Buffer.from(JWT_SECRET), {
|
||||
algorithms: ['HS256'],
|
||||
})
|
||||
const user_id = payload.user_id;
|
||||
|
||||
let user_id;
|
||||
|
||||
try {
|
||||
const { payload } = await jose.jwtVerify(user_token, Buffer.from(JWT_SECRET), {
|
||||
algorithms: ['HS256'],
|
||||
})
|
||||
user_id = payload.user_id;
|
||||
} catch (error) {
|
||||
return res.status(400).json({ status: 'ERROR', reason: "Invalid user_token" })
|
||||
}
|
||||
|
||||
const existingKeys = await prisma.userKey.findMany({ where: { user_id }, select: { key: true } });
|
||||
|
||||
if (existingKeys.length >= 3)
|
||||
@@ -55,7 +64,8 @@ const loginHandler = async (req, res) => {
|
||||
.json({ status: "OK" })
|
||||
|
||||
} catch (error) {
|
||||
return res.status(400).json({ status: 'ERROR', reason: 'Invalid User Token' })
|
||||
logError(error)
|
||||
return res.status(500).json({ status: 'ERROR', reason: 'Unexpected error happened' })
|
||||
}
|
||||
}
|
||||
|
||||
@@ -65,8 +75,7 @@ const loginHandler = async (req, res) => {
|
||||
const user = await getUserByPubKey(key)
|
||||
if (user === null) {
|
||||
|
||||
// Check if user had a previous account using this wallet
|
||||
|
||||
// Check if user had a previous account using this wallet
|
||||
const oldAccount = await prisma.user.findFirst({
|
||||
where: {
|
||||
pubKey: key
|
||||
@@ -136,7 +145,8 @@ const loginHandler = async (req, res) => {
|
||||
return res.status(200).json({ status: "OK" })
|
||||
|
||||
} catch (error) {
|
||||
return res.status(400).json({ status: 'ERROR', reason: 'Unexpected error happened, please try again' })
|
||||
logError(error)
|
||||
return res.status(500).json({ status: 'ERROR', reason: 'Unexpected error happened, please try again' })
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
9
api/utils/logger.js
Normal file
9
api/utils/logger.js
Normal file
@@ -0,0 +1,9 @@
|
||||
|
||||
function logError(error) {
|
||||
console.log("Unexpected Error: ");
|
||||
console.log(error);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
logError
|
||||
}
|
||||
Reference in New Issue
Block a user