根据规则删除 Firestore 文档

huangapple go评论82阅读模式
英文:

Delete firestore document based on rule

问题

我创建了一个应用程序,人们可以上传一些关于自己的图片。

现在,为了处理人们上传不适当图片的情况,我创建了一个举报系统。我所做的基本上是每当有人举报一张图片时,举报人的ID就会被添加到firestore中的一个数组中,就像这样:

db
    .collection("Reports")
    .document(ImageID)
    .update("Reports", FieldValue.arrayUnion(UID));

现在,我想设置一个规则,例如,如果数组的大小为5(有5个不同的人举报了这张图片),它将自动从云中删除该图片。

有没有办法可以在每次都读取数组并检查其大小之外实现这个目标?

谢谢

英文:

I created an app where people can upload some images of some themselves.

Now, In order to deal with cases where people can upload inappropriate images, I created a reporting system. What I'm doing is basically every time someone reports the image, the ID of the reporting person is added to an array in firestore like this:

db
    .collection( "Reports" )
    .document( ImageID )
    .update( "Reports", FieldValue.arrayUnion( UID ) );

Now, I want to set a rule that if for example, the size of the array is 5 (5 different people reports the image) that it will automatically delete that image from the cloud.

Is there any way I can do it instead of every time reading the array and check ist size?

Thank you

答案1

得分: 1

你可以在你的Reports集合上创建一个触发器。

export const updateReportTrigger = functions.firestore
  .document('Reports/{ImageID}')
  .onUpdate(onUpdate)

async function onUpdate({ before, after }, context) {
  const newData = after.data()
  if (newData && newData.Reports && newData.Reports.length > 5) {
    // 在此处放置你的删除逻辑
    // 你可以通过 context.params.ImageID 访问文档的 ID
  }
}
英文:

You can create a trigger on your Reports collection.

export const updateReportTrigger = functions.firestore
  .document('Reports/{ImageID}')
  .onUpdate(onUpdate)

async function onUpdate({ before, after }, context) {
  const newData = after.data()
  if (newData && newData.Reports && newData.Reports.length > 5) {
    // put your delete logic here
    // you can access the document id through context.params.ImageID
  }
}

答案2

得分: 0

最常见的做法是通过云函数来实现,云函数是一种在某些情况下自动触发的服务器端代码,比如在将某些内容写入Firestore时触发。请参阅关于云函数的文档以及关于Firestore 触发器的页面。


另一种(但涉及更多)方法是通过查询和安全规则来保护访问。在这种情况下,您需要:

  1. 在文档中添加 reportCount 字段,并确保它随着报告的提交而同步更新。
  2. 在应用代码中使用查询,仅请求 reportCount 小于 5 的图像。
  3. 使用安全规则仅允许带有该条件的查询

如前所述,这需要编写更多代码,但优点是不涉及服务器端代码,并且在对文档进行过多报告时可以立即阻止访问。

英文:

The most common way to do that would be through Cloud Functions, which are server-side code that is automatically triggered when (for this use-case) something is written to Firestore. See the documentation on Cloud Functions and the page on Firestore triggers.


An alternate (but more involved) would be to secure access through a query and security rules. In this scenario you'd:

  1. Add a reportCount to the document, and ensure this gets updated in sync with the reports.
  2. From the application code use a query to only request images with a reportCount less than 5.
  3. Use security rules to only allow queries with that clause in it.

As said, this is more involved in the amount of code you write, but the advantage is that no server-side code is involved, and that documents are immediately blocked when too many reports come in for them.

huangapple
  • 本文由 发表于 2020年9月26日 03:59:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/64070632.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定