mirror of
https://github.com/omnivore-app/omnivore.git
synced 2026-03-11 08:54:26 +00:00
Merge pull request #1523 from omnivore-app/group-labels
Create labels and rules when creating or joining groups
This commit is contained in:
commit
53bd6a96f2
5 changed files with 96 additions and 0 deletions
|
|
@ -25,6 +25,7 @@ import {
|
|||
} from '../../generated/graphql'
|
||||
import {
|
||||
createGroup,
|
||||
createLabelAndRuleForGroup,
|
||||
getInviteUrl,
|
||||
getRecommendationGroups,
|
||||
joinGroup,
|
||||
|
|
@ -71,6 +72,8 @@ export const createGroupResolver = authorized<
|
|||
expiresInDays: input.expiresInDays,
|
||||
})
|
||||
|
||||
await createLabelAndRuleForGroup(uid, group.name)
|
||||
|
||||
const inviteUrl = getInviteUrl(invite)
|
||||
const user = userDataToUser(userData)
|
||||
|
||||
|
|
@ -263,6 +266,8 @@ export const joinGroupResolver = authorized<
|
|||
|
||||
const group = await joinGroup(user, inviteCode)
|
||||
|
||||
await createLabelAndRuleForGroup(user.id, group.name)
|
||||
|
||||
return {
|
||||
group,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,6 +8,9 @@ import { RecommendationGroup, User as GraphqlUser } from '../generated/graphql'
|
|||
import { getRepository } from '../entity/utils'
|
||||
import { homePageURL } from '../env'
|
||||
import { userDataToUser } from '../utils/helpers'
|
||||
import { createLabel } from './labels'
|
||||
import { createRule } from './rules'
|
||||
import { RuleActionType } from '../entity/rule'
|
||||
|
||||
export const createGroup = async (input: {
|
||||
admin: User
|
||||
|
|
@ -200,3 +203,27 @@ export const leaveGroup = async (
|
|||
return true
|
||||
})
|
||||
}
|
||||
|
||||
export const createLabelAndRuleForGroup = async (
|
||||
userId: string,
|
||||
groupName: string
|
||||
) => {
|
||||
// create a new label for the group
|
||||
const label = await createLabel(userId, { name: groupName })
|
||||
|
||||
// create a rule to add the label to all pages in the group
|
||||
await createRule(userId, {
|
||||
name: groupName,
|
||||
actions: [
|
||||
{
|
||||
type: RuleActionType.AddLabel,
|
||||
params: [label.id],
|
||||
},
|
||||
{
|
||||
type: RuleActionType.SendNotification,
|
||||
params: [groupName],
|
||||
},
|
||||
],
|
||||
filter: `recommendedBy:"${groupName}"`,
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ import { addLabelInPage } from '../elastic/labels'
|
|||
import { getRepository } from '../entity/utils'
|
||||
import { Link } from '../entity/link'
|
||||
import DataLoader from 'dataloader'
|
||||
import { generateRandomColor } from '../utils/helpers'
|
||||
|
||||
const batchGetLabelsFromLinkIds = async (
|
||||
linkIds: readonly string[]
|
||||
|
|
@ -76,3 +77,29 @@ export const getLabelsByIds = async (
|
|||
select: ['id', 'name', 'color', 'description', 'createdAt'],
|
||||
})
|
||||
}
|
||||
|
||||
export const createLabel = async (
|
||||
userId: string,
|
||||
label: {
|
||||
name: string
|
||||
color?: string
|
||||
description?: string
|
||||
}
|
||||
): Promise<Label> => {
|
||||
const existingLabel = await getRepository(Label).findOneBy({
|
||||
user: { id: userId },
|
||||
name: ILike(label.name),
|
||||
})
|
||||
|
||||
if (existingLabel) {
|
||||
return existingLabel
|
||||
}
|
||||
|
||||
// create a new label and assign a random color if not provided
|
||||
label.color = label.color || generateRandomColor()
|
||||
|
||||
return getRepository(Label).save({
|
||||
...label,
|
||||
user: { id: userId },
|
||||
})
|
||||
}
|
||||
|
|
|
|||
27
packages/api/src/services/rules.ts
Normal file
27
packages/api/src/services/rules.ts
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
import { Rule, RuleAction } from '../entity/rule'
|
||||
import { getRepository } from '../entity/utils'
|
||||
import { ILike } from 'typeorm'
|
||||
|
||||
export const createRule = async (
|
||||
userId: string,
|
||||
rule: {
|
||||
name: string
|
||||
description?: string
|
||||
actions: RuleAction[]
|
||||
filter: string
|
||||
}
|
||||
): Promise<Rule> => {
|
||||
const existingRule = await getRepository(Rule).findOneBy({
|
||||
user: { id: userId },
|
||||
name: ILike(rule.name),
|
||||
})
|
||||
|
||||
if (existingRule) {
|
||||
return existingRule
|
||||
}
|
||||
|
||||
return getRepository(Rule).save({
|
||||
...rule,
|
||||
user: { id: userId },
|
||||
})
|
||||
}
|
||||
|
|
@ -273,3 +273,13 @@ export const wordsCount = (text: string, isHtml = true): number => {
|
|||
export const isBase64Image = (str: string): boolean => {
|
||||
return str.startsWith('data:image/')
|
||||
}
|
||||
|
||||
export const generateRandomColor = (): string => {
|
||||
return (
|
||||
'#' +
|
||||
Math.floor(Math.random() * 16777215)
|
||||
.toString(16)
|
||||
.padStart(6, '0')
|
||||
.toUpperCase()
|
||||
)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue