Search code examples
javascriptfeedgetstream-io

GetStream creates same activity in different feeds


I built this piece of code to add tags to my tag feed.

buildActivity = (model,obj) => {
  return {
    ...{
      actor: `user:model.user`,
      verb: 'is',
      object: `model:${model.id}`,
      foreign_id: `model:${model.id}`,
      time: model.createdAt.toDate(),
    },
    ...(obj ? obj : {})
  }
}
addActivitiesToTagFeed = async (model,tags) => {
  const promises = []
  for(let i=0;i<tags.length;i++){
    const tag = tags[i]
    const activity = buildActivity(model,{target: `tag:${tag}`})
    const feed = stream.feed('tag', tag)
    promises.push(feed.addActivity(activity))
  }
  await Promise.all(promises)
}

Which I limited to max 3 tags. I can have like tag:netflix tag:films tag:suspense

The problem is somehow the activity created by addActivity is the same to all tags. The same target, even the same activity id. It is breaking my 'tag_aggregated' that only follows one of those tags.

Ideas anyone how to fix it?


Solution

  • Good news

    Feeds (in my case tag feed) can't have differents activities with same foreign_id + time. To fixed I changed my code

    buildActivity = (model,obj) => {
      return {
        ...{
          actor: `user:model.user`,
          verb: 'is',
          object: `model:${model.id}`,
          foreign_id: `model:${model.id}`,
          time: model.createdAt.toDate(),
        },
        ...(obj ? obj : {})
      }
    }
    addActivitiesToTagFeed = async (model,tags) => {
      const promises = []
      for(let i=0;i<tags.length;i++){
        const tag = tags[i]
    
        // THIS LINE changing the foreign_id for each activity
        const activity = buildActivity(model,{target: `tag:${tag}`,foreign_id: `model:${model.id}:${tag}`})
    
        const feed = stream.feed('tag', tag)
        promises.push(feed.addActivity(activity))
      }
      await Promise.all(promises)
    }
    

    It fixed my problem. In GetStream documentation doesn't mention it but makes sense, because foreign_id + time is a secondary key to find unique record.