Beispiel #1
0
def get_contacts(uid, last_update_date, size):
    contacts = Contact.find_by_cursor(uid, last_update_date, size)
    next_cursor = None
    has_more = False
    if len(contacts) == size:
        has_more = True
        update_timestamp = datetime_to_timestamp(contacts[-1].updated_at)
        next_cursor = encode_id(timestamp_to_str(update_timestamp))
    uids = set()
    for contact in contacts:
        uids.add(contact.uidA)
        uids.add(contact.uidB)
    users = User.find_by_ids(uids)
    dict_users = list_to_dict(users, "id")
    json_contacts = []
    for contact in contacts:
        cuid = contact.uidB if contact.uidA == uid else contact.uidA
        user = dict_users.get(cuid)
        if user:
            json_contacts.append({
                'id':
                encode_id(contact.id),
                'user':
                user.to_dict(),
                'type':
                contact.type,
                'update_at':
                datetime_to_timestamp(contact.updated_at)
            })
    return {
        "has_more": has_more,
        "next_cursor": next_cursor,
        "contacts": json_contacts
    }
Beispiel #2
0
def get_rc_token(user):
    body = {
        'userId': encode_id(user.id),
        'name': user.user_name,
        'portraitUri': "http://39.107.239.252/2018/avatar/England-round.png"
    }
    app_key = os.getenv("RONG_KEY")
    app_sec = os.getenv("RONG_SEC")
    timestamp = int(time.time() * 1000)
    nonce = hashlib.md5(str(time.time())).hexdigest()
    v = "{}{}{}".format(app_sec, nonce, timestamp)
    sig = hashlib.sha1(v).hexdigest()
    headers = {
        'App-Key': app_key,
        'Timestamp': str(timestamp),
        "Nonce": nonce,
        "Signature": sig
    }
    resp = requests.post('https://api.cn.ronghub.com/user/getToken.json',
                         data=body,
                         headers=headers)
    print resp.text
    if resp.status_code == 200:
        return json.loads(resp.text)["token"]
    else:
        return None
Beispiel #3
0
 def to_dict(self):
     return {
         'id': encode_id(self.id),
         'from_user': self.from_user.to_dict(),
         'text': self.text,
         'update_at': datetime_to_timestamp(self.update_at)
     }
Beispiel #4
0
 def to_dict(self):
     return {
         'id': encode_id(self.id),
         'from_user': self.fromUser.to_dict(),
         'type': self.type,
         'create_at': datetime_to_timestamp(self.create_at)
     }
Beispiel #5
0
 def to_dict(self):
     return {
         "id": encode_id(self.id),
         "name": self.name,
         "icon": self.icon,
         "alias": self.alias
     }
Beispiel #6
0
 def to_dict(self):
     return {
         "id": encode_id(self.id),
         "start_year": self.start_year,
         "start_month": self.start_month,
         "end_year": self.end_year,
         "end_month": self.end_month,
         "title": self.title,
         "company": self.company.to_dict()
     }
Beispiel #7
0
 def to_dict(self):
     return {
         'id': encode_id(self.id),
         'user': self.user.to_dict(),
         'type': self.type,
         'text': self.text,
         'like_count': self.like_count,
         'comment_count': self.comment_count,
         'create_at': datetime_to_timestamp(self.create_at)
     }
Beispiel #8
0
 def to_dict(self):
     return {
         "id": encode_id(self.id),
         "user": self.user.to_dict(),
         "userA": self.userA.to_dict(),
         "userB": self.userB.to_dict(),
         "type": self.type,
         "status": self.status,
         "comment": self.comment,
         "create_at": datetime_to_timestamp(self.created_at)
     }
Beispiel #9
0
def accept_contact_request(id, uid, accept):
    request = ContactRequest.query.filter(
        ContactRequest.id == id, ContactRequest.uidB == uid).one_or_none()
    if request and request.status == ContactRequestStatus.Pending:
        request.status = ContactRequestStatus.Accepted if accept else ContactRequestStatus.Rejected
        request.end_at = arrow.utcnow().naive
        db.session.commit()
        if accept:
            result = Contact.add(request.uidA, request.uidB, request.type)
            _set_user_for_requests([request])
            if result:
                # Update the endorsement total contacts
                Endorsement.update_total_contacts_count(request.uidA, 1)
                Endorsement.update_total_contacts_count(request.uidB, 1)
                message = "已成为联系人"
                uidA = encode_id(request.uidA)
                uidB = encode_id(request.uidB)
                rc_service.send_private_notification(uidA, uidB, message)
                rc_service.send_private_notification(uidB, uidA, message)
            return request.to_dict()
Beispiel #10
0
def get_contact_requests(uid, last_request_id, size):
    requests = ContactRequest.find_by_cursor(uid, last_request_id, size)
    _set_user_for_requests(requests)
    next_cursor = None
    has_more = False
    if len(requests) == size:
        next_cursor = encode_id(requests[-1].id)
        has_next = True
    return {
        "next_cursor": next_cursor,
        "has_more": has_more,
        "requests": [_.to_dict() for _ in requests]
    }
Beispiel #11
0
def get_feeds(uid, last_id, size):
    feeds = Feed.find_by_cursor(last_id, size)
    has_more = False
    next_cursor = None
    if len(feeds) == size:
        has_more = True
        next_cursor = encode_id(feeds[-1].id)
    dict_feeds = []
    for feed in feeds:
        feed_like = FeedLike.find_by_uid(uid, feed.id)
        liked = (feed_like is not None and feed_like.status == FeedLikeStatus.Liked)
        dict_feed = feed.to_dict()
        dict_feed['liked'] = liked
        if feed.images:
            images = [image.to_dict() for image in Image.find_by_ids(feed.images)]
            dict_feed['images'] = images
        dict_feeds.append(dict_feed)
    return {
        "has_more": has_more,
        "next_cursor": next_cursor,
        "feeds": dict_feeds
    }
Beispiel #12
0
def refresh_user_info(user):
    rc = RongCloud(app_key=os.getenv("RONG_KEY"),
                   app_secret=os.getenv("RONG_SEC"))
    rc.User.refresh(encode_id(user.id), user.user_name, user.avatar_url)