Esempio n. 1
0
def show_post(post_id,
              last=False,
              all=False,
              show=False,
              offset=None,
              limit=None):
    try:
        post = posts.show_post(post_id)
    except PostNotFound:
        return xmpp_template('post_not_found', post_id=post_id)
    except (SubscribeError, PostAuthorError):
        return xmpp_template('post_denied', post_id=post_id)

    subscribed = post.check_subscriber(env.user) if env.user.id else False

    updates = []
    for i, upd in enumerate(post.updates()):
        updates.append({'no': i + 1, 'text': upd['text']})

    comments = []

    posts.clear_unread_posts(post_id)
    if last or all or show:
        if offset:
            offset = int(offset)
        if limit:
            limit = int(limit)
        else:
            limit = 10
        res = post.comments(last=last, all=all, offset=offset, limit=limit)
        for c in res:
            comments.append({
                'comment_id': c.id,
                'to_comment_id': c.to_comment_id,
                'comment_author': c.author.login,
                'comment_text': c.text,
                'is_rec': c.is_rec,
                'files': c.files
            })
        cnt = None
        posts.clear_unread_comments(post_id, map(lambda c: c.id, res))
    else:
        cnt = post.comments_count()

    return xmpp_template('post',
                         post_id=post_id,
                         author=post.author.login,
                         type=post.type,
                         private=post.private,
                         title=post.title,
                         link=post.link,
                         tags=post.tags,
                         text=post.text,
                         files=post.files,
                         subscribed=subscribed,
                         updates=updates,
                         comments=comments,
                         comments_count=cnt,
                         archive=post.archive,
                         rec_users=post.recommended_users())
Esempio n. 2
0
def show_post(id):
    post = posts.show_post(id)

    if env.request.method == 'POST':
        return add_comment(post.id)

    if not env.owner or env.owner.id != post.author.id:
        return Response(redirect='%s://%s.%s/%s' % \
                        (env.request.protocol,
                         post.author.login.lower(), settings.domain, id))

    comments = post.comments(cuser=env.user)

    if env.user.is_authorized():
        posts.clear_unread_posts(id)
        if comments:
            posts.clear_unread_comments(id)

    errors = []
    if env.request.args('expired'):
        errors.append('expired')
    if env.request.args('commented'):
        errors.append('commented')

    sess = Session()

    tree = env.request.args('tree')
    if tree:
        if tree.lower() in ('0', 'false', 'f'):
            tree = False
        else:
            tree = True
        sess['ctree'] = tree
        sess.save()
    elif sess['ctree'] is not None:
        tree = sess['ctree']
    else:
        env.user.get_profile('tree')

    comments_count = len(comments)
    if tree:
        cout = {}
        for c in comments:
            cout[c.id] = c
            if c.to_comment_id and c.to_comment_id in cout:
                cout[c.to_comment_id].comments.append(c)
            else:
                c.to_comment_id = None
        comments = filter(lambda c: not c.to_comment_id, cout.itervalues())

    section = 'messages' if post.private else ''

    return render('/post.html',
                  post=post,
                  comments=comments,
                  comments_count=comments_count,
                  tree=tree,
                  errors=errors,
                  section=section)
Esempio n. 3
0
def show_post(id):
    post = posts.show_post(id)

    if env.request.method == 'POST':
        return add_comment(post.id)

    if not env.owner or env.owner.id != post.author.id:
        return Response(redirect='%s://%s.%s/%s' % \
                        (env.request.protocol,
                         post.author.login.lower(), settings.domain, id))

    comments = post.comments(cuser=env.user)

    if env.user.is_authorized():
        posts.clear_unread_posts(id)
        if comments:
            posts.clear_unread_comments(id)

    errors = []
    if env.request.args('expired'):
        errors.append('expired')
    if env.request.args('commented'):
        errors.append('commented')

    sess = Session()

    tree = env.request.args('tree')
    if tree:
        if tree.lower() in ('0', 'false', 'f'):
            tree = False
        else:
            tree = True
        sess['ctree'] = tree
        sess.save()
    elif sess['ctree'] is not None:
        tree = sess['ctree']
    else:
        env.user.get_profile('tree')

    comments_count = len(comments)
    if tree:
        cout = {}
        for c in comments:
            cout[c.id] = c
            if c.to_comment_id and c.to_comment_id in cout:
                cout[c.to_comment_id].comments.append(c)
            else:
                c.to_comment_id = None
        comments = filter(lambda c: not c.to_comment_id, cout.itervalues())

    section = 'messages' if post.private else ''

    return render('/post.html', post=post, comments=comments,
                  comments_count=comments_count, tree=tree,
                  errors=errors, section=section)
