title: How to design a thread style messaging system on Google App Engine
body: <p>I'm trying to design a gmail like thread messaging system on Google App Engine by using Django-nonrel. I've watched "Building Scalable, Complex Apps on App Engine" and read the following questions on SO.</p>
  
  <p><a href="https://stackoverflow.com/questions/6349529/app-engine-messaging-system-with-message-status-design-pattern/6351131#6351131">App Engine Messaging System with Message Status - Design Pattern</a><br>
  <a href="https://stackoverflow.com/questions/2662212/database-design-in-google-app-engine">database design in google app engine</a></p>
  
  <p>I like the idea Kevin pointed at (<a href="https://stackoverflow.com/questions/6349529/app-engine-messaging-system-with-message-status-design-pattern/6351131#6351131">App Engine Messaging System with Message Status - Design Pattern</a>), so my current models are like these:</p>
  
  <pre><code>class Message(models.Model):
      sender = models.ForeignKey(User)
      content = models.TextField()
      thread_id = models.IntegerField()
  
  class MessageReceivers(models.Model):
      message = models.ForeignKey(Message)
      users = ListField(models.CharField())
      status = models.IntegerField()  # 1 unread, 2 read ....
  </code></pre>
  
  <p>ListField on Django-nonrel is from here:<br>
  <a href="http://www.allbuttonspressed.com/blog/django/2010/03/Updates-on-djangoappengine" rel="nofollow noreferrer">http://www.allbuttonspressed.com/blog/django/2010/03/Updates-on-djangoappengine</a></p>
  
  <p>And here is my problem, I don't know how to achieve to display a count of messages in each thread like gmail. Let me clarify my point.<br>
  When a user, Sirius, sends a message to Harry and Ron. Models (simplified for explanation) should be like these:</p>
  
  <pre><code>Message
  id = 1
  sender = Sirius
  thread_id = 1
  
  MessageReceivers
  message = 1
  users = [Harry, Ron]
  </code></pre>
  
  <p>Then, Harry replies to Sirius, but for some reason he doesn't include Ron:</p>
  
  <pre><code>Message
  id = 2
  sender = Harry
  thread_id = 1
  
  MessageReceivers
  message = 2
  users = [Sirius]
  </code></pre>
  
  <p>And Sirius replies to Harry too, but he includes Ron again:</p>
  
  <pre><code>Message
  id = 3
  sender = Sirius
  thread_id = 1
  
  MessageReceivers
  message = 3
  users = [Haary, Ron]
  </code></pre>
  
  <p>And when Sirius and Harry see their inbox, there is the thread with a count of messages (3). And Ron sees his inbox there is the thread with a count of messages (2). How should I count those numbers? Should I make a new model to keep them?</p>
  
  <p>Thanks in advance,<br>
  Yoo</p>
  
  <p><strong>EDIT</strong><br>
  As Kevin introduced me to a Thread model and I realized that I don't need a relation index model for my requirements, so my current models look like the following.</p>
  
  <pre><code>class Message(models.Model):
      sender = models.ForeignKey(User)
      content = models.TextField()
  
  class Thread(models.Model):
      user = models.ForeignKey(User)
      messages = ListField(models.CharField())    # array of message id
      lastmodified = models.DateTimeField()
      # could have a status field for read or unread, not sure for now
  </code></pre>
  
  <p>I might find difficulties in these models in further development, but I would go with them for the time being :)</p>