Skip to content

Commit

Permalink
Merge pull request #1603 from serlo/ZixjK-kulla-2024-06-18-15-20
Browse files Browse the repository at this point in the history
fix(thread): Allow addind threads on user + taxonomyTerms
  • Loading branch information
AndreasHuber authored Jun 18, 2024
2 parents 7503d33 + b449d03 commit 38b9a90
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions packages/server/src/schema/thread/resolvers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as auth from '@serlo/authorization'
import * as t from 'io-ts'
import type { RowDataPacket } from 'mysql2/promise'

import {
Expand All @@ -22,6 +23,7 @@ import {
DiscriminatorType,
EntityDecoder,
NotificationEventType,
TaxonomyTermDecoder,
UserDecoder,
UuidDecoder,
} from '~/model/decoder'
Expand Down Expand Up @@ -210,14 +212,19 @@ export const resolvers: Resolvers = {
const { objectId, title, content, subscribe, sendEmail } = payload.input

const object = await UuidResolver.resolveWithDecoder(
EntityDecoder,
t.union([EntityDecoder, TaxonomyTermDecoder, UserDecoder]),
{ id: objectId },
context,
)

const scope =
'instance' in object
? auth.instanceToScope(object.instance)
: auth.Scope.Serlo

assertUserIsAuthenticated(userId)
await assertUserIsAuthorized({
guard: auth.Thread.createThread(auth.instanceToScope(object.instance)),
guard: auth.Thread.createThread(scope),
message: 'You are not allowed to create a thread on this object.',
context,
})
Expand All @@ -229,14 +236,15 @@ export const resolvers: Resolvers = {
"insert into uuid (discriminator) values ('comment')",
)

const instance = 'instance' in object ? object.instance : 'de'
await database.mutate(
`
insert into comment
(id, instance_id, author_id, uuid_id, title, content)
select ?, instance.id, ?, ?, ?, ?
from instance where instance.subdomain = ?
`,
[threadId, userId, objectId, title, content, object.instance],
[threadId, userId, objectId, title, content, instance],
)

await setSubscription(
Expand Down

0 comments on commit 38b9a90

Please sign in to comment.