Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ISSUE-413: Change post colour based on author's role #461

Merged
merged 12 commits into from
Jan 26, 2024
3 changes: 2 additions & 1 deletion backend/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ export enum SocketEvent {
TRACING_DISABLED = 'TRACING_DISABLED',
}

export const POST_COLOR = '#FFE663';
export const STUDENT_POST_COLOR = '#FFE678';
export const TEACHER_POST_COLOR = '#28C8FF';

export const POST_DEFAULT_OPACITY = 1;
export const POST_DEFAULT_BORDER = 'black';
Expand Down
8 changes: 4 additions & 4 deletions backend/src/models/Post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ export class DisplayAttributes {
@prop({ required: false })
public borderColor?: string;

@prop({ required: false })
public fillColor?: string;
@prop({ required: true })
public fillColor!: string;

@prop({ required: false })
public opacity?: number;
Expand Down Expand Up @@ -83,8 +83,8 @@ export class PostModel {
@prop({ required: true, type: () => [TagModel] })
public tags!: TagModel[];

@prop({ required: false, type: () => DisplayAttributes })
public displayAttributes?: DisplayAttributes;
@prop({ required: true, type: () => DisplayAttributes })
public displayAttributes!: DisplayAttributes;
}

export default getModelForClass(PostModel);
9 changes: 6 additions & 3 deletions backend/src/socket/events/post.events.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Server, Socket } from 'socket.io';
import {
POST_COLOR,
POST_DEFAULT_OPACITY,
POST_MOVING_FILL,
POST_MOVING_OPACITY,
SocketEvent,
STUDENT_POST_COLOR,
} from '../../constants';
import { CommentModel } from '../../models/Comment';
import { UpvoteModel } from '../../models/Upvote';
Expand All @@ -21,6 +21,7 @@ import {
import dalVote from '../../repository/dalVote';
import WorkflowManager from '../../agents/workflow.agent';
import { TaskActionType } from '../../models/Workflow';
import { getDefaultPostColor } from '../../utils/board.helpers';

class PostCreate {
static type: SocketEvent = SocketEvent.POST_CREATE;
Expand Down Expand Up @@ -92,10 +93,12 @@ class PostStopMove {
static async handleEvent(
input: SocketPayload<PostStopMoveEventInput>
): Promise<PostModel | null> {
const post = await dalPost.update(input.eventData.postID, {
const postID = input.eventData.postID;
const defaultFill = await getDefaultPostColor(postID);
const post = await dalPost.update(postID, {
displayAttributes: {
position: { left: input.eventData.left, top: input.eventData.top },
fillColor: POST_COLOR,
fillColor: defaultFill ?? STUDENT_POST_COLOR,
opacity: POST_DEFAULT_OPACITY,
},
});
Expand Down
22 changes: 21 additions & 1 deletion backend/src/utils/board.helpers.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import { mongo } from 'mongoose';
import { DEFAULT_TAGS, POST_TAGGED_BORDER_THICKNESS } from '../constants';
import {
DEFAULT_TAGS,
POST_TAGGED_BORDER_THICKNESS,
STUDENT_POST_COLOR,
TEACHER_POST_COLOR,
} from '../constants';
import { PermissionsModel } from '../models/Board';
import Tag, { TagModel } from '../models/Tag';
import { Role } from '../models/User';
import dalPost from '../repository/dalPost';
import dalUser from '../repository/dalUser';

export function getDefaultBoardPermissions(): PermissionsModel {
return {
Expand Down Expand Up @@ -31,3 +39,15 @@ export function getDefaultBoardTags(boardID: string): TagModel[] {
};
});
}

export async function getDefaultPostColor(
postID: string
): Promise<string | null> {
const post = await dalPost.getById(postID);
if (!post) return null;

const user = await dalUser.findByUserID(post.userID);
if (!user) return null;

return user.role == Role.STUDENT ? STUDENT_POST_COLOR : TEACHER_POST_COLOR;
}
22 changes: 16 additions & 6 deletions frontend/src/app/components/add-post-modal/add-post.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,24 @@ import Post, {
MultipleChoiceOptions,
} from 'src/app/models/post';
import { Tag } from 'src/app/models/tag';
import User from 'src/app/models/user';
import { BoardService } from 'src/app/services/board.service';
import User, { Role } from 'src/app/models/user';
import { CanvasService } from 'src/app/services/canvas.service';
import { UserService } from 'src/app/services/user.service';
import { BoardService } from 'src/app/services/board.service';
import { PostService } from 'src/app/services/post.service';
import { ProjectService } from 'src/app/services/project.service';
import { SnackbarService } from 'src/app/services/snackbar.service';
import { SocketService } from 'src/app/services/socket.service';
import {
NEEDS_ATTENTION_TAG,
POST_COLOR,
POST_TAGGED_BORDER_THICKNESS,
STUDENT_POST_COLOR,
TEACHER_POST_COLOR,
SocketEvent,
} from 'src/app/utils/constants';
import { MyErrorStateMatcher } from 'src/app/utils/ErrorStateMatcher';
import { FabricUtils } from 'src/app/utils/FabricUtils';
import Utils, { generateUniqueID } from 'src/app/utils/Utils';
import { generateUniqueID } from 'src/app/utils/Utils';

export interface AddPostDialog {
type: PostType;
Expand Down Expand Up @@ -72,6 +74,7 @@ export class AddPostComponent {
matcher = new MyErrorStateMatcher();

constructor(
public userService: UserService,
public canvasService: CanvasService,
public boardService: BoardService,
public projectService: ProjectService,
Expand Down Expand Up @@ -188,6 +191,7 @@ export class AddPostComponent {
borderWidth: containsAttentionTag
? POST_TAGGED_BORDER_THICKNESS
: undefined,
fillColor: this.defaultPostFill(),
};

return {
Expand Down Expand Up @@ -217,7 +221,7 @@ export class AddPostComponent {
title: this.title,
desc: this.message,
tags: this.tags,
displayAttributes: null,
displayAttributes: { fillColor: this.defaultPostFill() },
};
}

Expand All @@ -233,7 +237,7 @@ export class AddPostComponent {
title: this.title,
desc: this.message,
tags: this.tags,
displayAttributes: null,
displayAttributes: { fillColor: this.defaultPostFill() },
};
}

Expand Down Expand Up @@ -322,6 +326,12 @@ export class AddPostComponent {
this.dialogRef.close();
}

defaultPostFill() {
return this.userService.user?.role === Role.TEACHER
? TEACHER_POST_COLOR
: STUDENT_POST_COLOR;
}

onNoClick(): void {
this.dialogRef.close();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,12 +235,14 @@ export class BucketsModalComponent implements OnInit, OnDestroy {
(tag) => tag.name == NEEDS_ATTENTION_TAG.name
);

const fill = await this.fabricUtils.defaultPostColor(htmlPost.post.userID);
const renderAttr: DisplayAttributes = {
position: {
left: this.Xoffset,
top: this.Yoffset,
},
lock: !this.board.permissions.allowStudentMoveAny,
fillColor: fill,
borderColor: containsAttentionTag ? NEEDS_ATTENTION_TAG.color : undefined,
borderWidth: containsAttentionTag
? POST_TAGGED_BORDER_THICKNESS
Expand Down
28 changes: 13 additions & 15 deletions frontend/src/app/components/canvas/canvas.component.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
import {
Component,
OnDestroy,
OnInit,
ViewChild,
TemplateRef,
} from '@angular/core';
import { Component, OnDestroy, OnInit } from '@angular/core';
import { fabric } from 'fabric';
import { Canvas } from 'fabric/fabric-impl';

import { MatDialog, MatDialogRef } from '@angular/material/dialog';
import { MatDialog } from '@angular/material/dialog';

import Post, { PostType } from '../../models/post';

Expand All @@ -25,6 +19,7 @@ import {
POST_MOVING_FILL,
POST_MOVING_OPACITY,
SocketEvent,
STUDENT_POST_COLOR,
} from 'src/app/utils/constants';
import { UserService } from 'src/app/services/user.service';
import { Board, BoardPermissions, BoardScope } from 'src/app/models/board';
Expand All @@ -35,7 +30,6 @@ import { UpvotesService } from 'src/app/services/upvotes.service';
import { CreateWorkflowModalComponent } from '../create-workflow-modal/create-workflow-modal.component';
import { BucketsModalComponent } from '../buckets-modal/buckets-modal.component';
import { ListModalComponent } from '../list-modal/list-modal.component';
import { POST_COLOR } from 'src/app/utils/constants';
import { FileUploadService } from 'src/app/services/fileUpload.service';
import { SnackbarService } from 'src/app/services/snackbar.service';
import { TaskModalComponent } from '../task-modal/task-modal.component';
Expand All @@ -48,7 +42,6 @@ import { getErrorMessage } from 'src/app/utils/Utils';
import { Subscription } from 'rxjs';
import { FabricPostComponent } from '../fabric-post/fabric-post.component';
import { TraceService } from 'src/app/services/trace.service';
import { DistributionWorkflow } from 'src/app/models/workflow';
import Upvote from 'src/app/models/upvote';
import { ManageGroupModalComponent } from '../groups/manage-group-modal/manage-group-modal.component';
import { TodoListModalComponent } from '../todo-list-modal/todo-list-modal.component';
Expand Down Expand Up @@ -105,7 +98,6 @@ export class CanvasComponent implements OnInit, OnDestroy {
protected fabricUtils: FabricUtils,
private router: Router,
private activatedRoute: ActivatedRoute,
private confirmationRef: MatDialogRef<TemplateRef<any>>,
public snackbarService: SnackbarService,
public dialog: MatDialog,
public fileUploadService: FileUploadService,
Expand Down Expand Up @@ -248,8 +240,12 @@ export class CanvasComponent implements OnInit, OnDestroy {

const { left, top } = post.displayAttributes.position;

this.fabricUtils.animateToPosition(existing, left, top, () => {
existing = this.fabricUtils.setFillColor(existing, POST_COLOR);
this.fabricUtils.animateToPosition(existing, left, top, async () => {
const fill = await this.fabricUtils.defaultPostColor(post.userID);
existing = this.fabricUtils.setFillColor(
existing,
fill ?? STUDENT_POST_COLOR
);
existing = this.fabricUtils.setOpacity(existing, POST_DEFAULT_OPACITY);
existing = this.fabricUtils.setPostMovement(
existing,
Expand Down Expand Up @@ -600,6 +596,7 @@ export class CanvasComponent implements OnInit, OnDestroy {
if (!(isStudentAndVisible || IsTeacherAndVisisble)) {
this.updateAuthorNames(post.postID, 'Anonymous');
} else {
console.log('can');
this.userService.getOneById(post.userID).then((user: any) => {
this.updateAuthorNames(post.postID, user.username);
});
Expand Down Expand Up @@ -762,13 +759,14 @@ export class CanvasComponent implements OnInit, OnDestroy {
}
};

const handleDroppedPost = (e) => {
const handleDroppedPost = async (e) => {
if (!isMovingPost) return;

let obj = e.target;
isMovingPost = false;

obj = this.fabricUtils.setFillColor(obj, POST_COLOR);
const fill = await this.fabricUtils.defaultPostColor(obj.userID);
obj = this.fabricUtils.setFillColor(obj, fill ?? STUDENT_POST_COLOR);
obj = this.fabricUtils.setOpacity(obj, POST_DEFAULT_OPACITY);
this.canvas.renderAll();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { Component, Inject } from '@angular/core';
import { fabric } from 'fabric';
import Post from 'src/app/models/post';
import {
POST_COLOR,
POST_DEFAULT_BORDER,
POST_DEFAULT_BORDER_THICKNESS,
STUDENT_POST_COLOR,
} from 'src/app/utils/constants';
import { numDigits } from 'src/app/utils/Utils';

Expand Down Expand Up @@ -156,7 +156,7 @@ export class FabricPostComponent extends fabric.Group {
desc.getScaledHeight() +
commentButton.getScaledHeight() +
CONTENT_EXTRA_HEIGHT,
fill: fillColor ?? POST_COLOR,
fill: fillColor ?? STUDENT_POST_COLOR,
rx: 20,
ry: 20,
strokeWidth: borderWidth ?? POST_DEFAULT_BORDER_THICKNESS,
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/app/components/html-post/html-post.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import { UpvotesService } from 'src/app/services/upvotes.service';
import { PostService } from 'src/app/services/post.service';
import { SocketService } from 'src/app/services/socket.service';
import { UserService } from 'src/app/services/user.service';
import { POST_COLOR } from 'src/app/utils/constants';
import { getErrorMessage } from 'src/app/utils/Utils';
import Upvote from 'src/app/models/upvote';

Expand Down Expand Up @@ -53,7 +52,7 @@ export class HtmlPostComponent implements OnInit {

user: AuthUser;

postColor: string = POST_COLOR;
postColor: string;

showUsername = false;

Expand All @@ -72,6 +71,7 @@ export class HtmlPostComponent implements OnInit {

ngOnInit(): void {
this.user = this.userService.user!;
this.postColor = this.post.post.displayAttributes.fillColor;
}

openPostDialog(commentPress = false) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
} from 'src/app/components/add-post-modal/add-post.component';
import User from 'src/app/models/user';
import Upvote from 'src/app/models/upvote';
import { FabricUtils } from 'src/app/utils/FabricUtils';

@Component({
selector: 'app-list-modal',
Expand Down Expand Up @@ -56,6 +57,7 @@ export class ListModalComponent implements OnInit, OnDestroy {
public canvasService: CanvasService,
public socketService: SocketService,
public converters: Converters,
public fabricUtils: FabricUtils,
@Inject(MAT_DIALOG_DATA) public data: any
) {
this.board = data.board;
Expand Down Expand Up @@ -235,13 +237,14 @@ export class ListModalComponent implements OnInit, OnDestroy {
const containsAttentionTag = htmlPost.post.tags.find(
(tag) => tag.name == NEEDS_ATTENTION_TAG.name
);

const fill = await this.fabricUtils.defaultPostColor(htmlPost.post.userID);
const renderAttr: DisplayAttributes = {
position: {
left: this.Xoffset,
top: this.Yoffset,
},
lock: !this.board.permissions.allowStudentMoveAny,
fillColor: fill,
borderColor: containsAttentionTag ? NEEDS_ATTENTION_TAG.color : undefined,
borderWidth: containsAttentionTag
? POST_TAGGED_BORDER_THICKNESS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import Post, {
} from 'src/app/models/post';
import { DELETE } from '@angular/cdk/keycodes';
import { SocketEvent } from 'src/app/utils/constants';
import { POST_COLOR } from 'src/app/utils/constants';
import { ConfirmModalComponent } from '../confirm-modal/confirm-modal.component';
import { SocketService } from 'src/app/services/socket.service';
import { CanvasService } from 'src/app/services/canvas.service';
Expand Down Expand Up @@ -129,6 +128,7 @@ export class PostModalComponent {
this.showComments = data?.commentPress ? true : false;
this.postService.get(data.post.postID).then(async (p: Post) => {
this.post = p;
this.postColor = p.displayAttributes.fillColor;
this.title = p.title;
this.editingTitle = linkifyStr(p.title, {
defaultProtocol: 'https',
Expand Down Expand Up @@ -180,7 +180,6 @@ export class PostModalComponent {
this.showAuthorName =
(isStudent && data.board.permissions.showAuthorNameStudent) ||
(isTeacher && data.board.permissions.showAuthorNameTeacher);
this.postColor = POST_COLOR;
}

ngOnInit(): void {
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/app/models/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export class DisplayAttributes {
lock?: boolean;
borderWidth?: number;
borderColor?: string;
fillColor?: string;
fillColor: string;
opacity?: number;
}

Expand Down Expand Up @@ -42,5 +42,5 @@ export default class Post {
author: string;
tags: Tag[];

displayAttributes: DisplayAttributes | null;
displayAttributes: DisplayAttributes;
}
Loading