Excluding data from firestore search - firebase

I'm working on a dating app. Right now we don't want to show the data of users that users have already seen.
For example, let's suppose if A has seen B and C.
now we want to show more users to A. Then B and C should be excluded from that list.
What I'm doing is getting data from firebase like [B, C, D, E, F] as B, C is already seen. So removing those we have [D, E, F].
What's the issue:
Actually getting the same data, again and again, makes more reading numbers. It also slows down the getting of data process. Like if we get a list from firebase and all the profiles in that list are already seen then the device again sends a request to firebase and gets more profiles. it does the same for the new list and if these are also seen so it makes another request again. This takes time and users have to wait until there are profiles to swipe.
Can we make something like that? We just save a list of all the seen profiles at any point and make a rule to exclude those profiles from the search result.
We don't need those profiles at one point but we need to access those profiles to show user he has seen these profiles.
Any help will be appreciated.
I got a little similar case but I didn’t understand
{
"rules": {
"Children": {
"$child_id": {
".read": "auth != null &&
root.child('Family').child(
root.child('User').child(
data.child('parentId').val()
).val()
).child('members').child(auth.uid).exists()
}
}
}
}
Update:
there are different node:
database
|
`- users
| |
| `- userId
| | |
| | `-name
| | `-id
| | `-address
| | `-..... more details
| `- userId
| | |
| | `-name
| | `-id
| | `-address
| | `-..... more details
| |
| `- more users . .
|
`- swipes
| |
| `- uniqueId
| | |
| | `- whoSwiped: id
| | `- whoIsSwiped: id
| | `- swipedType: like/dislike
| | `- createdAt: date
| `- uniqueId
| |
| `- whoSwiped: id
| `- whoIsSwiped: id
| `- swipedType: like/dislike
| `- createdAt: date
first swiped id is fetched then user from "users" node is fetched ids that are already swiped are removed from the users' list.

Related

Firebase security rules: Get a document with a space in the documentID

I am writing firebase security rules, and I am attempting to get a document that may have a space in its documentID.
I have the following snippet which works well when the document does not have a space
function isAdminOfCompany(companyName) {
let company = get(/databases/$(database)/documents/Companies/$(companyName));
return company.data.authorizedUsers[request.auth.uid].access == "ADMIN;
}
Under the collection, "Companies", I have a document called "Test" and another called "Test Company" - Trying to get the document corresponding to "Test" works just fine, but "Test Company" does not seem to work, as the company variable (first line into the function) is equal to null as per the firebase security rules "playground".
My thought is that there is something to do with URL encoding, but replacing the space in a documentID to "%20" or a "+" does not change the result. Perhaps spaces are illegal characters for documentIDs (https://cloud.google.com/firestore/docs/best-practices lists a few best practices)
Any help would be appreciated!
EDIT: As per a few comments, I Will add some additional images/explanations below.
Here is the structure of my database
And here is what fields are present in the user documents
In short, the following snippet reproduces the problem (I am not actually using this, but it demonstrates the issue the same way)
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /Users/{user} {
allow update: if findPermission(resource.data.company) == "MASTER"
}
function findPermission(companyName) {
let c = get(path("/databases/" + database + "/documents/Companies/" + companyName));
return c.data.authorizedUsers[request.auth.uid].access;
}
}
}
When I try to update a user called test#email.com (which belongs to company "Test"), the operation is permitted, and everything works exactly as expected.
The issue arises when a user, called test2#email.com, who belongs to company "Test Company" comes along and makes the same request (with authorization email/uid updated in playground, to match what is actually found in the company structure), the request fails. The request fails, since the get() call (line 1 of the function) cannot find the Company document corresponding to "Test Company" - indicated by the variable "c" being null in the screenshot (see below) - IT IS NOT NULL WHEN LOOKING FOR "Test"
Below is a screenshot of the error message, as well as some of the relevant variables when the error occurs
Check to see what type of space just in case it is another non-printable character. You could convert it to Unicode, and check what it might be. However, it is considered bad practice to use spaces in naming variables and data structures. There are so many different types to consider.
| Unicode | HTML | Description | Example |
|---------|--------|--------------------|---------|
| U+0020 | &#32 | Space | [ ] |
| U+00A0 | &#160 | No-Break Space | [ ] |
| U+2000 | &#8192 | En Quad | [ ] |
| U+2001 | &#8193 | Em Quad | [ ] |
| U+2002 | &#8194 | En Space | [ ] |
| U+2003 | &#8195 | Em Space | [ ] |
| U+2004 | &#8196 | Three-Per-Em Space | [ ] |
| U+2005 | &#8197 | Four-Per-Em Space | [ ] |
| U+2006 | &#8198 | Six-Per-Em Space | [ ] |
| U+2007 | &#8199 | Figure Space | [ ] |
| U+2008 | &#8200 | Punctuation Space | [ ] |
| U+2009 | &#8201 | Thin Space | [ ] |
| U+200A | &#8202 | Hair Space | [ ] |

Firebase how to index this data to be fetched?

I have a DB with users and items.
Every user has languages, which is an array of languages, for example ['en', 'ar']
Every item has language, which is a string, for example 'en'.
How can I index my items, such that I can get a list of the last X items in an array of languages? (i.e - latest 5 items who are either 'en' or 'ar')
For a single language the solution is simple - have an index that has the language key, and array of item keys ordered by whatever.
Please note that the Firebase official documentation recommends against using arrays. IMHO, the main problem in your project is that you trying to use an array, which is an anti-pattern when it comes to Firebase. Beside that, one of the many reasons Firebase recommends against using arrays is that it makes the security rules impossible to write. Because a Firebase database is structured as pairs of key and values, the best option is to use a Map.
To achieve what you want, i recomand you using a database structure which looks like this:
Firebase-root
|
--- users
| |
| --- UID1
| | |
| | --- languages
| | |
| | --- languageId1: true
| | |
| | --- languageId2: true
| |
| --- UID2
| |
| --- languages
| |
| --- languageId3: true
| |
| --- languageId4: true
|
--- languages
| |
| --- languageId1
| | |
| | --- languageName: "en"
| | |
| | --- items
| | |
| | --- itemId1: true
| |
| --- languageId2: "ar"
| | |
| | --- languageName: "ar"
| | |
| | --- items
| | |
| | --- itemId2: true
|
--- item
|
--- itemId1
| |
| --- title: "ItemTitleEn"
| |
| --- en: true
|
--- itemId2
|
--- title: "ItemTitleAr"
|
--- ar: true
Now, with this database structure you can achieve everything you want. For example, you can query your database to display all languages from you database. You can also display all the languages of a single user.
If you want to query your database for the last x item which have the language set to en, you just need to put a listener on the items node and create a query using functions like: orderByChild(), equalsTo() and limitToLast(). Such a query should look like this:
query = rootRef.child("items")
.orderByChild("en")
.equalsTo(true)
.limitToLast(5)
.addListener(/* ... */)
EDIT: Unfortunately Firebase does not allow multiple conditions in a query. So in Firebase there is no where clause that sounds like this: WHERE language = "en" AND language = "ar". So to solve this, you need to put a listener on the other node, on languages node.
The flow is as follows:
yourRef = rootRef.child("languages"); //only one listener
yourRef.addListener(
1. Create a list
2. get items from dataSnapshot.child("languageId1").child("items").getChildren()
3. add **en** items to the list
4. get items from dataSnapshot.child("languageId2").child("items").getChildren()
5. add **ar** items to the list
6. display the list that contains data from both languages
)
Hope it helps.

MariaDB InnoDB table: how to find statement causing "waiting for table metadata lock"

How do I determine what the SQL statement is of the thread ID showing up in a metadata lock info row (SELECT * FROM information_schema.metadata_lock_info) on MariaDB?
Server version: 10.0.15-MariaDB MariaDB Server
All of the related questions dive into the "Waiting for table metadata lock" from a MySQL perspective, but that does not help with MariaDB since their introspection is implemented differently from what I can tell. Googling around does not turn up a whole lot.
A "show full processlist" gives rows like:
| 57295 | main | localhost | joints | Execute | 50 | Waiting for table metadata lock | select ...
Which does show the statement, but does not show that it has the lock either. So, I turned on metadata lock info as explained here [0]. This only provides the thread ID of the lock holder, but not the statement:
MariaDB [joints]> SELECT * FROM information_schema.metadata_lock_info;
+-----------+--------------------------+-----------------+----------------------+--------------+----------------+
| THREAD_ID | LOCK_MODE | LOCK_DURATION | LOCK_TYPE | TABLE_SCHEMA | TABLE_NAME |
+-----------+--------------------------+-----------------+----------------------+--------------+----------------+
| 57322 | MDL_INTENTION_EXCLUSIVE | MDL_EXPLICIT | Global read lock | | |
| 57322 | MDL_SHARED_NO_READ_WRITE | MDL_EXPLICIT | Table metadata lock | joints | 16_study |
| 57322 | MDL_INTENTION_EXCLUSIVE | MDL_EXPLICIT | Schema metadata lock | joints | |
| 57269 | MDL_SHARED_READ | MDL_TRANSACTION | Table metadata lock | joints | authentication |
| 57301 | MDL_SHARED_READ | MDL_TRANSACTION | Table metadata lock | joints | authentication |
| 57280 | MDL_SHARED_READ | MDL_TRANSACTION | Table metadata lock | joints | authentication |
| 57317 | MDL_SHARED_READ | MDL_TRANSACTION | Table metadata lock | joints | ship |
| 57271 | MDL_SHARED_READ | MDL_TRANSACTION | Table metadata lock | joints | administration |
| 57264 | MDL_SHARED_READ | MDL_TRANSACTION | Table metadata lock | joints | server |
+-----------+--------------------------+-----------------+----------------------+--------------+----------------+
What I really want is to see the "join" of both of those outputs at the moment the locking is happening. I do not see a way to join the data from these two "tables" since the former does not appear to be a table. I'd like to avoid getting:
ERROR 1933 (HY000): Target is not running an EXPLAINable command
while attempting to do it in real-time, due to the thread ending while being inspected.
[0] https://mariadb.com/kb/en/mariadb/metadata_lock_info/
THREAD_ID maps to information_schema.PROCESSLIST.ID (the first column in show
[full] processlist;. ie:
SELECT * FROM information_schema.METADATA_LOCK_INFO AS mli
JOIN information_schema.PROCESSLIST AS pl ON mli.THREAD_ID = pl.ID
I am preferential towards something like the following to make it easier to see the what is happening (the newlines don't work well with the cli though):
SELECT
mli.THREAD_ID, mli.LOCK_MODE, mli.LOCK_TYPE,
CAST(GROUP_CONCAT(DISTINCT CONCAT(mli.TABLE_SCHEMA, '.', mli.TABLE_NAME) ORDER BY mli.TABLE_SCHEMA, mli.TABLE_NAME SEPARATOR '\n') AS CHAR) AS locked_tables,
pl.USER, pl.HOST, pl.DB, pl.COMMAND, pl.TIME, pl.STATE, pl.INFO, pl.QUERY_ID, pl.TID
FROM information_schema.METADATA_LOCK_INFO AS mli
JOIN information_schema.PROCESSLIST AS pl ON mli.THREAD_ID = pl.ID
GROUP BY mli.THREAD_ID, mli.LOCK_MODE, mli.LOCK_TYPE
ORDER BY time DESC, pl.ID;
Especially interesting is when pl.COMMAND = 'Sleep' as that indicates some connection pool or other (mostly read-only) program is holding open connections that have locks on them.

Firebase security rules with external id

For some applications my team creates authenticated users with a password/email combination. This will get the user an firebase user uid. The problem with this is that the keys in firebase itself are external id's, and they do not match the auth.uid. How would I go about creating security rules then?
Sample auth.uid:
9dkad6c7-s649-9623-99e2-5a0dbgf5dfdz
Then a sample of the structure:
database
|
—— conversations
|
——{external id 1}
| |
| ——{external id 2}
| |
| {data here}
|
messages
|
——{externalid1|externalid2}
| |
| —{-KFasdahsduids}
| |
| {data here}
|
|
users
|
——{externalId}
| |
| {first name}
| {last name}
| {firebaseUID}
| {more data here}
|
——{externalId2}
|
{first name}
{lastname}
{firebaseUID}
{more data here}
The problem really is that the auth.uid is not the same as the external ones, and we really need those external id's. Can I do something with the UID that is stored in the /users/? Any suggestions?

Routing checking user role Symfony2

I have two bundles and I want routes from one of the bundle only accessible if the user have a defined role.
The logic from the router matcher should be:
if the user have the role
| name | path | success |
|------------------|-------|---------|
| bundle_1_route_1 | / | false |
| bundle_1_route_2 | /test | true |
If the user don't have the role
| name | path | success |
|------------------|-------|---------|
| bundle_1_route_1 | / | false |
| bundle_1_route_2 | /test | false |
| bundle_2_route_1 | /aaa | false |
| bundle_2_route_2 | /test | true |
The problem is I can't do that using the security because the path are the same
I tried with the #security annotation http://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/security.html
But I have an access denied when on bundle_1_route_2 with no role and the others urls are not checked.
I want to continue checking all the available url if the role is not matching
I found an other solution, but this is not very clean, and it will create error if the session does not exists
bundle_1:
resource: "#Bundle1/Controller/"
type: annotation
prefix: /
condition: "'ROLE_FILTER' in request.getSession().get('bundle1.user').getRoles()"
Is there a way to create completely custom conditions on routing ?

Resources