-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: render (most) report updates nicely
- Loading branch information
Showing
3 changed files
with
253 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
232 changes: 232 additions & 0 deletions
232
src/components/common/messaging/PlatformModerationMessage.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
); | ||
}, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters