@@ -28,7 +28,9 @@ class ReactionCounts:
2828 node_id : str
2929 up : int
3030 down : int
31+ upvotes : int
3132 fetched_at : int
33+ reactions : dict [str , int ]
3234
3335
3436@dataclass (frozen = True , slots = True )
@@ -71,7 +73,10 @@ def reactions(self, resource_ids: Iterable[str]) -> dict[str, ReactionCounts]:
7173 with self .connect () as connection :
7274 rows = connection .execute (REACTIONS , (json .dumps (keys ),)).fetchall ()
7375 return {
74- row [0 ]: ReactionCounts (node_id = row [1 ], up = row [2 ], down = row [3 ], fetched_at = row [4 ])
76+ row [0 ]: ReactionCounts (
77+ node_id = row [1 ], up = row [2 ], down = row [3 ], upvotes = row [4 ], fetched_at = row [5 ],
78+ reactions = json .loads (row [6 ]),
79+ )
7580 for row in rows
7681 }
7782
@@ -86,7 +91,10 @@ def tracked_reactions(
8691 with self .connect () as connection :
8792 rows = connection .execute (TRACKED_REACTIONS , (after , fetched_before , limit )).fetchall ()
8893 return [
89- (row [0 ], ReactionCounts (node_id = row [1 ], up = row [2 ], down = row [3 ], fetched_at = row [4 ]))
94+ (row [0 ], ReactionCounts (
95+ node_id = row [1 ], up = row [2 ], down = row [3 ], upvotes = row [4 ], fetched_at = row [5 ],
96+ reactions = json .loads (row [6 ]),
97+ ))
9098 for row in rows
9199 ]
92100
@@ -101,6 +109,8 @@ def put_discussion(
101109 url : str ,
102110 up : int = 0 ,
103111 down : int = 0 ,
112+ upvotes : int = 0 ,
113+ reactions : dict [str , int ] | None = None ,
104114 fetched_at : int | None = None ,
105115 ) -> None :
106116 fetched = int (time .time ()) if fetched_at is None else fetched_at
@@ -119,22 +129,42 @@ def put_discussion(
119129 fetched ,
120130 ),
121131 )
132+ connection .execute (
133+ "UPDATE discussions SET upvotes = ? WHERE resource_id = ?" ,
134+ (upvotes , resource_id ),
135+ )
136+ if reactions :
137+ connection .executemany (
138+ "INSERT OR REPLACE INTO reaction_counts (resource_id, reaction, count) VALUES (?, ?, ?)" ,
139+ [(resource_id , name , count ) for name , count in reactions .items ()],
140+ )
122141
123142 def update_reactions (
124143 self ,
125144 * ,
126145 node_id : str ,
127146 up : int ,
128147 down : int ,
148+ upvotes : int ,
149+ reactions : dict [str , int ],
129150 locked : bool ,
130151 github_updated_at : int | None ,
131152 fetched_at : int ,
132153 ) -> bool :
133154 with self .connect () as connection :
134155 cursor = connection .execute (
135156 UPDATE_REACTIONS ,
136- (up , down , locked , github_updated_at , fetched_at , node_id ),
157+ (up , down , upvotes , locked , github_updated_at , fetched_at , node_id ),
137158 )
159+ resource = connection .execute (
160+ "SELECT resource_id FROM discussions WHERE github_node_id = ?" , (node_id ,)
161+ ).fetchone ()
162+ if resource is not None :
163+ connection .execute ("DELETE FROM reaction_counts WHERE resource_id = ?" , (resource [0 ],))
164+ connection .executemany (
165+ "INSERT INTO reaction_counts (resource_id, reaction, count) VALUES (?, ?, ?)" ,
166+ [(resource [0 ], name , count ) for name , count in reactions .items ()],
167+ )
138168 return cursor .rowcount == 1
139169
140170 def adjust_reactions (
0 commit comments