Skip to content

Allow StreamUser as actor in UpdateActivity type - #678

Open
theluckystrike wants to merge 1 commit into
GetStream:mainfrom
theluckystrike:fix/update-activity-actor-type
Open

theluckystrike wants to merge 1 commit into
GetStream:mainfrom
theluckystrike:fix/update-activity-actor-type

Conversation

@theluckystrike

Copy link
Copy Markdown

Fixes #629.

NewActivity already types actor as string | StreamUser, whereas UpdateActivity restricted actor to string. Allowing string | StreamUser lets enriched activity responses from getActivities be passed directly to updateActivity without type assertions.

@JimmyPettersson85 JimmyPettersson85 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 StreamUser should be accepted, also apply utils.replaceStreamObjects(activities) in updateActivities, mirroring the add path.
  • To actually address #629, EnrichedUser would need handling too (sending its id rather 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 showing updateActivities sends actor: "SU:x" for a StreamUser.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Unable to use the GetActivities response for actor in UpdateActivity

2 participants