Esempio n. 1
0
File: api.py Progetto: strogo/gaeaib
    def post(self, board, thread):
        # FIXME: move subscribe crap somewhere out

        key = "updatetime-thread-%s-%d" % (board, thread)
        person_cookie = self.get_secure_cookie("person", True)
        person = person_cookie.get("update", str(uuid()))

        # FIXME: tt sucks, temporary workarround
        while len(person + key) % 4:
            person += "="

        person_cookie["update"] = person

        watchers = memcache.get(key) or []

        nwatchers = util.watchers_clean(watchers, person)
        nwatchers.insert(0, (time(), person))

        memcache.set(key, nwatchers, time=self.WATCH_TIME)

        token = memcache.get(person + key)

        if not token:
            token = channel.create_channel(person + key)
            memcache.set(person + key, token)

        post_level = util.post_level(self.request.remote_addr)

        rb = rainbow.make_rainbow(self.request.remote_addr, board, thread)

        util.watchers_send(watchers, key, {"evt": "enter", "rainbow": rb})

        return json_response({"token": token, "post_quota": post_level, "watcher_time": self.WATCH_TIME})
Esempio n. 2
0
def process(thread_key):
  logging.info('process %r' % thread_key)
  thread = db.get(thread_key)

  render = Render(thread = thread)

  for idx,post in enumerate(thread.posts):
    if 'text' in post:
      post['text_html'] = markup(
            board=thread.board, postid=post.get("post"),
            data=escape(post.get('text', '')),
      )
      if 'rainbow_html' in post:
        post.pop("rainbow_html")
        post['rainbow'] = rainbow.make_rainbow(post['rainbow'])

    if 'image' in post and not post.get("key"):
      post.pop("image")
      post['name'] = 'Kuroneko'

    if idx == 0:
      render.create(post)
    else:
      render.append(post)

  thread.put()

  render.save()
Esempio n. 3
0
def option_saem(request, data):
  if data.get('name') != 'SAEM':
    return

  rb = rainbow.make_rainbow(data)
  data['rainbow'] = rb
  data['rainbow_html'] = rainbow.rainbow(rb)
Esempio n. 4
0
File: api.py Progetto: a37912/gaeaib
  def post(self, board, thread):
    # FIXME: move subscribe crap somewhere out

    rb = rainbow.make_rainbow(self.request.remote_addr, board, thread)
    person_cookie = self.get_secure_cookie("person", True, max_age=MONTH*12)
    person = person_cookie.get("update", str(uuid()))

    person_cookie['update'] = person

    subid = "%s/%s/%s/%d" %(rb, person, board, thread, )


    token = memcache.get(subid)

    if not token:
      token = channel.create_channel(subid)
      memcache.set(subid, token)

    try:
        query = 'thread:%d board:%s' %(thread, board)

        matcher.subscribe(util.Post, query, subid,
        topic='post',
        lease_duration_sec=self.WATCH_TIME)
    except OverQuotaError:
        logging.error("subscribe failed")
        token = None

    post_level = util.post_level(self.request.remote_addr)

    """
    if self.NEWFAG and \
      not person_cookie.get("postcount") > self.OLDFAG and \
      (board, thread) not in self.NEWFAG:
          post_level = "err"
    """


    return json_response( 
        {
          "token" : token,
          "post_quota" : post_level,
          "watcher_time" : self.WATCH_TIME,
        }
    )
Esempio n. 5
0
def do_render_cache(cursor=None):
  thq = Thread.all()

  if cursor:
    thq.with_cursor(cursor)

  thread = thq.get()

  if not thread:
    logging.info("stop thread clean")
    return

  board = thread.board
  render = Render(board=board, thread = thread.id)

  for idx,post in enumerate(thread.posts):
    if 'text' in post:
      post['text_html'] = markup(
            board=board, postid=post.get("post"),
            data=escape(post.get('text', '')),
      )
      if 'rainbow_html' in post:
        post.pop("rainbow_html")
        post['rainbow'] = rainbow.make_rainbow(post['rainbow'])

    if 'image' in post and not post.get("key"):
      post.pop("image")
      post['name'] = 'Kuroneko'

    if idx == 0:
      render.create(post)
    else:
      render.append(post)

  if len(thread.posts) > 1:
    thread.put()
  else:
    thread.delete()

  render.save()

  deferred.defer(do_render_cache, thq.cursor())