Esempio n. 4
0
def show_post(post_id, last=False, all=False, show=False,
              offset=None, limit=None):
    try:
        post = posts.show_post(post_id)
    except PostNotFound:
        return xmpp_template('post_not_found', post_id=post_id)
    except (SubscribeError, PostAuthorError):
        return xmpp_template('post_denied', post_id=post_id)

    subscribed = post.check_subscriber(env.user) if env.user.id else False

    updates = []
    for i, upd in enumerate(post.updates()):
        updates.append({'no': i+1, 'text': upd['text']})

    comments = []

    posts.clear_unread_posts(post_id)
    if last or all or show:
        if offset:
            offset = int(offset)
        if limit:
            limit = int(limit)
        else:
            limit = 10
        res = post.comments(last=last, all=all, offset=offset, limit=limit)
        for c in res:
            comments.append({
                'comment_id': c.id,
                'to_comment_id': c.to_comment_id,
                'comment_author': c.author.login,
                'comment_text': c.text,
                'is_rec': c.is_rec,
                'files': c.files
            })
        cnt = None
        posts.clear_unread_comments(post_id, map(lambda c: c.id, res))
    else:
        cnt = post.comments_count()

    return xmpp_template('post', post_id=post_id, author=post.author.login,
                                 type=post.type, private=post.private,
                                 title=post.title, link=post.link,
                                 tags=post.tags, text=post.text,
                                 tune=post.tune, files=post.files,
                                 subscribed=subscribed,
                                 updates=updates, comments=comments,
                                 comments_count=cnt,
                                 archive=post.archive,
                                 rec_users=post.recommended_users())
Esempio n. 5
0
def show_post(id):
    post = posts.show_post(id)

    comments = post.comments(cuser=env.user)

    if env.user.is_authorized():
        posts.clear_unread_posts(id)
        if comments:
            posts.clear_unread_comments(id)

    menu = 'messages' if post.private else ''

    return Response(template='/pages/post.html', menu=menu, owner=post.author,
                    p={
                        'post': post,
                        'comments_count': post.comments_count(),
                        'subscribed': post.check_subscriber(env.user),
                        'bookmarked': post.check_bookmarked(env.user),
                        'recommended': post.check_recommended(env.user),
                        'rec_users': post.recommended_users()
                    }, comments=comments)
Esempio n. 6
0
def show_post(id):
    post = posts.show_post(id)

    comments = post.comments(cuser=env.user)

    if env.user.is_authorized():
        posts.clear_unread_posts(id)
        if comments:
            posts.clear_unread_comments(id)

    menu = 'messages' if post.private else ''

    return Response(template='/pages/post.html',
                    menu=menu,
                    owner=post.author,
                    p={
                        'post': post,
                        'comments_count': post.comments_count(),
                        'subscribed': post.check_subscriber(env.user),
                        'bookmarked': post.check_bookmarked(env.user),
                        'recommended': post.check_recommended(env.user),
                        'rec_users': post.recommended_users()
                    },
                    comments=comments)
Esempio n. 7
0
    def handle_message(self, msg):
        """Handle message
        """

        if 'presence' in msg:
            pass

        if 'body' in msg and check_stoplist(msg['body']):
            return

        try:
            jid, resource = msg['from'].split('/', 1)
        except ValueError:
            jid = msg['from']
            resource = None

        env.user = ImUser('xmpp', jid)
        env.jid = jid
        env.resource = resource

        try:
            if 'receipt' in msg and msg['receipt']:
                try:
                    uid, post_id, comment_id = msg['receipt'].split('_')
                    posts.clear_unread_comments(post_id, int(comment_id))
                    return
                except ValueError:
                    try:
                        uid, post_id = msg['receipt'].split('_')
                        posts.clear_unread_posts(post_id)
                        return
                    except ValueError:
                        pass
        except NotAuthorized:
            pass

        if env.user.get_profile('im.auto_switch'):
            bare_from = msg['from'].split('/', 1)[0]
            env.user.set_active_account('xmpp', bare_from)

        def _route(user, jid, msg):
            session = env.user.session()
            try:
                return session(msg['body'])
            except SessionCallError:
                pass

            message = env.user.resolve_aliases(msg['body'])

            args = {}
            for r in self.route:
                m = re.search(r['resource'], msg['resource'])
                if m:
                    args = m.groupdict()
                    route = r['route']
                    break

            for regex, view in route:
                match = re.search(regex, message)
                if match:
                    for g, val in match.groupdict().iteritems():
                        args[g] = val
                    log.debug(">>> %s routed to %s(%s) via '%s'" % \
                              (jid, view.__name__, str(args), regex.pattern))
                    return view(**args)

        _presence = False

        try:
            reply = _route(env.user, jid, msg)
            if 'body' in reply and reply['body']:
                reply['body'] = re.sub(r'&#(\d+);',
                                       lambda c: chr(int(c.group(1))),
                                       reply['body'])
                reply['body'] = u''.join([ c if c == '\n' or ord(c) > 16 else ' ' \
                                           for c  in reply['body'] ])

            if '_presence' in reply and reply['_presence']:
                _presence = True

            if isinstance(reply, (str, unicode)):
                reply = {'body': reply}
        except NotAuthorized:
            reply = xmpp_template('user_not_authorized')
        except PointError, e:
            reply = {'body': "%s: %s" % (e.__class__.__name__, e.message)}
Esempio n. 8
0
def clear_unread_comments():
    posts.clear_unread_comments()
    return Response(redirect=env.request.referer)
Esempio n. 9
0
    def handle_message(self, msg):
        """Handle message
        """

        if "presence" in msg:
            pass

        if "body" in msg and check_stoplist(msg["body"]):
            return

        try:
            jid, resource = msg["from"].split("/", 1)
        except ValueError:
            jid = msg["from"]
            resource = None

        env.user = ImUser("xmpp", jid)
        env.jid = jid
        env.resource = resource

        try:
            if "receipt" in msg and msg["receipt"]:
                try:
                    uid, post_id, comment_id = msg["receipt"].split("_")
                    posts.clear_unread_comments(post_id, int(comment_id))
                    return
                except ValueError:
                    try:
                        uid, post_id = msg["receipt"].split("_")
                        posts.clear_unread_posts(post_id)
                        return
                    except ValueError:
                        pass
        except NotAuthorized:
            pass

        if env.user.get_profile("im.auto_switch"):
            bare_from = msg["from"].split("/", 1)[0]
            env.user.set_active_account("xmpp", bare_from)

        def _route(user, jid, msg):
            session = env.user.session()
            try:
                return session(msg["body"])
            except SessionCallError:
                pass

            message = env.user.resolve_aliases(msg["body"])

            args = {}
            for r in self.route:
                m = re.search(r["resource"], msg["resource"])
                if m:
                    args = m.groupdict()
                    route = r["route"]
                    break

            for regex, view in route:
                match = re.search(regex, message)
                if match:
                    for g, val in match.groupdict().iteritems():
                        args[g] = val
                    log.debug(">>> %s routed to %s(%s) via '%s'" % (jid, view.__name__, str(args), regex.pattern))
                    return view(**args)

        _presence = False

        try:
            reply = _route(env.user, jid, msg)
            if "body" in reply and reply["body"]:
                reply["body"] = re.sub(r"&#(\d+);", lambda c: chr(int(c.group(1))), reply["body"])
                reply["body"] = u"".join([c if c == "\n" or ord(c) > 16 else " " for c in reply["body"]])

            if "_presence" in reply and reply["_presence"]:
                _presence = True

            if isinstance(reply, (str, unicode)):
                reply = {"body": reply}
        except NotAuthorized:
            reply = xmpp_template("user_not_authorized")
        except PointError, e:
            reply = {"body": "%s: %s" % (e.__class__.__name__, e.message)}
Esempio n. 10
0
def show_post(id, page=None):
    post = posts.show_post(id)

    if env.request.method == 'POST':
        return add_comment(post.id)

    if not env.owner or env.owner.id != post.author.id:
        return Response(redirect='%s://%s.%s/%s' % \
                        (env.request.protocol,
                         post.author.login.lower(), settings.domain, id))

    errors = []
    if env.request.args('expired'):
        errors.append('expired')
    if env.request.args('commented'):
        errors.append('commented')

    sess = Session()

    tree = env.request.args('tree')

    if tree:
        if tree.lower() in ('0', 'false', 'f'):
            tree = False
        else:
            tree = True
        sess['ctree'] = tree
        sess.save()
    elif sess['ctree'] is not None:
        tree = sess['ctree']
    else:
        env.user.get_profile('tree')

    comments_count = post.comments_count()

    if comments_count > 1000:
        climit = 100

        tree = False

        last_page = int(math.ceil(float(comments_count) / climit))

        try:
            page = int(page)
        except (TypeError, ValueError):
            page = last_page

        cstart = (page - 1) * climit
        comments = post.comments(cuser=env.user, offset=cstart, limit=climit)
    else:
        comments = post.comments(cuser=env.user)
        page = None
        last_page = None

    if env.user.is_authorized():
        posts.clear_unread_posts(id)
        if comments:
            posts.clear_unread_comments(id)

    if tree:
        cout = {}
        for c in comments:
            cout[c.id] = c
            if c.to_comment_id and c.to_comment_id in cout:
                cout[c.to_comment_id].comments.append(c)
            else:
                c.to_comment_id = None
        comments = filter(lambda c: not c.to_comment_id, cout.itervalues())

    sess = Session()
    clear_post_input = sess['clear_post_input']
    if clear_post_input:
        sess['clear_post_input'] = False
        sess.save()

    section = 'messages' if post.private else ''

    return render('/post.html',
                  post=post,
                  comments=comments,
                  comments_count=comments_count,
                  tree=tree,
                  errors=errors,
                  section=section,
                  page=page,
                  last_page=last_page,
                  clear_post_input=clear_post_input)
