-
Notifications
You must be signed in to change notification settings - Fork 1
/
db_actions.py
107 lines (93 loc) · 3.49 KB
/
db_actions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
from boto3.dynamodb.conditions import Attr
def update_users_followers(username, follower_id, table, remove=False):
'''
Find all the users that %username% follows and
update their "followers" list and "followers_count" amount
'''
item = table.get_item(Key={'username': username}).get('Item', False)
item['followers'].remove(follower_id) if remove else item['followers'].append(follower_id)
table.update_item(
Key={
'username': username
},
UpdateExpression='SET followers = :val1',
ExpressionAttributeValues={
':val1': item['followers'],
},
)
def follow_user(username, user_id, table):
item = table.get_item(Key={'username': username})['Item']
new_follow = set([user_id]) - set(item['follow']) - set([username])
if not new_follow:
return False
new_item = table.update_item(
Key={
'username': username
},
UpdateExpression='SET follow = list_append(follow, :val1), follow_count = follow_count + :val2',
ExpressionAttributeValues={
':val1': list(new_follow),
':val2': len(new_follow)
},
ReturnValues="UPDATED_NEW"
)
update_users_followers(user_id, username, table, remove=False)
return True
# update_user_real_follow_count(username)
def get_followers_list(username, table):
user_following = table.get_item(Key={'username': username})['Item']['follow']
return table.scan(
FilterExpression=Attr('username').is_in(user_following)
)['Items']
def unfollow_user(username, user_id, table):
item = table.get_item(Key={'username': username})['Item']
item['follow'].remove(user_id)
table.update_item(
Key={
'username': username
},
UpdateExpression='SET follow = :val1',
ExpressionAttributeValues={
':val1': item['follow'],
}
)
update_users_followers(user_id, username, table, remove=True)
def create_user(update, table):
username = str(update['message']['chat']['id'])
followers = table.scan(
FilterExpression=Attr('follow').contains(username)
)
table.put_item(
Item={
'username': username,
'first_name': update.message.from_user.first_name.upper(),
'last_name': update.message.from_user.last_name.upper() if update.message.from_user.last_name else None,
'follow': [],
'follow_count': 0,
'followers': [x['username'] for x in followers['Items']],
'photo_id': 0
}
)
def update_user(update, table):
username = str(update['message']['chat']['id'])
followers = table.scan(
FilterExpression=Attr('follow').contains(username)
)
item = table.get_item(Key={'username': username})['Item']
item['first_name'] = update.message.from_user.first_name.upper()
if update.message.from_user.last_name:
item['last_name'] = update.message.from_user.last_name.upper()
item['follow_count'] = len(item['follow'])
item['followers'] = [x['username'] for x in followers['Items']]
item['photo_id'] = item.get('photo_id', 0)
table.put_item(Item=item)
def update_user_photo(photo, username, table):
table.update_item(
Key={
'username': username
},
UpdateExpression='SET photo_id = :val1',
ExpressionAttributeValues={
':val1': photo[-1]['file_id'],
},
)