]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/utils.ts
Fix margin-content and miniature thumbnail width on mobile, fix media queries for...
[github/Chocobozzz/PeerTube.git] / server / models / utils.ts
index 674ddcbe47260277ad03c1ba1edbafab29da83c5..bdf2291f0ceb5b34ed3c089de5e71044ec7304b8 100644 (file)
@@ -1,7 +1,24 @@
 import { Model, Sequelize } from 'sequelize-typescript'
 import validator from 'validator'
 import { Col } from 'sequelize/types/lib/utils'
-import { literal, OrderItem } from 'sequelize'
+import { literal, OrderItem, Op } from 'sequelize'
+
+type Primitive = string | Function | number | boolean | Symbol | undefined | null
+type DeepOmitHelper<T, K extends keyof T> = {
+  [P in K]: // extra level of indirection needed to trigger homomorhic behavior
+  T[P] extends infer TP // distribute over unions
+    ? TP extends Primitive
+      ? TP // leave primitives and functions alone
+      : TP extends any[]
+        ? DeepOmitArray<TP, K> // Array special handling
+        : DeepOmit<TP, K>
+    : never
+}
+type DeepOmit<T, K> = T extends Primitive ? T : DeepOmitHelper<T, Exclude<keyof T, K>>
+
+type DeepOmitArray<T extends any[], K> = {
+  [P in keyof T]: DeepOmit<T[P], K>
+}
 
 type SortType = { sortModel: string, sortValue: string }
 
@@ -190,9 +207,22 @@ function buildDirectionAndField (value: string) {
   return { direction, field }
 }
 
+function searchAttribute (sourceField, targetField) {
+  if (sourceField) {
+    return {
+      [targetField]: {
+        [Op.iLike]: `%${sourceField}%`
+      }
+    }
+  } else {
+    return {}
+  }
+}
+
 // ---------------------------------------------------------------------------
 
 export {
+  DeepOmit,
   buildBlockedAccountSQL,
   buildLocalActorIdsIn,
   SortType,
@@ -210,7 +240,8 @@ export {
   parseAggregateResult,
   getFollowsSort,
   buildDirectionAndField,
-  createSafeIn
+  createSafeIn,
+  searchAttribute
 }
 
 // ---------------------------------------------------------------------------