MCPcopy Create free account
hub / github.com/TimMcCool/scratchattach / _update_from_json

Method _update_from_json

scratchattach/site/activity.py:242–316  ·  view source on GitHub ↗

Update using JSON, used in the classroom API.

(self, data: dict)

Source from the content-addressed store, hash-verified

240 return True
241
242 def _update_from_json(self, data: dict):
243 """
244 Update using JSON, used in the classroom API.
245 """
246 activity_type = data["type"]
247
248 _time = data.get("datetime_created")
249
250 if "actor" in data:
251 self.username = data["actor"]["username"]
252 else:
253 self.username = data.get("actor_username")
254
255 self.recipient_username = None
256 if recipient := data.get("recipient"):
257 self.recipient_username = recipient["username"]
258 elif ru := data.get("recipient_username"):
259 self.recipient_username = ru
260 elif project_creator := data.get("project_creator"):
261 self.recipient_username = project_creator["username"]
262
263 # Even if `activity_type` is an invalid value; it will default to 'user performed an action'
264 self.actor_username = self.username
265 self.raw = data
266 self.datetime_created = _time
267
268 # NOTE: some type values are treated the same here
269 # this is by design. the scratch HTML does this using a switch statement
270 self.type = {
271 0: ActivityTypes.followuser,
272 1: ActivityTypes.followstudio,
273 2: ActivityTypes.loveproject,
274 3: ActivityTypes.favoriteproject,
275 7: ActivityTypes.addprojecttostudio,
276 8: ActivityTypes.shareproject,
277 9: ActivityTypes.shareproject,
278 10: ActivityTypes.shareproject,
279 11: ActivityTypes.remixproject,
280 # type 12 does not exist in the HTML. That's why it was removed, not merged with type 13.
281 13: ActivityTypes.createstudio,
282 15: ActivityTypes.updatestudio,
283 16: ActivityTypes.removeprojectfromstudio,
284 17: ActivityTypes.removeprojectfromstudio,
285 18: ActivityTypes.removeprojectfromstudio,
286 19: ActivityTypes.removeprojectfromstudio,
287 20: ActivityTypes.promotetomanager,
288 21: ActivityTypes.promotetomanager,
289 22: ActivityTypes.promotetomanager,
290 23: ActivityTypes.updateprofile,
291 24: ActivityTypes.updateprofile,
292 25: ActivityTypes.updateprofile,
293 26: ActivityTypes.addcomment,
294 27: ActivityTypes.addcomment,
295 None: ActivityTypes.performaction, # this one is just to satisfy type checkers
296 }.get(activity_type, ActivityTypes.performaction)
297
298 self.followed_username = data.get("followed_username", self.followed_username)
299 self.gallery_id = data.get("gallery", self.gallery_id)

Callers 1

activityMethod · 0.80

Calls 1

getMethod · 0.45

Tested by

no test coverage detected