Ejemplo n.º 1
0
def test_user_info():
    response_json = api.get_user_info('invalid_user')
    print(json.dumps(response_json, indent=3))
    assert len(response_json)==0
    response_json = api.add_user(USER1_UID, USER1_NAME)
    print("Add user response:\n{}".format(json.dumps(response_json, indent=3)))
    response_json = api.get_user_info(USER1_UID)
    print(json.dumps(response_json, indent=3))
    assert len(response_json)>0
Ejemplo n.º 2
0
def test_notifications():
    api.add_user(USER1_UID, USER1_NAME)
    response_json = api.get_user_info(USER1_UID)
    print(json.dumps(response_json, indent=3))
    api.add_user(USER2_UID, USER2_NAME)
    response_json = api.get_user_info(USER2_UID)
    print(json.dumps(response_json, indent=3))
    new_agreed_answer = "new agreed answer"
    print("Agreed answer: {}\n".format(new_agreed_answer))
    api.store_response(EID_1, USER1_UID, new_agreed_answer)
    api.store_response(EID_1, USER2_UID, new_agreed_answer)
    api.store_response(EID_1, USER1_UID, random_string_generator(15))
    api.store_response(EID_1, USER1_UID, random_string_generator(15))
    api.store_response(EID_1, USER1_UID, random_string_generator(15))    
    api.store_response(EID_1, USER1_UID, random_string_generator(15))   
Ejemplo n.º 3
0
 def last_5_members(self):
   members = self.info.get('members')
   if members:
     members = list(set(members))[-5:]
     members = [api.get_user_info(user_id) for user_id in members]
     members = sorted(members, key=lambda k: k.last_online, reverse=False)
     return members
Ejemplo n.º 4
0
 def wrapper(update, context, *args, **kwargs):
     if (get_user_info(update.effective_user.id)):
         func(update, context, *args, **kwargs)
     else:
         context.bot.send_message(chat_id=update.effective_user.id,
                                  text="no {}".format(
                                      update.effective_user.id))
Ejemplo n.º 5
0
    def seen_by(self):
        if not self.info:
            return []
        users = []
        user_ids = set()
        if self.info.get('read_receipts'):
            # dirty hack
            if self.info['owner'] == self.info['read_receipts'][0]['user_id']:
                self.info['read_receipts'].pop(0)

            for record in self.info.get('read_receipts')[::-1]:
                timestamp = record.get('timestamp')
                user_id = record.get('user_id')

                if user_id not in user_ids and not api.is_group(
                        user_id, db_name=self.db_name):
                    users.append({
                        'user':
                        api.get_user_info(user_id, db_name=self.db_name),
                        'timestamp':
                        timestamp
                    })
                    user_ids.add(user_id)
        users.sort(key=lambda k: k.get('timestamp'), reverse=True)
        return users
Ejemplo n.º 6
0
 def last_5_members(self):
   members = self.info.get('members')
   if members:
     members = list(set(members))[-5:]
     members = [api.get_user_info(user_id) for user_id in members]
     members = sorted(members, key=lambda k: k.last_online, reverse=False)
     return members
Ejemplo n.º 7
0
def get_user_information():
    """
    Возвращает информацию по пользователю

    :return: Информация по пользователю
    :rtype: str
    """

    # get data
    try:
        raw_data = request.data.decode()
        data = json.loads(raw_data)

        # fill params
        ssid = data.get('sid')
        user_id = data.get('user_id')

        # invoke data mining
        user_info = get_user_info(user_id, ssid)

        # build JSON string
        user_info = json.dumps(user_info)

    except BaseException as exc:
        return str(exc), 500

    return str(user_info)
Ejemplo n.º 8
0
 def viewers(self):
   if self.info.has_key('viewers'):
     viewers_list = [api.get_user_info(user_id) for user_id in self.info.get('viewers')]
     groups = users = []
     for i in viewers_list:
       if i.is_group():
         groups.append(i)
       else:
         users.append(i)
     users.extend(groups)
     return users