Esempio n. 6
0
def option_saem(request, data):
  if data.get('name') != 'SAEM':
    return

  rb = rainbow.make_rainbow(data.get("rainbow"), data.get("post"))
  data['rainbow'] = rb
Esempio n. 7
0
def save_post(request, data, board, thread, ip):

  def board_increment():
    board_db = BoardCounter.get_by_key_name(board)

    if not board_db:
      board_db = BoardCounter(key_name = board, thread = [])

    board_db.counter += 1
    board_db.put()

    return board_db.counter

  postid = db.run_in_transaction(board_increment,)

  # create new thread
  new = False
  if thread == 'new':
    new = True
    if data.get("sage"):
      raise NotFound() # FIXME: move to form

    thread = postid
    posts = []
    thread_db = Thread.create(thread, board)
    thread_db.posts = []
    thread_db.subject = data.get("subject")[:SUBJECT_MAX]
  else:
    thread = int(thread)

    thread_db = Thread.load(thread, board)

    if not thread_db:
      raise NotFound()

  rb = rainbow.make_rainbow(ip, board, thread)
  data['rainbow'] = rb
  data['overlay'] = board in OVER
  
  data['text_html'] = markup(
        board=board, postid=postid,
        data=escape(data.get('text')),
  )

  # save thread and post number
  data['post'] = postid
  data['thread'] = thread
  now = datetime.now()
  data['time'] = now.strftime("%Y-%m-%d, %H:%M")
  data['timestamp'] = int(now.strftime("%s"))

  img_key = data.get("key")

  if img_key:
    blob_key = blobstore.BlobKey(img_key)
    blob_info = blobstore.BlobInfo.get(blob_key)

    data['image'] = {
        "size" : blob_info.size,
        "content_type" : blob_info.content_type,
        "full" : images.get_serving_url(img_key),
        "thumb" : images.get_serving_url(img_key, 200),
    }

  for fname in OPTIONS.get(board, []):
    func = globals().get('option_'+fname)

    if func:
      func(request, data)

  thread_db.posts.append(data)
  thread_db.put()

  r = Render(thread=thread_db)
  r.post_html = ''
  r.add(data, new) # WARNING: side effect on data
  r.save()

  deferred.defer(save_post_defer,
      thread_db.boards, thread,
      r.post_html, data.get('text_html'),
      postid,
      len(thread_db.posts),
      data.get("sage"),
  )

  # send notify
  thread_flag = 'new' if new else 'sage' if data.get("sage") else 'bump'
  match_msg = Post(board = board, thread = thread, thread_flag = thread_flag)
  match_msg.data = dict(
    board = board,
    thread = thread,
    html = r.post_html,
    text = data.get('text'),
    last = postid,
    count = len(thread_db.posts),
    evt = 'newpost'
  )

  matcher.match(match_msg, topic='post',
      result_task_queue='postnotify')

  return postid, thread
