require "uuid" require "uuid/json" class TodoD end class TodoD::List enum PermissionLevel None = -1 Read Post Edit Admin def to_json(o) to_s.downcase.to_json(o) end end JSON.mapping({ id: String, title: String, permissions: Hash(String, Array(Int32)), extra_properties: Hash(String, JSON::Any), tasks: { type: Array(String), default: [] of String } }) def initialize(@title, user, @extra_properties = Hash(String, JSON::Any).new) @id = UUID.random.to_s @permissions = { "admin" => [user.uid], "edit" => [] of Int32, "post" => [] of Int32, "read" => [] of Int32 } @tasks = [] of String end def users_with_read_permissions : Array(Int32) @permissions.map do |level, uids| # Would raise on invalid level. level = PermissionLevel.parse level uids end.flatten end def is_admin?(uid) @permissions["admin"].any? &.==(uid) end def is_editor?(uid) @permissions.any? do |level, uids| level = PermissionLevel.parse level uids.any? do |id| id == uid && level >= PermissionLevel::Edit end end end def is_reader?(uid) @permissions.any? do |level, uids| level = PermissionLevel.parse level uids.any? do |id| id == uid && level >= PermissionLevel::Read end end end def set_permission(uid : Int32, level : PermissionLevel) @permissions.each &.[1].select!(&.!=(uid)) return if level.none? @permissions[level.to_s].push uid end end class TodoD::Task # Should we hardcode more properties here? JSON.mapping({ id: String, list: String, title: String, description: { type: String, default: "" }, creator: Int32, assigned_to: Int32?, priority: String?, tags: { type: Array(String), default: [] of String }, extra_properties: { type: Hash(String, JSON::Any), default: {} of String => JSON::Any } }) def initialize(@list, @title, @creator, @description = "", @assigned_to = nil, @priority = nil, @tags = [] of String, @extra_properties = {} of String => JSON::Any) @id = UUID.random.to_s end end