ONEARMY / community-platform

A platform to build useful communities that aim to tackle global problems
https://platform.onearmy.earth
MIT License
1.08k stars 358 forks source link

[Cloud function]: Daily sync of research meta-data #3641

Open benfurber opened 3 weeks ago

benfurber commented 3 weeks ago

Due to the move to discussion documents holding the comments for each research update and changing comments from being deleted from the DB to be updated to _deleted, it's much more common and noticiable that the comment count stored for each research articles (totalCommentCount) gets out of sync with the reality.

Solution: Accept that certain changes in data will be fallible and have a cloud function that runs daily for research that:

  1. Identifies all the currently not _deleted and published research articles.
  2. Identifies all the published and not _deleted updates for each article.
  3. Identifies discussion document for each research update (found through the _id of an update being the sourceId of the discussion).
  4. Count the not _deleted comments for the discussion.

That should then give a total that can be set as the totalCommentCount for the research.

mariojsnunes commented 3 weeks ago

why is _deleted an issue? a onWrite function for the discussion could filter that for the totalCommentCount update

benfurber commented 3 weeks ago

There are definitely alternative bottom-up functions that could be done to fulfill the same brief:

  1. onUpdate of discussion, update research (like you say). Plus,
  2. onUpdate of research, check for _deleted research updates.

Pros and cons of both approaches and I don't really mind which. Mainly just detailed the first approach I thought of in case a new contributor fancied picking this up. :)

mariojsnunes commented 3 weeks ago

Thinking more on this, having a background job is a good enough solution for now.

As an alternative/complement to the proposed solution: