...
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
|
||||
## Overview
|
||||
|
||||
This document provides clear instructions for AI agents to create new HeroDB models similar to `comment.v`. These models are used to store structured data in Redis using the HeroDB system.
|
||||
This document provides clear instructions for AI agents to create new HeroDB models similar to `message.v`. These models are used to store structured data in Redis using the HeroDB system.
|
||||
|
||||
## Key Concepts
|
||||
|
||||
@@ -107,7 +107,7 @@ Add your model to the ModelsFactory struct in `factory.v`:
|
||||
```v
|
||||
pub struct ModelsFactory {
|
||||
pub mut:
|
||||
comments DBCalendar
|
||||
messages DBCalendar
|
||||
// ... other models
|
||||
}
|
||||
```
|
||||
@@ -118,7 +118,7 @@ And initialize it in the `new()` function:
|
||||
pub fn new() !ModelsFactory {
|
||||
mut mydb := db.new()!
|
||||
return ModelsFactory{
|
||||
comments: DBCalendar{
|
||||
messages: DBCalendar{
|
||||
db: &mydb
|
||||
}
|
||||
// ... initialize other models
|
||||
|
||||
@@ -25,9 +25,9 @@ pub fn (mut self DB) set[T](obj_ T) !T {
|
||||
e.add_i64(obj.updated_at)
|
||||
e.add_u32(obj.securitypolicy)
|
||||
e.add_u32(obj.tags)
|
||||
e.add_u16(u16(obj.comments.len))
|
||||
for comment in obj.comments {
|
||||
e.add_u32(comment)
|
||||
e.add_u16(u16(obj.messages.len))
|
||||
for message in obj.messages {
|
||||
e.add_u32(message)
|
||||
}
|
||||
obj.dump(mut e)!
|
||||
self.redis.hset(self.db_name[T](), obj.id.str(), e.data.bytestr())!
|
||||
@@ -57,7 +57,7 @@ pub fn (mut self DB) get_data[T](id u32) !(T, []u8) {
|
||||
base.securitypolicy = e.get_u32()!
|
||||
base.tags = e.get_u32()!
|
||||
for _ in 0 .. e.get_u16()! {
|
||||
base.comments << e.get_u32()!
|
||||
base.messages << e.get_u32()!
|
||||
}
|
||||
return base, e.data
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ pub mut:
|
||||
updated_at i64
|
||||
securitypolicy u32
|
||||
tags u32 // when we set/get we always do as []string but this can then be sorted and md5ed this gies the unique id of tags
|
||||
comments []u32
|
||||
messages []u32
|
||||
}
|
||||
|
||||
@[heap]
|
||||
|
||||
@@ -1,33 +0,0 @@
|
||||
module db
|
||||
|
||||
import crypto.md5
|
||||
|
||||
@[params]
|
||||
pub struct CommentArg {
|
||||
pub mut:
|
||||
comment string
|
||||
parent u32
|
||||
author u32
|
||||
}
|
||||
|
||||
pub fn (mut self DB) comments_get(args []CommentArg) ![]u32 {
|
||||
return args.map(self.comment_get(it.comment)!)
|
||||
}
|
||||
|
||||
pub fn (mut self DB) comment_get(comment string) !u32 {
|
||||
comment_fixed := comment.to_lower_ascii().trim_space()
|
||||
return if comment_fixed.len > 0 {
|
||||
hash := md5.hexhash(comment_fixed)
|
||||
comment_found := self.redis.hget('db:comments', hash)!
|
||||
if comment_found == '' {
|
||||
id := self.new_id()!
|
||||
self.redis.hset('db:comments', hash, id.str())!
|
||||
self.redis.hset('db:comments', id.str(), comment_fixed)!
|
||||
id
|
||||
} else {
|
||||
comment_found.u32()
|
||||
}
|
||||
} else {
|
||||
0
|
||||
}
|
||||
}
|
||||
33
lib/hero/db/helpers_messages.v
Normal file
33
lib/hero/db/helpers_messages.v
Normal file
@@ -0,0 +1,33 @@
|
||||
module db
|
||||
|
||||
import crypto.md5
|
||||
|
||||
@[params]
|
||||
pub struct MessageArg {
|
||||
pub mut:
|
||||
message string
|
||||
parent u32
|
||||
author u32
|
||||
}
|
||||
|
||||
pub fn (mut self DB) messages_get(args []MessageArg) ![]u32 {
|
||||
return args.map(self.message_get(it.message)!)
|
||||
}
|
||||
|
||||
pub fn (mut self DB) message_get(message string) !u32 {
|
||||
message_fixed := message.to_lower_ascii().trim_space()
|
||||
return if message_fixed.len > 0 {
|
||||
hash := md5.hexhash(message_fixed)
|
||||
message_found := self.redis.hget('db:messages', hash)!
|
||||
if message_found == '' {
|
||||
id := self.new_id()!
|
||||
self.redis.hset('db:messages', hash, id.str())!
|
||||
self.redis.hset('db:messages', id.str(), message_fixed)!
|
||||
id
|
||||
} else {
|
||||
message_found.u32()
|
||||
}
|
||||
} else {
|
||||
0
|
||||
}
|
||||
}
|
||||
@@ -335,7 +335,7 @@ pub mut:
|
||||
is_template bool // Added missing is_template field
|
||||
securitypolicy u32
|
||||
tags []string
|
||||
comments []db.CommentArg
|
||||
messages []db.MessageArg
|
||||
}
|
||||
|
||||
// get new calendar event, not from the DB
|
||||
@@ -370,7 +370,7 @@ pub fn (mut self DBCalendarEvent) new(args CalendarEventArg) !CalendarEvent {
|
||||
o.description = args.description
|
||||
o.securitypolicy = args.securitypolicy
|
||||
o.tags = self.db.tags_get(args.tags)!
|
||||
o.comments = self.db.comments_get(args.comments)!
|
||||
o.messages = self.db.messages_get(args.messages)!
|
||||
o.updated_at = ourtime.now().unix()
|
||||
|
||||
// Convert string times to Unix timestamps
|
||||
|
||||
@@ -29,7 +29,7 @@ fn test_calendar_event_new() ! {
|
||||
is_template: false // Added missing is_template field
|
||||
securitypolicy: 0
|
||||
tags: []string{}
|
||||
comments: []db.CommentArg{}
|
||||
messages: []db.MessageArg{}
|
||||
}
|
||||
|
||||
calendar_event := db_calendar_event.new(args)!
|
||||
@@ -81,7 +81,7 @@ fn test_calendar_event_crud_operations() ! {
|
||||
is_template: false // Added missing is_template field
|
||||
securitypolicy: 0
|
||||
tags: []string{}
|
||||
comments: []db.CommentArg{}
|
||||
messages: []db.MessageArg{}
|
||||
}
|
||||
|
||||
mut calendar_event := db_calendar_event.new(args)!
|
||||
@@ -143,7 +143,7 @@ fn test_calendar_event_crud_operations() ! {
|
||||
timezone: 'EST'
|
||||
securitypolicy: 0
|
||||
tags: []string{}
|
||||
comments: []db.CommentArg{}
|
||||
messages: []db.MessageArg{}
|
||||
}
|
||||
|
||||
mut updated_event := db_calendar_event.new(updated_args)!
|
||||
@@ -217,7 +217,7 @@ fn test_calendar_event_attendees_encoding_decoding() ! {
|
||||
is_template: false // Added missing is_template field
|
||||
securitypolicy: 0
|
||||
tags: []string{}
|
||||
comments: []db.CommentArg{}
|
||||
messages: []db.MessageArg{}
|
||||
}
|
||||
|
||||
mut calendar_event := db_calendar_event.new(args)!
|
||||
@@ -314,7 +314,7 @@ fn test_calendar_event_registration_desks_encoding_decoding() ! {
|
||||
is_template: false
|
||||
securitypolicy: 0
|
||||
tags: []string{}
|
||||
comments: []db.CommentArg{}
|
||||
messages: []db.MessageArg{}
|
||||
}
|
||||
|
||||
mut calendar_event := db_calendar_event.new(args)!
|
||||
@@ -363,7 +363,7 @@ fn test_calendar_event_docs_encoding_decoding() ! {
|
||||
is_template: false
|
||||
securitypolicy: 0
|
||||
tags: []string{}
|
||||
comments: []db.CommentArg{}
|
||||
messages: []db.MessageArg{}
|
||||
}
|
||||
|
||||
mut calendar_event := db_calendar_event.new(args)!
|
||||
@@ -429,7 +429,7 @@ fn test_calendar_event_type_name() ! {
|
||||
timezone: 'UTC'
|
||||
securitypolicy: 0
|
||||
tags: []string{}
|
||||
comments: []db.CommentArg{}
|
||||
messages: []db.MessageArg{}
|
||||
}
|
||||
|
||||
calendar_event := db_calendar_event.new(args)!
|
||||
@@ -465,7 +465,7 @@ fn test_calendar_event_description() ! {
|
||||
timezone: 'UTC'
|
||||
securitypolicy: 0
|
||||
tags: []string{}
|
||||
comments: []db.CommentArg{}
|
||||
messages: []db.MessageArg{}
|
||||
}
|
||||
|
||||
calendar_event := db_calendar_event.new(args)!
|
||||
@@ -505,7 +505,7 @@ fn test_calendar_event_example() ! {
|
||||
timezone: 'UTC'
|
||||
securitypolicy: 0
|
||||
tags: []string{}
|
||||
comments: []db.CommentArg{}
|
||||
messages: []db.MessageArg{}
|
||||
}
|
||||
|
||||
calendar_event := db_calendar_event.new(args)!
|
||||
|
||||
@@ -108,7 +108,7 @@ pub mut:
|
||||
is_archived bool
|
||||
securitypolicy u32
|
||||
tags []string
|
||||
comments []db.CommentArg
|
||||
messages []db.MessageArg
|
||||
}
|
||||
|
||||
// get new chat group, not from the DB
|
||||
@@ -124,7 +124,7 @@ pub fn (mut self DBChatGroup) new(args ChatGroupArg) !ChatGroup {
|
||||
o.description = args.description
|
||||
o.securitypolicy = args.securitypolicy
|
||||
o.tags = self.db.tags_get(args.tags)!
|
||||
o.comments = self.db.comments_get(args.comments)!
|
||||
o.messages = self.db.messages_get(args.messages)!
|
||||
o.updated_at = ourtime.now().unix()
|
||||
|
||||
return o
|
||||
|
||||
@@ -207,7 +207,7 @@ pub mut:
|
||||
mentions []u32
|
||||
securitypolicy u32
|
||||
tags []string
|
||||
comments []db.CommentArg
|
||||
messages []db.MessageArg
|
||||
}
|
||||
|
||||
// get new chat message, not from the DB
|
||||
@@ -229,7 +229,7 @@ pub fn (mut self DBChatMessage) new(args ChatMessageArg) !ChatMessage {
|
||||
o.description = args.description
|
||||
o.securitypolicy = args.securitypolicy
|
||||
o.tags = self.db.tags_get(args.tags)!
|
||||
o.comments = self.db.comments_get(args.comments)!
|
||||
o.messages = self.db.messages_get(args.messages)!
|
||||
o.updated_at = ourtime.now().unix()
|
||||
|
||||
return o
|
||||
|
||||
@@ -1,291 +0,0 @@
|
||||
module heromodels
|
||||
|
||||
import freeflowuniverse.herolib.hero.db
|
||||
import freeflowuniverse.herolib.data.ourtime
|
||||
|
||||
fn test_comment_new() ! {
|
||||
// Initialize DBComments for testing
|
||||
mut mydb := db.new_test()!
|
||||
mut db_comments := DBComments{
|
||||
db: &mydb
|
||||
}
|
||||
|
||||
// Test creating a new comment
|
||||
mut args := CommentArg{
|
||||
subject: 'Test Subject'
|
||||
comment: 'This is a test comment.'
|
||||
parent: 0
|
||||
author: 1
|
||||
to: [u32(2), u32(3)]
|
||||
cc: [u32(4), u32(5)]
|
||||
}
|
||||
|
||||
comment := db_comments.new(args)!
|
||||
|
||||
assert comment.subject == 'Test Subject'
|
||||
assert comment.comment == 'This is a test comment.'
|
||||
assert comment.parent == 0
|
||||
assert comment.author == 1
|
||||
assert comment.to.len == 2
|
||||
assert comment.to[0] == 2
|
||||
assert comment.to[1] == 3
|
||||
assert comment.cc.len == 2
|
||||
assert comment.cc[0] == 4
|
||||
assert comment.cc[1] == 5
|
||||
assert comment.updated_at > 0
|
||||
|
||||
println('✓ Comment new test passed!')
|
||||
}
|
||||
|
||||
fn test_comment_crud_operations() ! {
|
||||
// Initialize DBComments for testing
|
||||
mut mydb := db.new_test()!
|
||||
mut db_comments := DBComments{
|
||||
db: &mydb
|
||||
}
|
||||
|
||||
// Create a new comment
|
||||
mut args := CommentArg{
|
||||
subject: 'CRUD Test Subject'
|
||||
comment: 'This is a test comment for CRUD operations.'
|
||||
parent: 0
|
||||
author: 1
|
||||
to: [u32(2), u32(3)]
|
||||
cc: [u32(4), u32(5)]
|
||||
}
|
||||
|
||||
mut comment := db_comments.new(args)!
|
||||
|
||||
// Test set operation
|
||||
comment = db_comments.set(comment)!
|
||||
original_id := comment.id
|
||||
|
||||
// Test get operation
|
||||
retrieved_comment := db_comments.get(original_id)!
|
||||
assert retrieved_comment.subject == 'CRUD Test Subject'
|
||||
assert retrieved_comment.comment == 'This is a test comment for CRUD operations.'
|
||||
assert retrieved_comment.parent == 0
|
||||
assert retrieved_comment.author == 1
|
||||
assert retrieved_comment.id == original_id
|
||||
assert retrieved_comment.to.len == 2
|
||||
assert retrieved_comment.to[0] == 2
|
||||
assert retrieved_comment.to[1] == 3
|
||||
assert retrieved_comment.cc.len == 2
|
||||
assert retrieved_comment.cc[0] == 4
|
||||
assert retrieved_comment.cc[1] == 5
|
||||
|
||||
// Test exist operation
|
||||
exists := db_comments.exist(original_id)!
|
||||
assert exists == true
|
||||
|
||||
// Test update
|
||||
mut updated_args := CommentArg{
|
||||
subject: 'Updated Test Subject'
|
||||
comment: 'This is an updated test comment.'
|
||||
parent: 10
|
||||
author: 2
|
||||
to: [u32(6)]
|
||||
cc: [u32(7), u32(8), u32(9)]
|
||||
}
|
||||
|
||||
mut updated_comment := db_comments.new(updated_args)!
|
||||
updated_comment.id = original_id
|
||||
updated_comment = db_comments.set(updated_comment)!
|
||||
|
||||
// Verify update
|
||||
final_comment := db_comments.get(original_id)!
|
||||
assert final_comment.subject == 'Updated Test Subject'
|
||||
assert final_comment.comment == 'This is an updated test comment.'
|
||||
assert final_comment.parent == 10
|
||||
assert final_comment.author == 2
|
||||
assert final_comment.to.len == 1
|
||||
assert final_comment.to[0] == 6
|
||||
assert final_comment.cc.len == 3
|
||||
assert final_comment.cc[0] == 7
|
||||
assert final_comment.cc[1] == 8
|
||||
assert final_comment.cc[2] == 9
|
||||
|
||||
// Test delete operation
|
||||
db_comments.delete(original_id)!
|
||||
|
||||
// Verify deletion
|
||||
exists_after_delete := db_comments.exist(original_id)!
|
||||
assert exists_after_delete == false
|
||||
|
||||
println('✓ Comment CRUD operations test passed!')
|
||||
}
|
||||
|
||||
fn test_comment_encoding_decoding() ! {
|
||||
// Initialize DBComments for testing
|
||||
mut mydb := db.new_test()!
|
||||
mut db_comments := DBComments{
|
||||
db: &mydb
|
||||
}
|
||||
|
||||
// Create a new comment with all fields populated
|
||||
mut args := CommentArg{
|
||||
subject: 'Encoding Test Subject'
|
||||
comment: 'This is a test comment for encoding/decoding.'
|
||||
parent: 5
|
||||
author: 10
|
||||
to: [u32(20), u32(30), u32(40)]
|
||||
cc: [u32(50), u32(60)]
|
||||
}
|
||||
|
||||
mut comment := db_comments.new(args)!
|
||||
|
||||
// Add send_log data manually
|
||||
mut send_log1 := SendLog{
|
||||
to: [u32(100), u32(101)]
|
||||
cc: [u32(102)]
|
||||
status: .sent
|
||||
timestamp: 1678886400 // Example timestamp
|
||||
}
|
||||
mut send_log2 := SendLog{
|
||||
to: [u32(200)]
|
||||
cc: []u32{}
|
||||
status: .received
|
||||
timestamp: 1678886500 // Example timestamp
|
||||
}
|
||||
comment.send_log = [send_log1, send_log2]
|
||||
|
||||
// Save the comment
|
||||
comment = db_comments.set(comment)!
|
||||
comment_id := comment.id
|
||||
|
||||
// Retrieve and verify all fields were properly encoded/decoded
|
||||
retrieved_comment := db_comments.get(comment_id)!
|
||||
|
||||
assert retrieved_comment.subject == 'Encoding Test Subject'
|
||||
assert retrieved_comment.comment == 'This is a test comment for encoding/decoding.'
|
||||
assert retrieved_comment.parent == 5
|
||||
assert retrieved_comment.author == 10
|
||||
assert retrieved_comment.to.len == 3
|
||||
assert retrieved_comment.to[0] == 20
|
||||
assert retrieved_comment.to[1] == 30
|
||||
assert retrieved_comment.to[2] == 40
|
||||
assert retrieved_comment.cc.len == 2
|
||||
assert retrieved_comment.cc[0] == 50
|
||||
assert retrieved_comment.cc[1] == 60
|
||||
|
||||
// Verify send_log
|
||||
assert retrieved_comment.send_log.len == 2
|
||||
assert retrieved_comment.send_log[0].to.len == 2
|
||||
assert retrieved_comment.send_log[0].to[0] == 100
|
||||
assert retrieved_comment.send_log[0].to[1] == 101
|
||||
assert retrieved_comment.send_log[0].cc.len == 1
|
||||
assert retrieved_comment.send_log[0].cc[0] == 102
|
||||
assert retrieved_comment.send_log[0].status == .sent
|
||||
assert retrieved_comment.send_log[0].timestamp == 1678886400
|
||||
|
||||
assert retrieved_comment.send_log[1].to.len == 1
|
||||
assert retrieved_comment.send_log[1].to[0] == 200
|
||||
assert retrieved_comment.send_log[1].cc.len == 0
|
||||
assert retrieved_comment.send_log[1].status == .received
|
||||
assert retrieved_comment.send_log[1].timestamp == 1678886500
|
||||
|
||||
println('✓ Comment encoding/decoding test passed!')
|
||||
}
|
||||
|
||||
fn test_comment_type_name() ! {
|
||||
// Initialize DBComments for testing
|
||||
mut mydb := db.new_test()!
|
||||
mut db_comments := DBComments{
|
||||
db: &mydb
|
||||
}
|
||||
|
||||
// Create a new comment
|
||||
mut args := CommentArg{
|
||||
subject: 'Type Name Test Subject'
|
||||
comment: 'This is a test comment for type name.'
|
||||
parent: 0
|
||||
author: 1
|
||||
to: []u32{}
|
||||
cc: []u32{}
|
||||
}
|
||||
|
||||
comment := db_comments.new(args)!
|
||||
|
||||
// Test type_name method
|
||||
type_name := comment.type_name()
|
||||
assert type_name == 'comments'
|
||||
|
||||
println('✓ Comment type_name test passed!')
|
||||
}
|
||||
|
||||
fn test_comment_description() ! {
|
||||
// Initialize DBComments for testing
|
||||
mut mydb := db.new_test()!
|
||||
mut db_comments := DBComments{
|
||||
db: &mydb
|
||||
}
|
||||
|
||||
// Create a new comment
|
||||
mut args := CommentArg{
|
||||
subject: 'Description Test Subject'
|
||||
comment: 'This is a test comment for description method.'
|
||||
parent: 0
|
||||
author: 1
|
||||
to: []u32{}
|
||||
cc: []u32{}
|
||||
}
|
||||
|
||||
comment := db_comments.new(args)!
|
||||
|
||||
// Test description method for each methodname
|
||||
assert comment.description('set') == 'Create or update a comment. Returns the ID of the comment.'
|
||||
assert comment.description('get') == 'Retrieve a comment by ID. Returns the comment object.'
|
||||
assert comment.description('delete') == 'Delete a comment by ID. Returns true if successful.'
|
||||
assert comment.description('exist') == 'Check if a comment exists by ID. Returns true or false.'
|
||||
assert comment.description('list') == 'List all comments. Returns an array of comment objects.'
|
||||
assert comment.description('unknown') == 'This is generic method for the root object, TODO fill in, ...'
|
||||
|
||||
println('✓ Comment description test passed!')
|
||||
}
|
||||
|
||||
fn test_comment_example() ! {
|
||||
// Initialize DBComments for testing
|
||||
mut mydb := db.new_test()!
|
||||
mut db_comments := DBComments{
|
||||
db: &mydb
|
||||
}
|
||||
|
||||
// Create a new comment
|
||||
mut args := CommentArg{
|
||||
subject: 'Example Test Subject'
|
||||
comment: 'This is a test comment for example method.'
|
||||
parent: 0
|
||||
author: 1
|
||||
to: []u32{}
|
||||
cc: []u32{}
|
||||
}
|
||||
|
||||
comment := db_comments.new(args)!
|
||||
|
||||
// Test example method for each methodname
|
||||
set_call, set_result := comment.example('set')
|
||||
assert set_call == '{"comment": {"comment": "This is a test comment.", "parent": 0, "author": 1}}'
|
||||
assert set_result == '1'
|
||||
|
||||
get_call, get_result := comment.example('get')
|
||||
assert get_call == '{"id": 1}'
|
||||
assert get_result == '{"comment": "This is a test comment.", "parent": 0, "author": 1}'
|
||||
|
||||
delete_call, delete_result := comment.example('delete')
|
||||
assert delete_call == '{"id": 1}'
|
||||
assert delete_result == 'true'
|
||||
|
||||
exist_call, exist_result := comment.example('exist')
|
||||
assert exist_call == '{"id": 1}'
|
||||
assert exist_result == 'true'
|
||||
|
||||
list_call, list_result := comment.example('list')
|
||||
assert list_call == '{}'
|
||||
assert list_result == '[{"comment": "This is a test comment.", "parent": 0, "author": 1}]'
|
||||
|
||||
unknown_call, unknown_result := comment.example('unknown')
|
||||
assert unknown_call == '{}'
|
||||
assert unknown_result == '{}'
|
||||
|
||||
println('✓ Comment example test passed!')
|
||||
}
|
||||
@@ -4,7 +4,7 @@ import freeflowuniverse.herolib.hero.db
|
||||
|
||||
pub struct ModelsFactory {
|
||||
pub mut:
|
||||
comments DBComments
|
||||
messages DBMessages
|
||||
calendar DBCalendar
|
||||
calendar_event DBCalendarEvent
|
||||
group DBGroup
|
||||
@@ -18,7 +18,7 @@ pub mut:
|
||||
pub fn new() !ModelsFactory {
|
||||
mut mydb := db.new()!
|
||||
return ModelsFactory{
|
||||
comments: DBComments{
|
||||
messages: DBMessages{
|
||||
db: &mydb
|
||||
}
|
||||
calendar: DBCalendar{
|
||||
|
||||
@@ -5,29 +5,28 @@ import freeflowuniverse.herolib.data.ourtime
|
||||
import freeflowuniverse.herolib.hero.db
|
||||
|
||||
@[heap]
|
||||
pub struct Comment {
|
||||
pub struct Message {
|
||||
db.Base
|
||||
pub mut:
|
||||
// id u32
|
||||
subject string
|
||||
comment string
|
||||
parent u32 // id of parent comment if any, 0 means none
|
||||
message string
|
||||
parent u32 // id of parent message if any, 0 means none
|
||||
author u32 // links to user
|
||||
to []u32 // if comment/message has been sent to someone specifically
|
||||
to []u32 // if message/message has been sent to someone specifically
|
||||
cc []u32 // like to but then for cc
|
||||
send_log []SendLog
|
||||
}
|
||||
|
||||
pub struct SendLog {
|
||||
pub mut:
|
||||
to []u32 // if comment/message has been sent to someone specifically
|
||||
to []u32 // if message/message has been sent to someone specifically
|
||||
cc []u32 // like to but then for cc
|
||||
status SendStatus
|
||||
timestamp u64 //when was it done
|
||||
}
|
||||
|
||||
pub struct SendStatus {
|
||||
pub mut:
|
||||
enum SendStatus {
|
||||
sent
|
||||
received
|
||||
acknowledged
|
||||
@@ -37,40 +36,40 @@ pub mut:
|
||||
//////////TO BE GENERATED BY AI////////////////////////////////
|
||||
///BASIC CRUD FUNCTIONS
|
||||
|
||||
pub struct DBComments {
|
||||
pub struct DBMessages {
|
||||
pub mut:
|
||||
db &db.DB @[skip; str: skip]
|
||||
}
|
||||
|
||||
@[params]
|
||||
pub struct CommentListArg {
|
||||
pub struct MessageListArg {
|
||||
pub mut:
|
||||
parent u32
|
||||
author u32
|
||||
limit int = 100 // Default limit is 100
|
||||
}
|
||||
|
||||
pub fn (self Comment) type_name() string {
|
||||
return 'comments'
|
||||
pub fn (self Message) type_name() string {
|
||||
return 'messages'
|
||||
}
|
||||
|
||||
// return example rpc call and result for each methodname
|
||||
pub fn (self Comment) description(methodname string) string {
|
||||
pub fn (self Message) description(methodname string) string {
|
||||
match methodname {
|
||||
'set' {
|
||||
return 'Create or update a comment. Returns the ID of the comment.'
|
||||
return 'Create or update a message. Returns the ID of the message.'
|
||||
}
|
||||
'get' {
|
||||
return 'Retrieve a comment by ID. Returns the comment object.'
|
||||
return 'Retrieve a message by ID. Returns the message object.'
|
||||
}
|
||||
'delete' {
|
||||
return 'Delete a comment by ID. Returns true if successful.'
|
||||
return 'Delete a message by ID. Returns true if successful.'
|
||||
}
|
||||
'exist' {
|
||||
return 'Check if a comment exists by ID. Returns true or false.'
|
||||
return 'Check if a message exists by ID. Returns true or false.'
|
||||
}
|
||||
'list' {
|
||||
return 'List all comments. Returns an array of comment objects.'
|
||||
return 'List all messages. Returns an array of message objects.'
|
||||
}
|
||||
else {
|
||||
return 'This is generic method for the root object, TODO fill in, ...'
|
||||
@@ -79,13 +78,13 @@ pub fn (self Comment) description(methodname string) string {
|
||||
}
|
||||
|
||||
// return example rpc call and result for each methodname
|
||||
pub fn (self Comment) example(methodname string) (string, string) {
|
||||
pub fn (self Message) example(methodname string) (string, string) {
|
||||
match methodname {
|
||||
'set' {
|
||||
return '{"comment": {"comment": "This is a test comment.", "parent": 0, "author": 1}}', '1'
|
||||
return '{"message": {"message": "This is a test message.", "parent": 0, "author": 1}}', '1'
|
||||
}
|
||||
'get' {
|
||||
return '{"id": 1}', '{"comment": "This is a test comment.", "parent": 0, "author": 1}'
|
||||
return '{"id": 1}', '{"message": "This is a test message.", "parent": 0, "author": 1}'
|
||||
}
|
||||
'delete' {
|
||||
return '{"id": 1}', 'true'
|
||||
@@ -94,7 +93,7 @@ pub fn (self Comment) example(methodname string) (string, string) {
|
||||
return '{"id": 1}', 'true'
|
||||
}
|
||||
'list' {
|
||||
return '{}', '[{"comment": "This is a test comment.", "parent": 0, "author": 1}]'
|
||||
return '{}', '[{"message": "This is a test message.", "parent": 0, "author": 1}]'
|
||||
}
|
||||
else {
|
||||
return '{}', '{}'
|
||||
@@ -102,9 +101,9 @@ pub fn (self Comment) example(methodname string) (string, string) {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn (self Comment) dump(mut e encoder.Encoder) ! {
|
||||
pub fn (self Message) dump(mut e encoder.Encoder) ! {
|
||||
e.add_string(self.subject)
|
||||
e.add_string(self.comment)
|
||||
e.add_string(self.message)
|
||||
e.add_u32(self.parent)
|
||||
e.add_u32(self.author)
|
||||
e.add_list_u32(self.to)
|
||||
@@ -119,9 +118,9 @@ pub fn (self Comment) dump(mut e encoder.Encoder) ! {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn (mut self DBComments) load(mut o Comment, mut e encoder.Decoder) ! {
|
||||
pub fn (mut self DBMessages) load(mut o Message, mut e encoder.Decoder) ! {
|
||||
o.subject = e.get_string()!
|
||||
o.comment = e.get_string()!
|
||||
o.message = e.get_string()!
|
||||
o.parent = e.get_u32()!
|
||||
o.author = e.get_u32()!
|
||||
o.to = e.get_list_u32()!
|
||||
@@ -146,21 +145,21 @@ pub fn (mut self DBComments) load(mut o Comment, mut e encoder.Decoder) ! {
|
||||
}
|
||||
|
||||
@[params]
|
||||
pub struct CommentArg {
|
||||
pub struct MessageArg {
|
||||
pub mut:
|
||||
subject string
|
||||
comment string @[required]
|
||||
message string @[required]
|
||||
parent u32
|
||||
author u32
|
||||
to []u32
|
||||
cc []u32
|
||||
}
|
||||
|
||||
// get new comment, not from the DB
|
||||
pub fn (mut self DBComments) new(args CommentArg) !Comment {
|
||||
mut o := Comment{
|
||||
// get new message, not from the DB
|
||||
pub fn (mut self DBMessages) new(args MessageArg) !Message {
|
||||
mut o := Message{
|
||||
subject: args.subject
|
||||
comment: args.comment
|
||||
message: args.message
|
||||
parent: args.parent
|
||||
author: args.author
|
||||
to: args.to
|
||||
@@ -171,49 +170,49 @@ pub fn (mut self DBComments) new(args CommentArg) !Comment {
|
||||
return o
|
||||
}
|
||||
|
||||
pub fn (mut self DBComments) set(o Comment) !Comment {
|
||||
pub fn (mut self DBMessages) set(o Message) !Message {
|
||||
// Use db set function which returns the object with assigned ID
|
||||
return self.db.set[Comment](o)!
|
||||
return self.db.set[Message](o)!
|
||||
}
|
||||
|
||||
pub fn (mut self DBComments) delete(id u32) ! {
|
||||
self.db.delete[Comment](id)!
|
||||
pub fn (mut self DBMessages) delete(id u32) ! {
|
||||
self.db.delete[Message](id)!
|
||||
}
|
||||
|
||||
pub fn (mut self DBComments) exist(id u32) !bool {
|
||||
return self.db.exists[Comment](id)!
|
||||
pub fn (mut self DBMessages) exist(id u32) !bool {
|
||||
return self.db.exists[Message](id)!
|
||||
}
|
||||
|
||||
pub fn (mut self DBComments) get(id u32) !Comment {
|
||||
mut o, data := self.db.get_data[Comment](id)!
|
||||
pub fn (mut self DBMessages) get(id u32) !Message {
|
||||
mut o, data := self.db.get_data[Message](id)!
|
||||
mut e_decoder := encoder.decoder_new(data)
|
||||
self.load(mut o, mut e_decoder)!
|
||||
return o
|
||||
}
|
||||
|
||||
pub fn (mut self DBComments) list(args CommentListArg) ![]Comment {
|
||||
pub fn (mut self DBMessages) list(args MessageListArg) ![]Message {
|
||||
// Require at least one parameter to be provided
|
||||
if args.parent == 0 && args.author == 0 {
|
||||
return error('At least one filter parameter must be provided')
|
||||
}
|
||||
|
||||
// Get all comments from the database
|
||||
all_comments := self.db.list[Comment]()!.map(self.get(it)!)
|
||||
// Get all messages from the database
|
||||
all_messages := self.db.list[Message]()!.map(self.get(it)!)
|
||||
|
||||
// Apply filters
|
||||
mut filtered_comments := []Comment{}
|
||||
for comment in all_comments {
|
||||
mut filtered_messages := []Message{}
|
||||
for message in all_messages {
|
||||
// Filter by parent if provided
|
||||
if args.parent != 0 && comment.parent != args.parent {
|
||||
if args.parent != 0 && message.parent != args.parent {
|
||||
continue
|
||||
}
|
||||
|
||||
// Filter by author if provided
|
||||
if args.author != 0 && comment.author != args.author {
|
||||
if args.author != 0 && message.author != args.author {
|
||||
continue
|
||||
}
|
||||
|
||||
filtered_comments << comment
|
||||
filtered_messages << message
|
||||
}
|
||||
|
||||
// Limit results to 100 or the specified limit
|
||||
@@ -221,9 +220,9 @@ pub fn (mut self DBComments) list(args CommentListArg) ![]Comment {
|
||||
if limit > 100 {
|
||||
limit = 100
|
||||
}
|
||||
if filtered_comments.len > limit {
|
||||
return filtered_comments[..limit]
|
||||
if filtered_messages.len > limit {
|
||||
return filtered_messages[..limit]
|
||||
}
|
||||
|
||||
return filtered_comments
|
||||
return filtered_messages
|
||||
}
|
||||
291
lib/hero/heromodels/message_test.v
Normal file
291
lib/hero/heromodels/message_test.v
Normal file
@@ -0,0 +1,291 @@
|
||||
module heromodels
|
||||
|
||||
import freeflowuniverse.herolib.hero.db
|
||||
import freeflowuniverse.herolib.data.ourtime
|
||||
|
||||
fn test_message_new() ! {
|
||||
// Initialize DBMessages for testing
|
||||
mut mydb := db.new_test()!
|
||||
mut db_messages := DBMessages{
|
||||
db: &mydb
|
||||
}
|
||||
|
||||
// Test creating a new message
|
||||
mut args := MessageArg{
|
||||
subject: 'Test Subject'
|
||||
message: 'This is a test message.'
|
||||
parent: 0
|
||||
author: 1
|
||||
to: [u32(2), u32(3)]
|
||||
cc: [u32(4), u32(5)]
|
||||
}
|
||||
|
||||
message := db_messages.new(args)!
|
||||
|
||||
assert message.subject == 'Test Subject'
|
||||
assert message.message == 'This is a test message.'
|
||||
assert message.parent == 0
|
||||
assert message.author == 1
|
||||
assert message.to.len == 2
|
||||
assert message.to[0] == 2
|
||||
assert message.to[1] == 3
|
||||
assert message.cc.len == 2
|
||||
assert message.cc[0] == 4
|
||||
assert message.cc[1] == 5
|
||||
assert message.updated_at > 0
|
||||
|
||||
println('✓ Message new test passed!')
|
||||
}
|
||||
|
||||
fn test_message_crud_operations() ! {
|
||||
// Initialize DBMessages for testing
|
||||
mut mydb := db.new_test()!
|
||||
mut db_messages := DBMessages{
|
||||
db: &mydb
|
||||
}
|
||||
|
||||
// Create a new message
|
||||
mut args := MessageArg{
|
||||
subject: 'CRUD Test Subject'
|
||||
message: 'This is a test message for CRUD operations.'
|
||||
parent: 0
|
||||
author: 1
|
||||
to: [u32(2), u32(3)]
|
||||
cc: [u32(4), u32(5)]
|
||||
}
|
||||
|
||||
mut message := db_messages.new(args)!
|
||||
|
||||
// Test set operation
|
||||
message = db_messages.set(message)!
|
||||
original_id := message.id
|
||||
|
||||
// Test get operation
|
||||
retrieved_message := db_messages.get(original_id)!
|
||||
assert retrieved_message.subject == 'CRUD Test Subject'
|
||||
assert retrieved_message.message == 'This is a test message for CRUD operations.'
|
||||
assert retrieved_message.parent == 0
|
||||
assert retrieved_message.author == 1
|
||||
assert retrieved_message.id == original_id
|
||||
assert retrieved_message.to.len == 2
|
||||
assert retrieved_message.to[0] == 2
|
||||
assert retrieved_message.to[1] == 3
|
||||
assert retrieved_message.cc.len == 2
|
||||
assert retrieved_message.cc[0] == 4
|
||||
assert retrieved_message.cc[1] == 5
|
||||
|
||||
// Test exist operation
|
||||
exists := db_messages.exist(original_id)!
|
||||
assert exists == true
|
||||
|
||||
// Test update
|
||||
mut updated_args := MessageArg{
|
||||
subject: 'Updated Test Subject'
|
||||
message: 'This is an updated test message.'
|
||||
parent: 10
|
||||
author: 2
|
||||
to: [u32(6)]
|
||||
cc: [u32(7), u32(8), u32(9)]
|
||||
}
|
||||
|
||||
mut updated_message := db_messages.new(updated_args)!
|
||||
updated_message.id = original_id
|
||||
updated_message = db_messages.set(updated_message)!
|
||||
|
||||
// Verify update
|
||||
final_message := db_messages.get(original_id)!
|
||||
assert final_message.subject == 'Updated Test Subject'
|
||||
assert final_message.message == 'This is an updated test message.'
|
||||
assert final_message.parent == 10
|
||||
assert final_message.author == 2
|
||||
assert final_message.to.len == 1
|
||||
assert final_message.to[0] == 6
|
||||
assert final_message.cc.len == 3
|
||||
assert final_message.cc[0] == 7
|
||||
assert final_message.cc[1] == 8
|
||||
assert final_message.cc[2] == 9
|
||||
|
||||
// Test delete operation
|
||||
db_messages.delete(original_id)!
|
||||
|
||||
// Verify deletion
|
||||
exists_after_delete := db_messages.exist(original_id)!
|
||||
assert exists_after_delete == false
|
||||
|
||||
println('✓ Message CRUD operations test passed!')
|
||||
}
|
||||
|
||||
fn test_message_encoding_decoding() ! {
|
||||
// Initialize DBMessages for testing
|
||||
mut mydb := db.new_test()!
|
||||
mut db_messages := DBMessages{
|
||||
db: &mydb
|
||||
}
|
||||
|
||||
// Create a new message with all fields populated
|
||||
mut args := MessageArg{
|
||||
subject: 'Encoding Test Subject'
|
||||
message: 'This is a test message for encoding/decoding.'
|
||||
parent: 5
|
||||
author: 10
|
||||
to: [u32(20), u32(30), u32(40)]
|
||||
cc: [u32(50), u32(60)]
|
||||
}
|
||||
|
||||
mut message := db_messages.new(args)!
|
||||
|
||||
// Add send_log data manually
|
||||
mut send_log1 := SendLog{
|
||||
to: [u32(100), u32(101)]
|
||||
cc: [u32(102)]
|
||||
status: .sent
|
||||
timestamp: 1678886400 // Example timestamp
|
||||
}
|
||||
mut send_log2 := SendLog{
|
||||
to: [u32(200)]
|
||||
cc: []u32{}
|
||||
status: .received
|
||||
timestamp: 1678886500 // Example timestamp
|
||||
}
|
||||
message.send_log = [send_log1, send_log2]
|
||||
|
||||
// Save the message
|
||||
message = db_messages.set(message)!
|
||||
message_id := message.id
|
||||
|
||||
// Retrieve and verify all fields were properly encoded/decoded
|
||||
retrieved_message := db_messages.get(message_id)!
|
||||
|
||||
assert retrieved_message.subject == 'Encoding Test Subject'
|
||||
assert retrieved_message.message == 'This is a test message for encoding/decoding.'
|
||||
assert retrieved_message.parent == 5
|
||||
assert retrieved_message.author == 10
|
||||
assert retrieved_message.to.len == 3
|
||||
assert retrieved_message.to[0] == 20
|
||||
assert retrieved_message.to[1] == 30
|
||||
assert retrieved_message.to[2] == 40
|
||||
assert retrieved_message.cc.len == 2
|
||||
assert retrieved_message.cc[0] == 50
|
||||
assert retrieved_message.cc[1] == 60
|
||||
|
||||
// Verify send_log
|
||||
assert retrieved_message.send_log.len == 2
|
||||
assert retrieved_message.send_log[0].to.len == 2
|
||||
assert retrieved_message.send_log[0].to[0] == 100
|
||||
assert retrieved_message.send_log[0].to[1] == 101
|
||||
assert retrieved_message.send_log[0].cc.len == 1
|
||||
assert retrieved_message.send_log[0].cc[0] == 102
|
||||
assert retrieved_message.send_log[0].status == .sent
|
||||
assert retrieved_message.send_log[0].timestamp == 1678886400
|
||||
|
||||
assert retrieved_message.send_log[1].to.len == 1
|
||||
assert retrieved_message.send_log[1].to[0] == 200
|
||||
assert retrieved_message.send_log[1].cc.len == 0
|
||||
assert retrieved_message.send_log[1].status == .received
|
||||
assert retrieved_message.send_log[1].timestamp == 1678886500
|
||||
|
||||
println('✓ Message encoding/decoding test passed!')
|
||||
}
|
||||
|
||||
fn test_message_type_name() ! {
|
||||
// Initialize DBMessages for testing
|
||||
mut mydb := db.new_test()!
|
||||
mut db_messages := DBMessages{
|
||||
db: &mydb
|
||||
}
|
||||
|
||||
// Create a new message
|
||||
mut args := MessageArg{
|
||||
subject: 'Type Name Test Subject'
|
||||
message: 'This is a test message for type name.'
|
||||
parent: 0
|
||||
author: 1
|
||||
to: []u32{}
|
||||
cc: []u32{}
|
||||
}
|
||||
|
||||
message := db_messages.new(args)!
|
||||
|
||||
// Test type_name method
|
||||
type_name := message.type_name()
|
||||
assert type_name == 'messages'
|
||||
|
||||
println('✓ Message type_name test passed!')
|
||||
}
|
||||
|
||||
fn test_message_description() ! {
|
||||
// Initialize DBMessages for testing
|
||||
mut mydb := db.new_test()!
|
||||
mut db_messages := DBMessages{
|
||||
db: &mydb
|
||||
}
|
||||
|
||||
// Create a new message
|
||||
mut args := MessageArg{
|
||||
subject: 'Description Test Subject'
|
||||
message: 'This is a test message for description method.'
|
||||
parent: 0
|
||||
author: 1
|
||||
to: []u32{}
|
||||
cc: []u32{}
|
||||
}
|
||||
|
||||
message := db_messages.new(args)!
|
||||
|
||||
// Test description method for each methodname
|
||||
assert message.description('set') == 'Create or update a message. Returns the ID of the message.'
|
||||
assert message.description('get') == 'Retrieve a message by ID. Returns the message object.'
|
||||
assert message.description('delete') == 'Delete a message by ID. Returns true if successful.'
|
||||
assert message.description('exist') == 'Check if a message exists by ID. Returns true or false.'
|
||||
assert message.description('list') == 'List all messages. Returns an array of message objects.'
|
||||
assert message.description('unknown') == 'This is generic method for the root object, TODO fill in, ...'
|
||||
|
||||
println('✓ Message description test passed!')
|
||||
}
|
||||
|
||||
fn test_message_example() ! {
|
||||
// Initialize DBMessages for testing
|
||||
mut mydb := db.new_test()!
|
||||
mut db_messages := DBMessages{
|
||||
db: &mydb
|
||||
}
|
||||
|
||||
// Create a new message
|
||||
mut args := MessageArg{
|
||||
subject: 'Example Test Subject'
|
||||
message: 'This is a test message for example method.'
|
||||
parent: 0
|
||||
author: 1
|
||||
to: []u32{}
|
||||
cc: []u32{}
|
||||
}
|
||||
|
||||
message := db_messages.new(args)!
|
||||
|
||||
// Test example method for each methodname
|
||||
set_call, set_result := message.example('set')
|
||||
assert set_call == '{"message": {"message": "This is a test message.", "parent": 0, "author": 1}}'
|
||||
assert set_result == '1'
|
||||
|
||||
get_call, get_result := message.example('get')
|
||||
assert get_call == '{"id": 1}'
|
||||
assert get_result == '{"message": "This is a test message.", "parent": 0, "author": 1}'
|
||||
|
||||
delete_call, delete_result := message.example('delete')
|
||||
assert delete_call == '{"id": 1}'
|
||||
assert delete_result == 'true'
|
||||
|
||||
exist_call, exist_result := message.example('exist')
|
||||
assert exist_call == '{"id": 1}'
|
||||
assert exist_result == 'true'
|
||||
|
||||
list_call, list_result := message.example('list')
|
||||
assert list_call == '{}'
|
||||
assert list_result == '[{"message": "This is a test message.", "parent": 0, "author": 1}]'
|
||||
|
||||
unknown_call, unknown_result := message.example('unknown')
|
||||
assert unknown_call == '{}'
|
||||
assert unknown_result == '{}'
|
||||
|
||||
println('✓ Message example test passed!')
|
||||
}
|
||||
@@ -197,7 +197,7 @@ pub mut:
|
||||
attendees_optional []u32
|
||||
securitypolicy u32
|
||||
tags []string
|
||||
comments []db.CommentArg
|
||||
messages []db.MessageArg
|
||||
}
|
||||
|
||||
// get new calendar, not from the DB
|
||||
@@ -219,7 +219,7 @@ pub fn (mut self DBPlanning) new(args PlanningArg) !Planning {
|
||||
o.description = args.description
|
||||
o.securitypolicy = args.securitypolicy
|
||||
o.tags = self.db.tags_get(args.tags)!
|
||||
o.comments = self.db.comments_get(args.comments)!
|
||||
o.messages = self.db.messages_get(args.messages)!
|
||||
o.updated_at = ourtime.now().unix()
|
||||
|
||||
return o
|
||||
|
||||
@@ -25,7 +25,7 @@ fn test_planning_new() ! {
|
||||
attendees_optional: []u32{}
|
||||
securitypolicy: 0
|
||||
tags: []string{}
|
||||
comments: []db.CommentArg{}
|
||||
messages: []db.MessageArg{}
|
||||
}
|
||||
|
||||
planning := db_planning.new(args)!
|
||||
@@ -68,7 +68,7 @@ fn test_planning_crud_operations() ! {
|
||||
attendees_optional: [200]
|
||||
securitypolicy: 0
|
||||
tags: []string{}
|
||||
comments: []db.CommentArg{}
|
||||
messages: []db.MessageArg{}
|
||||
}
|
||||
|
||||
mut planning := db_planning.new(args)!
|
||||
@@ -153,7 +153,7 @@ fn test_planning_crud_operations() ! {
|
||||
attendees_optional: []u32{}
|
||||
securitypolicy: 0
|
||||
tags: []string{}
|
||||
comments: []db.CommentArg{}
|
||||
messages: []db.MessageArg{}
|
||||
}
|
||||
|
||||
mut updated_planning := db_planning.new(updated_args)!
|
||||
@@ -249,7 +249,7 @@ fn test_planning_recurrence_rules_encoding_decoding() ! {
|
||||
attendees_optional: []u32{}
|
||||
securitypolicy: 0
|
||||
tags: []string{}
|
||||
comments: []db.CommentArg{}
|
||||
messages: []db.MessageArg{}
|
||||
}
|
||||
|
||||
mut planning := db_planning.new(args)!
|
||||
|
||||
@@ -191,7 +191,7 @@ pub mut:
|
||||
end_date string // Use ourtime module to convert to epoch
|
||||
securitypolicy u32
|
||||
tags []string
|
||||
comments []db.CommentArg
|
||||
messages []db.MessageArg
|
||||
}
|
||||
|
||||
// get new project, not from the DB
|
||||
@@ -208,7 +208,7 @@ pub fn (mut self DBProject) new(args ProjectArg) !Project {
|
||||
o.description = args.description
|
||||
o.securitypolicy = args.securitypolicy
|
||||
o.tags = self.db.tags_get(args.tags)!
|
||||
o.comments = self.db.comments_get(args.comments)!
|
||||
o.messages = self.db.messages_get(args.messages)!
|
||||
o.updated_at = ourtime.now().unix()
|
||||
|
||||
// Convert string dates to Unix timestamps
|
||||
|
||||
@@ -176,7 +176,7 @@ pub mut:
|
||||
children []u32
|
||||
securitypolicy u32
|
||||
tags []string
|
||||
comments []db.CommentArg
|
||||
messages []db.MessageArg
|
||||
}
|
||||
|
||||
// get new project issue, not from the DB
|
||||
@@ -237,7 +237,7 @@ pub fn (mut self DBProjectIssue) new(args ProjectIssueArg) !ProjectIssue {
|
||||
o.description = args.description
|
||||
o.securitypolicy = args.securitypolicy
|
||||
o.tags = self.db.tags_get(args.tags)!
|
||||
o.comments = self.db.comments_get(args.comments)!
|
||||
o.messages = self.db.messages_get(args.messages)!
|
||||
o.updated_at = ourtime.now().unix()
|
||||
|
||||
// Convert deadline string to Unix timestamp
|
||||
|
||||
@@ -8,9 +8,9 @@ curl -s http://localhost:9933/ \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{
|
||||
"jsonrpc": "2.0",
|
||||
"method": "comment_set",
|
||||
"method": "message_set",
|
||||
"params": {
|
||||
"comment": "Hello world!",
|
||||
"message": "Hello world!",
|
||||
"parent": 0,
|
||||
"author": 42
|
||||
},
|
||||
@@ -47,7 +47,7 @@ nc -U /tmp/heromodels
|
||||
|
||||
# then e.g. paste following in
|
||||
|
||||
{"jsonrpc":"2.0","method":"comment_set","params":{"comment":"Hello world!","parent":0,"author":42},"id":1}
|
||||
{"jsonrpc":"2.0","method":"message_set","params":{"message":"Hello world!","parent":0,"author":42},"id":1}
|
||||
|
||||
needs to be on one line for openrpc to work
|
||||
|
||||
|
||||
@@ -187,7 +187,7 @@ pub mut:
|
||||
acceptance_required bool // if set then admins need to approve
|
||||
securitypolicy u32
|
||||
tags []string
|
||||
comments []db.CommentArg
|
||||
messages []db.MessageArg
|
||||
}
|
||||
|
||||
pub fn (mut self DBRegistrationDesk) new(args RegistrationDeskArg) !RegistrationDesk {
|
||||
@@ -215,7 +215,7 @@ pub fn (mut self DBRegistrationDesk) new(args RegistrationDeskArg) !Registration
|
||||
// Set base fields
|
||||
o.securitypolicy = args.securitypolicy
|
||||
o.tags = self.db.tags_get(args.tags)!
|
||||
o.comments = self.db.comments_get(args.comments)!
|
||||
o.messages = self.db.messages_get(args.messages)!
|
||||
o.updated_at = ourtime.now().unix()
|
||||
|
||||
// Convert string times to Unix timestamps
|
||||
|
||||
@@ -23,7 +23,7 @@ fn test_registration_desk_new() ! {
|
||||
acceptance_required: true
|
||||
securitypolicy: 0
|
||||
tags: ["test", "registration"]
|
||||
comments: []
|
||||
messages: []
|
||||
}
|
||||
|
||||
registration_desk := db_registration_desk.new(args)!
|
||||
@@ -66,7 +66,7 @@ fn test_registration_desk_crud_operations() ! {
|
||||
acceptance_required: false
|
||||
securitypolicy: 0
|
||||
tags: ["crud", "test"]
|
||||
comments: []
|
||||
messages: []
|
||||
}
|
||||
|
||||
mut registration_desk := db_registration_desk.new(args)!
|
||||
@@ -109,7 +109,7 @@ fn test_registration_desk_crud_operations() ! {
|
||||
acceptance_required: true
|
||||
securitypolicy: 0
|
||||
tags: ["updated", "test"]
|
||||
comments: []
|
||||
messages: []
|
||||
}
|
||||
|
||||
mut updated_desk := db_registration_desk.new(updated_args)!
|
||||
@@ -160,7 +160,7 @@ fn test_registration_desk_registrations_encoding_decoding() ! {
|
||||
acceptance_required: true
|
||||
securitypolicy: 0
|
||||
tags: []
|
||||
comments: []
|
||||
messages: []
|
||||
}
|
||||
|
||||
mut registration_desk := db_registration_desk.new(args)!
|
||||
@@ -230,7 +230,7 @@ fn test_registration_desk_type_name() ! {
|
||||
acceptance_required: false
|
||||
securitypolicy: 0
|
||||
tags: []
|
||||
comments: []
|
||||
messages: []
|
||||
}
|
||||
|
||||
registration_desk := db_registration_desk.new(args)!
|
||||
@@ -262,7 +262,7 @@ fn test_registration_desk_description() ! {
|
||||
acceptance_required: false
|
||||
securitypolicy: 0
|
||||
tags: []
|
||||
comments: []
|
||||
messages: []
|
||||
}
|
||||
|
||||
registration_desk := db_registration_desk.new(args)!
|
||||
@@ -298,7 +298,7 @@ fn test_registration_desk_example() ! {
|
||||
acceptance_required: false
|
||||
securitypolicy: 0
|
||||
tags: []
|
||||
comments: []
|
||||
messages: []
|
||||
}
|
||||
|
||||
registration_desk := db_registration_desk.new(args)!
|
||||
@@ -355,7 +355,7 @@ fn test_registration_desk_list() ! {
|
||||
acceptance_required: false
|
||||
securitypolicy: 0
|
||||
tags: []
|
||||
comments: []
|
||||
messages: []
|
||||
}
|
||||
|
||||
mut args2 := RegistrationDeskArg{
|
||||
@@ -370,7 +370,7 @@ fn test_registration_desk_list() ! {
|
||||
acceptance_required: true
|
||||
securitypolicy: 0
|
||||
tags: []
|
||||
comments: []
|
||||
messages: []
|
||||
}
|
||||
|
||||
mut desk1 := db_registration_desk.new(args1)!
|
||||
@@ -426,7 +426,7 @@ fn test_registration_desk_fs_items_encoding_decoding() ! {
|
||||
acceptance_required: false
|
||||
securitypolicy: 0
|
||||
tags: []
|
||||
comments: []
|
||||
messages: []
|
||||
}
|
||||
|
||||
mut registration_desk := db_registration_desk.new(args)!
|
||||
|
||||
@@ -15,10 +15,10 @@ pub mut:
|
||||
pub fn start(args ServerArgs) ! {
|
||||
mut openrpc_handler := openrpc.new_handler(openrpc_path)!
|
||||
|
||||
openrpc_handler.register_procedure_handle('comment_get', comment_get)
|
||||
openrpc_handler.register_procedure_handle('comment_set', comment_set)
|
||||
openrpc_handler.register_procedure_handle('comment_delete', comment_delete)
|
||||
openrpc_handler.register_procedure_handle('comment_list', comment_list)
|
||||
openrpc_handler.register_procedure_handle('message_get', message_get)
|
||||
openrpc_handler.register_procedure_handle('message_set', message_set)
|
||||
openrpc_handler.register_procedure_handle('message_delete', message_delete)
|
||||
openrpc_handler.register_procedure_handle('message_list', message_list)
|
||||
|
||||
openrpc_handler.register_procedure_handle('calendar_get', calendar_get)
|
||||
openrpc_handler.register_procedure_handle('calendar_set', calendar_set)
|
||||
|
||||
@@ -6,12 +6,12 @@
|
||||
},
|
||||
"methods": [
|
||||
{
|
||||
"name": "comment_get",
|
||||
"summary": "Get a comment by ID",
|
||||
"name": "message_get",
|
||||
"summary": "Get a message by ID",
|
||||
"params": [
|
||||
{
|
||||
"name": "id",
|
||||
"description": "ID of comment to fetch",
|
||||
"description": "ID of message to fetch",
|
||||
"required": true,
|
||||
"schema": {
|
||||
"type": "integer",
|
||||
@@ -20,20 +20,20 @@
|
||||
}
|
||||
],
|
||||
"result": {
|
||||
"name": "comment",
|
||||
"description": "Comment object",
|
||||
"name": "message",
|
||||
"description": "Message object",
|
||||
"schema": {
|
||||
"$ref": "#/components/schemas/Comment"
|
||||
"$ref": "#/components/schemas/Message"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "comment_set",
|
||||
"summary": "Create or update a comment",
|
||||
"name": "message_set",
|
||||
"summary": "Create or update a message",
|
||||
"params": [
|
||||
{
|
||||
"name": "comment",
|
||||
"description": "Comment text",
|
||||
"name": "message",
|
||||
"description": "Message text",
|
||||
"required": true,
|
||||
"schema": {
|
||||
"type": "string"
|
||||
@@ -41,7 +41,7 @@
|
||||
},
|
||||
{
|
||||
"name": "parent",
|
||||
"description": "ID of parent comment if any, 0 means none",
|
||||
"description": "ID of parent message if any, 0 means none",
|
||||
"schema": {
|
||||
"type": "integer",
|
||||
"minimum": 0
|
||||
@@ -58,7 +58,7 @@
|
||||
],
|
||||
"result": {
|
||||
"name": "id",
|
||||
"description": "ID of the created/updated comment",
|
||||
"description": "ID of the created/updated message",
|
||||
"schema": {
|
||||
"type": "integer",
|
||||
"minimum": 0
|
||||
@@ -66,12 +66,12 @@
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "comment_delete",
|
||||
"summary": "Delete a comment by ID",
|
||||
"name": "message_delete",
|
||||
"summary": "Delete a message by ID",
|
||||
"params": [
|
||||
{
|
||||
"name": "id",
|
||||
"description": "ID of comment to delete",
|
||||
"description": "ID of message to delete",
|
||||
"required": true,
|
||||
"schema": {
|
||||
"type": "integer",
|
||||
@@ -88,16 +88,16 @@
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "comment_list",
|
||||
"summary": "List all comments",
|
||||
"name": "message_list",
|
||||
"summary": "List all messages",
|
||||
"params": [],
|
||||
"result": {
|
||||
"name": "comments",
|
||||
"description": "List of all comment objects",
|
||||
"name": "messages",
|
||||
"description": "List of all message objects",
|
||||
"schema": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"$ref": "#/components/schemas/Comment"
|
||||
"$ref": "#/components/schemas/Message"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -400,12 +400,12 @@
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "comments",
|
||||
"description": "Comments for the calendar event",
|
||||
"name": "messages",
|
||||
"description": "Messages for the calendar event",
|
||||
"schema": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"$ref": "#/components/schemas/CommentArg"
|
||||
"$ref": "#/components/schemas/MessageArg"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -542,12 +542,12 @@
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "comments",
|
||||
"description": "Comments for the chat group",
|
||||
"name": "messages",
|
||||
"description": "Messages for the chat group",
|
||||
"schema": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"$ref": "#/components/schemas/CommentArg"
|
||||
"$ref": "#/components/schemas/MessageArg"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -752,12 +752,12 @@
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "comments",
|
||||
"description": "Comments for the chat message",
|
||||
"name": "messages",
|
||||
"description": "Messages for the chat message",
|
||||
"schema": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"$ref": "#/components/schemas/CommentArg"
|
||||
"$ref": "#/components/schemas/MessageArg"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1128,12 +1128,12 @@
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "comments",
|
||||
"description": "Comments for the project issue",
|
||||
"name": "messages",
|
||||
"description": "Messages for the project issue",
|
||||
"schema": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"$ref": "#/components/schemas/CommentArg"
|
||||
"$ref": "#/components/schemas/MessageArg"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1312,12 +1312,12 @@
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "comments",
|
||||
"description": "Comments for the project",
|
||||
"name": "messages",
|
||||
"description": "Messages for the project",
|
||||
"schema": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"$ref": "#/components/schemas/CommentArg"
|
||||
"$ref": "#/components/schemas/MessageArg"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1488,8 +1488,8 @@
|
||||
}
|
||||
},
|
||||
{
|
||||
"name": "comments",
|
||||
"description": "Comments for the user",
|
||||
"name": "messages",
|
||||
"description": "Messages for the user",
|
||||
"schema": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
@@ -1575,7 +1575,7 @@
|
||||
"type": "integer",
|
||||
"minimum": 0
|
||||
},
|
||||
"comments": {
|
||||
"messages": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "integer",
|
||||
@@ -1584,16 +1584,16 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
"Comment": {
|
||||
"title": "Comment",
|
||||
"description": "A comment object",
|
||||
"Message": {
|
||||
"title": "Message",
|
||||
"description": "A message object",
|
||||
"allOf": [
|
||||
{
|
||||
"$ref": "#/components/schemas/Base"
|
||||
}
|
||||
],
|
||||
"properties": {
|
||||
"comment": {
|
||||
"message": {
|
||||
"type": "string"
|
||||
},
|
||||
"parent": {
|
||||
@@ -2166,12 +2166,12 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
"CommentArg": {
|
||||
"title": "CommentArg",
|
||||
"description": "A comment argument",
|
||||
"MessageArg": {
|
||||
"title": "MessageArg",
|
||||
"description": "A message argument",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"comment": {
|
||||
"message": {
|
||||
"type": "string"
|
||||
},
|
||||
"parent": {
|
||||
|
||||
@@ -33,7 +33,7 @@ pub mut:
|
||||
timezone string
|
||||
securitypolicy u32
|
||||
tags []string
|
||||
comments []db.CommentArg
|
||||
messages []db.MessageArg
|
||||
}
|
||||
|
||||
@[params]
|
||||
@@ -78,7 +78,7 @@ pub fn calendar_event_set(request Request) !Response {
|
||||
timezone: payload.timezone
|
||||
securitypolicy: payload.securitypolicy
|
||||
tags: payload.tags
|
||||
comments: payload.comments
|
||||
messages: payload.messages
|
||||
)!
|
||||
|
||||
calendar_event_obj = mydb.calendar_event.set(calendar_event_obj)!
|
||||
|
||||
@@ -22,7 +22,7 @@ pub mut:
|
||||
is_archived bool
|
||||
securitypolicy u32
|
||||
tags []string
|
||||
comments []db.CommentArg
|
||||
messages []db.MessageArg
|
||||
}
|
||||
|
||||
@[params]
|
||||
@@ -56,7 +56,7 @@ pub fn chat_group_set(request Request) !Response {
|
||||
is_archived: payload.is_archived
|
||||
securitypolicy: payload.securitypolicy
|
||||
tags: payload.tags
|
||||
comments: payload.comments
|
||||
messages: payload.messages
|
||||
)!
|
||||
|
||||
chat_group_obj=mydb.chat_group.set( chat_group_obj)!
|
||||
|
||||
@@ -28,7 +28,7 @@ pub mut:
|
||||
mentions []u32
|
||||
securitypolicy u32
|
||||
tags []string
|
||||
comments []db.CommentArg
|
||||
messages []db.MessageArg
|
||||
}
|
||||
|
||||
@[params]
|
||||
@@ -68,7 +68,7 @@ pub fn chat_message_set(request Request) !Response {
|
||||
mentions: payload.mentions
|
||||
securitypolicy: payload.securitypolicy
|
||||
tags: payload.tags
|
||||
comments: payload.comments
|
||||
messages: payload.messages
|
||||
)!
|
||||
|
||||
chat_message_obj=mydb.chat_message.set( chat_message_obj)!
|
||||
|
||||
@@ -4,69 +4,69 @@ import json
|
||||
import freeflowuniverse.herolib.schemas.jsonrpc { Request, Response, new_response_true, new_response_u32 }
|
||||
import freeflowuniverse.herolib.hero.heromodels
|
||||
|
||||
// Comment-specific argument structures
|
||||
// Message-specific argument structures
|
||||
@[params]
|
||||
pub struct CommentGetArgs {
|
||||
pub struct MessageGetArgs {
|
||||
pub mut:
|
||||
id u32 @[required]
|
||||
}
|
||||
|
||||
@[params]
|
||||
pub struct CommentSetArgs {
|
||||
pub struct MessageSetArgs {
|
||||
pub mut:
|
||||
comment string @[required]
|
||||
message string @[required]
|
||||
parent u32
|
||||
author u32
|
||||
}
|
||||
|
||||
@[params]
|
||||
pub struct CommentDeleteArgs {
|
||||
pub struct MessageDeleteArgs {
|
||||
pub mut:
|
||||
id u32 @[required]
|
||||
}
|
||||
|
||||
pub fn comment_get(request Request) !Response {
|
||||
payload := jsonrpc.decode_payload[CommentGetArgs](request.params) or {
|
||||
pub fn message_get(request Request) !Response {
|
||||
payload := jsonrpc.decode_payload[MessageGetArgs](request.params) or {
|
||||
return jsonrpc.invalid_params
|
||||
}
|
||||
|
||||
mut mydb := heromodels.new()!
|
||||
comment := mydb.comments.get(payload.id)!
|
||||
message := mydb.messages.get(payload.id)!
|
||||
|
||||
return jsonrpc.new_response(request.id, json.encode(comment))
|
||||
return jsonrpc.new_response(request.id, json.encode(message))
|
||||
}
|
||||
|
||||
pub fn comment_set(request Request) !Response {
|
||||
payload := jsonrpc.decode_payload[CommentSetArgs](request.params) or {
|
||||
pub fn message_set(request Request) !Response {
|
||||
payload := jsonrpc.decode_payload[MessageSetArgs](request.params) or {
|
||||
return jsonrpc.invalid_params
|
||||
}
|
||||
|
||||
mut mydb := heromodels.new()!
|
||||
mut comment_obj := mydb.comments.new(
|
||||
comment: payload.comment
|
||||
mut message_obj := mydb.messages.new(
|
||||
message: payload.message
|
||||
parent: payload.parent
|
||||
author: payload.author
|
||||
)!
|
||||
|
||||
comment_obj=mydb.comments.set( comment_obj)!
|
||||
message_obj=mydb.messages.set( message_obj)!
|
||||
|
||||
return new_response_u32(request.id, comment_obj.id)
|
||||
return new_response_u32(request.id, message_obj.id)
|
||||
}
|
||||
|
||||
pub fn comment_delete(request Request) !Response {
|
||||
payload := jsonrpc.decode_payload[CommentDeleteArgs](request.params) or {
|
||||
pub fn message_delete(request Request) !Response {
|
||||
payload := jsonrpc.decode_payload[MessageDeleteArgs](request.params) or {
|
||||
return jsonrpc.invalid_params
|
||||
}
|
||||
|
||||
mut mydb := heromodels.new()!
|
||||
mydb.comments.delete(payload.id)!
|
||||
mydb.messages.delete(payload.id)!
|
||||
|
||||
return new_response_true(request.id) // return true as jsonrpc (bool)
|
||||
}
|
||||
|
||||
pub fn comment_list(request Request) !Response {
|
||||
pub fn message_list(request Request) !Response {
|
||||
mut mydb := heromodels.new()!
|
||||
comments := mydb.comments.list()!
|
||||
messages := mydb.messages.list()!
|
||||
|
||||
return jsonrpc.new_response(request.id, json.encode(comments))
|
||||
return jsonrpc.new_response(request.id, json.encode(messages))
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ pub mut:
|
||||
end_date string // Use ourtime module to convert to epoch
|
||||
securitypolicy u32
|
||||
tags []string
|
||||
comments []db.CommentArg
|
||||
messages []db.MessageArg
|
||||
}
|
||||
|
||||
@[params]
|
||||
@@ -64,7 +64,7 @@ pub fn project_set(request Request) !Response {
|
||||
end_date: payload.end_date
|
||||
securitypolicy: payload.securitypolicy
|
||||
tags: payload.tags
|
||||
comments: payload.comments
|
||||
messages: payload.messages
|
||||
)!
|
||||
|
||||
project_obj = mydb.project.set(project_obj)!
|
||||
|
||||
@@ -33,7 +33,7 @@ pub mut:
|
||||
children []u32
|
||||
securitypolicy u32
|
||||
tags []string
|
||||
comments []db.CommentArg
|
||||
messages []db.MessageArg
|
||||
}
|
||||
|
||||
@[params]
|
||||
@@ -78,7 +78,7 @@ pub fn project_issue_set(request Request) !Response {
|
||||
children: payload.children
|
||||
securitypolicy: payload.securitypolicy
|
||||
tags: payload.tags
|
||||
comments: payload.comments
|
||||
messages: payload.messages
|
||||
)!
|
||||
|
||||
project_issue_obj=mydb.project_issue.set( project_issue_obj)!
|
||||
|
||||
@@ -26,7 +26,7 @@ pub mut:
|
||||
status heromodels.UserStatus
|
||||
securitypolicy u32
|
||||
tags u32
|
||||
comments []u32
|
||||
messages []u32
|
||||
}
|
||||
|
||||
@[params]
|
||||
@@ -65,7 +65,7 @@ pub fn user_set(request Request) !Response {
|
||||
status: payload.status
|
||||
securitypolicy: payload.securitypolicy
|
||||
tags: payload.tags
|
||||
comments: payload.comments
|
||||
messages: payload.messages
|
||||
)!
|
||||
|
||||
user_obj=mydb.user.set( user_obj)!
|
||||
|
||||
@@ -115,7 +115,7 @@ pub mut:
|
||||
status UserStatus
|
||||
securitypolicy u32
|
||||
tags u32
|
||||
comments []u32
|
||||
messages []u32
|
||||
}
|
||||
|
||||
pub struct DBUser {
|
||||
@@ -148,7 +148,7 @@ pub fn (mut self DBUser) new(args UserArg) !User {
|
||||
o.description = args.description
|
||||
o.securitypolicy = args.securitypolicy
|
||||
o.tags = args.tags
|
||||
o.comments = args.comments
|
||||
o.messages = args.messages
|
||||
o.updated_at = ourtime.now().unix()
|
||||
|
||||
return o
|
||||
|
||||
Reference in New Issue
Block a user