forked from paulpierre/informer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
152 lines (131 loc) · 6.78 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
from sqlalchemy import Boolean, Column, Integer, String, DateTime, ForeignKey, Table
from sqlalchemy.orm import relationship
from datetime import datetime
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
class Account(Base):
"""
The telegram account being used as the sock puppet
"""
__tablename__ = 'account'
id = Column(Integer, index=True, primary_key=True, autoincrement=True)
account_id = Column(Integer, nullable=False, index=True)
account_api_id = Column(Integer, default=None, nullable=False)
account_api_hash = Column(String(50), default=None, nullable=False)
account_is_bot = Column(Boolean(), default=None)
account_is_verified = Column(Boolean(), default=None)
account_is_restricted = Column(Boolean(), default=None)
account_first_name = Column(String(50), default=None)
account_last_name = Column(String(50), default=None)
account_user_name = Column(String(100), default=None, nullable=False)
account_phone = Column(String(25), unique=True, default=None, nullable=False)
account_tlogin = Column(DateTime, default=None)
account_is_enabled = Column(Boolean(), default=True)
account_tcreate = Column(DateTime, default=datetime.now())
account_tmodified = Column(DateTime, default=datetime.now())
channels = relationship('Channel', back_populates='accounts')
messages = relationship('Message', back_populates='account')
class Channel(Base):
"""
The telegram channel the user is in
"""
__tablename__ = 'channel'
id = Column(Integer, primary_key=True, index=True)
channel_id = Column(Integer, unique=True, index=True, nullable=True) # The URL can come first but the channel ID populated later
channel_name = Column(String(256), default=None, nullable=True)
channel_title = Column(String(256), default=None, nullable=True)
channel_url = Column(String(256), nullable=True)
account_id = Column(Integer, ForeignKey('account.account_id'), nullable=False) # The account ID (bot) that spawned the channel
channel_is_mega_group = Column(Boolean(), nullable=True)
channel_is_group = Column(Boolean(), nullable=True)
channel_is_private = Column(Boolean(), nullable=True)
channel_is_broadcast = Column(Boolean(), nullable=True)
channel_access_hash = Column(String(50), nullable=True)
channel_size = Column(Integer, nullable=True)
channel_is_enabled = Column(Boolean(), nullable=True, default=True)
channel_tcreate = Column(DateTime, default=datetime.now())
messages = relationship('Message')
accounts = relationship('Account', back_populates='channels')
notifications = relationship('Notification', back_populates='channel')
class ChatUser(Base):
"""
The participant of a chat on telegram
"""
__tablename__ = 'chat_user'
id = Column(Integer, primary_key=True, index=True)
chat_user_id = Column(Integer, unique=True, index=True, nullable=False)
chat_user_is_bot = Column(Boolean(), default=None)
chat_user_is_verified = Column(Boolean(), default=None)
chat_user_is_restricted = Column(Boolean(), default=None)
chat_user_first_name = Column(String(50), default=None)
chat_user_last_name = Column(String(50), default=None)
chat_user_name = Column(String(100), default=None)
chat_user_phone = Column(String(25), default=None)
chat_user_tlogin = Column(DateTime, default=None)
chat_user_tcreate = Column(DateTime, default=datetime.now())
chat_user_tmodified = Column(DateTime, default=datetime.now())
messages = relationship('Message')
class Keyword(Base):
"""
This is the keyword to be alerted by
"""
__tablename__ = 'keyword'
keyword_id = Column(Integer, primary_key=True, index=True)
keyword_description = Column(String(256), default=None, nullable=False)
keyword_regex = Column(String(256), unique=True, nullable=False)
keyword_is_enabled = Column(Boolean(), nullable=True, default=True)
keyword_tmodified = Column(DateTime, default=datetime.now())
keyword_tcreate = Column(DateTime, default=datetime.now())
notifications = relationship('Notification')
class Message(Base):
"""
The actual message from a channel and from a user
"""
__tablename__ = 'message'
message_id = Column(Integer, primary_key=True, index=True)
chat_user_id = Column(Integer, ForeignKey('chat_user.chat_user_id'), nullable=False)
account_id = Column(Integer, ForeignKey('account.account_id'), nullable=False) # The account ID (bot)
channel_id = Column(Integer, ForeignKey('channel.channel_id'), nullable=False)
keyword_id = Column(Integer, ForeignKey('keyword.keyword_id'), nullable=False)
message_text = Column(String(10000), default=None)
message_is_mention = Column(Boolean(), default=None)
message_is_scheduled = Column(Boolean(), default=None)
message_is_fwd = Column(Boolean(), default=None)
message_is_reply = Column(Boolean(), default=None)
message_is_bot = Column(Boolean(), default=None)
message_is_group = Column(Boolean(), default=None)
message_is_private = Column(Boolean(), default=None)
message_is_channel = Column(Boolean(), default=None)
message_channel_size = Column(Integer, default=None)
message_tcreate = Column(DateTime, default=datetime.now())
user = relationship('ChatUser', back_populates='messages')
account = relationship('Account', back_populates='messages')
channel = relationship('Channel', back_populates='messages')
notifications = relationship('Notification')
class Monitor(Base):
"""
Channels to join and monitor
"""
__tablename__ = 'monitor'
monitor_id = Column(Integer, primary_key=True, index=True)
channel_id = Column(Integer, ForeignKey('channel.id'), nullable=False)
account_id = Column(Integer, ForeignKey('account.account_id'), nullable=False) # The account ID (bot)
monitor_tcreate = Column(DateTime, default=datetime.now())
monitor_tmodified = Column(DateTime, default=datetime.now())
channel = relationship('Channel')
class Notification(Base):
"""
A log of notifications of keywords detected
"""
__tablename__ = 'notification'
id = Column(Integer, primary_key=True, index=True)
keyword_id = Column(Integer, ForeignKey('keyword.keyword_id'), nullable=False)
message_id = Column(Integer, ForeignKey('message.message_id'), nullable=False)
channel_id = Column(Integer, ForeignKey('channel.channel_id'), nullable=False)
account_id = Column(Integer, ForeignKey('account.account_id'), nullable=False) # The account ID (bot)
chat_user_id = Column(Integer, ForeignKey('chat_user.chat_user_id'), nullable=False)
notification_tnotify = Column(DateTime, default=datetime.now())
keyword = relationship('Keyword')
message = relationship('Message')
channel = relationship('Channel')
user = relationship('ChatUser')