Esempio n. 1
0
    def post(self, mid):
        is_shared = self.get_argument("is_shared", "off")
        is_shared = True if is_shared == "on" else False
        content = self.get_argument("content")
        source_mid = self.get_argument("source_mid")
        upper = Reply.getObjectByObjectId(mid)
        source = Topic.getObjectByObjectId(source_mid)
        if upper and source:
            reply = Reply.save_reply(source=source, content=content, owner=self.current_user, upper=upper)
            names_list = get_usernames(content)
            for username in names_list:
                send_mentions(sender=self.current_user, username=username, reply=reply)

            if is_shared:
                url = getattr(self.opts, "%s_update_url" %self.current_user.port)
                source_url = "%s/r/%s" %(self.prefix_url, reply.mid)
                status = "%s\n%s" %(content.replace("#%s" %self.current_user.port, ""), source_url)
                
                message_request(
                    url, 
                    self.current_user.port, 
                    self.current_user.access_token, 
                    status,
                    app_key=getattr(self.opts, "%s_key" %self.current_user.port),
                    openid=self.current_user.oauth_id,
                    client_ip=self.request.remote_ip)
      
            self.finishedMsg(status="success", info="post reply success", next=self.request.path)
        else:
            self.finishedMsg(status="error", info="topic not found")  
Esempio n. 2
0
    def post(self, mid):
        content = self.get_argument("content", None)
        is_shared = self.get_argument("is_shared", "off")
        is_shared = True if is_shared == "on" else False
        if content and mid:
            if 5 < len(content) < 280:
                topic = Topic.getObjectByObjectId(mid)
                if topic:
                    reply = Reply.save_reply(
                        source=topic,
                        content=content, 
                        owner=self.current_user,
                        topic=topic
                    )

                    # Update level if level=1
                    self.current_user.upgrade_level(xp=1)

                    # Send mention for @username format notification
                    names_list = get_usernames(content)
                    for username in names_list:
                        send_mentions(sender=self.current_user, username=username, reply=reply)
                    
                    if is_shared:
                        url = getattr(self.opts, "%s_update_url" %self.current_user.port)
                        source_url = "%s/t/%s" %(self.prefix_url, topic.mid)                        
                        status = "%s\n%s" %(content.replace("#%s" %self.current_user.port, ""), source_url)
                        attachments = self.dumpsJson(title="GitPub", 
                            href="%s/t/%s" %(self.prefix_url, topic.mid), description="From GitPub: ")
                        message_request(
                            url, 
                            self.current_user.port, 
                            self.current_user.access_token, 
                            status,
                            app_key=getattr(self.opts, "%s_key" %self.current_user.port),
                            openid=self.current_user.oauth_id,
                            client_ip=self.request.remote_ip,
                            attachments=attachments)
                                          
                    self.finishedMsg(status="success", info="post reply success", next=self.request.path)
                else:
                    self.finishedMsg(status="error", info="wrong topic shorted")
            else:
                self.finishedMsg(status="error", info="reply too long or too short")
        else:
            self.finishedMsg(status="error", info="incorrect arguments")
Esempio n. 3
0
    def post(self):
        content = self.get_argument("content").encode("utf-8")
        is_shared = self.get_argument("is_shared", "off")
        is_shared = True if is_shared == "on" else False

        topic = Topic.create(member=self.current_user, source_created=datetime.now(), 
            created=datetime.now(), content=content)
        self.current_user.upgrade_level(xp=1)

        #Send mention for @username format notification
        names_list = get_usernames(content)
        for username in names_list:
            send_mentions(sender=self.current_user, username=username, topic=topic)

        if is_shared:    
            identity = None
            request_url = self.oauth_update_url + "?" + urlencode({"access_token": self.current_user.access_token})
            header = HTTPHeaders({'Authorization': 'OAuth2 %s' % self.current_user.access_token})
            http_client = AsyncHTTPClient()

            if self.current_user.port == "weibo":                
                http_client.fetch(request_url, method="POST", body=urlencode({'status': content}), 
                    callback=(yield gen.Callback("resp")), headers=header)

                response = yield gen.Wait("resp")
                if response.code == 200:
                    body = json.loads(response.body)
                    identity = "%s#%s" %(body['id'], self.current_user.port)
                    topic.identity = identity
                    topic.save()                    
                    self.finishedMsg(status="success", info="Post topic success", next="/t/%s" %topic.mid)
                else:
                    self.finishedMsg(status="error", info="Post to weibo failed")

            elif self.current_user.port == "tencent":
                kwargs = {"access_token": self.current_user.access_token, 
                            "oauth_consumer_key": getattr(self.opts, "%s_key" %self.current_user.port),
                            "openid": self.current_user.oauth_id,
                            "client_ip": self.request.remote_ip,
                            "oauth_version": "2.a",
                            "scope":"all",
                            "format": "json",
                            "content": content}
                
                http_client.fetch(request_url, method="POST", body=urlencode(kwargs), 
                    callback=(yield gen.Callback("resp")), headers=header)

                response = yield gen.Wait("resp")
                if response.code == 200:
                    body = json.loads(response.body)
                    if body.get("errcode") == "ok":
                        identity = "%s#%s" %(body["data"]["id"], self.current_user.port)
                        topic.identity = identity
                        topic.save()
                        self.finishedMsg(status="success", info="Post topic success", next="/t/%s" %topic.mid)
                    else:
                        self.finishedMsg(status="error", info="Post to tencent failed")
                else:
                    self.finishedMsg(status="error", info="Post to tencent failed")
                

            elif self.current_user.port == "douban":
                header = HTTPHeaders({'Authorization': 'Bearer %s' % self.current_user.access_token})
                attachments = self.dumpsJson(title="GitPub", 
                    href="%s/t/%s" %(self.prefix_url, topic.mid), description="From GitPub: ")
                kwargs = {"access_token": self.current_user.access_token, 
                          "source": getattr(self.opts, "%s_key" %self.current_user.port),
                          "text": content,
                          "attachments": attachments}
                
                http_client.fetch(self.oauth_update_url, method="POST", body=urlencode(kwargs), 
                    callback=(yield gen.Callback("resp")), headers=header)

                response = yield gen.Wait("resp")
                if response.code == 200:
                    body = json.loads(response.body)
                    identity = "%s#%s" %(body["id"], self.current_user.port)
                    topic.identity = identity
                    topic.save()
                    self.finishedMsg(status="success", info="Post topic success", next="/t/%s" %topic.mid)
                else:
                    self.finishedMsg(status="error", info="Post to douban failed")
            else:
                raise HTTPError(500, "Port not supported")
        else:
            self.finishedMsg(status="success", info="Post topic success", next="/t/%s" %topic.mid)