def inbox_update(self, as_actor: ap.Person, update: ap.Update) -> None: obj = update.get_object() if obj.ACTIVITY_TYPE == ap.ActivityType.NOTE: DB.activities.update_one( {"activity.object.id": obj.id}, {"$set": { "activity.object": obj.to_dict() }}, ) elif obj.has_type(ap.ActivityType.QUESTION): choices = obj._data.get("oneOf", obj.anyOf) total_replies = 0 _set = {} for choice in choices: answer_key = _answer_key(choice["name"]) cnt = choice["replies"]["totalItems"] total_replies += cnt _set[f"meta.question_answers.{answer_key}"] = cnt _set["meta.question_replies"] = total_replies DB.activities.update_one( { "box": Box.INBOX.value, "activity.object.id": obj.id }, {"$set": _set})
def _update_process_inbox(update: ap.Update, new_meta: _NewMeta) -> None: _logger.info(f"process_inbox activity={update!r}") obj = update.get_object() if obj.ACTIVITY_TYPE == ap.ActivityType.NOTE: update_one_activity({"activity.object.id": obj.id}, {"$set": { "activity.object": obj.to_dict() }}) elif obj.has_type(ap.ActivityType.QUESTION): choices = obj._data.get("oneOf", obj.anyOf) total_replies = 0 _set = {} for choice in choices: answer_key = _answer_key(choice["name"]) cnt = choice["replies"]["totalItems"] total_replies += cnt _set[f"meta.question_answers.{answer_key}"] = cnt _set["meta.question_replies"] = total_replies update_one_activity({ **in_inbox(), **by_object_id(obj.id) }, {"$set": _set}) # Also update the cached copies of the question (like Announce and Like) DB.activities.update_many(by_object_id(obj.id), upsert({MetaKey.OBJECT: obj.to_dict()})) elif obj.has_type(ap.ACTOR_TYPES): actor = ap.fetch_remote_activity(obj.id, no_cache=True) update_cached_actor(actor) else: raise ValueError(f"don't know how to update {obj!r}")
def inbox_update(self, as_actor: ap.Person, update: ap.Update) -> None: obj = update.get_object() if obj.ACTIVITY_TYPE == ap.ActivityType.NOTE: DB.activities.update_one( {"activity.object.id": obj.id}, {"$set": {"activity.object": obj.to_dict()}}, )
def update_remote_track(actor_id: int, update: ap.Update) -> int: """ :param actor_id: an Actor db ID :param obj: a Little Boxes Audio object :return: nothing """ obj = update.get_object() current_app.logger.debug(f"asked to update a track {obj!r}") current_app.logger.debug(f"obj id {obj.id}") act_id = strip_end(obj.id, "/activity") original_activity = Activity.query.filter(Activity.url == act_id).first() if not original_activity: # TODO(dashie) we should fetch it if not found current_app.logger.error("fetching unknown activity not yet implemented") raise NotImplementedError update_activity = Activity.query.filter(Activity.url == strip_end(update.id, "/activity")).first() if not update_activity: current_app.logger.error(f"cannot get update activity from db {update!r}") return track = Sound.query.filter(Sound.activity_id == original_activity.id).first() if not track: current_app.logger.error(f"update_remote_track: {original_activity!r} has no associated sound") return # Update what is allowed to change # If not found they should fallback to the actual .thing of the object in db track.title = update_activity.payload.get("object", {}).get("name", track.title) track.description = update_activity.payload.get("object", {}).get("content", track.description) track.genre = update_activity.payload.get("object", {}).get("genre", track.genre) track.licence = int(update_activity.payload.get("object", {}).get("licence", {}).get("id", 0)) old_remote_artwork_uri = track.remote_artwork_uri track.remote_artwork_uri = update_activity.payload.get("object", {}).get("artwork", track.remote_artwork_uri) if old_remote_artwork_uri != track.remote_artwork_uri: pass # TODO(call delete and refetch of artwork) tags = update_activity.payload.get("object", {}).get("tags", None) if tags: # First remove tags which have been removed for tag in track.tags: if tag.name not in tags: track.tags.remove(tag) # Then add the new ones if new for tag in tags: if tag not in [a.name for a in track.tags]: dbt = SoundTag.query.filter(SoundTag.name == tag).first() if not dbt: dbt = SoundTag(name=tag) db.session.add(dbt) track.tags.append(dbt) # Purge orphaned tags for otag in SoundTag.query.filter(and_(~SoundTag.sounds.any(), ~SoundTag.albums.any())).all(): db.session.delete(otag) db.session.commit() return track.id
def inbox_update(self, as_actor: ap.Person, update: ap.Update) -> None: obj = update.get_object() current_app.logger.debug(f"inbox_update {obj.ACTIVITY_TYPE} {obj!r} as {as_actor!r}") db_actor = Actor.query.filter(Actor.url == as_actor.id).first() if not db_actor: current_app.logger.error(f"cannot find actor {as_actor!r}") return if obj.ACTIVITY_TYPE == ap.ActivityType.PERSON: update_remote_actor(db_actor.id, obj) else: raise NotImplementedError
def inbox_update(self, as_actor: ap.Person, update: ap.Update) -> None: obj = update.get_object() current_app.logger.debug(f"inbox_update {obj.ACTIVITY_TYPE} {obj!r} as {as_actor!r}") db_actor = Actor.query.filter(Actor.url == as_actor.id).first() if not db_actor: current_app.logger.error(f"cannot find actor {as_actor!r}") return if obj.ACTIVITY_TYPE == ap.ActivityType.PERSON: update_remote_actor(db_actor.id, obj) elif obj.ACTIVITY_TYPE == ap.ActivityType.AUDIO: sound_id = update_remote_track(db_actor.id, update) from tasks import fetch_remote_artwork fetch_remote_artwork.delay(sound_id, update=True) else: raise NotImplementedError