-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
170 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,5 +2,79 @@ | |
from django.core.mail import send_mail | ||
from settings import ADMINS | ||
|
||
import time, redis | ||
import settings | ||
|
||
def _get_redis(): | ||
return redis.Redis(host='localhost', port=6379, db=0) | ||
|
||
def user_online_group(user, group, now=None, r=None): | ||
now = now or (int(time.time()) / 60) | ||
r = r or _get_redis() | ||
|
||
for i in xrange(now, now - settings.USER_IDLE_TIME, -1): | ||
if r.sismember('%i_online_%i' % (group.id, i), user.id): | ||
return True | ||
|
||
return False | ||
|
||
def set_user_online_global(user, now=None, r=None): | ||
now = now or (int(time.time()) / 60) | ||
r = r or _get_redis() | ||
|
||
global_online_now = 'online_%i' % now | ||
|
||
preexist = r.exists(global_online_now) | ||
|
||
r.sadd(global_online_now, user.id) | ||
|
||
if not preexist: | ||
r.expire(global_online_now, 300) | ||
|
||
def set_user_online_group(user, group, now=None, r=None): | ||
now = now or (int(time.time()) / 60) | ||
r = r or _get_redis() | ||
|
||
group_online_now = '%i_online_%i' % (group.id, now) | ||
user_groups = 'user_%i_groups' % user.id | ||
|
||
r.sadd(user_groups, group.id) | ||
|
||
preexist = r.exists(group_online_now) | ||
r.sadd(group_online_now, user.id) | ||
|
||
if not preexist: | ||
r.expire(group_online_now, 300) | ||
|
||
def set_user_offline(user, r=None): | ||
r = r or _get_redis() | ||
|
||
now = int(time.time()) / 60 | ||
|
||
online = False | ||
groups = [] | ||
|
||
# remove from online | ||
for i in xrange(now, now-settings.USER_IDLE_TIME, -1): | ||
if r.srem('online_%i' % i, user.id): | ||
online = True | ||
|
||
# if user was online | ||
if online: | ||
# for each group it was in, remove from online | ||
for group_id in r.smembers('user_%i_groups' % user.id): | ||
group_id = int(group_id) | ||
online = False | ||
for i in xrange(now, now-settings.USER_IDLE_TIME, -1): | ||
if r.srem('%i_online_%i' % (group_id, i), user.id): | ||
online = True | ||
# if removed, add to list | ||
if online: groups.append(group_id) | ||
|
||
r.delete('user_%i_groups' % user.id) | ||
|
||
return groups | ||
|
||
|
||
def send_new_user_email(user): | ||
return send_mail("[muxlist] New user: %s" % user.username, render_to_string('email/new_user.html', {'user': user}), '[email protected]', [a[1] for a in ADMINS]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,24 @@ | ||
from django.http import HttpResponse, Http404 | ||
from django.shortcuts import get_object_or_404 | ||
|
||
def heartbeat(request): | ||
if not request.is_authenticated(): raise Http404 | ||
from muxlist.mix.models import Group, _get_redis | ||
import json, time | ||
|
||
request.user.heartbeat() | ||
from muxlist.comet.utils import send_user_leave | ||
from muxlist.account.utils import set_user_offline | ||
|
||
import settings | ||
|
||
class LightweightGroup(object): | ||
def __init__(self, group_id): | ||
self.id = group_id | ||
|
||
def disconnect(request): | ||
if not request.user.is_authenticated(): raise Http404 | ||
|
||
user = request.user | ||
|
||
for group_id in set_user_offline(user): | ||
send_user_leave(user, LightweightGroup(int(group_id))) | ||
|
||
return HttpResponse() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters