aboutsummaryrefslogblamecommitdiff
path: root/lib/listenbrainz_util.ml
blob: f0b90dde1897dc73b7f8da4b27efd003a2fd4498 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

                   













                                                                        

                                                           
                                    



                                        


                                   

                                                              



                               

                                                      
open Listenbrainz_t

let uuid_v4_re =
  (* UUIDs are 8-4-4-4-12 hex digits.
     * The third block of version 4 UUIDS starts with a 4. *)
  let four_hex = "[0-9a-f][0-9a-f][0-9a-f][0-9a-f]"
  in Str.regexp_case_fold ("^" ^ four_hex ^ four_hex
                           ^ "-" ^ four_hex
                           ^ "-4[0-9a-f][0-9a-f][0-9a-f]"
                           ^ "-" ^ four_hex
                           ^ "-" ^ four_hex ^ four_hex ^ four_hex ^ "$")
let tm_unwrap tm =
  let (f, _) = Unix.mktime tm in f

let validate_mbid mbid = Str.string_match uuid_v4_re mbid 0

let validate_isrc isrc = Str.string_match uuid_v4_re isrc 0

let validate_post_submit_listens s =
  match s.listen_type with
  | `Single -> List.length s.payload = 1
  | _ -> true

let validate_get_validate_token r =
  r.code = 200 || r.code = 400

let validate_get_user_playing_now (r : get_user_playing_now) =
  match r.payload.listens with
  | [] -> true
  | [l] -> l.listened_at = None
  | _ -> false

let validate_get_user_listens (r : get_user_listens) =
  List.length r.payload.listens = r.payload.count