Skip to content

Commit

Permalink
Queue up invites
Browse files Browse the repository at this point in the history
  • Loading branch information
mdirolf committed Jan 27, 2012
1 parent b9347a9 commit 578d83f
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 22 deletions.
13 changes: 13 additions & 0 deletions db.py
Expand Up @@ -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})
6 changes: 2 additions & 4 deletions github.py
Expand Up @@ -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):
Expand Down
7 changes: 7 additions & 0 deletions test/test_www.py
Expand Up @@ -2,6 +2,7 @@
import re
import StringIO
import sys
import time
import unittest
import urllib
sys.path[0:0] = [""]
Expand All @@ -21,6 +22,9 @@
RATE_LIMITED = False


www.SLEEP_INTERVAL = 0.1


# A little monkey-patching
def our_urlopen(url, params=None):
global GITHUB
Expand Down Expand Up @@ -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"]))
Expand All @@ -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"]))
Expand Down Expand Up @@ -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"]))
Expand Down
69 changes: 51 additions & 18 deletions www.py
Expand Up @@ -3,6 +3,8 @@
import json
import re
import sys
import threading
import time
import urlparse

import decorator
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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"])
Expand Down Expand Up @@ -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":
Expand Down

0 comments on commit 578d83f

Please sign in to comment.