質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Firebase

Firebaseは、Googleが提供するBasSサービスの一つ。リアルタイム通知可能、並びにアクセス制御ができるオブジェクトデータベース機能を備えます。さらに認証機能、アプリケーションのログ解析機能などの利用も可能です。

Q&A

0回答

331閲覧

投稿のミュートとユーザーのブロック方法を教えていただきたいです

aaa_5179

総合スコア0

Firebase

Firebaseは、Googleが提供するBasSサービスの一つ。リアルタイム通知可能、並びにアクセス制御ができるオブジェクトデータベース機能を備えます。さらに認証機能、アプリケーションのログ解析機能などの利用も可能です。

0グッド

0クリップ

投稿2021/03/30 04:06

前提・実現したいこと

SNSのようなアプリを作っているのですが、投稿のミュートやユーザーのブロック機能を実現したいです。
バックエンドはFirebaseを使っております。

発生している問題・エラーメッセージ

投稿のミュートやユーザーのブロック機能の実装方法がわからず詰まっております。

該当のソースコード

import UIKit import Firebase import SDWebImage final class PostedListViewController: UIViewController,UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate { private var postedListener: ListenerRegistration? private var posts = [Post]() private var currentPosts = [Post]() @IBOutlet private weak var tableView: UITableView! var selectedImage: String? var selectedUserName: String? var selectedUserPart: String? var selectedUserArea: String? var selectedUserWanted: String? var selectedUserPostedLabel: String? var selecteduserPostedDetail: String? var selectedUserWantedPart: String? var selectedDocumentId: String? var selectedUserId: String? override func viewDidLoad() { super.viewDidLoad() tableView.delegate = self tableView.dataSource = self tableView.estimatedRowHeight = 66 tableView.rowHeight = UITableView.automaticDimension postButton.layer.cornerRadius = 35 searchBar.delegate = self fetchAllPostedInfoFromFirestore() currentPosts = posts blockedPostsInfo() } private func fetchAllPostedInfoFromFirestore() { postedListener?.remove() postedListener = Firestore.firestore().collection("postedContents").order(by: "createdAt") .addSnapshotListener { (snapshots, err) in if let err = err { print("posted情報の取得に失敗しました。(err)") return } snapshots?.documentChanges.forEach({ (documentChange) in switch documentChange.type { case .added: self.handleAddedDocumentChange(documentChange: documentChange) case .modified, .removed: print("nothing to do") } }) } } private func handleAddedDocumentChange(documentChange: DocumentChange) { let dic = documentChange.document.data() var post = Post(dic: dic) post.documentId = documentChange.document.documentID guard let uid = Auth.auth().currentUser?.uid else { return } Firestore.firestore().collection("users").document(post.uid).getDocument { (userSnapshot, err) in if let err = err { print("投稿者の情報の取得に失敗しました。(err)") return } print("投稿者の情報の取得に成功しました") guard let dic = userSnapshot?.data() else { return } var user = User(dic: dic) post.postUser = user self.posts.append(post) self.tableView.reloadData() } } private func mutedPosts() { } func blockedPostsInfo() { guard let uid = Auth.auth().currentUser?.uid else { return } Firestore.firestore().collection("users/(uid)/mutedPosts").getDocuments { (snapShots, error) in if let error = error { print("ブロックした投稿情報の取得に失敗しました") return } snapShots?.documents.forEach({ (snapShot) in let dic = snapShot.data() let blockedPosts = BlockedPosts(dic: dic) self.blockedPosts.append(blockedPosts) }) } } func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? { let uid = (Auth.auth().currentUser?.uid)! let postUserId = currentPosts[indexPath.row].postUser?.uid let mutedPostDocumentId = currentPosts[indexPath.row].documentId! let docData = [ "blockedPostUserId": postUserId, "blockedPostsId": currentPosts[indexPath.row].documentId, "createdAt": Timestamp() ] as [String : Any] let muteAction = UIContextualAction(style: .destructive, title: "非表示", handler: { (action: UIContextualAction, view: UIView, completion: (Bool) -> Void) in Firestore.firestore().collection("users").document(uid).collection("mutedPosts").document(mutedPostDocumentId).setData(docData){ error in if let error = error { print("投稿情報の非表示失敗: " + error.localizedDescription) let dialog = UIAlertController(title: "投稿情報の非表示失敗", message: error.localizedDescription, preferredStyle: .alert) dialog.addAction(UIAlertAction(title: "OK", style: .default)) self.present(dialog, animated: true, completion: nil) } else { print("投稿情報の非表示成功") self.currentPosts.remove(at: indexPath.row) tableView.deleteRows(at: [indexPath as IndexPath], with: UITableView.RowAnimation.fade) } } }) muteAction.backgroundColor = UIColor(red: 214/255.0, green: 69/255.0, blue: 65/255.0, alpha: 1) muteAction.image = UIImage(systemName: "speaker.slash.circle.fill") let swipeActionConfig = UISwipeActionsConfiguration(actions: [muteAction]) swipeActionConfig.performsFirstActionWithFullSwipe = false return swipeActionConfig } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return currentPosts.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! UsersTableViewCell cell.userProfileImageView.sd_setImage(with: URL(string: currentPosts[indexPath.row].postUser!.profileImageUrl), completed: nil) cell.userProfileImageView.layer.cornerRadius = 35 cell.userProfileImageView.layer.borderWidth = 2.0 cell.userProfileImageView.layer.borderColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1) cell.usersInfoLabel.text = currentPosts[indexPath.row].postUser?.username cell.userPostedLabel.text = currentPosts[indexPath.row].postedLabel cell.userPostedLabel.numberOfLines = 0 cell.postedDateLabel.text = dateFormatterForDateLabel(date: currentPosts[indexPath.row].createdAt.dateValue() ?? Date()) return cell } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { tableView.deselectRow(at: indexPath, animated: true) } }

試したこと

FirestoreのセキュリティルールでmutedPostsコレクションやblockedUsersコレクションに入っていないなら読み込めるようにしようとしましたがうまくいきませんでした。また、アプリ側でこれらのコレクションに入っている情報を呼び出してセルに表示させないような方法も考えましたが、具体的にどのようなコードを書けばいいのかわからない状態です。

補足情報(FW/ツールのバージョンなど)

usersコレクションのサブコレクションとしてmutedPostsコレクションとblockedUsersコレクションを作っています。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問