Skip to content

Commit

Permalink
Push up knowledge of the author's name
Browse files Browse the repository at this point in the history
Refs #2014
  • Loading branch information
thewilkybarkid committed Oct 10, 2024
1 parent 04c79bd commit 972f052
Show file tree
Hide file tree
Showing 3 changed files with 169 additions and 178 deletions.
32 changes: 22 additions & 10 deletions src/Program.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { FetchHttpClient } from '@effect/platform'
import { type Array, Effect, flow, Layer, Match, pipe, PubSub, Runtime } from 'effect'
import type { ReadonlyNonEmptyArray } from 'fp-ts/lib/ReadonlyNonEmptyArray.js'
import * as TE from 'fp-ts/lib/TaskEither.js'
import { DeprecatedLoggerEnv, DeprecatedSleepEnv, EventStore, ExpressConfig } from './Context.js'
import { makeDeprecatedSleepEnv } from './DeprecatedServices.js'
import * as Feedback from './Feedback/index.js'
Expand Down Expand Up @@ -107,18 +106,31 @@ const publishFeedback = Layer.effect(
),
)

const name = yield* pipe(
Effect.promise(
getNameFromOrcid(feedback.authorId)({ orcidApiUrl, orcidApiToken, fetch, ...sleep, ...logger }),
),
Effect.andThen(
flow(
Match.value,
Match.when({ _tag: 'Left' }, () => Effect.fail(new Feedback.UnableToPublishFeedback({}))),
Match.when({ _tag: 'Right' }, response => Effect.succeed(response.right)),
Match.exhaustive,
),
),
Effect.filterOrElse(
value => value !== undefined,
() => Effect.fail(new Feedback.UnableToPublishFeedback({})),
),
)

