This commit is contained in:
2025-09-17 17:15:34 +02:00
parent 6b4f015ac0
commit af64993c7e
8 changed files with 367 additions and 431 deletions

View File

@@ -18,33 +18,82 @@ pub enum AccountStatus {
pub struct Account {
db.Base
pub mut:
owner_id u32 //link to user
location_id u32 //link to location, 0 is none
owner_id u32 // link to user
location_id u32 // link to location, 0 is none
accountpolicies []AccountPolicy
assets []AccountAsset
assetid u32
last_activity u64
administrators []u32
assets []AccountAsset
assetid u32
last_activity u64
administrators []u32
}
// AccountPolicy represents a set of rules for an account
pub struct AccountPolicy {
pub mut:
policy_id u32 @[index]
admins []u32 //people who can transfer money out
min_signatures u8 //nr of people who need to sign
limits
whitelist_out []u32 //where money can go to
whitelist_in []u32 //where money can come from
lock_till u64 //date in epoch till no money can be transfered, only after
admin_lock_type LockType
admin_lock_till u64 //date in epoch when admin can unlock (0 means its free), this is unlock for changing this policy
admin_unlock []u32 //users who can unlock the admin policy
admin_unlock_min_signature u8 //nr of signatures from the adminunlock
policy_id u32 @[index]
admins []u32 // people who can transfer money out
min_signatures u8 // nr of people who need to sign
limits []AccountLimit
whitelist_out []u32 // where money can go to
whitelist_in []u32 // where money can come from
lock_till u64 // date in epoch till no money can be transfered, only after
admin_lock_type LockType
admin_lock_till u64 // date in epoch when admin can unlock (0 means its free), this is unlock for changing this policy
admin_unlock []u32 // users who can unlock the admin policy
admin_unlock_min_signature u8 // nr of signatures from the adminunlock
clawback_accounts []u32 // account(s) which can clawback
clawback_min_signatures u8
clawback_from u64 // from epoch money can be clawed back, 0 is always
clawback_till u64 // till which date
}
pub enum LockType{
pub fn (self AccountPolicy) dump(mut e encoder.Encoder) ! {
e.add_u32(self.policy_id)
e.add_list_u32(self.admins)
e.add_u8(self.min_signatures)
e.add_u32(u32(self.limits.len))
for limit in self.limits {
limit.dump(mut e)!
}
e.add_list_u32(self.whitelist_out)
e.add_list_u32(self.whitelist_in)
e.add_u64(self.lock_till)
e.add_u8(u8(self.admin_lock_type))
e.add_u64(self.admin_lock_till)
e.add_list_u32(self.admin_unlock)
e.add_u8(self.admin_unlock_min_signature)
e.add_list_u32(self.clawback_accounts)
e.add_u8(self.clawback_min_signatures)
e.add_u64(self.clawback_from)
e.add_u64(self.clawback_till)
}
fn (mut self AccountPolicy) load(mut e encoder.Decoder) ! {
self.policy_id = e.get_u32()!
self.admins = e.get_list_u32()!
self.min_signatures = e.get_u8()!
limits_len := e.get_u32()!
self.limits = []AccountLimit{cap: int(limits_len)}
for _ in 0 .. limits_len {
mut limit := AccountLimit{}
limit.load(mut e)!
self.limits << limit
}
self.whitelist_out = e.get_list_u32()!
self.whitelist_in = e.get_list_u32()!
self.lock_till = e.get_u64()!
self.admin_lock_type = unsafe { LockType(e.get_u8()!) }
self.admin_lock_till = e.get_u64()!
self.admin_unlock = e.get_list_u32()!
self.admin_unlock_min_signature = e.get_u8()!
self.clawback_accounts = e.get_list_u32()!
self.clawback_min_signatures = e.get_u8()!
self.clawback_from = e.get_u64()!
self.clawback_till = e.get_u64()!
}
pub enum LockType {
locked_till
locked
free
@@ -52,28 +101,48 @@ pub enum LockType{
pub struct AccountLimit {
pub mut:
amount f64
amount f64
asset_id u32
period AccountLimitPeriodLimit
period AccountLimitPeriodLimit
}
pub fn (self AccountLimit) dump(mut e encoder.Encoder) ! {
e.add_f64(self.amount)
e.add_u32(self.asset_id)
e.add_u8(u8(self.period))
}
pub enum AccountLimitPeriodLimit{
fn (mut self AccountLimit) load(mut e encoder.Decoder) ! {
self.amount = e.get_f64()!
self.asset_id = e.get_u32()!
self.period = unsafe { AccountLimitPeriodLimit(e.get_u8()!) }
}
pub enum AccountLimitPeriodLimit {
daily
weekly
monthly
}
pub struct AccountAsset {
db.Base
pub mut:
assetid u32
balance f64
metadata map[string]string
assetid u32
balance f64
metadata map[string]string
}
pub fn (self AccountAsset) dump(mut e encoder.Encoder) ! {
e.add_u32(self.assetid)
e.add_f64(self.balance)
e.add_map_string_string(self.metadata)
}
fn (mut self AccountAsset) load(mut e encoder.Decoder) ! {
self.assetid = e.get_u32()!
self.balance = e.get_f64()!
self.metadata = e.get_map_string_string()!
}
pub struct DBAccount {
pub mut:
@@ -102,7 +171,7 @@ pub fn (self Account) description(methodname string) string {
return 'List all accounts. Returns an array of account objects.'
}
else {
return 'Account management operations'
return 'This is generic method for the root object, TODO fill in, ...'
}
}
}
@@ -110,10 +179,10 @@ pub fn (self Account) description(methodname string) string {
pub fn (self Account) example(methodname string) (string, string) {
match methodname {
'set' {
return '{"account": {"owner_id": 1, "address": "addr123", "balance": 1000.0, "currency": "USD", "assetid": 1}}', '1'
return '{"account": {"owner_id": 1, "location_id": 1, "accountpolicies": [], "assets": [], "assetid": 1, "last_activity": 0, "administrators": []}}', '1'
}
'get' {
return '{"id": 1}', '{"owner_id": 1, "address": "addr123", "balance": 1000.0, "currency": "USD", "assetid": 1}'
return '{"id": 1}', '{"owner_id": 1, "location_id": 1, "accountpolicies": [], "assets": [], "assetid": 1, "last_activity": 0, "administrators": []}'
}
'delete' {
return '{"id": 1}', 'true'
@@ -122,7 +191,7 @@ pub fn (self Account) example(methodname string) (string, string) {
return '{"id": 1}', 'true'
}
'list' {
return '{}', '[{"owner_id": 1, "address": "addr123", "balance": 1000.0, "currency": "USD", "assetid": 1}]'
return '{}', '[{"owner_id": 1, "location_id": 1, "accountpolicies": [], "assets": [], "assetid": 1, "last_activity": 0, "administrators": []}]'
}
else {
return '{}', '{}'
@@ -132,51 +201,105 @@ pub fn (self Account) example(methodname string) (string, string) {
pub fn (self Account) dump(mut e encoder.Encoder) ! {
e.add_u32(self.owner_id)
e.add_string(self.address)
e.add_f64(self.balance)
e.add_string(self.currency)
e.add_u32(self.location_id)
e.add_u32(u32(self.accountpolicies.len))
for policy in self.accountpolicies {
policy.dump(mut e)!
}
e.add_u32(u32(self.assets.len))
for asset in self.assets {
asset.dump(mut e)!
}
e.add_u32(self.assetid)
e.add_u64(self.last_activity)
e.add_list_u32(self.administrators)
e.add_u32(self.accountpolicy)
}
fn (mut self DBAccount) load(mut o Account, mut e encoder.Decoder) ! {
o.owner_id = e.get_u32()!
o.address = e.get_string()!
o.balance = e.get_f64()!
o.currency = e.get_string()!
o.location_id = e.get_u32()!
policies_len := e.get_u32()!
o.accountpolicies = []AccountPolicy{cap: int(policies_len)}
for _ in 0 .. policies_len {
mut policy := AccountPolicy{}
policy.load(mut e)!
o.accountpolicies << policy
}
assets_len := e.get_u32()!
o.assets = []AccountAsset{cap: int(assets_len)}
for _ in 0 .. assets_len {
mut asset := AccountAsset{}
asset.load(mut e)!
o.assets << asset
}
o.assetid = e.get_u32()!
o.last_activity = e.get_u64()!
o.administrators = e.get_list_u32()!
o.accountpolicy = e.get_u32()!
}
@[params]
pub struct AccountPolicyArg {
pub mut:
policy_id u32
admins []u32
min_signatures u8
limits []AccountLimit
whitelist_out []u32
whitelist_in []u32
lock_till u64
admin_lock_type LockType
admin_lock_till u64
admin_unlock []u32
admin_unlock_min_signature u8
clawback_accounts []u32
clawback_min_signatures u8
clawback_from u64
clawback_to u64
}
pub struct AccountArg {
pub mut:
name string
description string
owner_id u32
address string
balance f64
currency string
assetid u32
last_activity u64
administrators []u32
accountpolicy u32
name string
description string
owner_id u32
location_id u32
accountpolicies []AccountPolicyArg
assets []AccountAsset
assetid u32
last_activity u64
administrators []u32
}
pub fn (mut self DBAccount) new(args AccountArg) !Account {
mut accountpolicies := []AccountPolicy{}
for policy_arg in args.accountpolicies {
accountpolicies << AccountPolicy{
policy_id: policy_arg.policy_id
admins: policy_arg.admins
min_signatures: policy_arg.min_signatures
limits: policy_arg.limits
whitelist_out: policy_arg.whitelist_out
whitelist_in: policy_arg.whitelist_in
lock_till: policy_arg.lock_till
admin_lock_type: policy_arg.admin_lock_type
admin_lock_till: policy_arg.admin_lock_till
admin_unlock: policy_arg.admin_unlock
admin_unlock_min_signature: policy_arg.admin_unlock_min_signature
clawback_accounts: policy_arg.clawback_accounts
clawback_min_signatures: policy_arg.clawback_min_signatures
clawback_from: policy_arg.clawback_from
clawback_till: policy_arg.clawback_to
}
}
mut o := Account{
owner_id: args.owner_id
address: args.address
balance: args.balance
currency: args.currency
assetid: args.assetid
last_activity: args.last_activity
administrators: args.administrators
accountpolicy: args.accountpolicy
owner_id: args.owner_id
location_id: args.location_id
accountpolicies: accountpolicies
assets: args.assets
assetid: args.assetid
last_activity: args.last_activity
administrators: args.administrators
}
o.name = args.name
@@ -207,4 +330,4 @@ pub fn (mut self DBAccount) get(id u32) !Account {
pub fn (mut self DBAccount) list() ![]Account {
return self.db.list[Account]()!.map(self.get(it)!)
}
}

View File

@@ -1,6 +0,0 @@
module models_ledger
import freeflowuniverse.herolib.data.encoder
import freeflowuniverse.herolib.data.ourtime
import freeflowuniverse.herolib.hero.db

View File

@@ -5,20 +5,20 @@ import freeflowuniverse.herolib.data.encoder
import freeflowuniverse.herolib.data.ourtime
import freeflowuniverse.herolib.hero.db
// Asset represents an asset in the financial system
// Asset represents a digital or physical item of value within the system.
@[heap]
pub struct Asset {
db.Base
pub mut:
address string @[index]
asset_type string
issuer u32 //link to a user
supply f64
decimals u8
is_frozen bool
metadata map[string]string
administrators []u32
min_signatures u32
address string @[required; index] // The unique address or identifier for the asset.
asset_type string @[required] // The type of the asset (e.g., 'token', 'nft').
issuer u32 @[required] // The user ID of the issuer of the asset.
supply f64 // The total supply of the asset.
decimals u8 // The number of decimal places for the asset's value.
is_frozen bool // Indicates if the asset is currently frozen and cannot be transferred.
metadata map[string]string // A map for storing arbitrary metadata as key-value pairs.
administrators []u32 // A list of user IDs that are administrators for this asset.
min_signatures u32 // The minimum number of signatures required for administrative actions.
}
pub struct DBAsset {
@@ -31,54 +31,33 @@ pub fn (self Asset) type_name() string {
}
pub fn (self Asset) description(methodname string) string {
match methodname {
'set' {
return 'Create or update an asset. Returns the ID of the asset.'
}
'get' {
return 'Retrieve an asset by ID. Returns the asset object.'
}
'delete' {
return 'Delete an asset by ID. Returns true if successful.'
}
'exist' {
return 'Check if an asset exists by ID. Returns true or false.'
}
'list' {
return 'List all assets. Returns an array of asset objects.'
}
else {
return 'Asset management operations'
}
return match methodname {
'set' { 'Create or update an asset. Returns the ID of the asset.' }
'get' { 'Retrieve an asset by its unique ID.' }
'delete' { 'Deletes an asset by its unique ID.' }
'exist' { 'Checks if an asset with the given ID exists.' }
'find' { 'Finds assets based on a filter expression.' }
'count' { 'Counts the number of assets that match a filter expression.' }
'list' { 'Lists all assets, optionally filtered and sorted.' }
else { 'An asset represents a digital or physical item of value.' }
}
}
pub fn (self Asset) example(methodname string) (string, string) {
match methodname {
'set' {
return '{"asset": {"address": "asset123", "assetid": 1, "asset_type": "token", "issuer": 1, "supply": 1000000.0, "decimals": 8}}', '1'
}
'get' {
return '{"id": 1}', '{"address": "asset123", "assetid": 1, "asset_type": "token", "issuer": 1, "supply": 1000000.0, "decimals": 8}'
}
'delete' {
return '{"id": 1}', 'true'
}
'exist' {
return '{"id": 1}', 'true'
}
'list' {
return '{}', '[{"address": "asset123", "assetid": 1, "asset_type": "token", "issuer": 1, "supply": 1000000.0, "decimals": 8}]'
}
else {
return '{}', '{}'
}
return match methodname {
'set' { '{"asset": {"id": 1, "address": "G...", "asset_type": "token", "issuer": 1, "supply": 1000000.0, "decimals": 8}}', '1' }
'get' { '{"id": 1}', '{"id": 1, "address": "G...", "asset_type": "token", "issuer": 1, "supply": 1000000.0, "decimals": 8}' }
'delete' { '{"id": 1}', 'true' }
'exist' { '{"id": 1}', 'true' }
'find' { '{"filter": "address=\'G...\'"}', '[{"id": 1, "address": "G...", "asset_type": "token", "issuer": 1, "supply": 1000000.0, "decimals": 8}]' }
'count' { '{"filter": "address=\'G...\'"}', '1' }
'list' { '{}', '[{"id": 1, "address": "G...", "asset_type": "token", "issuer": 1, "supply": 1000000.0, "decimals": 8}]' }
else { '{}', '{}' }
}
}
pub fn (self Asset) dump(mut e encoder.Encoder) ! {
e.add_string(self.address)
e.add_u32(self.assetid)
e.add_string(self.asset_type)
e.add_u32(self.issuer)
e.add_f64(self.supply)
@@ -98,7 +77,6 @@ pub fn (self Asset) dump(mut e encoder.Encoder) ! {
fn (mut self DBAsset) load(mut o Asset, mut e encoder.Decoder) ! {
o.address = e.get_string()!
o.assetid = e.get_u32()!
o.asset_type = e.get_string()!
o.issuer = e.get_u32()!
o.supply = e.get_f64()!
@@ -124,7 +102,6 @@ pub mut:
name string
description string
address string
assetid u32
asset_type string
issuer u32
supply f64
@@ -138,7 +115,6 @@ pub mut:
pub fn (mut self DBAsset) new(args AssetArg) !Asset {
mut o := Asset{
address: args.address
assetid: args.assetid
asset_type: args.asset_type
issuer: args.issuer
supply: args.supply

View File

@@ -31,44 +31,45 @@ pub enum DNSZoneStatus {
archived
}
// DNSRecord represents a DNS record configuration
// DNSRecord represents a DNS record configuration.
pub struct DNSRecord {
pub mut:
subdomain string
record_type NameType
value string
priority u32
ttl u32
is_active bool
cat NameCat
is_wildcard bool
subdomain string @[required] // The subdomain for the record (e.g., 'www', '@' for the root).
record_type NameType @[required] // The type of the DNS record (e.g., A, AAAA, CNAME).
value string @[required] // The value of the DNS record (e.g., an IP address, a hostname).
priority u32 // The priority of the record, used for MX records.
ttl u32 // The Time To Live for the record in seconds.
is_active bool // Indicates if the record is currently active.
cat NameCat // The category of the DNS record, for internal classification.
is_wildcard bool // Indicates if this is a wildcard record (e.g., '*.example.com').
}
// SOARecord represents SOA (Start of Authority) record for a DNS zone
// SOARecord represents the Start of Authority (SOA) record for a DNS zone.
pub struct SOARecord {
pub mut:
zone_id u32
primary_ns string
admin_email string
serial u64
refresh u32
retry u32
expire u32
minimum_ttl u32
is_active bool
zone_id u32 // The ID of the zone this SOA record belongs to.
primary_ns string @[required] // The primary name server for the zone.
admin_email string @[required] // The email address of the zone administrator.
serial u64 @[required] // The serial number of the zone, incremented on changes.
refresh u32 // The time in seconds before the zone should be refreshed.
retry u32 // The time in seconds before a failed refresh should be retried.
expire u32 // The time in seconds before a secondary server should stop answering for the zone.
minimum_ttl u32 // The minimum TTL for records in the zone.
is_active bool // Indicates if the SOA record is currently active.
}
// DNSZone represents a DNS zone with its configuration and records
// DNSZone represents a domain name and its associated DNS records.
@[heap]
pub struct DNSZone {
db.Base
pub mut:
domain string @[index]
dnsrecords []DNSRecord
administrators []u32
status DNSZoneStatus
metadata map[string]string
soarecord []SOARecord
domain string @[index; required] // The domain name of the zone (e.g., 'example.com').
dnsrecords []DNSRecord // A list of DNS records associated with this zone.
administrators []u32 // A list of user IDs that are administrators for this zone.
min_signatures u32 // The minimum number of signatures required for administrative actions.
status DNSZoneStatus // The current status of the DNS zone (e.g., active, suspended).
metadata map[string]string // A map for storing arbitrary metadata as key-value pairs.
soarecord []SOARecord // The Start of Authority (SOA) record for this zone.
}
pub struct DBDNSZone {
@@ -81,54 +82,34 @@ pub fn (self DNSZone) type_name() string {
}
pub fn (self DNSZone) description(methodname string) string {
match methodname {
'set' {
return 'Create or update a DNS zone. Returns the ID of the DNS zone.'
}
'get' {
return 'Retrieve a DNS zone by ID. Returns the DNS zone object.'
}
'delete' {
return 'Delete a DNS zone by ID. Returns true if successful.'
}
'exist' {
return 'Check if a DNS zone exists by ID. Returns true or false.'
}
'list' {
return 'List all DNS zones. Returns an array of DNS zone objects.'
}
else {
return 'DNS zone management operations'
}
return match methodname {
'set' { 'Create or update a DNS zone. Returns the ID of the DNS zone.' }
'get' { 'Retrieve a DNS zone by its unique ID.' }
'delete' { 'Deletes a DNS zone by its unique ID.' }
'exist' { 'Checks if a DNS zone with the given ID exists.' }
'find' { 'Finds DNS zones based on a filter expression.' }
'count' { 'Counts the number of DNS zones that match a filter expression.' }
'list' { 'Lists all DNS zones, optionally filtered and sorted.' }
else { 'A DNS zone represents a domain name and its associated records.' }
}
}
pub fn (self DNSZone) example(methodname string) (string, string) {
match methodname {
'set' {
return '{"dnszone": {"domain": "example.com", "status": "active"}}', '1'
}
'get' {
return '{"id": 1}', '{"domain": "example.com", "status": "active"}'
}
'delete' {
return '{"id": 1}', 'true'
}
'exist' {
return '{"id": 1}', 'true'
}
'list' {
return '{}', '[{"domain": "example.com", "status": "active"}]'
}
else {
return '{}', '{}'
}
return match methodname {
'set' { '{"dnszone": {"id": 1, "domain": "example.com", "status": "active"}}', '1' }
'get' { '{"id": 1}', '{"id": 1, "domain": "example.com", "status": "active"}' }
'delete' { '{"id": 1}', 'true' }
'exist' { '{"id": 1}', 'true' }
'find' { '{"filter": "domain=\'example.com\'"}', '[{"id": 1, "domain": "example.com", "status": "active"}]' }
'count' { '{"filter": "domain=\'example.com\'"}', '1' }
'list' { '{}', '[{"id": 1, "domain": "example.com", "status": "active"}]' }
else { '{}', '{}' }
}
}
pub fn (self DNSZone) dump(mut e encoder.Encoder) ! {
e.add_string(self.domain)
// dnsrecords
e.add_int(self.dnsrecords.len)
for record in self.dnsrecords {
@@ -141,17 +122,18 @@ pub fn (self DNSZone) dump(mut e encoder.Encoder) ! {
e.add_int(int(record.cat))
e.add_bool(record.is_wildcard)
}
e.add_list_u32(self.administrators)
e.add_int(int(self.status))
e.add_u32(self.min_signatures)
// metadata map
e.add_int(self.metadata.len)
for key, value in self.metadata {
e.add_string(key)
e.add_string(value)
}
// soarecords
e.add_int(self.soarecord.len)
for soa in self.soarecord {
@@ -169,27 +151,28 @@ pub fn (self DNSZone) dump(mut e encoder.Encoder) ! {
fn (mut self DBDNSZone) load(mut o DNSZone, mut e encoder.Decoder) ! {
o.domain = e.get_string()!
// dnsrecords
records_len := e.get_int()!
o.dnsrecords = []DNSRecord{cap: records_len}
for _ in 0 .. records_len {
record := DNSRecord{
subdomain: e.get_string()!
record_type: NameType(e.get_int()!)
record_type: unsafe { NameType(e.get_int()!) }
value: e.get_string()!
priority: e.get_u32()!
ttl: e.get_u32()!
is_active: e.get_bool()!
cat: NameCat(e.get_int()!)
cat: unsafe { NameCat(e.get_int()!) }
is_wildcard: e.get_bool()!
}
o.dnsrecords << record
}
o.administrators = e.get_list_u32()!
o.status = DNSZoneStatus(e.get_int()!)
o.status = unsafe { DNSZoneStatus(e.get_int()!) }
o.min_signatures = e.get_u32()!
// metadata map
metadata_len := e.get_int()!
o.metadata = map[string]string{}
@@ -198,7 +181,7 @@ fn (mut self DBDNSZone) load(mut o DNSZone, mut e encoder.Decoder) ! {
value := e.get_string()!
o.metadata[key] = value
}
// soarecords
soa_len := e.get_int()!
o.soarecord = []SOARecord{cap: soa_len}
@@ -227,6 +210,7 @@ pub mut:
dnsrecords []DNSRecord
administrators []u32
status DNSZoneStatus
min_signatures u32
metadata map[string]string
soarecord []SOARecord
}
@@ -237,6 +221,7 @@ pub fn (mut self DBDNSZone) new(args DNSZoneArg) !DNSZone {
dnsrecords: args.dnsrecords
administrators: args.administrators
status: args.status
min_signatures: args.min_signatures
metadata: args.metadata
soarecord: args.soarecord
}
@@ -269,4 +254,4 @@ pub fn (mut self DBDNSZone) get(id u32) !DNSZone {
pub fn (mut self DBDNSZone) list() ![]DNSZone {
return self.db.list[DNSZone]()!.map(self.get(it)!)
}
}

View File

@@ -5,6 +5,22 @@ import freeflowuniverse.herolib.data.encoder
import freeflowuniverse.herolib.data.ourtime
import freeflowuniverse.herolib.hero.db
// Group represents a collection of users with shared permissions and access.
@[heap]
pub struct Group {
db.Base
pub mut:
group_name string @[index; required] // The unique name of the group.
dnsrecords []u32 // A list of DNS record IDs associated with this group.
administrators []u32 // A list of user IDs that are administrators for this group.
min_signatures u32 // The minimum number of signatures required for administrative actions.
config GroupConfig // The configuration settings for the group.
status GroupStatus // The current status of the group (e.g., active, inactive).
visibility Visibility // The visibility level of the group (e.g., public, private).
created u64 // The timestamp when the group was created.
updated u64 // The timestamp when the group was last updated.
}
// GroupStatus defines the lifecycle of a group
pub enum GroupStatus {
active
@@ -15,33 +31,18 @@ pub enum GroupStatus {
// Visibility controls who can discover or view the group
pub enum Visibility {
public_
private_
public
private
unlisted
}
// GroupConfig holds rules that govern group membership and behavior
// GroupConfig defines the settings and rules for a group.
pub struct GroupConfig {
pub mut:
max_members u32
allow_guests bool
auto_approve bool
require_invite bool
}
// Group represents a collaborative or access-controlled unit within the system
@[heap]
pub struct Group {
db.Base
pub mut:
group_name string @[index]
dnsrecords []u32
administrators []u32
config GroupConfig
status GroupStatus
visibility Visibility
created u64
updated u64
max_members u32 // The maximum number of members allowed in the group.
allow_guests bool // Whether non-members are allowed to view the group's content.
auto_approve bool // Whether new members are automatically approved or require manual approval.
require_invite bool // Whether a user must be invited to join the group.
}
pub struct DBGroup {
@@ -54,48 +55,28 @@ pub fn (self Group) type_name() string {
}
pub fn (self Group) description(methodname string) string {
match methodname {
'set' {
return 'Create or update a group. Returns the ID of the group.'
}
'get' {
return 'Retrieve a group by ID. Returns the group object.'
}
'delete' {
return 'Delete a group by ID. Returns true if successful.'
}
'exist' {
return 'Check if a group exists by ID. Returns true or false.'
}
'list' {
return 'List all groups. Returns an array of group objects.'
}
else {
return 'Group management operations'
}
return match methodname {
'set' { 'Create or update a group. Returns the ID of the group.' }
'get' { 'Retrieve a group by its unique ID.' }
'delete' { 'Deletes a group by its unique ID.' }
'exist' { 'Checks if a group with the given ID exists.' }
'find' { 'Finds groups based on a filter expression.' }
'count' { 'Counts the number of groups that match a filter expression.' }
'list' { 'Lists all groups, optionally filtered and sorted.' }
else { 'A group represents a collection of users with shared permissions and access.' }
}
}
pub fn (self Group) example(methodname string) (string, string) {
match methodname {
'set' {
return '{"group": {"name": "Development Team", "description": "Core development group", "status": "active", "visibility": "private"}}', '1'
}
'get' {
return '{"id": 1}', '{"name": "Development Team", "description": "Core development group", "status": "active", "visibility": "private"}'
}
'delete' {
return '{"id": 1}', 'true'
}
'exist' {
return '{"id": 1}', 'true'
}
'list' {
return '{}', '[{"name": "Development Team", "description": "Core development group", "status": "active", "visibility": "private"}]'
}
else {
return '{}', '{}'
}
return match methodname {
'set' { '{"group": {"id": 1, "name": "Development Team", "description": "Core development group", "status": "active", "visibility": "private"}}', '1' }
'get' { '{"id": 1}', '{"id": 1, "name": "Development Team", "description": "Core development group", "status": "active", "visibility": "private"}' }
'delete' { '{"id": 1}', 'true' }
'exist' { '{"id": 1}', 'true' }
'find' { '{"filter": "name=\'Development Team\'"}', '[{"id": 1, "name": "Development Team", "description": "Core development group", "status": "active", "visibility": "private"}]' }
'count' { '{"filter": "name=\'Development Team\'"}', '1' }
'list' { '{}', '[{"id": 1, "name": "Development Team", "description": "Core development group", "status": "active", "visibility": "private"}]' }
else { '{}', '{}' }
}
}
@@ -103,13 +84,13 @@ pub fn (self Group) dump(mut e encoder.Encoder) ! {
e.add_string(self.group_name)
e.add_list_u32(self.dnsrecords)
e.add_list_u32(self.administrators)
e.add_u32(self.min_signatures)
// GroupConfig
e.add_u32(self.config.max_members)
e.add_bool(self.config.allow_guests)
e.add_bool(self.config.auto_approve)
e.add_bool(self.config.require_invite)
e.add_int(int(self.status))
e.add_int(int(self.visibility))
e.add_u64(self.created)
@@ -120,17 +101,17 @@ fn (mut self DBGroup) load(mut o Group, mut e encoder.Decoder) ! {
o.group_name = e.get_string()!
o.dnsrecords = e.get_list_u32()!
o.administrators = e.get_list_u32()!
o.min_signatures = e.get_u32()!
// GroupConfig
o.config = GroupConfig{
max_members: e.get_u32()!
allow_guests: e.get_bool()!
auto_approve: e.get_bool()!
max_members: e.get_u32()!
allow_guests: e.get_bool()!
auto_approve: e.get_bool()!
require_invite: e.get_bool()!
}
o.status = GroupStatus(e.get_int()!)
o.visibility = Visibility(e.get_int()!)
o.status = unsafe { GroupStatus(e.get_int()!) }
o.visibility = unsafe { Visibility(e.get_int()!) }
o.created = e.get_u64()!
o.updated = e.get_u64()!
}
@@ -143,6 +124,7 @@ pub mut:
group_name string
dnsrecords []u32
administrators []u32
min_signatures u32
config GroupConfig
status GroupStatus
visibility Visibility
@@ -155,6 +137,7 @@ pub fn (mut self DBGroup) new(args GroupArg) !Group {
group_name: args.group_name
dnsrecords: args.dnsrecords
administrators: args.administrators
min_signatures: args.min_signatures
config: args.config
status: args.status
visibility: args.visibility
@@ -190,4 +173,4 @@ pub fn (mut self DBGroup) get(id u32) !Group {
pub fn (mut self DBGroup) list() ![]Group {
return self.db.list[Group]()!.map(self.get(it)!)
}
}

View File

@@ -25,12 +25,12 @@ pub enum MemberStatus {
pub struct Member {
db.Base
pub mut:
group_id u32 @[index]
user_id u32 @[index]
role MemberRole
status MemberStatus
join_date u64
last_activity u64
group_id u32 @[index]
user_id u32 @[index]
role MemberRole
status MemberStatus
join_date u64
last_activity u64
}
pub struct DBMember {
@@ -100,8 +100,8 @@ pub fn (self Member) dump(mut e encoder.Encoder) ! {
fn (mut self DBMember) load(mut o Member, mut e encoder.Decoder) ! {
o.group_id = e.get_u32()!
o.user_id = e.get_u32()!
o.role = MemberRole(e.get_u8()!)
o.status = MemberStatus(e.get_u8()!)
o.role = unsafe { MemberRole(e.get_u8()!) }
o.status = unsafe { MemberStatus(e.get_u8()!) }
o.join_date = e.get_u64()!
o.last_activity = e.get_u64()!
}
@@ -113,7 +113,7 @@ pub mut:
description string
group_id u32
user_id u32
role MemberRole = .member
role MemberRole = .member
status MemberStatus = .pending
}
@@ -124,8 +124,8 @@ pub fn (mut self DBMember) new(args MemberArg) !Member {
user_id: args.user_id
role: args.role
status: args.status
join_date: now
last_activity: now
join_date: u64(now)
last_activity: u64(now)
}
o.name = args.name
@@ -156,4 +156,4 @@ pub fn (mut self DBMember) get(id u32) !Member {
pub fn (mut self DBMember) list() ![]Member {
return self.db.list[Member]()!.map(self.get(it)!)
}
}

View File

@@ -5,6 +5,19 @@ import freeflowuniverse.herolib.data.encoder
import freeflowuniverse.herolib.data.ourtime
import freeflowuniverse.herolib.hero.db
// User represents a user in the heroledger system
@[heap]
pub struct User {
db.Base
pub mut:
username string @[index]
pubkey string @[index]
email []string
status UserStatus
userprofile []SecretBox
kyc []SecretBox
}
// UserStatus represents the status of a user in the system
pub enum UserStatus {
active
@@ -34,21 +47,21 @@ pub mut:
// KYCInfo contains KYC information for a user
pub struct KYCInfo {
pub mut:
user_id u32
full_name string
date_of_birth u64
address string
phone_number string
id_number string
id_type string
id_expiry u64
kyc_status KYCStatus
kyc_verified bool
kyc_verified_by u32
kyc_verified_at u64
kyc_rejected_reason string
kyc_signature u32
metadata map[string]string
user_id u32
full_name string
date_of_birth u64
address string
phone_number string
id_number string
id_type string
id_expiry u64
kyc_status KYCStatus
kyc_verified bool
kyc_verified_by u32
kyc_verified_at u64
kyc_rejected_reason string
kyc_signature u32
metadata map[string]string
}
// SecretBox represents encrypted data storage
@@ -58,19 +71,6 @@ pub mut:
nonce []u8
}
// User represents a user in the heroledger system
@[heap]
pub struct User {
db.Base
pub mut:
username string @[index]
pubkey string @[index]
email []string
status UserStatus
userprofile []SecretBox
kyc []SecretBox
}
pub struct DBUser {
pub mut:
db &db.DB @[skip; str: skip]
@@ -147,23 +147,23 @@ fn (mut self DBUser) load(mut o User, mut e encoder.Decoder) ! {
o.username = e.get_string()!
o.pubkey = e.get_string()!
o.email = e.get_list_string()!
o.status = UserStatus(e.get_int()!)
o.status = unsafe { UserStatus(e.get_int()!) }
profile_len := e.get_int()!
o.userprofile = []SecretBox{cap: profile_len}
for _ in 0 .. profile_len {
profile := SecretBox{
data: e.get_list_u8()!
data: e.get_list_u8()!
nonce: e.get_list_u8()!
}
o.userprofile << profile
}
kyc_len := e.get_int()!
o.kyc = []SecretBox{cap: kyc_len}
for _ in 0 .. kyc_len {
kyc_item := SecretBox{
data: e.get_list_u8()!
data: e.get_list_u8()!
nonce: e.get_list_u8()!
}
o.kyc << kyc_item
@@ -221,4 +221,4 @@ pub fn (mut self DBUser) get(id u32) !User {
pub fn (mut self DBUser) list() ![]User {
return self.db.list[User]()!.map(self.get(it)!)
}
}

View File

@@ -1,125 +0,0 @@
// lib/threefold/models_ledger/usergroupmembership.v
module models_ledger
import freeflowuniverse.herolib.data.encoder
import freeflowuniverse.herolib.data.ourtime
import freeflowuniverse.herolib.hero.db
// UserGroupMembership represents the membership relationship between users and groups
@[heap]
pub struct UserGroupMembership {
db.Base
pub mut:
user_id u32 @[index]
group_ids []u32
}
pub struct DBUserGroupMembership {
pub mut:
db &db.DB @[skip; str: skip]
}
pub fn (self UserGroupMembership) type_name() string {
return 'usergroupmembership'
}
pub fn (self UserGroupMembership) description(methodname string) string {
match methodname {
'set' {
return 'Create or update a user group membership. Returns the ID of the membership.'
}
'get' {
return 'Retrieve a user group membership by ID. Returns the membership object.'
}
'delete' {
return 'Delete a user group membership by ID. Returns true if successful.'
}
'exist' {
return 'Check if a user group membership exists by ID. Returns true or false.'
}
'list' {
return 'List all user group memberships. Returns an array of membership objects.'
}
else {
return 'User group membership management operations'
}
}
}
pub fn (self UserGroupMembership) example(methodname string) (string, string) {
match methodname {
'set' {
return '{"usergroupmembership": {"user_id": 1, "group_ids": [1, 2, 3]}}', '1'
}
'get' {
return '{"id": 1}', '{"user_id": 1, "group_ids": [1, 2, 3]}'
}
'delete' {
return '{"id": 1}', 'true'
}
'exist' {
return '{"id": 1}', 'true'
}
'list' {
return '{}', '[{"user_id": 1, "group_ids": [1, 2, 3]}]'
}
else {
return '{}', '{}'
}
}
}
pub fn (self UserGroupMembership) dump(mut e encoder.Encoder) ! {
e.add_u32(self.user_id)
e.add_list_u32(self.group_ids)
}
fn (mut self DBUserGroupMembership) load(mut o UserGroupMembership, mut e encoder.Decoder) ! {
o.user_id = e.get_u32()!
o.group_ids = e.get_list_u32()!
}
@[params]
pub struct UserGroupMembershipArg {
pub mut:
name string
description string
user_id u32
group_ids []u32
}
pub fn (mut self DBUserGroupMembership) new(args UserGroupMembershipArg) !UserGroupMembership {
mut o := UserGroupMembership{
user_id: args.user_id
group_ids: args.group_ids
}
o.name = args.name
o.description = args.description
o.updated_at = ourtime.now().unix()
return o
}
pub fn (mut self DBUserGroupMembership) set(o UserGroupMembership) !UserGroupMembership {
return self.db.set[UserGroupMembership](o)!
}
pub fn (mut self DBUserGroupMembership) delete(id u32) ! {
self.db.delete[UserGroupMembership](id)!
}
pub fn (mut self DBUserGroupMembership) exist(id u32) !bool {
return self.db.exists[UserGroupMembership](id)!
}
pub fn (mut self DBUserGroupMembership) get(id u32) !UserGroupMembership {
mut o, data := self.db.get_data[UserGroupMembership](id)!
mut e_decoder := encoder.decoder_new(data)
self.load(mut o, mut e_decoder)!
return o
}
pub fn (mut self DBUserGroupMembership) list() ![]UserGroupMembership {
return self.db.list[UserGroupMembership]()!.map(self.get(it)!)
}