1 /// NOTE: These permissions are for UI cosmetics only!
2 /// They should be in sync with the backend Policies.
4 import * as Episode from './Episode';
5 import Round from './Round';
7 export const hasGlobalRole = (user, role) =>
8 user && role && user.global_roles && user.global_roles.includes(role);
10 export const isAdmin = user => user && user.role === 'admin';
12 export const isSameUser = (user, subject) => user && subject && user.id === subject.id;
16 export const isChannelAdmin = (user, channel) =>
17 user && channel && user.channel_crews &&
18 user.channel_crews.find(c => c.role === 'admin' && c.channel_id === channel.id);
20 export const isAnyChannelAdmin = user =>
21 user && user.channel_crews && user.channel_crews.find(c => c.role === 'admin');
25 export const mayEditContent = user =>
26 user && hasGlobalRole(user, 'content');
30 export const isCommentator = (user, episode) => {
31 if (!user || !episode || !episode.crew) return false;
32 return !!episode.crew.find(c => c.role === "commentary" && c.user_id === user.id);
35 export const isTracker = (user, episode) => {
36 if (!user || !episode || !episode.crew) return false;
37 return !!episode.crew.find(c => c.role === "tracking" && c.user_id === user.id);
40 export const episodeHasChannel = (episode, channel) =>
41 episode && channel && episode.channels && episode.channels.find(c => c.id === channel.id);
43 export const mayRestreamEpisodes = user => isAnyChannelAdmin(user);
45 export const mayEditRestream = (user, episode, channel) =>
46 episodeHasChannel(episode, channel) && isChannelAdmin(user, channel);
48 export const canApplyForEpisode = (user, episode, as) => {
49 if (!user) return false;
50 if (as === 'commentary') return Episode.acceptsComms(episode) && !isCommentator(user, episode);
51 if (as === 'tracking') return Episode.acceptsTrackers(episode) && !isTracker(user, episode);
55 export const applicableChannels = (user, episode, as) => {
56 if (!user || !episode) return [];
57 const assigned_channels = (episode.crew || [])
58 .filter(c => c.user_id === user.id)
59 .map(c => c.channel_id);
60 const channels = episode.channels || [];
61 if (as === 'commentary') return channels.filter(c =>
62 c.pivot && c.pivot.accept_comms && !assigned_channels.includes(c.id));
63 if (as === 'tracking') return channels.filter(c =>
64 c.pivot && c.pivot.accept_tracker && !assigned_channels.includes(c.id));
68 export const canRestreamEpisode = (user, episode) => {
69 if (!user || !episode || !mayRestreamEpisodes(user)) return false;
70 const available_channels = user.channel_crews
71 .filter(c => c.role === 'admin')
72 .map(c => c.channel_id);
73 const claimed_channels = ((episode && episode.channels) || []).map(c => c.id);
74 const remaining_channels = available_channels.filter(id => !claimed_channels.includes(id));
75 return remaining_channels.length > 0;
80 export const isApplicant = (user, tournament) => {
81 if (!user || !tournament || !tournament.applications) {
84 return tournament.applications.find(p => p.user && p.user.id == user.id);
87 export const isDeniedApplicant = (user, tournament) => {
88 if (!user || !tournament || !tournament.applications) {
91 const applicant = tournament.applications.find(p => p.user && p.user.id == user.id);
92 return applicant && applicant.denied;
95 export const isParticipant = (user, tournament) =>
96 user && tournament && tournament.participants &&
97 tournament.participants.find(p => p.user && p.user.id == user.id);
99 export const isRunner = (user, tournament) => {
100 const p = isParticipant(user, tournament);
101 return p && p.roles && p.roles.includes('runner');
104 export const isTournamentAdmin = (user, tournament) => {
105 const p = isParticipant(user, tournament);
106 return p && p.roles && p.roles.includes('admin');
109 export const isTournamentCrew = (user, tournament) =>
110 isTournamentAdmin(user, tournament) || isTournamentMonitor(user, tournament);
112 export const isTournamentMonitor = (user, tournament) => {
113 const p = isParticipant(user, tournament);
114 return p && p.roles && p.roles.includes('monitor');
117 export const hasFinished = (user, round) =>
118 user && round && round.results &&
119 round.results.find(r => r.user_id == user.id && r.has_finished);
121 export const mayAddRounds = (user, tournament) =>
122 !tournament.locked &&
123 (isRunner(user, tournament) || isTournamentAdmin(user, tournament));
125 export const mayApply = (user, tournament) =>
126 user && tournament && tournament.accept_applications &&
127 !isRunner(user, tournament) && !isApplicant(user, tournament);
129 export const mayHandleApplications = (user, tournament) =>
130 tournament && tournament.accept_applications && isTournamentAdmin(user, tournament);
132 export const mayReportResult = (user, tournament) => {
133 if (!user || !tournament) return false;
134 if (tournament.type === 'open-async') return true;
135 return isRunner(user, tournament);
138 export const mayEditRound = (user, tournament) =>
139 !tournament.locked && isTournamentAdmin(user, tournament);
141 export const mayLockRound = (user, tournament) =>
142 !tournament.locked && isTournamentAdmin(user, tournament);
144 export const maySetSeed = (user, tournament, round) =>
146 (isRunner(user, tournament) || isTournamentAdmin(user, tournament));
148 export const mayUpdateTournament = (user, tournament) =>
149 isAdmin(user) || isTournamentAdmin(user, tournament);
151 export const mayViewProtocol = (user, tournament) =>
152 isTournamentCrew(user, tournament);
154 export const maySeeResults = (user, tournament, round) =>
156 hasFinished(user, round) ||
157 isTournamentMonitor(user, tournament) ||
158 Round.isComplete(tournament, round);
162 export const mayManageTwitchBot = user => isAnyChannelAdmin(user);
166 export const mayEditNickname = (user, subject) =>
167 isSameUser(user, subject);
169 export const mayEditStreamLink = (user, subject) =>
170 isSameUser(user, subject);