Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
Sharkey
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Dima Krasner
Sharkey
Commits
bc2c40a6
Commit
bc2c40a6
authored
3 years ago
by
syuilo
Browse files
Options
Downloads
Patches
Plain Diff
refactor and performance improvements
parent
7a4c3bab
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
packages/backend/src/models/repositories/note.ts
+65
-61
65 additions, 61 deletions
packages/backend/src/models/repositories/note.ts
with
65 additions
and
61 deletions
packages/backend/src/models/repositories/note.ts
+
65
−
61
View file @
bc2c40a6
...
...
@@ -71,6 +71,69 @@ async function hideNote(packedNote: Packed<'Note'>, meId: User['id'] | null) {
}
}
async
function
populatePoll
(
note
:
Note
,
meId
:
User
[
'
id
'
]
|
null
)
{
const
poll
=
await
Polls
.
findOneByOrFail
({
noteId
:
note
.
id
});
const
choices
=
poll
.
choices
.
map
(
c
=>
({
text
:
c
,
votes
:
poll
.
votes
[
poll
.
choices
.
indexOf
(
c
)],
isVoted
:
false
,
}));
if
(
meId
)
{
if
(
poll
.
multiple
)
{
const
votes
=
await
PollVotes
.
findBy
({
userId
:
meId
,
noteId
:
note
.
id
,
});
const
myChoices
=
votes
.
map
(
v
=>
v
.
choice
);
for
(
const
myChoice
of
myChoices
)
{
choices
[
myChoice
].
isVoted
=
true
;
}
}
else
{
const
vote
=
await
PollVotes
.
findOneBy
({
userId
:
meId
,
noteId
:
note
.
id
,
});
if
(
vote
)
{
choices
[
vote
.
choice
].
isVoted
=
true
;
}
}
}
return
{
multiple
:
poll
.
multiple
,
expiresAt
:
poll
.
expiresAt
,
choices
,
};
}
async
function
populateMyReaction
(
note
:
Note
,
meId
:
User
[
'
id
'
],
_hint_
?:
{
myReactions
:
Map
<
Note
[
'
id
'
],
NoteReaction
|
null
>
;
})
{
if
(
_hint_
?.
myReactions
)
{
const
reaction
=
_hint_
.
myReactions
.
get
(
note
.
id
);
if
(
reaction
)
{
return
convertLegacyReaction
(
reaction
.
reaction
);
}
else
if
(
reaction
===
null
)
{
return
undefined
;
}
// 実装上抜けがあるだけかもしれないので、「ヒントに含まれてなかったら(=undefinedなら)return」のようにはしない
}
const
reaction
=
await
NoteReactions
.
findOneBy
({
userId
:
meId
,
noteId
:
note
.
id
,
});
if
(
reaction
)
{
return
convertLegacyReaction
(
reaction
.
reaction
);
}
return
undefined
;
}
export
const
NoteRepository
=
db
.
getRepository
(
Note
).
extend
({
async
isVisibleForMe
(
note
:
Note
,
meId
:
User
[
'
id
'
]
|
null
):
Promise
<
boolean
>
{
// visibility が specified かつ自分が指定されていなかったら非表示
...
...
@@ -141,65 +204,6 @@ export const NoteRepository = db.getRepository(Note).extend({
const
note
=
typeof
src
===
'
object
'
?
src
:
await
this
.
findOneByOrFail
({
id
:
src
});
const
host
=
note
.
userHost
;
async
function
populatePoll
()
{
const
poll
=
await
Polls
.
findOneByOrFail
({
noteId
:
note
.
id
});
const
choices
=
poll
.
choices
.
map
(
c
=>
({
text
:
c
,
votes
:
poll
.
votes
[
poll
.
choices
.
indexOf
(
c
)],
isVoted
:
false
,
}));
if
(
poll
.
multiple
)
{
const
votes
=
await
PollVotes
.
findBy
({
userId
:
meId
!
,
noteId
:
note
.
id
,
});
const
myChoices
=
votes
.
map
(
v
=>
v
.
choice
);
for
(
const
myChoice
of
myChoices
)
{
choices
[
myChoice
].
isVoted
=
true
;
}
}
else
{
const
vote
=
await
PollVotes
.
findOneBy
({
userId
:
meId
!
,
noteId
:
note
.
id
,
});
if
(
vote
)
{
choices
[
vote
.
choice
].
isVoted
=
true
;
}
}
return
{
multiple
:
poll
.
multiple
,
expiresAt
:
poll
.
expiresAt
,
choices
,
};
}
async
function
populateMyReaction
()
{
if
(
options
?.
_hint_
?.
myReactions
)
{
const
reaction
=
options
.
_hint_
.
myReactions
.
get
(
note
.
id
);
if
(
reaction
)
{
return
convertLegacyReaction
(
reaction
.
reaction
);
}
else
if
(
reaction
===
null
)
{
return
undefined
;
}
// 実装上抜けがあるだけかもしれないので、「ヒントに含まれてなかったら(=undefinedなら)return」のようにはしない
}
const
reaction
=
await
NoteReactions
.
findOneBy
({
userId
:
meId
!
,
noteId
:
note
.
id
,
});
if
(
reaction
)
{
return
convertLegacyReaction
(
reaction
.
reaction
);
}
return
undefined
;
}
let
text
=
note
.
text
;
if
(
note
.
name
&&
(
note
.
url
??
note
.
uri
))
{
...
...
@@ -255,10 +259,10 @@ export const NoteRepository = db.getRepository(Note).extend({
_hint_
:
options
?.
_hint_
,
})
:
undefined
,
poll
:
note
.
hasPoll
?
populatePoll
()
:
undefined
,
poll
:
note
.
hasPoll
?
populatePoll
(
note
,
meId
)
:
undefined
,
...(
meId
?
{
myReaction
:
populateMyReaction
(),
myReaction
:
populateMyReaction
(
note
,
meId
,
options
?.
_hint_
),
}
:
{}),
}
:
{}),
});
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment