-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathpush-helper.py
More file actions
126 lines (104 loc) · 4.21 KB
/
Copy pathpush-helper.py
File metadata and controls
126 lines (104 loc) · 4.21 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
Push helper for TimeManagement app.
Handles incoming push notifications and formats them for display.
"""
import sys
import json
import sqlite3
from pathlib import Path
from datetime import datetime
# Database path for storing notifications
DB_DIR = Path.home() / ".local" / "share" / "ubtms" / "Databases"
def get_db_path():
"""Get the path to the app database."""
if DB_DIR.exists():
db_files = list(DB_DIR.glob("*.sqlite"))
if db_files:
return str(max(db_files, key=lambda p: p.stat().st_mtime))
return str(Path.home() / ".local" / "share" / "ubtms" / "timemanagement.db")
def store_notification(notif_type, message, payload):
"""Store notification in database for in-app display."""
try:
db_path = get_db_path()
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
# Ensure table exists
cursor.execute("""
CREATE TABLE IF NOT EXISTS notification (
id INTEGER PRIMARY KEY AUTOINCREMENT,
account_id INTEGER,
timestamp TEXT DEFAULT (datetime('now')),
message TEXT NOT NULL,
type TEXT CHECK(type IN ('Activity', 'Task', 'Project', 'ProjectUpdate', 'Timesheet', 'Sync')),
payload TEXT NOT NULL,
read_status INTEGER DEFAULT 0,
panel_invoked INTEGER DEFAULT 0
)
""")
# Upgrade path for existing databases created before panel_invoked existed.
cursor.execute("PRAGMA table_info(notification)")
columns = {row[1] for row in cursor.fetchall()}
if "panel_invoked" not in columns:
cursor.execute("ALTER TABLE notification ADD COLUMN panel_invoked INTEGER DEFAULT 0")
# Check for duplicate unread notification with same type+message
cursor.execute("""
SELECT COUNT(*) FROM notification
WHERE message = ? AND type = ? AND read_status = 0
""", (message, notif_type))
count = cursor.fetchone()[0]
if count > 0:
# Duplicate unread notification exists, skip
conn.close()
return
# Insert notification (account_id = 0 for push notifications)
cursor.execute("""
INSERT INTO notification (account_id, timestamp, message, type, payload, read_status, panel_invoked)
VALUES (?, ?, ?, ?, ?, 0, 1)
""", (0, datetime.utcnow().isoformat() + "Z", message, notif_type, json.dumps(payload)))
conn.commit()
conn.close()
except Exception:
pass # Silent failure - notification display is more important
def process_notification(input_data):
"""Process incoming notification and enhance if needed."""
try:
notification = json.loads(input_data)
# Extract notification details
notif = notification.get("notification", {})
card = notif.get("card", {})
summary = card.get("summary", "")
body = card.get("body", "")
# Determine notification type from summary
notif_type = "Task" # Default
if "Activity" in summary:
notif_type = "Activity"
elif "Project" in summary:
notif_type = "Project"
elif "Timesheet" in summary:
notif_type = "Timesheet"
# Store in database for in-app display
store_notification(notif_type, body, {"summary": summary, "source": "push"})
# Return formatted notification
return json.dumps(notification)
except (json.JSONDecodeError, KeyError):
# Return original if parsing fails
return input_data
def main():
if len(sys.argv) < 3:
sys.exit(1)
input_file, output_file = sys.argv[1:3]
try:
# Read input notification
with open(input_file, "r") as f:
input_data = f.read()
# Process notification
output_data = process_notification(input_data)
# Write to output file
with open(output_file, "w") as f:
f.write(output_data)
except Exception:
sys.exit(1)
if __name__ == "__main__":
main()