Skip to content

Commit

Permalink
feat: render (most) report updates nicely
Browse files Browse the repository at this point in the history
  • Loading branch information
Rexogamer committed Aug 6, 2023
1 parent 91a52d7 commit 63d39d5
Show file tree
Hide file tree
Showing 3 changed files with 253 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/components/common/messaging/Message.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import {enGB, enUS} from 'date-fns/locale';
import {Message as RevoltMessage} from 'revolt.js';
import {decodeTime} from 'ulid';

import {InviteEmbed, MessageEmbed, MessageReactions, ReplyMessage} from './';
import {InviteEmbed, MessageEmbed, MessageReactions, PlatformModerationMessage, ReplyMessage} from './';
import {app, client, openUrl} from '../../../Generic';
import {Avatar} from '../../../Profile';
import {currentTheme, styles} from '../../../Theme';
import {Text, Username} from '../atoms';
import {MarkdownView} from '../MarkdownView';
import {RE_INVITE} from '../../../lib/consts';
import {RE_INVITE, USER_IDS} from '../../../lib/consts';
import {getReadableFileSize, parseRevoltNodes} from '../../../lib/utils';
const Image = FastImage;

Expand Down Expand Up @@ -195,6 +195,24 @@ export const Message = observer((props: MessageProps) => {
// </Pressable>
// );
// }
if (props.message.channel?.recipient?._id === USER_IDS.platformModeration) {
return (
<TouchableOpacity
key={props.message._id}
activeOpacity={0.8}
delayLongPress={750}
onLongPress={props.onLongPress}>
<View
style={{
marginTop: app.settings.get(
'ui.messaging.messageSpacing',
) as number,
}}
/>
<PlatformModerationMessage message={props.message} />
</TouchableOpacity>
);
}
return (
<TouchableOpacity
key={props.message._id}
Expand Down
232 changes: 232 additions & 0 deletions src/components/common/messaging/PlatformModerationMessage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,232 @@
import React from 'react';
import {View} from 'react-native';
import {observer} from 'mobx-react-lite';

import MaterialIcon from 'react-native-vector-icons/MaterialIcons';

import {Message} from 'revolt.js';

import {MarkdownView} from '../MarkdownView';
import {currentTheme} from '../../../Theme';
import {Text} from '../atoms';

type Response = {
response: string;
match: string;
};

export const PlatformModerationMessage = observer(
({message}: {message: Message}) => {
const REPORT_ID_REGEX = /[A-Z0-9]{6}/;
const REPORT_TARGET_REGEX = /(@)?[^,]*/;
const REPORT_REASON_REGEX = /[^,]*/;

const reportResponses = [
{response: 'invalid', match: 'marked as invalid.'},
{
response: 'falseReport',
match: 'False reports may lead to additional action',
},
{
response: 'insufficientEvidence',
match: 'you have additional information to support your report',
},
{response: 'duplicate', match: 'as a duplicate.'},
{response: 'actionTaken', match: 'appropriate action has been taken.'},
] as Response[];

const isReport = message.content?.match('Your report');
const isStrike = message.content?.match('received an account strike');

let filteredContent;

try {
filteredContent = message.content
?.replace('Your report (', '')
.replace('Report (', '');
} catch (error) {
console.log(
`[PLATFORMMODERATIONMESSAGE] Something went wrong filtering message ${message._id}: ${error}`,
);
}

let response = 'UNKNOWN';
let responseText: string;
let responseType: 'POSITIVE' | 'MIXED' | 'NEGATIVE' | 'UNKNOWN';

for (const r of reportResponses) {
if (filteredContent?.match(r.match)) {
response = r.response;
}
}

switch (response) {
case 'invalid':
responseText = 'Invalid report';
responseType = 'NEGATIVE';
break;
case 'falseReport':
responseText = 'False report';
responseType = 'NEGATIVE';
break;
case 'insufficientEvidence':
responseText = 'Insufficient report';
responseType = 'MIXED';
break;
case 'duplicate':
responseText = 'Duplicate report';
responseType = 'NEGATIVE';
break;
case 'actionTaken':
responseText = 'Action taken';
responseType = 'POSITIVE';
break;
default:
responseText = 'Unknown response';
responseType = 'UNKNOWN';
}

const rawReportID = isReport
? filteredContent?.match(REPORT_ID_REGEX)
: undefined;

try {
filteredContent = filteredContent
?.replace(REPORT_ID_REGEX, '')
.replace(', ', '');
} catch (error) {
console.log(
`[PLATFORMMODERATIONMESSAGE] Something went wrong 2-filtering message ${message._id}: ${error}`,
);
}

const rawReportTarget = isReport
? filteredContent?.match(REPORT_TARGET_REGEX)
: undefined;

try {
filteredContent = filteredContent
?.replace(REPORT_TARGET_REGEX, '')
.replace(', ', '');
} catch (error) {
console.log(
`[PLATFORMMODERATIONMESSAGE] Something went wrong 3-filtering message ${message._id}: ${error}`,
);
}

const rawReportReason =
isReport && filteredContent
? filteredContent.match(REPORT_REASON_REGEX)
: undefined;

console.log(filteredContent);

try {
filteredContent = filteredContent
?.replace(REPORT_REASON_REGEX, '')
.replace(', ', '');
// .replace(/".*"/, '');
} catch (error) {
console.log(
`[PLATFORMMODERATIONMESSAGE] Something went wrong 4-filtering message ${message._id}: ${error}`,
);
}

console.log(filteredContent);

let reportID: string;

let reportTarget: string;

let reportReason: string;

try {
reportID = rawReportID ? rawReportID[0] : 'UNKNOWN';
} catch (error) {
console.log(
`[PLATFORMMODERATIONMESSAGE] Something went wrong getting report ID from message ${message._id}: ${error}`,
);
reportID = 'UNKNOWN';
}

try {
reportTarget = rawReportTarget ? rawReportTarget[0] : 'UNKNOWN';
} catch (error) {
console.log(
`[PLATFORMMODERATIONMESSAGE] Something went wrong getting report target from message ${message._id}: ${error}`,
);
reportTarget = 'UNKNOWN';
}

try {
reportReason = rawReportReason
? rawReportReason[0] ?? 'UNKNOWN'
: 'UNKNOWN';
} catch (error) {
console.log(
`[PLATFORMMODERATIONMESSAGE] Something went wrong getting report reason from message ${message._id}: ${error}`,
);
reportReason = 'UNKNOWN';
}

return (
<View
style={{
backgroundColor: currentTheme.backgroundSecondary,
padding: 8,
borderRadius: 8,
marginVertical: 2,
}}>
<Text type={'h1'}>
{isReport ? 'Report update' : isStrike ? 'Strike' : 'Alert'}
</Text>
{isReport ? (
<>
<Text colour={currentTheme.foregroundSecondary}>
{reportID !== 'UNKNOWN'
? `Report ${reportID}`
: 'Unknown report ID'}
{' | '}
{reportTarget !== 'UNKNOWN'
? `${reportTarget}`
: 'Unknown subject'}
{' | '}
{reportReason !== 'UNKNOWN'
? `${reportReason}`
: 'Unknown reason'}
</Text>
<Text type={'h2'}>Response</Text>
<View style={{flexDirection: 'row'}}>
<View style={{marginEnd: 4}}>
<MaterialIcon
color={
responseType === 'POSITIVE'
? currentTheme.statusOnline
: responseType === 'MIXED'
? currentTheme.statusIdle
: responseType === 'NEGATIVE'
? currentTheme.statusBusy
: currentTheme.statusInvisible
}
name={
responseType === 'POSITIVE'
? 'check'
: responseType === 'MIXED'
? 'warning'
: responseType === 'NEGATIVE'
? 'not-interested'
: 'question-mark'
}
size={16}
/>
</View>
<Text>{responseText}</Text>
</View>
</>
) : (
<MarkdownView>{message.content}</MarkdownView>
)}
</View>
);
},
);
1 change: 1 addition & 0 deletions src/components/common/messaging/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ export {InviteEmbed} from './InviteEmbed';
export {Message} from './Message';
export {MessageEmbed} from './MessageEmbed';
export {MessageReactions} from './MessageReactions';
export {PlatformModerationMessage} from './PlatformModerationMessage';
export {ReplyMessage} from './ReplyMessage';

0 comments on commit 63d39d5

Please sign in to comment.