In the past two decades, we have witnessed an explosion of social media content, through blogs, chat rooms…an almost limitless number of resources that allow users online to add their thoughts.
Not all comments and thoughts are received well, however. Through threatening to volatile wording, we can be on the receiving end of unwelcome messages.
In the past, websites and apps have used manual moderation, paid works, or volunteers who read through the content, and typically with guidelines choose which content can be allowed to view by the rest of the public!
To make this easier, Jigsaw recently published their Firebase Extension which utilizes the Perspective API. By installing the extension, we will demonstrate here how to build this in the app to speed up and improve moderation.
In this example, we are going to take advantage of Zapp by Invertase. This allows us to quickly prototype Flutter applications in the browser and to share and fork as we develop. Feel free to fork this example afterward to play around with this feature.
Create a new Firebase project, if you don’t have one already.
Navigate to ⚙️ => Project Settings.
Select to add a new App.
Choose the Flutter icon.
Now, we have our configuration, let’s create a new file in our project lib folder called firebase_options.dart. Make sure to fill in your own personal configuration at the start of the file
Next, we will add anonymous authentication through Firebase. This will provide a uid for the current users to that we can link a comment.
First, we are required to enable anonymous authentication. Navigate to the authentication section of your Firebase console -> Authentication -> Sign-In method -> Add new provider.
Select anonymous -> enable -> Save
To read and write Firestore document data, we will need to allow permissions. Navigate to your Firestore database and add the following Firestore rule:
When the application first loads, we will need to ensure that we have a user to post comments with, let’s start by adding a widget wrapper to handle authentication.
Create a folder called components in the lib folder and add a file called authentication.dart.
Next, paste the following:
This segment, allows a single child widget to be presented as the child in the widget.
The streamBuilder, connects through Firebase Authentication, and returns the current user.
When a user is found, the widget will return the provided child widget!
Now we have the widgets required to run an authenticated app, navigate to the main.dart file and add the following:
Run the app and you should now see the application working; after displaying a loading icon the page will then show the text “Comments Sample”
OK, so now have an app running and we are authenticated!
Let’s next create a widget for displaying our comments, this will:
Show no comments available as the default behavior
Display a full list of comments when available, showing the name of the user (defaulting to anonymous) and displaying the message that the users have provided.
Add a file called comments.dart in our components folder:
There is a lot to take in here, so let’s go through what this widget is achieving.
Firstly, we set up a listener on our `comments` collection, this matches the value that was defaulted when we Installed the Firebase Extension.
If no comments are included in the collection, the widget will display “No comments available”
When comments are available, the widget will map through each item:
If the `attribute_scores` toxicity rating is not available, then the text provided by the user and the message “Moderating comment will display”.
If a rating has been provided but it has been rated as too toxic then the comment will display with “Toxic message found, cannot display…”. In this example, we sat the hard-coded value of 0.5 as the score at which we deem the comment to be toxic.
Otherwise, the comment has passed the check and the comment is successfully displayed with the name of the user (anonymous) along with the provided message.
We can now add this widget to our main.dart file:
Re-running the app, you should now see:
As the final step, we now need to add a widget to add comments.
But first, some housekeeping on our future comments collection, before we add more messages it is important to note that Firestore does not automatically present documents in their natural order. To ensure comments appear in `ascending` order.
Navigate to your Firestore console and select the Indexes tab. Add a new index for createdAt:
This will ensure that when including a timestamp value for the document createdAt field, all comments appear in the order that they were added.
Create another file in the components folder called add_comment.dart.
In this example, we have included a streamBuilder to ensure that we have a valid user for adding a comment. This user information can then be used in the future development to use for customizing comments in the future, for example:
Display Name
Profile picture
For the purposes of this demo, we will have anonymous authentication so those fields will not be available. Instead of the user’s name, we will simply use the Id.
This simple form, allows the user to input a message. Selecting submit will add a new document for our new comment.
Let’s add this to our main.dart file:
The UI should now show this new widget at the bottom of the screen:
Tip: When adding a comment, monitoring the Firestore console will provide a visual insight into how the extension updates the document data.
Go ahead and a new comment.
The comments section will update automatically
After moderating, valid comments will appear in the list.
Toxic comments, however, will no longer be shown!
You can see from the rating, how toxic the comment was rated.
In this tutorial, we have learned how to create a Flutter app that allows for the moderation of text in your database. There are so many other ways to test the validation of your content and ways to handle that in your UI.
Feel free to clone the project in Zapp or elsewhere and share your ideas with us. We’d love to see what developers can create using the power of Firebase Extensions!
Stay tuned for more updates and exciting news that we will share in the future. Follow us on Invertase Twitter, Linkedin, and Youtube, and subscribe to our monthly newsletter to stay up-to-date. You may also join our Discord to have an instant conversation with us.
Artificial intelligence is all over the news, with new emergent applications of this technology popping in daily, from image recognition to help diagnose certain health conditions to clever AI chatbots that aid students in their classes.
You often want to test your knowledge when learning a new topic. You can get ready-made quizzes on the subject, but generally, they tend to be too broad. What if you only want to test yourself on the content you’ve just read? This is the problem statement we will tackle in this article: given some content, how can you create a quiz based on it?
Latest updates for Firestore BigQuery Export, Storage Resize Images, and the Firestore Send Email extension. Some general housekeeping also includes updated documentation resources and work improving our CI environment.