Source Code

;; adhan-alarm -- community adhan notification registry
(define-constant CONTRACT-OWNER tx-sender)
(define-constant ERR-AUTH (err u401))
(define-constant ERR-NONE (err u404))
(define-data-var alarm-count uint u0)
(define-map alarms uint { city: (string-utf8 100), muezzin: principal, method: (string-ascii 20), active: bool })
(define-map subscriptions { alarm-id: uint, user: principal } bool)

(define-public (create-alarm (city (string-utf8 100)) (method (string-ascii 20)))
  (let ((id (+ (var-get alarm-count) u1)))
    (var-set alarm-count id)
    (map-set alarms id { city: city, muezzin: tx-sender, method: method, active: true })
    (ok id)))

(define-public (subscribe (alarm-id uint))
  (begin (map-set subscriptions { alarm-id: alarm-id, user: tx-sender } true) (ok true)))

(define-public (unsubscribe (alarm-id uint))
  (begin (map-set subscriptions { alarm-id: alarm-id, user: tx-sender } false) (ok true)))

(define-public (deactivate (alarm-id uint))
  (let ((a (unwrap! (map-get? alarms alarm-id) ERR-NONE)))
    (asserts! (is-eq tx-sender (get muezzin a)) ERR-AUTH)
    (map-set alarms alarm-id (merge a { active: false })) (ok true)))

(define-read-only (get-alarm (id uint)) (map-get? alarms id))
(define-read-only (get-alarm-count) (var-get alarm-count))

Functions (6)

FunctionAccessArgs
create-alarmpubliccity: (string-utf8 100
subscribepublicalarm-id: uint
unsubscribepublicalarm-id: uint
deactivatepublicalarm-id: uint
get-alarmread-onlyid: uint
get-alarm-countread-only