summaryrefslogtreecommitdiff
path: root/elm/Lib/Api.elm
blob: 4af28ea6b649277f9ca9cf4cdc6ec522daca06d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
module Lib.Api exposing (..)

import Json.Encode as JE
import File exposing (File)
import Http

import Gen.Api exposing (..)


-- Handy state enum for forms
type State
  = Normal
  | Loading
  | Error Response


-- User-friendly error message if the response isn't what the code expected.
-- (Technically a good chunk of this function could also be automatically
-- generated by Elm.pm, but that wouldn't really have all that much value).
showResponse : Response -> String
showResponse res =
  let unexp = "Unexpected response, please report a bug."
  in case res of
    HTTPError (Http.Timeout)        -> "Network timeout, please try again later."
    HTTPError (Http.NetworkError)   -> "Network error, please try again later."
    HTTPError (Http.BadStatus r)    -> "Server error " ++ String.fromInt r ++ ", please try again later or report an issue if this persists."
    HTTPError (Http.BadBody r)      -> "Invalid response from the server, please report a bug (debug info: " ++ r ++")."
    HTTPError (Http.BadUrl _)       -> unexp
    Success                         -> unexp
    Redirect _                      -> unexp
    CSRF                            -> "Invalid CSRF token, please refresh the page and try again."
    Invalid                         -> "Invalid form data, please report a bug."
    Editsum                         -> "Invalid edit summary."
    Unauth                          -> "You do not have the permission to perform this action."
    Unchanged                       -> "No changes"
    Content _                       -> unexp
    BadLogin                        -> "Invalid username or password."
    LoginThrottle                   -> "Action throttled, too many failed login attempts."
    InsecurePass                    -> "Your chosen password is in a database of leaked passwords, please choose another one."
    BadEmail                        -> "Unknown email address."
    Bot                             -> "Invalid answer to the anti-bot question."
    Taken                           -> "Username already taken, please choose a different name."
    DoubleEmail                     -> "Email address already used for another account."
    DoubleIP                        -> "You can only register one account from the same IP within 24 hours."
    BadCurPass                      -> "Current password is invalid."
    MailChange                      -> unexp
    ImgFormat                       -> "Unrecognized image format, only JPEG and PNG are accepted."
    Image _ _ _                     -> unexp
    Releases _                      -> unexp
    BoardResult _                   -> unexp
    TagResult _                     -> unexp
    TraitResult _                   -> unexp
    VNResult _                      -> unexp
    ProducerResult _                -> unexp
    CharResult _                    -> unexp
    ImageResult _                   -> unexp


expectResponse : (Response -> msg) -> Http.Expect msg
expectResponse msg =
  let
    res r = msg <| case r of
      Err e -> HTTPError e
      Ok v -> v
  in Http.expectJson res decode


-- Send a POST request to a Perl `elm_api` endpoint
-- Should not be used directly, use the `send` function in the appropriate Gen.FormName module instead.
post : String -> JE.Value -> (Response -> msg) -> Cmd msg
post name body msg =
  Http.post
    { url = "/elm/" ++ name ++ ".json"
    , body = Http.jsonBody body
    , expect = expectResponse msg
    }


type ImageType
  = Ch
  | Cv
  | Sf

postImage : ImageType -> File -> (Response -> msg) -> Cmd msg
postImage ty file msg =
  Http.post
    { url  = "/elm/ImageUpload.json"
    , body = Http.multipartBody
      [ Http.stringPart "type" <| case ty of
          Cv -> "cv"
          Sf -> "sf"
          Ch -> "ch"
      , Http.filePart "img" file
      ]
    , expect = expectResponse msg
    }