Repository navigation
Allow StreamUser as actor in UpdateActivity type - #678
theluckystrike wants to merge 1 commit into
Conversation
JimmyPettersson85
left a comment
There was a problem hiding this comment.
Thanks for the PR! Unfortunately this doesn't resolve #629 as written, and it widens the type in a way that lets a call through which then breaks at runtime.
1. Doesn't fix the #629 repro
getActivities() returns FlatActivity[] | FlatActivityEnriched[]. On the enriched variant, actor is EnrichedUser (a plain { id, data, created_at, updated_at } object), not the StreamUser class (which has client, token, ref(), …). Type-checking the snippet from the issue with this patch applied still fails:
error TS2345: Argument of type 'FlatActivity[] | FlatActivityEnriched[]'
is not assignable to parameter of type 'UpdateActivity[]'.
... Type 'string | EnrichedUser' is not assignable to type 'string | StreamUser'.
Type 'EnrichedUser' is missing the following properties from type 'StreamUser': client, token, url, ref, and 7 more.
2. StreamUser isn't serialized on the update path
addActivity/addActivities run utils.replaceStreamObjects(), which turns a StreamUser into its SU:<id> ref. client.updateActivities() (src/client.ts) doesn't do this; it posts { activities } as is. With this change, updateActivity({ actor: client.user('x'), ... }) type-checks, but the entire StreamUser instance, including its client field, gets serialized into the request body instead of "SU:x". The current string-only type prevents that.
Suggestions
- If
StreamUsershould be accepted, also applyutils.replaceStreamObjects(activities)inupdateActivities, mirroring the add path. - To actually address #629,
EnrichedUserwould need handling too (sending itsidrather than the object), and the same is true for the other enriched fields (object,latest_reactions,own_reactions, …). A helper or a documented enriched → update mapping might be cleaner than widening the type. - Please add a type test under
types/and a unit test showingupdateActivitiessendsactor: "SU:x"for aStreamUser.
Fixes #629.
NewActivityalready typesactorasstring | StreamUser, whereasUpdateActivityrestrictedactortostring. Allowingstring | StreamUserlets enriched activity responses fromgetActivitiesbe passed directly toupdateActivitywithout type assertions.