def realtime_sync(user): channel_ids = ['qotd'] if user.is_authenticated(): channel_ids.append(user.redis.activity_stream_channel.channel) channel_ids.append(user.redis.coin_channel.channel) channels = {} for channel_id in channel_ids: channel = RealtimeChannel(channel_id) channels[channel_id] = channel.sync() return channels
def followee_posted(): Actions.followee_posted(author, comment) #TODO this should happen because of the above Actions.followee_posted for follower_id in author.redis.new_followers.zrange(0, -1): RealtimeChannel('user:{}:rt_tab_badges'.format(follower_id), 1).publish({'tab_badge_update': 'home'})
def realtime(): try: channels = [ self.svc.channels.get(RealtimeChannel(channel_id)) for channel_id in channel_ids ] backlogs = yield wait_all([ channel.backlog(msg_id) for channel, msg_id in zip(channels, msg_ids) ]) except Channel.DisconnectedError: request_finish_json(request, { 'success': False, 'reason': "Disconnected from Redis" }) else: backlog = {} for _, channel_backlog in backlogs: backlog.update(channel_backlog) xhr = RealtimeXhrListener(request) if backlog: xhr.send(backlog) else: for channel in channels: channel.join(xhr)
def _publish_quest_of_the_day(self): signals.current_quest_changed.send(ScheduledQuest, instance=self) RealtimeChannel('qotd', 1).publish({'quest_id': self.quest_id}) push_notification('quest_of_the_day', _(u"Today's Quest: %(quest_title)s" % {'quest_title': self.quest.title}), extra_metadata={'quest_id': self.quest.id}, badge=1)
def test_realtime(request): from canvas.redis_models import RealtimeChannel from django.http import HttpResponse RealtimeChannel('qotd', 1).publish({'quest_id': 1006}) return HttpResponse('ok')
class CommentDetailsRealtimeMixin(object): updates_channel = property( lambda self: RealtimeChannel('cu:%s' % self.id, 5, ttl=24 * 60 * 60))
def updates_channel(followed_user): if hasattr(followed_user, 'id'): followed_user = followed_user.id return RealtimeChannel('fu:{}'.format(followed_user), 20)
def debug_fact_channel(): from canvas.redis_models import RealtimeChannel return RealtimeChannel('debug_fact', 30)
class Tag(object): top = property(lambda self: DateKey(lambda key: RedisLastBumpedBuffer(key, 30*30), self.base_key, ':top')) updates_channel = property(lambda self: RealtimeChannel('tu:%s' % self.name, 5, ttl=24*60*60)) def __repr__(self): return self.name def __init__(self, name): self.name = name.lower().strip().replace('#', '') self.base_key = 'tag:{}:posts'.format(self.name) self.new = RedisSortedSet(self.base_key) self.images_only = RedisLastBumpedBuffer(self.base_key + ':images', 1000) self.popular = RedisLastBumpedBuffer(self.base_key + ':popular', 1000) self.post_count = RedisKey(self.base_key + ':count') def to_client(self): return self.name def tag_comment(self, comment, timestamp=None): if timestamp is None: timestamp = Services.time.time() self.new.zadd(int(comment.id), timestamp) all_tags.sadd(self.name) if comment.reply_content is not None: self.images_only.bump(int(comment.id), score=timestamp) count = self.post_count.incr() self.updates_channel.publish({'post': comment.id, 'tag': self.name, 'count': count}) def untag_comment(self, comment): self.new.zrem(comment.id) self.images_only.remove(comment.id) self.popular.remove(comment.id) def get_absolute_url(self): return '/x/' + self.name.replace('#','') def user_is_following(self, user): if not user.is_authenticated(): return False return self.name in user.redis.followed_tags def merge_top_scores(self, day=None): """ Merges daily top scores into monthly and monthly into yearly top scores for this group for the given day and the 365 days before it. If `day` is `None`, defaults to today. """ if not day: day = Services.time.today() # Merge today + last 365 days days = [day - datetime.timedelta(n) for n in range(366)] months = defaultdict(list) for day in days: months[(day.year, day.month)].append(day) years = defaultdict(list) for (year, month) in months.keys(): years[year].append(month) for (year, month), days in months.iteritems(): dest = self.top.month(datetime.date(year, month, 1)) source_keys = [self.top.day(day).key for day in days] redis.zunionstore(dest.key, source_keys, aggregate='max') dest.truncate(2) for year, year_months in years.iteritems(): dest = self.top.year(datetime.date(year, 1, 1)) source_keys = [self.top.month(datetime.date(year, month, 1)).key for month in year_months] redis.zunionstore(dest.key, source_keys, aggregate='max') dest.truncate(5)
def alert_followers(): for follower_id in request.user.redis.new_followers.zrange(0, -1): RealtimeChannel('user:{}:rt_tab_badges'.format(follower_id), 1).publish({'tab_badge_update': 'draw'})
def add_invite(self, quest): self.bump(quest.id, truncate=False) RealtimeChannel('user:{}:rt_tab_badges'.format(self.user_id), 1).publish({'tab_badge_update': 'draw'})