Esempio n. 8
0
def save_post(request, data, board, thread):

  board_db = Board.get_by_key_name(board)

  if not board_db:
    board_db = Board(key_name = board, thread = [])

  board_db.counter += 1

  # create new thread
  new = False
  if thread == 'new':
    new = True
    if data.get("sage"):
      raise NotFound() # FIXME: move to form

    thread = board_db.counter
    posts = []
    thread_db = Thread.create(thread, board)
    thread_db.posts = []
    thread_db.subject = data.get("subject")[:SUBJECT_MAX]
  else:
    thread = int(thread)
    #if thread not in board_db.thread:
    #  raise NotFound()

    if thread in board_db.thread and not data.get("sage"):
      board_db.thread.remove(thread)

    thread_db = Thread.load(thread, board)

    if not thread_db:
      raise NotFound()

  if not data.get("sage"):
    board_db.thread.insert(0, thread)

  board_db.thread = board_db.thread[:THREAD_PER_PAGE*BOARD_PAGES]

  rb = rainbow.make_rainbow(request.remote_addr, board, thread)
  data['rainbow'] = rb
  data['rainbow_html'] = rainbow.rainbow(rb)
  data['text_html'] = markup(
        board=board, postid=board_db.counter,
        data=escape(data.get('text', '')),
  )

  # FIXME: move to field
  data['name'] = data.get("name") or "Anonymous"

  # save thread and post number
  data['post'] = board_db.counter
  data['thread'] = thread
  now = datetime.now()
  data['time'] = now.strftime("%Y-%m-%d, %H:%M")
  data['timestamp'] = int(now.strftime("%s"))

  img_key = data.get("key")

  if img_key:
    blob_key = blobstore.BlobKey(img_key)
    blob_info = blobstore.BlobInfo.get(blob_key)

    data['image'] = {
        "size" : blob_info.size,
        "content_type" : blob_info.content_type,
        "full" : images.get_serving_url(img_key),
        "thumb" : images.get_serving_url(img_key, 200),
    }

  for fname in board_options.get(board, []):
    func = globals().get('option_'+fname)

    if func:
      func(request, data)

  thread_db.posts.append(data)

  db.put( (thread_db, board_db))
  Cache.delete(
    (
      dict(Board=board),
    )
  )
  memcache.set("threadlist-%s" % board, board_db.thread)

  memcache.set("post-%s-%d" %(board, board_db.counter), data)

  r = Render(board, thread)
  r.add(data, new)
  r.save()

  key = "update-thread-%s-%d" % (board, thread)
  if not new:
    send = { 
        "html" : r.post_html, 
        "evt" : "newpost" ,
        "count" : len(thread_db.posts),
        "last" : board_db.counter,
    }
    watchers = memcache.get(key) or []
    for person in watchers:
      logging.info("send data to key %s" % (person+key))
      channel.send_message(person+key, dumps(send))

  return board_db.counter, thread
Esempio n. 9
0
def save_post(request, data, board, thread):

  board_db = Board.get_by_key_name(board)

  if not board_db:
    board_db = Board(key_name = board, thread = [])

  board_db.counter += 1

  # create new thread
  new = False
  if thread == 'new':
    new = True
    if data.get("sage"):
      raise NotFound() # FIXME: move to form

    thread = board_db.counter
    posts = []
    thread_db = Thread.create(thread, board)
    thread_db.posts = []
    thread_db.subject = data.get("subject")[:SUBJECT_MAX]
  else:
    thread = int(thread)

    if thread in board_db.thread and not data.get("sage"):
      board_db.thread.remove(thread)

    thread_db = Thread.load(thread, board)

    if not thread_db:
      raise NotFound()

  if not data.get("sage"):
    board_db.thread.insert(0, thread)

  per_page = get_config('aib.ib', 'thread_per_page')
  pages = get_config('aib.ib', 'board_pages')

  board_db.thread = board_db.thread[:per_page*pages]

  rb = rainbow.make_rainbow(request.remote_addr, board, thread)
  data['rainbow'] = rb
  data['overlay'] = board in OVER
  
  data['text_html'] = markup(
        board=board, postid=board_db.counter,
        data=escape(data.get('text', '')),
  )

  # save thread and post number
  data['post'] = board_db.counter
  data['thread'] = thread
  now = datetime.now()
  data['time'] = now.strftime("%Y-%m-%d, %H:%M")
  data['timestamp'] = int(now.strftime("%s"))

  img_key = data.get("key")

  if img_key:
    blob_key = blobstore.BlobKey(img_key)
    blob_info = blobstore.BlobInfo.get(blob_key)

    data['image'] = {
        "size" : blob_info.size,
        "content_type" : blob_info.content_type,
        "full" : images.get_serving_url(img_key),
        "thumb" : images.get_serving_url(img_key, 200),
    }

  for fname in OPTIONS.get(board, []):
    func = globals().get('option_'+fname)

    if func:
      func(request, data)

  thread_db.posts.append(data)

  db.put( (thread_db, board_db))
  Cache.remove("board", board)

  r = Render(board, thread)
  r.add(data, new)
  r.save()

  deferred.defer(rss.add, board, thread, board_db.counter, 
	data.get("text_html") )

  if not new:
    deferred.defer(
        watchers_post_notify,
        board, thread, r.post_html, 
        len(thread_db.posts), board_db.counter
    )

  return board_db.counter, thread