return yield* pipe(
Effect.promise(
createFeedbackOnZenodo({ feedback, prereview })({
createFeedbackOnZenodo({
feedback: { ...feedback, author: { name, orcid: feedback.authorId } },
prereview,
})({
fetch,
getNameFromOrcid: orcid =>
pipe(
getNameFromOrcid(orcid)({ orcidApiUrl, orcidApiToken, fetch, ...sleep, ...logger }),
TE.filterOrElse(
name => typeof name === 'string',
() => 'unavailable',
),
),
publicUrl,
zenodoApiKey,
zenodoUrl,
Expand Down
47 changes: 16 additions & 31 deletions src/zenodo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import * as RTE from 'fp-ts/lib/ReaderTaskEither.js'
import * as RA from 'fp-ts/lib/ReadonlyArray.js'
import * as RNEA from 'fp-ts/lib/ReadonlyNonEmptyArray.js'
import type * as T from 'fp-ts/lib/Task.js'
import type * as TE from 'fp-ts/lib/TaskEither.js'
import { constVoid, flow, identity, pipe } from 'fp-ts/lib/function.js'
import { isString, toUpperCase } from 'fp-ts/lib/string.js'
import httpErrors, { type HttpError } from 'http-errors'
Expand Down Expand Up @@ -44,11 +43,10 @@ import {
updateDeposition,
uploadFile,
} from 'zenodo-ts'
import type * as Feedback from './Feedback/index.js'
import type { Prereview as PrereviewType } from './Prereview.js'
import { getClubByName, getClubName } from './club-details.js'
import { type SleepEnv, reloadCache, revalidateIfStale, timeoutRequest, useStaleCache } from './fetch.js'
import { plainText, sanitizeHtml } from './html.js'
import { type Html, plainText, sanitizeHtml } from './html.js'
import type { Prereview as PreprintPrereview } from './preprint-reviews-page/index.js'
import {
type GetPreprintEnv,
Expand Down Expand Up @@ -92,10 +90,6 @@ export interface IsReviewRequestedEnv {
isReviewRequested: (preprint: PreprintId) => T.Task<boolean>
}

export interface GetNameFromOrcidEnv {
getNameFromOrcid: (orcid: Orcid) => TE.TaskEither<'unavailable', NonEmptyString>
}

const wasPrereviewRemoved = (id: number): R.Reader<WasPrereviewRemovedEnv, boolean> =>
R.asks(({ wasPrereviewRemoved }) => wasPrereviewRemoved(id))

Expand All @@ -107,9 +101,6 @@ const getPreprintSubjects = (
const isReviewRequested = (preprint: PreprintId): RT.ReaderTask<IsReviewRequestedEnv, boolean> =>
R.asks(({ isReviewRequested }) => isReviewRequested(preprint))

const getNameFromOrcid = (orcid: Orcid): RTE.ReaderTaskEither<GetNameFromOrcidEnv, 'unavailable', NonEmptyString> =>
R.asks(({ getNameFromOrcid }) => getNameFromOrcid(orcid))

const getPrereviewsPageForSciety = flow(
(page: number) =>
new URLSearchParams({
Expand Down Expand Up @@ -491,27 +482,24 @@ export const addAuthorToRecordOnZenodo = (
RTE.bimap(() => 'unavailable', constVoid),
)

interface FeedbackToPublish {
author: { name: NonEmptyString; orcid?: Orcid }
authorId: Orcid
feedback: Html
prereviewId: number
}

export const createFeedbackOnZenodo: (params: {
feedback: Feedback.FeedbackBeingPublished
feedback: FeedbackToPublish
prereview: PrereviewType
}) => RTE.ReaderTaskEither<
GetNameFromOrcidEnv & PublicUrlEnv & ZenodoAuthenticatedEnv & L.LoggerEnv,
'unavailable',
[Doi, number]
> = ({ feedback, prereview }) =>
}) => RTE.ReaderTaskEither<PublicUrlEnv & ZenodoAuthenticatedEnv & L.LoggerEnv, 'unavailable', [Doi, number]> = ({
feedback,
prereview,
}) =>
pipe(
RTE.Do,
RTE.apS(
'creator',
pipe(
getNameFromOrcid(feedback.authorId),
RTE.map(name => ({ name, orcid: feedback.authorId })),
),
),
RTE.apSW('deposition', createEmptyDeposition()),
RTE.bindW('metadata', ({ creator }) =>
RTE.fromReader(createDepositMetadataForFeedback({ creator, feedback, prereview })),
),
RTE.apSW('metadata', RTE.fromReader(createDepositMetadataForFeedback({ feedback, prereview }))),
RTE.chainW(({ deposition, metadata }) => updateDeposition(metadata, deposition)),
RTE.chainFirstW(
uploadFile({
Expand All @@ -525,7 +513,6 @@ export const createFeedbackOnZenodo: (params: {
flow(
error => ({
error: match(error)
.with('unavailable', () => ({}))
.with(P.instanceOf(Error), error => error.message)
.with({ status: P.number }, response => `${response.status} ${response.statusText}`)
.with({ _tag: P.string }, D.draw)
Expand Down Expand Up @@ -588,12 +575,10 @@ export const createRecordOnZenodo: (
)

function createDepositMetadataForFeedback({
creator,
feedback,
prereview,
}: {
creator: { name: NonEmptyString; orcid?: Orcid }
feedback: Feedback.FeedbackBeingPublished
feedback: FeedbackToPublish
prereview: PrereviewType
}) {
return pipe(
Expand All @@ -604,7 +589,7 @@ function createDepositMetadataForFeedback({
upload_type: 'publication',
publication_type: 'other',
title: plainText`Feedback on a PREreview of “${prereview.preprint.title}”`.toString(),
creators: [creator],
creators: [feedback.author],
description: `<p><strong>This Zenodo record is a permanently preserved version of feedback on a PREreview. You can view the complete PREreview and feedback at <a href="${url.href}">${url.href}</a>.</strong></p>
${feedback.feedback.toString()}`,
Expand Down
Loading

0 comments on commit 972f052

Please sign in to comment.