Esempio n. 11
0
def clear_unread_comments():
    posts.clear_unread_comments()
    return Response(redirect=env.request.referer)
Esempio n. 12
0
    def handle_message(self, msg):
        """Handle message
        """

        if 'presence' in msg:
            pass

        if 'body' in msg and check_stoplist(msg['body']):
            return

        try:
            jid, resource = msg['from'].split('/', 1)
        except ValueError:
            jid = msg['from']
            resource = None

        env.user = ImUser('xmpp', jid)
        env.jid = jid
        env.resource = resource

        try:
            if 'receipt' in msg and msg['receipt']:
                try:
                    uid, post_id, comment_id = msg['receipt'].split('_')
                    posts.clear_unread_comments(post_id, int(comment_id))
                    return
                except ValueError:
                    try:
                        uid, post_id = msg['receipt'].split('_')
                        posts.clear_unread_posts(post_id)
                        return
                    except ValueError:
                        pass
        except NotAuthorized:
            pass

        if env.user.get_profile('im.auto_switch'):
            bare_from = msg['from'].split('/', 1)[0]
            env.user.set_active_account('xmpp', bare_from)

        def _route(user, jid, msg):
            session = env.user.session()
            try:
                return session(msg['body'])
            except SessionCallError:
                pass

            message = env.user.resolve_aliases(msg['body'])

            args = {}
            for r in self.route:
                m = re.search(r['resource'], msg['resource'])
                if m:
                    args = m.groupdict()
                    route = r['route']
                    break

            for regex, view in route:
                match = re.search(regex, message)
                if match:
                    for g, val in match.groupdict().iteritems():
                        args[g] = val
                    log.debug(">>> %s routed to %s(%s) via '%s'" % \
                              (jid, view.__name__, str(args), regex.pattern))
                    return view(**args)

        _presence = False

        try:
            reply = _route(env.user, jid, msg)
            if 'body' in reply and reply['body']:
                reply['body'] = re.sub(r'&#(\d+);',
                                       lambda c: chr(int(c.group(1))),
                                       reply['body'])
                reply['body'] = u''.join([ c if c == '\n' or ord(c) > 16 else ' ' \
                                           for c  in reply['body'] ])

            if '_presence' in reply and reply['_presence']:
                _presence = True

            if isinstance(reply, (str, unicode)):
                reply = {'body': reply}
        except NotAuthorized:
            reply = xmpp_template('user_not_authorized')
        except PointError, e:
            reply = {'body':"%s: %s" % (e.__class__.__name__, e.message)}
Esempio n. 13
0
def show_post(id, page=None):
    post = posts.show_post(id)

    if env.request.method == 'POST':
        return add_comment(post.id)

    if not env.owner or env.owner.id != post.author.id:
        return Response(redirect='%s://%s.%s/%s' % \
                        (env.request.protocol,
                         post.author.login.lower(), settings.domain, id))

    errors = []
    if env.request.args('expired'):
        errors.append('expired')
    if env.request.args('commented'):
        errors.append('commented')

    sess = Session()

    tree = env.request.args('tree')

    if tree:
        if tree.lower() in ('0', 'false', 'f'):
            tree = False
        else:
            tree = True
        sess['ctree'] = tree
        sess.save()
    elif sess['ctree'] is not None:
        tree = sess['ctree']
    else:
        env.user.get_profile('tree')

    comments_count = post.comments_count()

    if comments_count > 1000:
        climit = 100

        tree = False

        last_page = int(math.ceil(float(comments_count) / climit))

        try:
            page = int(page)
        except (TypeError, ValueError):
            page = last_page

        cstart = (page - 1) * climit
        comments = post.comments(cuser=env.user, offset=cstart, limit=climit)
    else:
        comments = post.comments(cuser=env.user)
        page = None
        last_page = None

    if env.user.is_authorized():
        posts.clear_unread_posts(id)
        if comments:
            posts.clear_unread_comments(id)

    if tree:
        cout = {}
        for c in comments:
            cout[c.id] = c
            if c.to_comment_id and c.to_comment_id in cout:
                cout[c.to_comment_id].comments.append(c)
            else:
                c.to_comment_id = None
        comments = filter(lambda c: not c.to_comment_id, cout.itervalues())

    sess = Session()
    clear_post_input = sess['clear_post_input']
    if clear_post_input:
        sess['clear_post_input'] = False
        sess.save()

    section = 'messages' if post.private else ''

    return render('/post.html', post=post, comments=comments,
                  comments_count=comments_count, tree=tree,
                  errors=errors, section=section,
                  page=page, last_page=last_page,
                  clear_post_input=clear_post_input)