如何使用Firebase在博客风格的应用程序上构建和监视用户帖子

时间:2018-11-10 00:53:55

标签: ios swift firebase firebase-realtime-database

我正在构建一个允许用户发布公开帖子的应用程序,您可以发布帖子并查看主页上的所有新帖子。我正在努力寻找最好的方法来有效地构建数据库数据并监视所有新发布的内容,因为当前如果用户已登录,他们可以无限制地发布任何内容。我需要一种有效的方法来筛选帖子,以查找上载到Firebase的内容,甚至需要一种解决方案,即在发布新帖子时将其发送以供审批,而不是直接在数据库上发送。我应该如何处理?

这是帖子上传到数据库的方式,我在authorscategories下也使用此结构的副本,我不确定这是否是一个好方法,或者不是,我是否应该找到一种在数据库中拥有一个帖子实例的方法,而是使用某种ID在不同情况下(例如,按作者或按类别)查找它?

     "posts": {
           -childGeneratedKey: {
                      author: "author",
                      body: "body",
                      category: "category",
                      post_date: "1541784510.682485",
                      post_id: "childAddedID",
                      title: "post_title",
                      votes:"0",
                     }
             -childGeneratedKey: {...
             }

这是我处理应用内发布的方式:

 func createPost(title: String, body: String) {

   let timeInterval = NSDate().timeIntervalSince1970
    let timeDouble = Double(timeInterval)

      ref = Database.database().reference()
        let posts = self.ref.child("posts")
        let post_id = posts.childByAutoId().key!
        let category = self.category
        let post_date = timeDouble

    if postTitle != "" {

        let newPost: Dictionary<String, Any> = [
            "title": title,
            "body": body,
            "votes": 0,
            "author": username,
            "post_id": post_id,
            "category": category,
            "post_date": post_date

        ]

        if privatePost == false {
        createNewPost(post: newPost, post_id: post_id, category: category)
        } else if privatePost == true {
            createnewPrivatePost(post: newPost, post_id: post_id, category: category)
        }

    }
}

func createNewPost(post: Dictionary<String, Any>, post_id: String, category: String) {

    let user_id = Auth.auth().currentUser?.uid
    let posts = self.ref.child("posts")
    let authors = self.ref.child("authors")
    let categories = self.ref.child("categories")
    let users = self.ref.child("users")

    posts.child(post_id).setValue(post)
    authors.child(user_id!).child("posts").child(post_id).setValue(post)
    categories.child(category).child("posts").child(post_id).setValue(post)
    users.child(user_id!).child("posts").child(post_id).setValue(post)


}

我发现很难找到最好的结构来处理这种类型的数据,并且很难找到一种有效的方法来筛选它,以确保内容是可以接受的(禁止滥用内容等)。请向我指出处理博客的最佳方法这样的帖子类型情况以及如何最好地监视设置到数据库的帖子,谢谢。

0 个答案:

没有答案