File size: 1,252 Bytes
da1822e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
"""
De-identify help feedback older than 30 days: delete the free-text comment and the
anonymous session id, keep page / vote / reason / device for trend reporting.

Run on a schedule (it is safe to run repeatedly):
    python -m scripts.deidentify_help_feedback

Needs the same FIREBASE_SERVICE_ACCOUNT environment as the API.
"""

import json
import os
import time

import firebase_admin
from firebase_admin import credentials, firestore


def run() -> int:
    if not firebase_admin._apps:
        sa = os.environ.get("FIREBASE_SERVICE_ACCOUNT")
        cred = credentials.Certificate(json.loads(sa)) if sa else credentials.ApplicationDefault()
        firebase_admin.initialize_app(cred)

    db = firestore.client()
    # One range filter only (no composite index needed); skip finished records here.
    due = db.collection("help_feedback").where("deidentifyAfter", "<=", time.time()).stream()
    count = 0
    for doc in due:
        if doc.to_dict().get("deidentified"):
            continue
        doc.reference.update({"comment": firestore.DELETE_FIELD, "sessionId": firestore.DELETE_FIELD, "deidentified": True})
        count += 1
    return count


if __name__ == "__main__":
    print(f"De-identified {run()} help feedback record(s).")