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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
|
"""models for the ``group_messaging`` app
"""
import datetime
from django.db import models
from django.contrib.auth.models import Group
from django.contrib.auth.models import User
MAX_TITLE_LENGTH = 80
MAX_SENDERS_INFO_LENGTH = 64
#dummy parse message function
parse_message = lambda v: v
GROUP_NAME_TPL = '_personal_%s'
def get_personal_group_by_user_id(user_id):
return Group.objects.get(name=GROUP_NAME_TPL % user_id)
def get_personal_groups_for_users(users):
"""for a given list of users return their personal groups"""
group_names = [(GROUP_NAME_TPL % user.id) for user in users]
return Group.objects.filter(name__in=group_names)
def get_personal_group(user):
"""returns personal group for the user"""
return get_personal_group_by_user_id(user.id)
def create_personal_group(user):
"""creates a personal group for the user"""
group = Group(name=GROUP_NAME_TPL % user.id)
group.save()
return group
class LastVisitTime(models.Model):
"""just remembers when a user has
last visited a given thread
"""
user = models.ForeignKey(User)
message = models.ForeignKey('Message')
at = models.DateTimeField(auto_now_add=True)
class Meta:
unique_together = ('user', 'message')
class SenderListManager(models.Manager):
"""model manager for the :class:`SenderList`"""
def get_senders_for_user(self, user=None):
"""returns query set of :class:`User`"""
user_groups = user.groups.all()
lists = self.filter(recipient__in=user_groups)
user_ids = lists.values_list(
'senders__id', flat=True
).distinct()
return User.objects.filter(id__in=user_ids)
class SenderList(models.Model):
"""a model to store denormalized data
about who sends messages to any given person
sender list is populated automatically
as new messages are created
"""
recipient = models.ForeignKey(Group, unique=True)
senders = models.ManyToManyField(User)
objects = SenderListManager()
class MessageMemo(models.Model):
"""A bridge between message recipients and messages
these records are only created when user sees a message.
The idea is that using groups as recipients, we can send
messages to massive numbers of users, without cluttering
the database.
Instead we'll be creating a "seen" message after user
reads the message.
"""
SEEN = 0
ARCHIVED = 1
STATUS_CHOICES = (
(SEEN, 'seen'),
(ARCHIVED, 'archived')
)
user = models.ForeignKey(User)
message = models.ForeignKey('Message')
status = models.SmallIntegerField(
choices=STATUS_CHOICES, default=SEEN
)
class Meta:
unique_together = ('user', 'message')
class MessageManager(models.Manager):
"""model manager for the :class:`Message`"""
def get_threads_for_user(self, user):
user_groups = user.groups.all()
return self.filter(
root=None,
message_type=Message.STORED,
recipients__in=user_groups
)
def create(self, **kwargs):
"""creates a message"""
root = kwargs.get('root', None)
if root is None:
parent = kwargs.get('parent', None)
if parent:
if parent.root:
root = parent.root
else:
root = parent
kwargs['root'] = root
headline = kwargs.get('headline', kwargs['text'])
kwargs['headline'] = headline[:MAX_TITLE_LENGTH]
kwargs['html'] = parse_message(kwargs['text'])
message = super(MessageManager, self).create(**kwargs)
#creator of message saw it by definition
#crate a "seen" memo for the sender, because we
#don't want to inform the user about his/her own post
sender = kwargs['sender']
MessageMemo.objects.create(
message=message, user=sender, status=MessageMemo.SEEN
)
return message
def create_thread(self, sender=None, recipients=None, text=None):
"""creates a stored message and adds recipients"""
message = self.create(
message_type=Message.STORED,
sender=sender,
senders_info=sender.username,
text=text,
)
message.add_recipients(recipients)
return message
def create_response(self, sender=None, text=None, parent=None):
message = self.create(
parent=parent,
message_type=Message.STORED,
sender=sender,
text=text,
)
#recipients are parent's recipients + sender
#creator of response gets memo in the "read" status
recipients = set(parent.recipients.all())
senders_group = get_personal_group(parent.sender)
recipients.add(senders_group)
message.add_recipients(recipients)
#add author of the parent as a recipient to parent
parent.add_recipients([senders_group])
#mark last active timestamp for the root message
#so that we know that this thread was most recently
#updated
message.update_root_info()
return message
class Message(models.Model):
"""the message model allowing users to send
messages to other users and groups, via
personal groups.
"""
STORED = 0
TEMPORARY = 1
ONE_TIME = 2
MESSAGE_TYPE_CHOICES = (
(STORED, 'email-like message, stored in the inbox'),
(ONE_TIME, 'will be shown just once'),
(TEMPORARY, 'will be shown until certain time')
)
message_type = models.SmallIntegerField(
choices=MESSAGE_TYPE_CHOICES,
default=STORED,
)
sender = models.ForeignKey(User, related_name='sent_messages')
senders_info = models.CharField(
max_length=MAX_SENDERS_INFO_LENGTH,
default=''
)#comma-separated list of a few names
recipients = models.ManyToManyField(Group)
root = models.ForeignKey(
'self', null=True,
blank=True, related_name='descendants'
)
parent = models.ForeignKey(
'self', null=True,
blank=True, related_name='children'
)
headline = models.CharField(max_length=MAX_TITLE_LENGTH)
text = models.TextField(
null=True, blank=True,
help_text='source text for the message, e.g. in markdown format'
)
html = models.TextField(
null=True, blank=True,
help_text='rendered html of the message'
)
sent_at = models.DateTimeField(auto_now_add=True)
last_active_at = models.DateTimeField(auto_now_add=True)
active_until = models.DateTimeField(blank=True, null=True)
objects = MessageManager()
def add_recipients(self, recipients):
"""adds recipients to the message
and updates the sender lists for all recipients
todo: sender lists may be updated in a lazy way - per user
"""
self.recipients.add(*recipients)
for recipient in recipients:
sender_list, created = SenderList.objects.get_or_create(recipient=recipient)
sender_list.senders.add(self.sender)
def update_root_info(self):
"""Update the last active at timestamp and
the contributors info, if relevant.
Root object will be saved to the database.
"""
self.root.last_active_at = datetime.datetime.now()
senders_names = self.root.senders_info.split(',')
if self.sender.username in senders_names:
senders_names.remove(self.sender.username)
senders_names.insert(0, self.sender.username)
self.root.senders_info = (','.join(senders_names))[:64]
self.root.save()
|