Skip to content

Commit

Permalink
Various improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
absidue committed Jan 22, 2023
1 parent 609aafd commit b697858
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,35 +86,21 @@ export default Vue.extend({

getCommentData: function () {
this.isLoading = true
if (process.env.IS_ELECTRON) {
switch (this.backendPreference) {
case 'local':
this.getCommentDataLocal()
break
case 'invidious':
this.getCommentDataInvidious()
break
}
} else {
if (!process.env.IS_ELECTRON || this.backendPreference === 'invidious') {
this.getCommentDataInvidious()
} else {
this.getCommentDataLocal()
}
},

getMoreComments: function () {
if (this.commentData.length === 0 || this.nextPageToken === null || typeof this.nextPageToken === 'undefined') {
showToast(this.$t('Comments.There are no more comments for this video'))
} else {
if (process.env.IS_ELECTRON) {
switch (this.backendPreference) {
case 'local':
this.getCommentDataLocal(true)
break
case 'invidious':
this.getCommentDataInvidious()
break
}
} else {
if (!process.env.IS_ELECTRON || this.backendPreference === 'invidious') {
this.getCommentDataInvidious()
} else {
this.getCommentDataLocal(true)
}
}
},
Expand Down Expand Up @@ -152,14 +138,8 @@ export default Vue.extend({
comments = await getLocalComments(this.id, this.sortNewest)
}

const parsedComments = []
for (const commentThread of comments.contents) {
const hasReplies = commentThread.has_replies
const hasOwnerReplied = hasReplies ? commentThread.comment_replies_data.has_channel_owner_replied : false
const replyToken = hasReplies ? commentThread : null

parsedComments.push(parseLocalComment(commentThread.comment, hasOwnerReplied, replyToken))
}
const parsedComments = comments.contents
.map(commentThread => parseLocalComment(commentThread.comment, commentThread))

if (more) {
this.commentData = this.commentData.concat(parsedComments)
Expand Down Expand Up @@ -195,10 +175,10 @@ export default Vue.extend({

if (comment.replies.length > 0) {
await commentThread.getContinuation()
comment.replies = comment.replies.concat(commentThread.replies.map(parseLocalComment))
comment.replies = comment.replies.concat(commentThread.replies.map(reply => parseLocalComment(reply)))
} else {
await commentThread.getReplies()
comment.replies = commentThread.replies.map(parseLocalComment)
comment.replies = commentThread.replies.map(reply => parseLocalComment(reply))
}

comment.replyToken = commentThread.has_continuation ? commentThread : null
Expand Down
32 changes: 27 additions & 5 deletions src/renderer/helpers/api/local.js
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ export function parseLocalTextRuns(runs, emojiSize = 16) {
// that way we avoid layout shifts when it loads
parsedRuns.push(`<img src="${emoji.image[0].url}" alt="${altText}" width="${emojiSize}" height="${emojiSize}" loading="lazy" style="vertical-align: middle">`)
} else {
const { text, endpoint } = run
const { text, bold, italics, strikethrough, endpoint } = run

if (endpoint && !text.startsWith('#')) {
switch (endpoint.metadata.page_type) {
Expand Down Expand Up @@ -423,7 +423,20 @@ export function parseLocalTextRuns(runs, emojiSize = 16) {
}
}
} else {
parsedRuns.push(text)
let formattedText = text
if (bold) {
formattedText = `<b>${formattedText}</b>`
}

if (italics) {
formattedText = `<i>${formattedText}</i>`
}

if (strikethrough) {
formattedText = `<s>${formattedText}</s>`
}

parsedRuns.push(formattedText)
}
}
}
Expand Down Expand Up @@ -452,8 +465,17 @@ export function mapLocalFormat(format) {

/**
* @param {import('youtubei.js/dist/src/parser/classes/comments/Comment').default} comment
* @param {import('youtubei.js/dist/src/parser/classes/comments/CommentThread').default} commentThread
*/
export function parseLocalComment(comment, hasOwnerReplied = false, replyToken = null) {
export function parseLocalComment(comment, commentThread = undefined) {
let hasOwnerReplied = false
let replyToken = null

if (commentThread?.has_replies) {
hasOwnerReplied = commentThread.comment_replies_data.has_channel_owner_replied
replyToken = commentThread
}

return {
dataType: 'local',
authorLink: comment.author.id,
Expand All @@ -462,10 +484,10 @@ export function parseLocalComment(comment, hasOwnerReplied = false, replyToken =
isPinned: comment.is_pinned,
isOwner: comment.author_is_channel_owner,
isMember: comment.is_member,
text: Autolinker.link(parseLocalTextRuns(comment.content.runs, 14)),
text: Autolinker.link(parseLocalTextRuns(comment.content.runs, 16)),
time: toLocalePublicationString({ publishText: comment.published.text.replace('(edited)', '').trim() }),
likes: comment.vote_count,
isHeadered: comment.is_hearted,
isHearted: comment.is_hearted,
numReplies: comment.reply_count,
hasOwnerReplied,
replyToken,
Expand Down

0 comments on commit b697858

Please sign in to comment.