Skip to content
Browse files

Queue up invites

  • Loading branch information...
1 parent b9347a9 commit 578d83fdbd2dc6151c55c84bbe2bb8ba0183e54f @mdirolf mdirolf committed Jan 27, 2012
Showing with 73 additions and 22 deletions.
  1. +13 −0 db.py
  2. +2 −4 github.py
  3. +7 −0 test/test_www.py
  4. +51 −18 www.py
View
13 db.py
@@ -51,3 +51,16 @@ def save_user(username, email_address, display_name):
def user(username):
return db.users.find_one({"_id": username})
+
+
+# Invite queue
+def pending_invite(repo_name, github_url, inviter, username, group_id):
+ db.invites.save({"repo_name": repo_name,
+ "github_url": github_url,
+ "inviter": inviter,
+ "username": username,
+ "group_id": group_id}, safe=True)
+
+
+def next_invite():
+ return db.invites.find_and_modify(remove=True, sort={'_id': -1})
View
6 github.py
@@ -115,17 +115,15 @@ def current_user():
def user_info(username):
existing = db.user(username)
if existing:
- return existing
+ return existing, True
u = "http://github.com/api/v2/json/user/show/" + username
data = json.loads(urlopen(u).read())
if "error" in data:
- if "Rate Limit" in data["error"][0]:
- raise RateLimited()
raise Error("GitHub error: " + repr(data["error"]))
data = data["user"]
db.save_user(username, data.get("email", None), data.get("name", None))
- return data
+ return data, False
def repos(org=None):
View
7 test/test_www.py
@@ -2,6 +2,7 @@
import re
import StringIO
import sys
+import time
import unittest
import urllib
sys.path[0:0] = [""]
@@ -21,6 +22,9 @@
RATE_LIMITED = False
+www.SLEEP_INTERVAL = 0.1
+
+
# A little monkey-patching
def our_urlopen(url, params=None):
global GITHUB
@@ -130,6 +134,7 @@ def test_add_user_without_name(self):
res = self.submit(res.form)
self.assertIn("Gitlist has been created", res)
+ time.sleep(0.7)
mailbox = sandbox.mailbox()
self.assertEqual(2, len(mailbox))
self.assertEqual(1, len(mailbox["test@example.com"]))
@@ -156,6 +161,7 @@ def test_create_own_existing(self):
res = self.submit(res.form)
self.assertIn("Gitlist has been created", res)
+ time.sleep(0.7)
mailbox = sandbox.mailbox()
self.assertEqual(2, len(mailbox))
self.assertEqual(1, len(mailbox["test@example.com"]))
@@ -185,6 +191,7 @@ def test_create_other_existing(self):
res = self.submit(res.form)
self.assertIn("Gitlist has been created", res)
+ time.sleep(0.7)
mailbox = sandbox.mailbox()
self.assertEqual(2, len(mailbox))
self.assertEqual(1, len(mailbox["test@example.com"]))
View
69 www.py
@@ -3,6 +3,8 @@
import json
import re
import sys
+import threading
+import time
import urlparse
import decorator
@@ -36,6 +38,52 @@
smtp_server=settings.relay_host)
+SLEEP_INTERVAL = 1
+
+class SendInvites(threading.Thread):
+ '''
+ We send invites from a separate thread to try to avoid going over
+ GH's rate limit (60 calls per minute for V2 API).
+ '''
+
+ def run(self):
+ while True:
+ invite = db.next_invite()
+ if not invite:
+ time.sleep(SLEEP_INTERVAL)
+ continue
+
+ repo_name = invite["repo_name"]
+ github_url = invite["github_url"]
+ inviter = invite["inviter"]
+ username = invite["username"]
+ group = fiesta.FiestaGroup.from_id(fiesta_api, invite["group_id"])
+
+ welcome_message = {"subject": "Invitation to %s@gitlists.com" % repo_name,
+ "markdown": """
+[%s](%s) invited you to a [Gitlist](https://gitlists.com) for [%s](%s). Gitlists are dead-simple mailing lists for GitHub projects.
+
+[Click here]($invite_url) to join the list. If you don't want to join, just ignore this message.
+
+Have a great day!
+""" % (inviter, "http://github.com/" + inviter, repo_name, github_url)}
+
+ user, cache = github.user_info(username)
+ if user.get("email", None):
+ group.add_member(user["email"],
+ display_name=user.get("name", ""),
+ welcome_message=welcome_message,
+ send_invite=True)
+
+ if not cache:
+ time.sleep(SLEEP_INTERVAL)
+
+
+# Invite thread
+send_invites = SendInvites()
+send_invites.start()
+
+
def gen_xsrf(actions):
xsrf = {}
for action in actions:
@@ -185,25 +233,9 @@ def repo_create(name, org=None):
display_name=user.get("name", ""),
welcome_message=welcome_message)
- welcome_message = {"subject": "Invitation to %s@gitlists.com" % repo["name"],
- "markdown": """
-[%s](%s) invited you to a [Gitlist](https://gitlists.com) for [%s](%s). Gitlists are dead-simple mailing lists for GitHub projects.
-
-[Click here]($invite_url) to join the list. If you don't want to join, just ignore this message.
-
-Have a great day!
-""" % (user["login"], "http://github.com/" + user["login"],
- repo["name"], github_url)}
-
for username in to_invite:
- member_user = github.user_info(username)
- if not member_user.get("email", None):
- continue
-
- group.add_member(member_user["email"],
- display_name=member_user.get("name", ""),
- welcome_message=welcome_message,
- send_invite=True)
+ db.pending_invite(repo["name"], github_url, user["login"],
+ username, group.id)
db.new_list(repo["name"], user["login"], group.id)
flask.flash("Your Gitlist has been created – check your email at '%s'." % user["email"])
@@ -265,6 +297,7 @@ def run(self):
if __name__ == '__main__':
port = 7176
+
if len(sys.argv) > 2:
port = int(sys.argv[2])
if settings.env == "prod":

0 comments on commit 578d83f

Please sign in to comment.
Something went wrong with that request. Please try again.