Allow to configure avatar source

Evgeny PisemskyFri Aug 23 14:00:14+0300 2024

8f41327

Allow to configure avatar source

gitile/config.scm

11
;;;; Copyright (C) 2021 Julien Lepiller <julien@lepiller.eu>
2+
;;;; Copyright (C) 2024 Evgeny Pisemsky <mail@pisemsky.site>
23
;;;;
34
;;;; SPDX-License-Identifier: AGPL-3.0-or-later
45
;;;;

2829
            config-base-git-url
2930
            config-index-title
3031
            config-intro
31-
            config-footer))
32+
            config-footer
33+
            avatar-url-template
34+
            avatar-hash-algorithm
35+
            set-parameters-from-config!))
3236
3337
(define-record-type <config>
3438
  (make-config port host database repositories base-git-url index-title intro footer)

4145
  (index-title  config-index-title)
4246
  (intro        config-intro)
4347
  (footer       config-footer))
48+
49+
(define avatar-url-template
50+
  (make-parameter "https://avatar.lepiller.eu/cat-avatar-generator.php?seed=$hash"))
51+
52+
(define avatar-hash-algorithm
53+
  (make-parameter 'sha1))
54+
55+
(define (set-parameters-from-config! config)
56+
  (let ((params (config-database config)))
57+
    (if (list? params)
58+
        (begin
59+
          (avatar-url-template
60+
           (or (assoc-ref params 'avatar-url-template)
61+
               (avatar-url-template)))
62+
          (avatar-hash-algorithm
63+
           (or (assoc-ref params 'avatar-hash-algorithm)
64+
               (avatar-hash-algorithm)))))))

gitile/pages.scm

2323
  #:use-module (gcrypt hash)
2424
  #:use-module (gitile code)
2525
  #:use-module (gitile repo)
26+
  #:use-module (gitile config)
2627
  #:use-module (git)
2728
  #:use-module (git types)
2829
  #:use-module (ice-9 match)
30+
  #:use-module (ice-9 string-fun)
2931
  #:use-module (rnrs bytevectors)
3032
  #:use-module (srfi srfi-1); fold
3133
  #:use-module (srfi srfi-19); date/time

170172
    ,(project-files repository-name repo)))
171173
172174
(define (author-image author)
173-
  (string-append "https://avatar.lepiller.eu/cat-avatar-generator.php?seed="
174-
                 (bytevector->base16-string
175-
                   (bytevector-hash
176-
                     (string->utf8 (signature-email author))
177-
                     (hash-algorithm sha1)))))
175+
  (string-replace-substring
176+
   (avatar-url-template) "$hash"
177+
   (bytevector->base16-string
178+
    (bytevector-hash
179+
     (string->utf8 (signature-email author))
180+
     (lookup-hash-algorithm (avatar-hash-algorithm))))))
178181
179182
(define (time->date-string time)
180183
  (date->string

scripts/gitile.in

4242
                              intro footer)))))
4343
           (_ (format #t "Usage: ~a [-c config-file]~%" (car args))))))
4444
    (set-owner-validation! #f)
45+
    (set-parameters-from-config! config)
4546
    (run-server (gitile-handler config)
4647
                #:port (config-port config))))
4748