Skip to content
This repository has been archived by the owner on Jan 21, 2024. It is now read-only.

Commit

Permalink
Merge pull request #44 from MitchTalmadge/develop
Browse files Browse the repository at this point in the history
Guild Storage Cache, Re-Enable Role Sync on Startup
  • Loading branch information
MitchTalmadge authored Nov 15, 2020
2 parents 6df5f64 + 6b56e97 commit a73970b
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/models/database/guild-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export interface IGuildStorage extends Document {
}

export const GuildStorageSchema = new Schema({
guildId: { type: Schema.Types.String, required: true },
guildId: { type: Schema.Types.String, required: true, index: true },

majorImplements: {
type: Map,
Expand Down
4 changes: 2 additions & 2 deletions src/models/database/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ export interface IUserCourseAssignment {
}

export const UserSchema = new Schema({
discordUserId: { type: Schema.Types.String, required: true, unique: true },
studentId: { type: Schema.Types.String, required: false },
discordUserId: { type: Schema.Types.String, required: true, unique: true, index: true },
studentId: { type: Schema.Types.String, required: false, index: true },
verificationStatus: { type: Schema.Types.Number, required: true, default: VerificationStatus.UNVERIFIED },
verificationCode: { type: Schema.Types.String, required: false },

Expand Down
36 changes: 28 additions & 8 deletions src/services/database/guild-storage.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,48 @@
import { GuildContext } from "guild-context";
import { Course, PartialCourse } from "models/course";
import { PartialCourse } from "models/course";
import { GuildStorage, IGuildStorage } from "models/database/guild-storage";
import { ICourseImplement } from "models/implement/course";
import { IMajorImplement } from "models/implement/major";
import { IVerificationImplement } from "models/implement/verification";
import { Major } from "models/major";

export class GuildStorageDatabaseService {
private static CACHE: {[guildId: string]: IGuildStorage} = {}

public static async findOrCreateGuildStorage(guildContext: GuildContext): Promise<IGuildStorage> {
// Check cache.
let storage = this.CACHE[guildContext.guild.id];
if(storage) {
return storage;
}

// Find existing storage.
let storage = await GuildStorage.findOne({ guildId: guildContext.guild.id }).exec();
storage = await GuildStorage.findOne({ guildId: guildContext.guild.id }).exec();
if (storage) {
this.cache(guildContext, storage);
return storage;
}

// Create a new storage.
guildContext.guildLog("Creating Guild Storage");
let majorImplements = new Map<string, {}>();
storage = await new GuildStorage(<IGuildStorage>{
storage = new GuildStorage(<IGuildStorage>{
guildId: guildContext.guild.id,
majorImplements,
}).save();
});
await this.saveAndCache(guildContext, storage);
guildContext.guildLog("Storage created: " + storage.id);
return storage;
}

private static cache(guildContext: GuildContext, storage: IGuildStorage): void {
this.CACHE[guildContext.guild.id] = storage;
}

private static async saveAndCache(guildContext: GuildContext, storage: IGuildStorage): Promise<void> {
this.cache(guildContext, await storage.save());
}

public static async getMajorImplement(guildContext: GuildContext, major: Major): Promise<IMajorImplement | undefined> {
const storage = await this.findOrCreateGuildStorage(guildContext);
const majorData = storage.majorImplements.get(major.prefix);
Expand All @@ -38,8 +56,9 @@ export class GuildStorageDatabaseService {
storage.majorImplements.set(major.prefix, implement);
else
storage.majorImplements.delete(major.prefix);

await storage.save();

storage.markModified("majorImplements");
await this.saveAndCache(guildContext, storage);
}

public static async getCourseImplement(guildContext: GuildContext, course: PartialCourse): Promise<ICourseImplement | undefined> {
Expand All @@ -63,7 +82,8 @@ export class GuildStorageDatabaseService {
else
majorImplement.courseImplements.delete(course.key);

await storage.save();
storage.markModified(`majorImplements.${course.major.prefix}.courseImplements`);
await this.saveAndCache(guildContext, storage);
}

public static async getVerificationImplement(guildContext: GuildContext): Promise<IVerificationImplement | undefined> {
Expand All @@ -74,6 +94,6 @@ export class GuildStorageDatabaseService {
public static async setVerificationImplement(guildContext: GuildContext, implement: IVerificationImplement): Promise<void> {
const storage = await this.findOrCreateGuildStorage(guildContext);
storage.verificationImplement = implement;
await storage.save();
await this.saveAndCache(guildContext, storage);
}
}
2 changes: 1 addition & 1 deletion src/services/discord/role-assignment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export class DiscordRoleAssignmentService {
}

// Apply assignments.
guildContext.guildDebug(`+${rolesToAdd.length}/-${rolesToRemove.length} roles for ${DiscordUtils.describeUserForLogs(discordMember.user)}.`);
guildContext.guildLog(`+${rolesToAdd.length}/-${rolesToRemove.length} roles for ${DiscordUtils.describeUserForLogs(discordMember.user)}.`);
if (rolesToAdd.length > 0)
discordMember = await discordMember.roles.add(rolesToAdd, "StudyBot automatic role assignment.");
if (rolesToRemove.length > 0)
Expand Down
4 changes: 2 additions & 2 deletions src/services/health-assurance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ export class HealthAssuranceService {
}

// Synchronize roles.
/* await MemberUpdateService.queueSynchronizeRolesManyMembers(
await MemberUpdateService.queueSynchronizeRolesManyMembers(
this.guildContext,
members.array().filter(m => m.user.id !== this.guildContext.guild.client.user.id)
); */
);

//const users = await UserDatabaseService.getAllUsers();

Expand Down
8 changes: 4 additions & 4 deletions src/services/implement/course/implement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,29 +81,29 @@ export class CourseImplementService {

// Main Role
if(!await guildContext.guild.roles.fetch(implement.mainRoleId)) {
guildContext.guildLog(`Creating missing main role for course ${course.key}`);
implement.mainRoleId = (await CourseRoleImplementService.createMainRole(guildContext, course)).id;
guildContext.guildLog(`Created missing main role for course ${course.key}`);
update = true;
}

// TA Role
if(!await guildContext.guild.roles.fetch(implement.taRoleId)) {
guildContext.guildLog(`Creating missing TA role for course ${course.key}`);
implement.taRoleId = (await CourseRoleImplementService.createTARole(guildContext, course)).id;
guildContext.guildLog(`Created missing TA role for course ${course.key}`);
update = true;
}

// Channels
for(let type of CourseImplementChannelType.values()) {
const channel = guildContext.guild.channels.resolve(implement.channelIds[type]);
if(!channel) {
guildContext.guildLog(`Creating missing ${CourseImplementChannelType[type]} channel for course ${course.key}`);
const majorCategoryId = await MajorImplementService.getCategoryIdForNewCourseImplement(guildContext, course.major, type);
implement.channelIds[type] = (await CourseChannelImplementService.createChannelByType(guildContext, type, course, majorCategoryId, implement.mainRoleId, implement.taRoleId)).id;
guildContext.guildLog(`Created missing ${CourseImplementChannelType[type]} channel for course ${course.key}`);
update = true;
} else {
guildContext.guildLog(`Resetting ${CourseImplementChannelType[type]} channel permissions for course ${course.key}`);
await CourseChannelImplementService.resetChannelPermissionsByType(guildContext, type, implement.mainRoleId, implement.taRoleId, channel);
guildContext.guildLog(`Reset ${CourseImplementChannelType[type]} channel permissions for course ${course.key}`);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/services/implement/major/implement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,8 @@ export class MajorImplementService {
}

// Pre-sort roles
mainRoles = mainRoles.sort((roleA, roleB) => roleA.name.localeCompare(roleB.name));
taRoles = taRoles.sort((roleA, roleB) => roleA.name.localeCompare(roleB.name));
mainRoles = mainRoles.sort((roleA, roleB) => roleB.name.localeCompare(roleA.name));
taRoles = taRoles.sort((roleA, roleB) => roleB.name.localeCompare(roleA.name));

// Calculate role positions
const rolePositions: Discord.RolePosition[] = [];
Expand Down

0 comments on commit a73970b

Please sign in to comment.