1
1
from datetime import datetime
2
- from typing import Any , Dict , Iterable , Optional , Union
2
+ from typing import Any , Dict , Iterable , Optional , Union , List
3
3
4
- from aleph_message .models import PostMessage
5
- from peewee import CharField , DateTimeField , Model
4
+ from aleph_message .models import PostMessage , MessageConfirmation
5
+ from peewee import CharField , Model , BooleanField , IntegerField , FloatField
6
6
from playhouse .shortcuts import model_to_dict
7
7
from playhouse .sqlite_ext import JSONField
8
8
9
9
from aleph .sdk .models import Post
10
- from aleph .sdk .node .common import db , pydantic_json_dumps
10
+ from aleph .sdk .node .common import db , pydantic_json_dumps , PydanticField
11
11
12
12
13
13
class PostModel (Model ):
@@ -16,56 +16,84 @@ class PostModel(Model):
16
16
"""
17
17
18
18
original_item_hash = CharField (primary_key = True )
19
- item_hash = CharField ()
20
- content = JSONField (json_dumps = pydantic_json_dumps )
21
19
original_type = CharField ()
22
- address = CharField ()
23
- ref = CharField (null = True )
20
+ original_signature = CharField ()
21
+ item_hash = CharField ()
22
+ chain = CharField (5 )
23
+ type = CharField ()
24
+ sender = CharField ()
24
25
channel = CharField (null = True )
25
- created = DateTimeField ()
26
- last_updated = DateTimeField ()
26
+ confirmations : PydanticField [MessageConfirmation ] = PydanticField (
27
+ type = MessageConfirmation , null = True
28
+ )
29
+ confirmed = BooleanField ()
30
+ signature = CharField ()
31
+ size = IntegerField (null = True )
32
+ time = FloatField ()
33
+ item_type = CharField (7 )
34
+ item_content = CharField (null = True )
35
+ content = JSONField (json_dumps = pydantic_json_dumps )
27
36
tags = JSONField (json_dumps = pydantic_json_dumps , null = True )
28
- chain = CharField (5 )
37
+ key = CharField (null = True )
38
+ ref = CharField (null = True )
39
+ content_type = CharField (null = True )
29
40
30
41
class Meta :
31
42
database = db
32
43
33
44
34
45
def post_to_model (post : Post ) -> Dict :
35
46
return {
36
- "item_hash" : str (post .item_hash ),
37
- "content" : post .content ,
47
+ "item_hash" : str (post .original_item_hash ),
48
+ "chain" : post .chain .value ,
49
+ "type" : post .type ,
50
+ "sender" : post .sender ,
51
+ "channel" : post .channel ,
52
+ "confirmations" : post .confirmations [0 ] if post .confirmations else None ,
53
+ "confirmed" : post .confirmed ,
54
+ "signature" : post .signature ,
55
+ "size" : post .size ,
56
+ "time" : post .time ,
38
57
"original_item_hash" : str (post .original_item_hash ),
39
- "original_type" : post .original_type ,
40
- "address" : post .address ,
58
+ "original_type" : post .original_type if post .original_type else post .type ,
59
+ "original_signature" : post .original_signature if post .original_signature else post .signature ,
60
+ "item_type" : post .item_type ,
61
+ "item_content" : post .item_content ,
62
+ "content" : post .content ,
63
+ "tags" : post .content .content .get ("tags" , None ) if hasattr (post .content , "content" ) else None ,
41
64
"ref" : post .ref ,
42
- "channel" : post .channel ,
43
- "created" : post .created ,
44
- "last_updated" : post .last_updated ,
45
65
}
46
66
47
67
48
68
def message_to_post (message : PostMessage ) -> Post :
49
- return Post .parse_obj (
50
- {
51
- "item_hash" : str (message .item_hash ),
52
- "content" : message .content ,
53
- "original_item_hash" : str (message .item_hash ),
54
- "original_type" : message .content .type
55
- if hasattr (message .content , "type" )
56
- else None ,
57
- "address" : message .sender ,
58
- "ref" : message .content .ref if hasattr (message .content , "ref" ) else None ,
59
- "channel" : message .channel ,
60
- "created" : datetime .fromtimestamp (message .time ),
61
- "last_updated" : datetime .fromtimestamp (message .time ),
62
- }
69
+ return Post (
70
+ chain = message .chain ,
71
+ item_hash = message .item_hash ,
72
+ sender = message .sender ,
73
+ type = message .content .type ,
74
+ channel = message .channel ,
75
+ confirmed = message .confirmed if message .confirmed else False ,
76
+ confirmations = message .confirmations if message .confirmations else [],
77
+ content = message .content ,
78
+ item_content = message .item_content ,
79
+ item_type = message .item_type ,
80
+ signature = message .signature ,
81
+ size = message .size if message .size else len (message .content .json ().encode ()),
82
+ time = message .time ,
83
+ original_item_hash = message .item_hash ,
84
+ original_signature = message .signature ,
85
+ original_type = message .content .type ,
86
+ hash = message .item_hash ,
87
+ ref = message .content .ref ,
63
88
)
64
89
65
90
66
91
def model_to_post (item : Any ) -> Post :
67
- to_exclude = [PostModel .tags , PostModel .chain ]
68
- return Post .parse_obj (model_to_dict (item , exclude = to_exclude ))
92
+ to_exclude = [PostModel .tags ]
93
+ model_dict = model_to_dict (item , exclude = to_exclude )
94
+ model_dict ["confirmations" ] = [model_dict ["confirmations" ]] if model_dict ["confirmations" ] else []
95
+ model_dict ["hash" ] = model_dict ["item_hash" ]
96
+ return Post .parse_obj (model_dict )
69
97
70
98
71
99
def query_field (field_name , field_values : Iterable [str ]):
@@ -88,14 +116,14 @@ def get_post_query(
88
116
start_date : Optional [Union [datetime , float ]] = None ,
89
117
end_date : Optional [Union [datetime , float ]] = None ,
90
118
):
91
- query = PostModel .select ().order_by (PostModel .created .desc ())
119
+ query = PostModel .select ().order_by (PostModel .time .desc ())
92
120
conditions = []
93
121
if types :
94
122
conditions .append (query_field ("original_type" , types ))
95
123
if refs :
96
124
conditions .append (query_field ("ref" , refs ))
97
125
if addresses :
98
- conditions .append (query_field ("address " , addresses ))
126
+ conditions .append (query_field ("sender " , addresses ))
99
127
if tags :
100
128
for tag in tags :
101
129
conditions .append (PostModel .tags .contains (tag ))
0 commit comments