mirror of
https://github.com/janoodleFTW/timy-messenger.git
synced 2026-01-22 18:05:13 +00:00
Added group-create.js script with example
This commit is contained in:
parent
096662ac20
commit
46effc5e50
1 changed files with 54 additions and 0 deletions
54
firebase/scripts/group-create.js
Normal file
54
firebase/scripts/group-create.js
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
const admin = require('firebase-admin');
|
||||
|
||||
var serviceAccount = require("../your-firebase-sdk-config-file.json");
|
||||
|
||||
admin.initializeApp({
|
||||
credential: admin.credential.cert(serviceAccount),
|
||||
databaseURL: "https://your-firebase-project.firebaseio.com"
|
||||
});
|
||||
|
||||
const db = admin.firestore();
|
||||
|
||||
(async () => {
|
||||
try {
|
||||
|
||||
// Get all users of the database
|
||||
const users = await db.collection('/users').listDocuments();
|
||||
const members = [];
|
||||
for (let i = 0; i < users.length; i++) {
|
||||
members[i] = (await users[i].get()).data().uid;
|
||||
}
|
||||
|
||||
// create a new 'dev' group
|
||||
const group = await db.collection('/groups').add({
|
||||
'abbreviation': 'DV',
|
||||
'color': 'fcba03',
|
||||
'name': 'dev',
|
||||
'members': members
|
||||
});
|
||||
|
||||
// add all users to the 'dev' group
|
||||
for (let i = 0; i < users.length; i++) {
|
||||
const joinedGroups = (await users[i].get()).data().joinedGroups;
|
||||
joinedGroups.push(group.id);
|
||||
users[i].update({
|
||||
'joinedGroups' : joinedGroups
|
||||
});
|
||||
}
|
||||
|
||||
// create a general channel
|
||||
db.collection('/groups')
|
||||
.doc(group.id)
|
||||
.collection('/channels')
|
||||
.add({
|
||||
'name': "general",
|
||||
'type': "TOPIC",
|
||||
'visibility': "OPEN"
|
||||
})
|
||||
|
||||
console.log(`Group with id ${group.id} created`);
|
||||
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
}
|
||||
})();
|
||||
Loading…
Add table
Add a link
Reference in a new issue