-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #117 from serafuku/yunochi/develop
🐛 글자수 제한 문제 수정
- Loading branch information
Showing
4 changed files
with
169 additions
and
129 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
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,17 @@ | ||
export function clampText(text: string, max_len: number, mandatoryTailString?: string, moreString?: string) { | ||
const mandatoryLen = mandatoryTailString?.length ?? 0; | ||
const textLen = text.length; | ||
const moreStringLen = moreString?.length ?? 0; | ||
if (textLen + mandatoryLen > max_len) { | ||
if (mandatoryLen + moreStringLen > max_len) { | ||
console.error(`Text length error! moreStringLen + moreStringLen > ${max_len}`); | ||
throw new Error(`Text length error! moreStringLen + moreStringLen > ${max_len}`); | ||
} | ||
const newText = | ||
text.substring(0, max_len - (mandatoryLen + moreStringLen)) + (moreString ?? '') + (mandatoryTailString ?? ''); | ||
return newText; | ||
} else { | ||
const newText = text + mandatoryTailString; | ||
return newText; | ||
} | ||
} |
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,66 @@ | ||
import { mastodonTootAnswers, MkNoteAnswers } from '@/app'; | ||
import { GetPrismaClient } from '@/app/api/_utils/getPrismaClient/get-prisma-client'; | ||
import { Logger } from '@/utils/logger/Logger'; | ||
import { user } from '@prisma/client'; | ||
|
||
export async function mastodonToot( | ||
{ | ||
user, | ||
}: { | ||
user: user; | ||
}, | ||
{ | ||
title, | ||
text, | ||
visibility, | ||
}: { | ||
title: string; | ||
text: string; | ||
visibility: MkNoteAnswers['visibility']; | ||
}, | ||
) { | ||
const tootLogger = new Logger('mastodonToot'); | ||
let newVisibility: 'public' | 'unlisted' | 'private'; | ||
switch (visibility) { | ||
case 'public': | ||
newVisibility = 'public'; | ||
break; | ||
case 'home': | ||
newVisibility = 'unlisted'; | ||
break; | ||
case 'followers': | ||
newVisibility = 'private'; | ||
break; | ||
default: | ||
newVisibility = 'public'; | ||
break; | ||
} | ||
const newAnswerToot: mastodonTootAnswers = { | ||
spoiler_text: title, | ||
status: text, | ||
visibility: newVisibility, | ||
}; | ||
try { | ||
const res = await fetch(`https://${user.hostName}/api/v1/statuses`, { | ||
method: 'POST', | ||
headers: { | ||
Authorization: `Bearer ${user.token}`, | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify(newAnswerToot), | ||
}); | ||
if (res.status === 401 || res.status === 403) { | ||
tootLogger.warn('User Revoked Access token. JWT를 Revoke합니다.. Detail:', await res.text()); | ||
const prisma = GetPrismaClient.getClient(); | ||
await prisma.user.update({ where: { handle: user.handle }, data: { jwtIndex: user.jwtIndex + 1 } }); | ||
throw new Error('Toot Create Fail! (Token Revoked)'); | ||
} else if (!res.ok) { | ||
throw new Error(`HTTP Error! status:${await res.text()}`); | ||
} else { | ||
tootLogger.log(`Toot Created! ${res.statusText}`); | ||
} | ||
} catch (err) { | ||
tootLogger.warn(`Toot Create Fail!`, err); | ||
throw err; | ||
} | ||
} |
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,59 @@ | ||
import { MkNoteAnswers } from '@/app'; | ||
import { GetPrismaClient } from '@/app/api/_utils/getPrismaClient/get-prisma-client'; | ||
import { Logger } from '@/utils/logger/Logger'; | ||
import { createHash } from 'crypto'; | ||
import { user, server } from '@prisma/client'; | ||
|
||
export async function mkMisskeyNote( | ||
{ | ||
user, | ||
server, | ||
}: { | ||
user: user; | ||
server: server; | ||
}, | ||
{ | ||
title, | ||
text, | ||
visibility, | ||
}: { | ||
title: string; | ||
text: string; | ||
visibility: MkNoteAnswers['visibility']; | ||
}, | ||
) { | ||
const NoteLogger = new Logger('mkMisskeyNote'); | ||
|
||
const i = createHash('sha256') | ||
.update(user.token + server.appSecret, 'utf-8') | ||
.digest('hex'); | ||
const newAnswerNote: MkNoteAnswers = { | ||
i: i, | ||
cw: title, | ||
text: text, | ||
visibility: visibility, | ||
}; | ||
try { | ||
const res = await fetch(`https://${user.hostName}/api/notes/create`, { | ||
method: 'POST', | ||
headers: { | ||
Authorization: `Bearer ${i}`, | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify(newAnswerNote), | ||
}); | ||
if (res.status === 401 || res.status === 403) { | ||
NoteLogger.warn('User Revoked Access token. JWT를 Revoke합니다... Detail:', await res.text()); | ||
const prisma = GetPrismaClient.getClient(); | ||
await prisma.user.update({ where: { handle: user.handle }, data: { jwtIndex: user.jwtIndex + 1 } }); | ||
throw new Error('Note Create Fail! (Token Revoked)'); | ||
} else if (!res.ok) { | ||
throw new Error(`Note Create Fail! ${await res.text()}`); | ||
} else { | ||
NoteLogger.log(`Note Created! ${res.statusText}`); | ||
} | ||
} catch (err) { | ||
NoteLogger.warn(err); | ||
throw err; | ||
} | ||
} |