<commit_before>{-# LANGUAGE OverloadedStrings #-}
module Controller.Excerpt
  ( postExcerpt
  , deleteExcerpt
  ) where

import Control.Monad (unless, when)
import Data.Maybe (isNothing)
import qualified Data.Text as T
import Network.HTTP.Types (conflict409)

import Has
import qualified JSON as JSON
import Model.Id
import Model.Permission
import Model.Release (EffectiveRelease(..))
import Model.Slot
import Model.Asset
import Model.AssetSegment
import Model.Excerpt
import Model.Notification.Types
import HTTP.Form.Deform
import HTTP.Path.Parser
import Action
import Controller.Permission
import Controller.Paths
import Controller.Form
import Controller.Notification
import Controller.AssetSegment

pathExcerpt :: PathParser (Id Slot, Id Asset)
pathExcerpt = pathJSON >/> pathSlotId </> pathId </< "excerpt"

postExcerpt :: ActionRoute (Id Slot, Id Asset)
postExcerpt = action POST pathExcerpt $ \(si, ai) -> withAuth $ do
  as <- getAssetSegment False PermissionEDIT False Nothing si ai
  e <- runForm Nothing $ do
    csrfForm
    Excerpt as <$> ("release" .:> deformNonEmpty deform)
  r <- changeExcerpt e
  unless r $ result $
    response conflict409 [] ("The requested excerpt overlaps an existing excerpt." :: T.Text)
  let notice t = createVolumeNotification (view e) $ \n -> (n t)
        { notificationContainerId = Just $ view e
        , notificationSegment = Just $ view e
        , notificationAssetId = Just $ view e
        , notificationRelease = excerptRelease e
        }
  when (isNothing $ assetExcerpt as) $
    notice NoticeExcerptVolume
  when (any ((effRelPublic . getAssetSegmentRelease2) as <) $ excerptRelease e) $
    notice NoticeReleaseExcerpt
  return $ okResponse [] $ JSON.pairs $ assetSegmentJSON (if r then as{ assetExcerpt = Just e } else as)
<commit_msg>Explicit type for post excerpt request<commit_after>{-# LANGUAGE OverloadedStrings #-}
module Controller.Excerpt
  ( postExcerpt
  , deleteExcerpt
  ) where

import Control.Monad (unless, when)
import Data.Maybe (isNothing)
import qualified Data.Text as T
import Network.HTTP.Types (conflict409)

import Has
import qualified JSON as JSON
import Model.Id
import Model.Permission
import Model.Release (EffectiveRelease(..))
import Model.Slot
import Model.Asset
import Model.AssetSegment
import Model.Excerpt
import Model.Notification.Types
import Model.Release.Types
import HTTP.Form.Deform
import HTTP.Path.Parser
import Action
import Controller.Permission
import Controller.Paths
import Controller.Form
import Controller.Notification
import Controller.AssetSegment

pathExcerpt :: PathParser (Id Slot, Id Asset)
pathExcerpt = pathJSON >/> pathSlotId </> pathId </< "excerpt"

data CreateOrUpdateExcerptRequest =
    CreateOrUpdateExcerptRequest (Maybe Release)

postExcerpt :: ActionRoute (Id Slot, Id Asset)
postExcerpt = action POST pathExcerpt $ \(si, ai) -> withAuth $ do
  as <- getAssetSegment False PermissionEDIT False Nothing si ai
  e <- runForm Nothing $ do
    csrfForm
    CreateOrUpdateExcerptRequest rel <- CreateOrUpdateExcerptRequest <$> ("release" .:> deformNonEmpty deform)
    pure (Excerpt as rel)
  r <- changeExcerpt e
  unless r $ result $
    response conflict409 [] ("The requested excerpt overlaps an existing excerpt." :: T.Text)
  let notice t = createVolumeNotification (view e) $ \n -> (n t)
        { notificationContainerId = Just $ view e
        , notificationSegment = Just $ view e
        , notificationAssetId = Just $ view e
        , notificationRelease = excerptRelease e
        }
  when (isNothing $ assetExcerpt as) $
    notice NoticeExcerptVolume
  when (any ((effRelPublic . getAssetSegmentRelease2) as <) $ excerptRelease e) $
    notice NoticeReleaseExcerpt
  return $ okResponse [] $ JSON.pairs $ assetSegmentJSON (if r then as{ assetExcerpt = Just e } else as)

{'fraction_non_alphanumeric': 0.08377100840336134, 'fraction_numerical': 0.003676470588235294, 'mean_word_length': 4.147297297297297, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3069383', 'n_tokens_mistral': 1131, 'n_tokens_neox': 1045, 'n_words': 365}