Skip to content

Commit

Permalink
revert some changes
Browse files Browse the repository at this point in the history
  • Loading branch information
asliayk committed Nov 19, 2024
1 parent c0c553d commit b4a0e9f
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
(reactionsUpdated)="onReactionsUpdated($event)"
(mayEditOrDeleteOutput)="onMayEditOrDelete($event)"
(isDeleteEvent)="onDeleteEvent(true)"
[hasChannelModerationRights]="hasChannelModerationRights()"
/>
</div>
</div>
Expand Down
1 change: 0 additions & 1 deletion src/main/webapp/app/shared/metis/post/post.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@
(mayEditOrDeleteOutput)="onMayEditOrDelete($event)"
(canPinOutput)="onCanPin($event)"
(isDeleteEvent)="onDeleteEvent(true)"
[hasChannelModerationRights]="hasChannelModerationRights()"
/>
}
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
(openPostingCreateEditModal)="createAnswerPostModal.open()"
(userReferenceClicked)="userReferenceClicked.emit($event)"
(channelReferenceClicked)="channelReferenceClicked.emit($event)"
[hasChannelModerationRights]="hasChannelModerationRights()"
/>
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
SimpleChanges,
ViewChild,
ViewContainerRef,
input,
} from '@angular/core';
import { PostingFooterDirective } from 'app/shared/metis/posting-footer/posting-footer.directive';
import { Post } from 'app/entities/metis/post.model';
Expand All @@ -37,6 +36,7 @@ export class PostFooterComponent extends PostingFooterDirective<Post> implements
@Input() readOnlyMode = false;
@Input() previewMode = false;
@Input() modalRef?: NgbModalRef;
@Input() hasChannelModerationRights = false;
@Input() showAnswers = false;
@Input() isCommunicationPage = false;
@Input() sortedAnswerPosts: AnswerPost[] = [];
Expand All @@ -48,7 +48,6 @@ export class PostFooterComponent extends PostingFooterDirective<Post> implements
@ViewChild(AnswerPostCreateEditModalComponent) answerPostCreateEditModal?: AnswerPostCreateEditModalComponent;
@ViewChild('createEditAnswerPostContainer', { read: ViewContainerRef }) containerRef!: ViewContainerRef;
@ViewChild('createAnswerPostModal') createAnswerPostModalComponent!: AnswerPostCreateEditModalComponent;
hasChannelModerationRights = input<boolean>(false);

createdAnswerPost: AnswerPost;
isAtLeastTutorInCourse = false;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component, EventEmitter, Input, OnChanges, OnInit, Output, input } from '@angular/core';
import { Component, EventEmitter, Input, OnChanges, OnInit, Output } from '@angular/core';
import { Reaction } from 'app/entities/metis/reaction.model';
import { PostingsReactionsBarDirective } from 'app/shared/metis/posting-reactions-bar/posting-reactions-bar.directive';
import { AnswerPost } from 'app/entities/metis/answer-post.model';
Expand Down Expand Up @@ -27,7 +27,6 @@ export class AnswerPostReactionsBarComponent extends PostingsReactionsBarDirecti
readonly faPencilAlt = faPencilAlt;
@Input() isEmojiCount: boolean = false;
@Output() postingUpdated = new EventEmitter<void>();
hasChannelModerationRights = input<boolean>(false);

constructor(metisService: MetisService) {
super(metisService);
Expand Down Expand Up @@ -71,7 +70,8 @@ export class AnswerPostReactionsBarComponent extends PostingsReactionsBarDirecti
this.isAnswerOfAnnouncement = getAsChannelDTO(this.posting.post?.conversation)?.isAnnouncementChannel ?? false;
const isCourseWideChannel = getAsChannelDTO(this.posting.post?.conversation)?.isCourseWide ?? false;
const isAtLeastInstructorInCourse = this.metisService.metisUserIsAtLeastInstructorInCourse();
const mayEditOrDeleteOtherUsersAnswer = (isCourseWideChannel && isAtLeastInstructorInCourse) || (this.hasChannelModerationRights() ?? false);
const mayEditOrDeleteOtherUsersAnswer =
(isCourseWideChannel && isAtLeastInstructorInCourse) || (getAsChannelDTO(this.metisService.getCurrentConversation())?.hasChannelModerationRights ?? false);
this.mayEditOrDelete = !this.isReadOnlyMode && (this.isAuthorOfPosting || mayEditOrDeleteOtherUsersAnswer);
this.mayEditOrDeleteOutput.emit(this.mayEditOrDelete);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component, EventEmitter, Input, OnChanges, OnDestroy, OnInit, Output, ViewChild, input } from '@angular/core';
import { Component, EventEmitter, Input, OnChanges, OnDestroy, OnInit, Output, ViewChild } from '@angular/core';
import { Reaction } from 'app/entities/metis/reaction.model';
import { Post } from 'app/entities/metis/post.model';
import { PostingsReactionsBarDirective } from 'app/shared/metis/posting-reactions-bar/posting-reactions-bar.directive';
Expand Down Expand Up @@ -52,7 +52,6 @@ export class PostReactionsBarComponent extends PostingsReactionsBarDirective<Pos
@Input() isEmojiCount = false;
@Input() hoverBar: boolean = true;
@ViewChild('createEditModal') createEditModal!: PostCreateEditModalComponent;
hasChannelModerationRights = input<boolean>(false);

