File size: 6,734 Bytes
cb3cbbc
061c9e5
 
d4cac88
 
 
 
 
 
061c9e5
2d2cc60
29b0887
 
 
 
 
 
 
 
061c9e5
 
 
2d2cc60
 
 
29b0887
061c9e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3ff66c4
 
061c9e5
3ff66c4
061c9e5
 
 
 
 
3ff66c4
 
 
061c9e5
3ff66c4
061c9e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9a6c1f1
061c9e5
 
cb3cbbc
 
 
3676820
 
 
cb3cbbc
3676820
 
 
 
 
 
 
 
 
 
 
 
cb3cbbc
 
 
3676820
 
 
cb3cbbc
3676820
 
 
 
 
 
 
 
 
cb3cbbc
 
 
3676820
 
 
cb3cbbc
3676820
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cb3cbbc
 
 
 
 
 
 
 
3676820
cb3cbbc
3676820
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
663f111
3676820
 
 
663f111
 
3676820
663f111
3676820
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215

// Firebase configuration
const firebaseConfig = {
    apiKey: "AIzaSyDqVv0wQ3X9X9X9X9X9X9X9X9X9X9X9X9X",
    authDomain: "edughost-12345.firebaseapp.com",
    projectId: "edughost-12345",
    storageBucket: "edughost-12345.appspot.com",
    messagingSenderId: "9876543210",
    appId: "1:9876543210:web:abcdef1234567890"
};

// Check if Firebase app already exists
let app;
if (!firebase.apps.length) {
    app = firebase.initializeApp(firebaseConfig);
} else {
    app = firebase.app();
}

const auth = firebase.auth();
const db = firebase.firestore();

// Make auth and db available globally
window.auth = auth;
window.db = db;
window.firebaseApp = app;
// Authentication state listener
auth.onAuthStateChanged((user) => {
    if (user) {
        console.log("User logged in:", user.email);
        document.querySelector('custom-email-list').setAttribute('user-id', user.uid);
        document.querySelector('custom-email-composer').setAttribute('user-id', user.uid);
    } else {
        console.log("User logged out");
    }
});

// Function to create user account
async function createUserAccount(email, password) {
    try {
        const userCredential = await auth.createUserWithEmailAndPassword(email, password);
        await db.collection('users').doc(userCredential.user.uid).set({
            email: email,
            createdAt: firebase.firestore.FieldValue.serverTimestamp()
        });
        return userCredential.user;
    } catch (error) {
        throw error;
    }
}
// Global email sending function
window.sendEmail = async function(userId, emailData) {
    try {
        const emailRef = await db.collection('emails').add({
            ...emailData,
            userId: userId,
            read: false,
            createdAt: firebase.firestore.FieldValue.serverTimestamp()
        });
        
        // Return the email ID for reference
        return emailRef.id;
    } catch (error) {
        console.error("Error sending email:", error);
        throw error;
    }
}
// Function to fetch emails
async function fetchEmails(userId) {
    try {
        const snapshot = await db.collection('emails')
            .where('userId', '==', userId)
            .orderBy('createdAt', 'desc')
            .get();
            
        return snapshot.docs.map(doc => ({
            id: doc.id,
            ...doc.data()
        }));
    } catch (error) {
        console.error("Error fetching emails:", error);
        return demoEmails; // Fallback to demo emails
    }
}
const demoEmails = [
    {
        id: '1',
        from: 'registrar@harvard.edu',
        subject: 'Fall Semester Registration',
        preview: 'Registration for Fall 2023 is now open. Click here to register...',
        date: '10 min ago',
        read: false,
        body: `Dear Student,

Registration for Fall 2023 courses is now open. Please log in to the student portal to select your courses.

Important dates:
- Add/Drop Period: August 28 - September 8
- Midterm Exams: October 16-20
- Final Exams: December 11-15

Regards,
Harvard Registrar's Office`
    },
    {
        id: '2',
        from: 'financialaid@yale.edu',
        subject: 'Financial Aid Package Update',
        preview: 'Your financial aid package for 2023-2024 has been updated...',
        date: '1 hour ago',
        read: true,
        body: `Dear Student,

We're writing to inform you that your financial aid package for the 2023-2024 academic year has been updated. 

Please log in to your Student Aid Portal to review the changes. If you have any questions, please contact our office at (203) 432-0441.

Sincerely,
Yale Financial Aid Office`
    },
    {
        id: '3',
        from: 'housing@mit.edu',
        subject: 'Dormitory Assignment',
        preview: 'Your dormitory assignment for Fall 2023 is now available...',
        date: '3 hours ago',
        read: true,
        body: `Hello,

Your dormitory assignment for Fall 2023 is now available. You've been assigned to Baker House, Room 412.

Move-in dates:
- Freshmen: August 28
- Returning Students: August 30

Please complete the housing questionnaire by August 15.

MIT Housing Office`
    },
    {
        id: '4',
        from: 'library@stanford.edu',
        subject: 'Book Due Reminder',
        preview: 'The book "Computer Science Principles" is due in 3 days...',
        date: '1 day ago',
        read: false,
        body: `Library Notice:

The following book checked out to your account is due soon:
- "Computer Science Principles" by John Smith
Due Date: July 15, 2023

You may renew this item online if no one has placed a hold on it.

Stanford University Libraries`
    },
    {
        id: '5',
        from: 'careercenter@princeton.edu',
        subject: 'Career Fair Invitation',
        preview: 'Princeton Fall Career Fair - Register Now...',
        date: '2 days ago',
        read: true,
        body: `Princeton Students,

You're invited to attend our Fall Career Fair on September 12, 2023 from 10am-3pm in Dillon Gym.

Over 150 employers will be recruiting for full-time positions and internships. Register now through Handshake.

Princeton Career Services`
    }
];
// In a real app, this would be replaced with actual API calls
function fetchEmails() {
    return new Promise(resolve => {
        setTimeout(() => resolve(demoEmails), 500);
    });
}
// Enhanced email generation with realistic patterns
function generateRandomEmail(domain) {
    const patterns = [
        () => `s${Math.floor(Math.random() * 1000000)}@${domain}`,
        () => `student${Math.floor(Math.random() * 1000)}@${domain}`,
        () => {
            const first = ['j', 'm', 'a', 's', 'k', 'd'][Math.floor(Math.random() * 6)];
            const last = ['smith', 'johnson', 'williams', 'brown', 'jones', 'miller'][Math.floor(Math.random() * 6)];
            return `${first}.${last}${Math.floor(Math.random() * 10)}@${domain}`;
        },
        () => {
            const year = new Date().getFullYear();
            return `classof${year - Math.floor(Math.random() * 4)}@${domain}`;
        }
    ];
    
    return patterns[Math.floor(Math.random() * patterns.length)]();
}

// Function to validate email format
function isValidEmail(email) {
    const re = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
    return re.test(email);
}

// Function to check if domain exists (simulated)
function checkDomainAvailability(domain) {
const availableDomains = [
        'ccsf.edu', 'mcc.edu', 'bmcc.cuny.edu', 'aacc.edu',
        'ucla.edu', 'umich.edu', 'utexas.edu', 'fsu.edu',
        'harvard.edu', 'yale.edu', 'princeton.edu', 'columbia.edu',
        'mit.edu', 'caltech.edu', 'stanford.edu', 'gatech.edu',
        'lsu.edu'
    ];
return availableDomains.includes(domain);
}