Ejemplo n.º 9
0
 def viewers(self):
   if self.info.has_key('viewers'):
     viewers_list = [api.get_user_info(user_id) for user_id in self.info.get('viewers')]
     groups = users = []
     for i in viewers_list:
       if i.is_group():
         groups.append(i)
       else:
         users.append(i)
     users.extend(groups)
     return users
Ejemplo n.º 10
0
 def members(self):
   members = self.info.get('members')
   if members:
     members = set(members)
     members = [api.get_user_info(user_id) for user_id in members]
     if self.id == 'public':
       members = sorted(members, key=lambda k: k.timestamp, reverse=True)
     else:
       members = sorted(members, key=lambda k: k.last_online, reverse=True)
     
     return members
   else:
     return []
Ejemplo n.º 11
0
 def members(self):
   members = self.info.get('members')
   if members:
     members = set(members)
     members = [api.get_user_info(user_id) for user_id in members]
     if self.id == 'public':
       members = sorted(members, key=lambda k: k.timestamp, reverse=True)
     else:
       members = sorted(members, key=lambda k: k.last_online, reverse=True)
     
     return members
   else:
     return []
Ejemplo n.º 12
0
 def seen_by(self):
   users = []
   user_ids = set()
   if self.info.get('read_receipts'):
     # dirty hack
     if self.info['owner'] == self.info['read_receipts'][0]['user_id']:
       self.info['read_receipts'].pop(0)
       
     for record in self.info.get('read_receipts')[::-1]:
       timestamp = record.get('timestamp')
       user_id = record.get('user_id')
         
       if user_id not in user_ids and not api.is_group(user_id):
         users.append({'user': api.get_user_info(user_id),
                       'timestamp': timestamp})
         user_ids.add(user_id)
   users.sort(key=lambda k: k.get('timestamp'), reverse=True)
   return users
Ejemplo n.º 13
0
 def read_receipts(self):
   users = []
   user_ids = set()
   if self.info.get('read_receipts'):
     # dirty hack
     if self.info['owner'] == self.info['read_receipts'][0]['user_id']:
       self.info['read_receipts'].pop(0)
       
     for record in self.info.get('read_receipts'):
       timestamp = record.get('timestamp')
       if timestamp > self.last_updated:
         user_id = record.get('user_id')
           
         if user_id not in user_ids and not api.is_group(user_id):
           users.append({'user': api.get_user_info(user_id),
                         'timestamp': timestamp})
           user_ids.add(user_id)
       
   users.sort(key=lambda k: k.get('timestamp'), reverse=True)
   return users
Ejemplo n.º 14
0
 def recently_viewed(self):
   records = self.info.get('recently_viewed', [])
   user_ids = []
   users = []
   records.reverse()
   for record in records:
     if not record:
       continue
     user_id = record['user_id']
     if user_id and user_id not in user_ids:
       user_ids.append(user_id)
       user = api.get_user_info(user_id)
       if user.id:
         users.append({'user': user,
                       'timestamp': record['timestamp']})
         
         if len(users) >= 5:
           break
   
   return users
Ejemplo n.º 15
0
 def recently_viewed(self):
   records = self.info.get('recently_viewed', [])
   user_ids = []
   users = []
   records.reverse()
   for record in records:
     if not record:
       continue
     user_id = record['user_id']
     if user_id and user_id not in user_ids:
       user_ids.append(user_id)
       user = api.get_user_info(user_id)
       if user.id:
         users.append({'user': user,
                       'timestamp': record['timestamp']})
         
         if len(users) >= 5:
           break
   
   return users
Ejemplo n.º 16
0
 def stats(self):
   info = {}
   for comment in self.info.get('comments', []):
     user_id = comment.get('owner')
     if info.has_key(user_id):
       info[user_id] += 1
     else:
       info[user_id] = 1
   
   owner_id = self.info.get('owner')
   if info.has_key(owner_id):
     info[owner_id] += 1
   else:
     info[owner_id] = 1
   
   out = []
   for user_id in info.keys():
     out.append({'user': api.get_user_info(user_id),
                 'post_count': info[user_id]})
   
   out.sort(key=lambda k: k['post_count'], reverse=True)
   return out
Ejemplo n.º 17
0
 def read_receipts(self):
   users = []
   user_ids = set()
   if self.info.get('read_receipts'):
     # dirty hack
     if self.info['owner'] == self.info['read_receipts'][0]['user_id']:
       self.info['read_receipts'].pop(0)
       
     for record in self.info.get('read_receipts'):
       timestamp = record.get('timestamp')
       if timestamp > self.last_updated:
         user_id = record.get('user_id')
           
         if user_id not in user_ids \
         and not api.is_group(user_id, db_name=self.db_name):
           users.append({'user': api.get_user_info(user_id, 
                                                   db_name=self.db_name),
                         'timestamp': timestamp})
           user_ids.add(user_id)
       
   users.sort(key=lambda k: k.get('timestamp'), reverse=True)
   return users
Ejemplo n.º 18
0
    def stats(self):
        info = {}
        for comment in self.info.get('comments', []):
            user_id = comment.get('owner')
            if info.has_key(user_id):
                info[user_id] += 1
            else:
                info[user_id] = 1

        owner_id = self.info.get('owner')
        if info.has_key(owner_id):
            info[owner_id] += 1
        else:
            info[owner_id] = 1

        out = []
        for user_id in info.keys():
            out.append({
                'user': api.get_user_info(user_id, db_name=self.db_name),
                'post_count': info[user_id]
            })

        out.sort(key=lambda k: k['post_count'], reverse=True)
        return out
Ejemplo n.º 19
0
 def sender(self):
   user_id = self.info.get('from')
   if user_id:
     return api.get_user_info(user_id, db_name=self.db_name)
Ejemplo n.º 20
0
 def owner(self):
   if self.info.has_key('message'):
     return api.get_user_info(self.info.get('owner'))
   else:
     return api.get_user_info(self.info.get('version')[-1].get('owner'))
Ejemplo n.º 21
0
 def owner(self):
     return api.get_user_info(self.info.get('owner'), db_name=self.db_name)
Ejemplo n.º 22
0
 def contacts(self):
   users = [api.get_user_info(user_id) for user_id in self.contact_ids]
   users.sort(key=lambda k: k.last_online, reverse=True)
   return users
Ejemplo n.º 23
0
 def starred_by(self):
     return [api.get_user_info(user_id, db_name=self.db_name) \
             for user_id in self.info.get('starred', [])[-5:]]
Ejemplo n.º 24
0
def autolink(text):
    if not text:
        return text

    key = '%s:autolink' % hash(text)
    out = cache.get(key, namespace="filters")
    if out:
        return out

    if re.match(EMAIL_RE, text):
        email = text
        user_id = api.get_user_id_from_email_address(email)
        user = api.get_user_info(user_id)
        return '<a href="/user/%s" class="async">%s</a>' % (user.id, user.name)

    s = text or ''
    s += ' '
    s = str(s)  # convert unicode to string
    s = s.replace('\r\n', '\n')

    urls = api.extract_urls(s)
    urls = list(set(urls))
    urls.sort(key=len, reverse=True)

    for url in urls:
        hash_string = md5(url).hexdigest()
        info = api.get_url_info(url)
        if not url.startswith('http'):
            s = s.replace(
                url, '<a href="http://%s/" target="_blank" title="%s">%s</a>' %
                (hash_string, info.title if info.title else hash_string,
                 hash_string))

        elif len(url) > 60:
            u = url[:60]

            for template in ['%s ', ' %s', '\n%s', '%s\n', '%s.', '%s,']:
                if template % url in s:
                    s = s.replace(
                        template % url, template %
                        ('<a href="%s" target="_blank" title="%s">%s</a>' %
                         (hash_string, info.title if info.title else
                          hash_string, md5(u + '...').hexdigest())))
                    break
        else:
            for template in ['%s ', ' %s', '\n%s', '%s\n', '%s.', '%s,']:
                if template % url in s:
                    s = s.replace(
                        template % url, template %
                        ('<a href="%s" target="_blank" title="%s">%s</a>' %
                         (hash_string, info.title
                          if info.title else hash_string, hash_string)))
                    break

    for url in urls:
        s = s.replace(md5(url).hexdigest(), url)
        if len(url) > 60 and url.startswith('http'):
            s = s.replace(md5(url[:60] + '...').hexdigest(), url[:60] + '...')

    mentions = MENTIONS_RE.findall(s)
    if mentions:
        for mention in mentions:
            if '](topic:' in mention:
                topic = re.compile('@\[(?P<name>.+)\]\((?P<id>.*)\)').match(
                    mention).groupdict()
                topic['id'] = topic['id'].split(':', 1)[-1]

                #TODO: update topic name?
                s = s.replace(
                    mention, '<a href="/chat/topic/%s" class="chat">%s</a>' %
                    (topic.get('id'), topic.get('name')))
            elif '](user:'******'@\[(?P<name>.+)\]\((?P<id>.*)\)').match(
                    mention).groupdict()
                user['id'] = user['id'].split(':', 1)[-1]
                s = s.replace(
                    mention,
                    '<a href="/user/%s" class="async"><span class="tag">%s</span></a>'
                    % (user.get('id'), user.get('name')))
            else:
                group = re.compile('@\[(?P<name>.+)\]\((?P<id>.*)\)').match(
                    mention).groupdict()
                group['id'] = group['id'].split(':', 1)[-1]
                s = s.replace(
                    mention,
                    '<a href="/group/%s" class="async"><span class="tag">%s</span></a>'
                    % (group.get('id'), group.get('name')))


#  hashtags = re.compile('(#\[.*?\))').findall(s)
#  if hashtags:
#    for hashtag in hashtags:
#      tag = re.compile('#\[(?P<name>.+)\]\((?P<id>.*)\)').match(hashtag).groupdict()
#      tag['id'] = tag['id'].split(':', 1)[-1]
#      s = s.replace(hashtag,
#           '<a href="?hashtag=%s" class="overlay"><span class="tag">%s</span></a>' % (tag.get('id'), tag.get('name')))

    cache.set(key, s, namespace="filters")
    return s
Ejemplo n.º 25
0
def last_starred_user(following_users, starred_list):
    user_ids = set([u
                    for u in following_users]).intersection(set(starred_list))
    if user_ids:
        user_id = list(user_ids)[-1]
        return api.get_user_info(user_id)
Ejemplo n.º 26
0
 def contacts(self):
   return [api.get_user_info(user_id) for user_id in self.contact_ids]
Ejemplo n.º 27
0
def last_starred_user(following_users, starred_list):
  user_ids = set([u for u in following_users]).intersection(set(starred_list))
  if user_ids:
    user_id = list(user_ids)[-1]
    return api.get_user_info(user_id)
Ejemplo n.º 28
0
def autolink(text):  
  if not text:
    return text
  
  key = '%s:autolink' % hash(text)
  out = cache.get(key, namespace="filters")
  if out:
    return out
  
  if re.match(EMAIL_RE, text):
    email = text 
    user_id = api.get_user_id_from_email_address(email)
    user = api.get_user_info(user_id)
    return '<a href="/user/%s" class="async">%s</a>' % (user.id, user.name)
    
  s = text or ''
  s += ' '
  s = str(s) # convert unicode to string
  s = s.replace('\r\n', '\n')

  
  urls = api.extract_urls(s)
  urls = list(set(urls))
  urls.sort(key=len, reverse=True)
  
  for url in urls:
    hash_string = md5(url).hexdigest()
    info = api.get_url_info(url)
    if not url.startswith('http'):
      s = s.replace(url, '<a href="http://%s/" target="_blank" title="%s">%s</a>' % (hash_string, info.title if info.title else hash_string, hash_string))
    
    elif len(url) > 60:
      u = url[:60]
        
      for template in ['%s ', ' %s', '\n%s', '%s\n', '%s.', '%s,']:
        if template % url in s:
          s = s.replace(template % url, 
                        template % ('<a href="%s" target="_blank" title="%s">%s</a>' % (hash_string, info.title if info.title else hash_string, md5(u + '...').hexdigest())))
          break
    else:
      for template in ['%s ', ' %s', '\n%s', '%s\n', '%s.', '%s,']:
        if template % url in s:
          s = s.replace(template % url, 
                        template % ('<a href="%s" target="_blank" title="%s">%s</a>' % (hash_string, info.title if info.title else hash_string, hash_string)))
          break
        
  for url in urls:
    s = s.replace(md5(url).hexdigest(), url)
    if len(url) > 60 and url.startswith('http'):
      s = s.replace(md5(url[:60] + '...').hexdigest(), url[:60] + '...')
      
  
  mentions = MENTIONS_RE.findall(s)
  if mentions:
    for mention in mentions:
      if '](topic:' in mention:
        topic = re.compile('@\[(?P<name>.+)\]\((?P<id>.*)\)').match(mention).groupdict()
        topic['id'] = topic['id'].split(':', 1)[-1]
        
        #TODO: update topic name?
        s = s.replace(mention, 
             '<a href="/chat/topic/%s" class="chat">%s</a>' % (topic.get('id'), topic.get('name')))
      elif '](user:'******'@\[(?P<name>.+)\]\((?P<id>.*)\)').match(mention).groupdict()
        user['id'] = user['id'].split(':', 1)[-1]
        s = s.replace(mention, 
             '<a href="/chat/user/%s" class="chat"><span class="tag">%s</span></a>' % (user.get('id'), user.get('name')))
      else:
        group = re.compile('@\[(?P<name>.+)\]\((?P<id>.*)\)').match(mention).groupdict()
        group['id'] = group['id'].split(':', 1)[-1]
        s = s.replace(mention, 
             '<a href="/group/%s" class="async"><span class="tag">%s</span></a>' % (group.get('id'), group.get('name')))
        
#  hashtags = re.compile('(#\[.*?\))').findall(s)
#  if hashtags:
#    for hashtag in hashtags:
#      tag = re.compile('#\[(?P<name>.+)\]\((?P<id>.*)\)').match(hashtag).groupdict()
#      tag['id'] = tag['id'].split(':', 1)[-1]
#      s = s.replace(hashtag, 
#           '<a href="?hashtag=%s" class="overlay"><span class="tag">%s</span></a>' % (tag.get('id'), tag.get('name')))
  
  cache.set(key, s, namespace="filters")
  return s
Ejemplo n.º 29
0
 def members(self):
   return [api.get_user_info(user_id) for user_id in self.member_ids]
Ejemplo n.º 30
0
 def user(self):
     user_id = self.info.get('user_id', self.info.get('owner'))
     return api.get_user_info(user_id, db_name=self.db_name)
Ejemplo n.º 31
0
 def receiver(self):
   return api.get_user_info(self.info.get('receiver'), db_name=self.db_name)
Ejemplo n.º 32
0
 def owner(self):
     if self.info.has_key('message'):
         return api.get_user_info(self.info.get('owner'))
     else:
         return api.get_user_info(self.info.get('version')[-1].get('owner'))
Ejemplo n.º 33
0
 def sender(self):
   user_id = self.info.get('sender')
   return api.get_user_info(user_id)
Ejemplo n.º 34
0
 def sender(self):
     user_id = self.info.get('from')
     if user_id:
         return api.get_user_info(user_id, db_name=self.db_name)