constructor(
metisService: MetisService,
Expand Down Expand Up @@ -200,7 +199,8 @@ export class PostReactionsBarComponent extends PostingsReactionsBarDirective<Pos
setMayEditOrDelete(): void {
this.isAtLeastInstructorInCourse = this.metisService.metisUserIsAtLeastInstructorInCourse();
const isCourseWideChannel = getAsChannelDTO(this.posting.conversation)?.isCourseWide ?? false;
const mayEditOrDeleteOtherUsersAnswer = (isCourseWideChannel && this.isAtLeastInstructorInCourse) || (this.hasChannelModerationRights() ?? false);
const mayEditOrDeleteOtherUsersAnswer =
(isCourseWideChannel && this.isAtLeastInstructorInCourse) || (getAsChannelDTO(this.metisService.getCurrentConversation())?.hasChannelModerationRights ?? false);
this.mayEditOrDelete = !this.readOnlyMode && !this.previewMode && (this.isAuthorOfPosting || mayEditOrDeleteOtherUsersAnswer);
this.mayEditOrDeleteOutput.emit(this.mayEditOrDelete);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@ import { MockRouter } from '../../../../../helpers/mocks/mock-router';
import { MockLocalStorageService } from '../../../../../helpers/mocks/service/mock-local-storage.service';
import { LocalStorageService, SessionStorageService } from 'ngx-webstorage';
import { By } from '@angular/platform-browser';
import { ReactingUsersOnPostingPipe } from 'app/shared/pipes/reacting-users-on-posting.pipe';
import { PLACEHOLDER_USER_REACTED, ReactingUsersOnPostingPipe } from 'app/shared/pipes/reacting-users-on-posting.pipe';
import { metisAnnouncement, metisCourse, metisPostExerciseUser1, metisPostInChannel, metisUser1, sortedAnswerArray } from '../../../../../helpers/sample/metis-sample-data';
import { EmojiComponent } from 'app/shared/metis/emoji/emoji.component';
import { NgbTooltip } from '@ng-bootstrap/ng-bootstrap';
import { NotificationService } from 'app/shared/notification/notification.service';
import { MockNotificationService } from '../../../../../helpers/mocks/service/mock-notification.service';
import { ConversationType } from 'app/entities/metis/conversation/conversation.model';
import { ConversationDTO, ConversationType } from 'app/entities/metis/conversation/conversation.model';
import { ChannelDTO } from 'app/entities/metis/conversation/channel.model';
import { User } from 'app/core/user/user.model';
import { provideHttpClient } from '@angular/common/http';
import { PostCreateEditModalComponent } from 'app/shared/metis/posting-create-edit-modal/post-create-edit-modal/post-create-edit-modal.component';
import { ConfirmIconComponent } from 'app/shared/confirm-icon/confirm-icon.component';
Expand All @@ -40,6 +41,7 @@ describe('PostReactionsBarComponent', () => {
let fixture: ComponentFixture<PostReactionsBarComponent>;
let debugElement: DebugElement;
let metisService: MetisService;
let accountService: AccountService;
let metisServiceUpdateDisplayPriorityMock: jest.SpyInstance;
let metisServiceUserIsAtLeastTutorStub: jest.SpyInstance;
let metisServiceUserIsAtLeastInstructorStub: jest.SpyInstance;
Expand Down Expand Up @@ -82,6 +84,7 @@ describe('PostReactionsBarComponent', () => {
.then(() => {
fixture = TestBed.createComponent(PostReactionsBarComponent);
metisService = TestBed.inject(MetisService);
accountService = TestBed.inject(AccountService);
debugElement = fixture.debugElement;
component = fixture.componentInstance;
metisServiceUpdateDisplayPriorityMock = jest.spyOn(metisService, 'updatePostDisplayPriority');
Expand Down Expand Up @@ -151,9 +154,14 @@ describe('PostReactionsBarComponent', () => {
component.previewMode = false;
component.isEmojiCount = false;

const channelConversation = {
type: ConversationType.CHANNEL,
hasChannelModerationRights: true,
} as ChannelDTO;

jest.spyOn(metisService, 'metisUserIsAuthorOfPosting').mockReturnValue(false);
jest.spyOn(metisService, 'metisUserIsAtLeastInstructorInCourse').mockReturnValue(false);
jest.spyOn(component, 'hasChannelModerationRights').mockReturnValue(true);
jest.spyOn(metisService, 'getCurrentConversation').mockReturnValue(channelConversation);

component.ngOnInit();
fixture.detectChanges();
Expand Down Expand Up @@ -217,75 +225,30 @@ describe('PostReactionsBarComponent', () => {
expect(getDeleteButton()).not.toBeNull();
});

describe('setMayEditOrDelete', () => {
beforeEach(() => {
jest.clearAllMocks();
component.readOnlyMode = false;
component.previewMode = false;
component.isAuthorOfPosting = false;
component.posting = {
conversation: {},
} as Post;

jest.spyOn(component.mayEditOrDeleteOutput, 'emit');
});

it('should allow edit/delete when user is the author and not in read-only or preview mode', () => {
component.isAuthorOfPosting = true;
jest.spyOn(metisService, 'metisUserIsAtLeastInstructorInCourse').mockReturnValue(false);
jest.spyOn(component, 'hasChannelModerationRights').mockReturnValue(false);

component.setMayEditOrDelete();

expect(component.mayEditOrDelete).toBeTrue();
expect(component.mayEditOrDeleteOutput.emit).toHaveBeenCalledWith(true);
});

it('should allow edit/delete when user has channel moderation rights', () => {
component.isAuthorOfPosting = false;
jest.spyOn(metisService, 'metisUserIsAtLeastInstructorInCourse').mockReturnValue(false);
jest.spyOn(component, 'hasChannelModerationRights').mockReturnValue(true);

component.setMayEditOrDelete();

expect(component.mayEditOrDelete).toBeTrue();
expect(component.mayEditOrDeleteOutput.emit).toHaveBeenCalledWith(true);
});

it('should not allow edit/delete when in read-only mode', () => {
component.readOnlyMode = true;
component.isAuthorOfPosting = true;
jest.spyOn(metisService, 'metisUserIsAtLeastInstructorInCourse').mockReturnValue(true);
jest.spyOn(component, 'hasChannelModerationRights').mockReturnValue(true);

component.setMayEditOrDelete();

expect(component.mayEditOrDelete).toBeFalse();
expect(component.mayEditOrDeleteOutput.emit).toHaveBeenCalledWith(false);
});

it('should not allow edit/delete when in preview mode', () => {
component.previewMode = true;
component.isAuthorOfPosting = true;
jest.spyOn(metisService, 'metisUserIsAtLeastInstructorInCourse').mockReturnValue(true);
jest.spyOn(component, 'hasChannelModerationRights').mockReturnValue(true);

component.setMayEditOrDelete();

expect(component.mayEditOrDelete).toBeFalse();
expect(component.mayEditOrDeleteOutput.emit).toHaveBeenCalledWith(false);
});

it('should not allow edit/delete when user is not author and lacks permissions', () => {
component.isAuthorOfPosting = false;
jest.spyOn(metisService, 'metisUserIsAtLeastInstructorInCourse').mockReturnValue(false);
jest.spyOn(component, 'hasChannelModerationRights').mockReturnValue(false);
it.each([
{ type: ConversationType.CHANNEL, hasChannelModerationRights: true } as ChannelDTO,
{ type: ConversationType.GROUP_CHAT, creator: { id: 99 } },
{ type: ConversationType.ONE_TO_ONE },
])('should initialize user authority and reactions correctly with same user', (dto: ConversationDTO) => {
component.posting!.author!.id = 99;
jest.spyOn(metisService, 'getCurrentConversation').mockReturnValue(dto);
jest.spyOn(accountService, 'userIdentity', 'get').mockReturnValue({ id: 99 } as User);

component.setMayEditOrDelete();
reactionToDelete.user = { id: 99 } as User;
post.reactions = [reactionToDelete];
component.posting = post;

expect(component.mayEditOrDelete).toBeFalse();
expect(component.mayEditOrDeleteOutput.emit).toHaveBeenCalledWith(false);
component.ngOnInit();
component.isEmojiCount = true;
fixture.detectChanges();
expect(component.reactionMetaDataMap).toEqual({
smile: {
count: 1,
hasReacted: true,
reactingUsers: [PLACEHOLDER_USER_REACTED],
},
});
expect(component.pinTooltip).toBe('artemisApp.metis.pinPostTooltip');
});

it.each`
Expand Down

0 comments on commit b4a0e9f

Please sign in to comment.