Ejemplo n.º 35
0
 def administrators(self):
   return [api.get_user_info(user_id) for user_id in self.info.get('leaders', [])]
Ejemplo n.º 36
0
 def receiver(self):
     user_id = self.info.get('to')
     return api.get_user_info(user_id, db_name=self.db_name)
Ejemplo n.º 37
0
 def owner(self):
     return api.get_user_info(
         self.info.get('version')[self.version_index].get('owner'),
         db_name=self.db_name)
Ejemplo n.º 38
0
 def members(self):
     return [api.get_user_info(user_id) for user_id in self.member_ids]
Ejemplo n.º 39
0
 def starred_by(self):
   return [api.get_user_info(user_id) for user_id in self.info.get('starred', [])[-5:]]
Ejemplo n.º 40
0
 def owner(self):
     return api.get_user_info(self.info.get('owner'))
Ejemplo n.º 41
0
 def user(self):
   user_id = self.info.get('user_id', self.info.get('owner'))
   return api.get_user_info(user_id)  
Ejemplo n.º 42
0
 def owner(self):
   return api.get_user_info(self.info.get('version')[self.version_index].get('owner'))
Ejemplo n.º 43
0
 def receiver(self):
   user_id = self.info.get('receiver')
   return api.get_user_info(user_id)
Ejemplo n.º 44
0
 def receiver(self):
   return api.get_user_info(self.info.get('receiver'))
Ejemplo n.º 45
0
 def receiver(self):
   user_id = self.info.get('to')
   return api.get_user_info(user_id, db_name=self.db_name)
Ejemplo n.º 46
0
 def liked_by(self):
     return [api.get_user_info(user_id, db_name=self.db_name) \
             for user_id in self.liked_user_ids]
Ejemplo n.º 47
0
 def sender(self):
   return api.get_user_info(self.info.get('sender'), db_name=self.db_name)
Ejemplo n.º 48
0
 def contacts(self):
     users = [api.get_user_info(user_id, db_name=self.db_name) \
              for user_id in self.contact_ids]
     users.sort(key=lambda k: k.last_online, reverse=True)
     return users
Ejemplo n.º 49
0
 def liked_by(self):
   return [api.get_user_info(user_id) for user_id in self.liked_user_ids]
Ejemplo n.º 50
0
 def following_details(self):
     return [api.get_user_info(user_id, db_name=self.db_name) \
             for user_id in api.get_following_users(self.info['_id'],
                                                    db_name=self.db_name)]
Ejemplo n.º 51
0
 def following_details(self):
   return [api.get_user_info(user_id) for user_id in api.get_following_users(self.info['_id'])]
Ejemplo n.º 52
0
 def sender(self):
   return api.get_user_info(self.info.get('sender'))
Ejemplo n.º 53
0
 def owner(self):
   return api.get_user_info(self.info.get('owner'), db_name=self.db_name)
Ejemplo n.º 54
0
 def leaders(self):
     return [api.get_user_info(user_id, db_name=self.db_name) \
             for user_id in self.info.get('leaders')]
Ejemplo n.º 55
0
 def leaders(self):
   return [api.get_user_info(user_id) for user_id in self.info.get('leaders')]
Ejemplo n.º 56
0
 def administrators(self):
     return [api.get_user_info(user_id, db_name=self.db_name) \
             for user_id in self.info.get('leaders', [])]
Ejemplo n.º 57
0
 def sender(self):
     return api.get_user_info(self.info.get('sender'), db_name=self.db_name)
Ejemplo n.º 58
0
 def administrators(self):
   return [api.get_user_info(user_id) for user_id in self.info.get('leaders', [])]
Ejemplo n.º 59
0
 def owner(self):
   return api.get_user_info(self.info.get('owner'))
Ejemplo n.º 60
0
 def receiver(self):
     return api.get_user_info(self.info.get('receiver'),
                              db_name=self.db_name)