content.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. import APIRepository from '../classes/APIRepository'
  2. import { appApi } from 'src/boot/axios'
  3. import { Content } from 'src/models/Content'
  4. import { ProductList } from 'src/models/Product'
  5. import { Comment } from 'src/models/Comment'
  6. import { APIGateway } from 'src/api/APIGateway'
  7. const APIAdresses = {
  8. base: '/c',
  9. search: '/search',
  10. admin: '/admin/contents',
  11. timestampSet: '/timepoint',
  12. show: (id) => '/c/' + id,
  13. delete: '/admin/contents/destroy',
  14. bulkUpdate: '/admin/contents/bulk-update',
  15. presigned: '/admin/upload/presigned-request',
  16. bulkEditText: '/admin/contents/bulk-edit-text',
  17. bulkEditTags: '/admin/contents/bulk-edit-tags',
  18. favored: (id) => '/c/' + id + '/favored',
  19. update: (id) => `/admin/contents/${id}/`,
  20. getTimestamp: (id) => `/timepoint/${id}`,
  21. showAdmin: (id) => '/admin/contents/' + id,
  22. deleteTimestamp: (id) => `/timepoint/${id}`,
  23. updateTimestamp: (id) => `/timepoint/${id}`,
  24. unfavored: (id) => '/c/' + id + '/unfavored',
  25. relatedProducts: (id) => '/c/' + id + '/products',
  26. timestampBookmarkStatus: (id, status) => '/c/timepoint/' + id + '/' + status,
  27. saveComment: '/comment',
  28. updateComment: (id) => '/comment/' + id,
  29. watchedVideo: '/watched',
  30. unWatchedVideo: '/unwatched',
  31. adminContent: {
  32. create: {
  33. base: '/admin/user'
  34. },
  35. edit: {
  36. base: '/admin/user/'
  37. },
  38. index: {
  39. base: '/admin/user'
  40. },
  41. show: {
  42. base: '/admin/user/'
  43. }
  44. },
  45. scheduleManagement: {
  46. create: {
  47. base: '/admin/user'
  48. },
  49. edit: {
  50. base: '/admin/user/'
  51. },
  52. index: {
  53. base: '/admin/user'
  54. },
  55. show: {
  56. base: '/admin/user/'
  57. }
  58. }
  59. }
  60. export default class ContentAPI extends APIRepository {
  61. constructor() {
  62. super('content', appApi, '/c/', new Content(), APIAdresses)
  63. this.CacheList = {
  64. admin: this.name + this.APIAdresses.admin,
  65. show: id => this.name + this.APIAdresses.show(id),
  66. favored: id => this.name + this.APIAdresses.favored(id),
  67. unfavored: id => this.name + this.APIAdresses.unfavored(id),
  68. showAdmin: id => this.name + this.APIAdresses.showAdmin(id),
  69. update: id => this.name + this.APIAdresses.update(id),
  70. relatedProducts: id => this.name + this.APIAdresses.relatedProducts(id),
  71. search: this.name + this.APIAdresses.search,
  72. delete: this.name + this.APIAdresses.delete,
  73. timestampSet: this.name + this.APIAdresses.timestampSet,
  74. bulkEditText: this.name + this.APIAdresses.bulkEditText,
  75. bulkUpdate: this.name + this.APIAdresses.bulkUpdate,
  76. bulkEditTags: this.name + this.APIAdresses.bulkEditTags,
  77. getTimestamp: id => this.name + this.APIAdresses.getTimestamp(id),
  78. updateTimestamp: id => this.name + this.APIAdresses.updateTimestamp(id),
  79. deleteTimestamp: id => this.name + this.APIAdresses.deleteTimestamp(id),
  80. presigned: this.name + this.APIAdresses.presigned
  81. }
  82. }
  83. show(data, cache = { TTL: 1000 }) {
  84. return this.sendRequest({
  85. apiMethod: 'get',
  86. api: this.api,
  87. request: this.APIAdresses.show(data),
  88. cacheKey: this.CacheList.show(data),
  89. ...(cache && { cache }),
  90. resolveCallback: (response) => {
  91. return new Content(response.data.data)
  92. },
  93. rejectCallback: (error) => {
  94. return error
  95. }
  96. })
  97. }
  98. favored(data = {}, cache = { TTL: 1000 }) {
  99. return this.sendRequest({
  100. apiMethod: 'post',
  101. api: this.api,
  102. request: this.APIAdresses.favored(data),
  103. cacheKey: this.CacheList.favored(data),
  104. ...(cache !== undefined && { cache }),
  105. resolveCallback: (response) => {
  106. return response.data
  107. },
  108. rejectCallback: (error) => {
  109. return error
  110. }
  111. })
  112. }
  113. unfavored(data = {}, cache = { TTL: 1000 }) {
  114. return this.sendRequest({
  115. apiMethod: 'post',
  116. api: this.api,
  117. request: this.APIAdresses.unfavored(data),
  118. cacheKey: this.CacheList.unfavored(data),
  119. ...(cache !== undefined && { cache }),
  120. resolveCallback: (response) => {
  121. return response.data
  122. },
  123. rejectCallback: (error) => {
  124. return error
  125. }
  126. })
  127. }
  128. showAdmin(contentId, cache) {
  129. return this.sendRequest({
  130. apiMethod: 'get',
  131. api: this.api,
  132. request: this.APIAdresses.showAdmin(contentId),
  133. cacheKey: this.CacheList.showAdmin(contentId),
  134. ...(cache && { cache }),
  135. resolveCallback: (response) => {
  136. const content = new Content(response.data.data)
  137. // fillFakeData(content)
  138. return content
  139. },
  140. rejectCallback: (error) => {
  141. return error
  142. }
  143. })
  144. }
  145. update(data = {}) {
  146. return this.sendRequest({
  147. apiMethod: 'put',
  148. api: this.api,
  149. request: this.APIAdresses.update(data.id),
  150. cacheKey: this.CacheList.update(data.id),
  151. resolveCallback: (response) => {
  152. return new Content(response.data.data)
  153. },
  154. rejectCallback: (error) => {
  155. return error
  156. },
  157. data: {
  158. display: 1,
  159. ...data
  160. }
  161. // data: this.getNormalizedSendData({
  162. // contentset_id: null, // contentSet Id
  163. // isFree: null, // contentSet Id
  164. // name: null, // Title for content,
  165. // description: null, // Description for content
  166. // thumbnail: null, // thumbnail for contentfd
  167. // validSinceDate: null, // time for publish content
  168. // forrest_tree: null, // tree for content
  169. // order: null, // order of content
  170. // enable: null, // content status
  171. // display: 1 // content display status
  172. // }, data)
  173. })
  174. }
  175. relatedProducts(contentId, cache) {
  176. return this.sendRequest({
  177. apiMethod: 'get',
  178. api: this.api,
  179. request: this.APIAdresses.relatedProducts(contentId),
  180. cacheKey: this.CacheList.relatedProducts(contentId),
  181. ...(cache && { cache }),
  182. resolveCallback: (response) => {
  183. return new ProductList(response.data.data)
  184. },
  185. rejectCallback: (error) => {
  186. return error
  187. }
  188. })
  189. }
  190. bulkEditText(data) {
  191. return this.sendRequest({
  192. apiMethod: 'put',
  193. api: this.api,
  194. request: this.APIAdresses.bulkEditText,
  195. cacheKey: this.CacheList.bulkEditText,
  196. ...(data?.cache && { cache: data.cache }),
  197. resolveCallback: (response) => {
  198. return response.data
  199. },
  200. rejectCallback: (error) => {
  201. return error
  202. },
  203. data
  204. })
  205. }
  206. bulkUpdate(data) {
  207. return this.sendRequest({
  208. apiMethod: 'put',
  209. api: this.api,
  210. request: this.APIAdresses.bulkUpdate,
  211. cacheKey: this.CacheList.bulkUpdate,
  212. ...(data?.cache && { cache: data.cache }),
  213. resolveCallback: (response) => {
  214. return response.data
  215. },
  216. rejectCallback: (error) => {
  217. return error
  218. },
  219. data
  220. })
  221. }
  222. bulkEditTags(data) {
  223. return this.sendRequest({
  224. apiMethod: 'put',
  225. api: this.api,
  226. request: this.APIAdresses.bulkEditTags,
  227. cacheKey: this.CacheList.bulkEditTags,
  228. ...(data?.cache && { cache: data.cache }),
  229. resolveCallback: (response) => {
  230. return response.data
  231. },
  232. rejectCallback: (error) => {
  233. return error
  234. },
  235. data
  236. })
  237. }
  238. deleteContents(data) {
  239. return this.sendRequest({
  240. apiMethod: 'post',
  241. api: this.api,
  242. request: this.APIAdresses.delete,
  243. cacheKey: this.CacheList.delete,
  244. ...(data?.cache && { cache: data.cache }),
  245. resolveCallback: (response) => {
  246. return response.data
  247. },
  248. rejectCallback: (error) => {
  249. return error
  250. },
  251. data
  252. })
  253. }
  254. search(data = {}) {
  255. return this.sendRequest({
  256. apiMethod: 'get',
  257. api: this.api,
  258. request: this.APIAdresses.search,
  259. cacheKey: this.CacheList.search,
  260. ...(data.cache && { cache: data.cache }),
  261. resolveCallback: (response) => {
  262. return {
  263. sets: response.data.data.sets,
  264. products: response.data.data.products,
  265. videos: response.data.data.videos
  266. }
  267. },
  268. rejectCallback: (error) => {
  269. return error
  270. }
  271. })
  272. }
  273. SetTimestamp(data = {}, cache) {
  274. return this.sendRequest({
  275. apiMethod: 'post',
  276. api: this.api,
  277. request: this.APIAdresses.timestampSet,
  278. cacheKey: this.CacheList.timestampSet,
  279. ...(cache && { cache }),
  280. resolveCallback: (response) => {
  281. return {
  282. timestamp: response.data
  283. }
  284. },
  285. rejectCallback: (error) => {
  286. return error
  287. },
  288. data: this.getNormalizedSendData({
  289. content_id: null, // content Id
  290. title: null, // Title for Timestamp
  291. time: null // time of Video for timestamp in seconds
  292. }, data.data)
  293. })
  294. }
  295. UpdateTimestamp(data = {}, cache) {
  296. return this.sendRequest({
  297. apiMethod: 'put',
  298. api: this.api,
  299. request: this.APIAdresses.updateTimestamp(data.id),
  300. cacheKey: this.CacheList.updateTimestamp(data.id),
  301. ...(cache && { cache }),
  302. resolveCallback: (response) => {
  303. return {
  304. timestamp: response.data
  305. }
  306. },
  307. rejectCallback: (error) => {
  308. return error
  309. },
  310. data: this.getNormalizedSendData({
  311. content_id: null, // content Id
  312. title: null, // Title for Timestamp
  313. time: null // time of Video for timestamp in seconds
  314. }, data)
  315. })
  316. }
  317. GetTimestamp(data = {}, cache) {
  318. return this.sendRequest({
  319. apiMethod: 'get',
  320. api: this.api,
  321. request: this.APIAdresses.getTimestamp(data.id),
  322. cacheKey: this.CacheList.getTimestamp(data.id),
  323. ...(cache && { cache }),
  324. resolveCallback: (response) => {
  325. return {
  326. timestamp: response.data
  327. }
  328. },
  329. rejectCallback: (error) => {
  330. return error
  331. }
  332. })
  333. }
  334. DeleteTimestamp(data = {}, cache) {
  335. return this.sendRequest({
  336. apiMethod: 'delete',
  337. api: this.api,
  338. request: this.APIAdresses.deleteTimestamp(data.id),
  339. cacheKey: this.CacheList.deleteTimestamp(data.id),
  340. ...(cache && { cache }),
  341. resolveCallback: (response) => {
  342. return {
  343. timestamp: response.data
  344. }
  345. },
  346. rejectCallback: (error) => {
  347. return error
  348. }
  349. })
  350. }
  351. getPresigned(data = {}) {
  352. return this.sendRequest({
  353. apiMethod: 'post',
  354. api: this.api,
  355. request: this.APIAdresses.presigned,
  356. cacheKey: this.CacheList.presigned,
  357. ...(data.cache && { cache: data.cache }),
  358. resolveCallback: (response) => {
  359. return response.data.data
  360. },
  361. rejectCallback: (error) => {
  362. return error
  363. },
  364. data: this.getNormalizedSendData({
  365. bucket: null, // file name(test)
  366. key: null // file with type(type.mp4)
  367. }, data.data)
  368. })
  369. }
  370. setBookmarkTimepointFavoredStatus(data = {}) {
  371. const mergedData = this.getNormalizedSendData({
  372. id: '',
  373. status: 'favored'
  374. }, data)
  375. return this.sendRequest({
  376. apiMethod: 'post',
  377. api: this.api,
  378. request: this.APIAdresses.timestampBookmarkStatus(mergedData.id, mergedData.status),
  379. resolveCallback: (response) => {
  380. const defaultMessageObject = {
  381. message: '' // String
  382. }
  383. return this.getNormalizedSendData(defaultMessageObject, response.data).message
  384. },
  385. rejectCallback: (error) => {
  386. return error
  387. }
  388. })
  389. }
  390. getConsultingContentList(isPro) {
  391. const setId = isPro ? 1597 : 1213
  392. return APIGateway.set.getContents(setId)
  393. }
  394. saveComment(data = {}) {
  395. const mergedData = this.getNormalizedSendData({
  396. commentable_id: '',
  397. commentable_type: 'content',
  398. comment: ''
  399. }, data)
  400. return this.sendRequest({
  401. apiMethod: 'post',
  402. api: this.api,
  403. request: this.APIAdresses.saveComment,
  404. resolveCallback: (response) => {
  405. return new Comment(response.data.data)
  406. },
  407. rejectCallback: (error) => {
  408. return error
  409. },
  410. data: mergedData
  411. })
  412. }
  413. updateComment(data = {}) {
  414. const mergedData = this.getNormalizedSendData({
  415. id: '',
  416. data: {}
  417. }, data)
  418. return this.sendRequest({
  419. apiMethod: 'post',
  420. api: this.api,
  421. request: this.APIAdresses.updateComment(mergedData.id),
  422. resolveCallback: (response) => {
  423. return new Comment(response.data.data)
  424. },
  425. rejectCallback: (error) => {
  426. return error
  427. },
  428. data: mergedData.data
  429. })
  430. }
  431. setVideoWatched(data = {}) {
  432. const mergedData = this.getNormalizedSendData({
  433. watchable_id: '',
  434. watchable_type: 'content'
  435. }, data)
  436. return this.sendRequest({
  437. apiMethod: 'post',
  438. api: this.api,
  439. request: this.APIAdresses.watchedVideo,
  440. resolveCallback: (response) => {
  441. const defaultResponseObject = {
  442. id: '',
  443. watchable_id: '',
  444. watchable_type: 'content',
  445. watchable: new Content(),
  446. seconds_watched: null
  447. }
  448. return this.getNormalizedSendData(defaultResponseObject, response.data.data)
  449. },
  450. rejectCallback: (error) => {
  451. return error
  452. },
  453. data: mergedData
  454. })
  455. }
  456. setVideoUnWatched(data = {}) {
  457. const mergedData = this.getNormalizedSendData({
  458. watchable_id: '',
  459. watchable_type: 'content'
  460. }, data)
  461. return this.sendRequest({
  462. apiMethod: 'post',
  463. api: this.api,
  464. request: this.APIAdresses.unWatchedVideo,
  465. resolveCallback: (response) => {
  466. const defaultResponseObject = {
  467. message: '' // String
  468. }
  469. return this.getNormalizedSendData(defaultResponseObject, response.data.data)
  470. },
  471. rejectCallback: (error) => {
  472. return error
  473. },
  474. data: mergedData
  475. })
  476. }
  477. }
  478. // const fillFakeData = (content) => {
  479. // content.forrest_tree_tags = forrestTreeTags
  480. // content.hls = 'https://alaatv.com/hls/input.m3u8'
  481. // }
  482. // const forrestTreeTags = [
  483. // {
  484. // id: '63ff427566344faf860f0f9f',
  485. // title: 'دبیر 1',
  486. // parent: {
  487. // id: '63f37272c590054efc012d12',
  488. // title: 'دبیر'
  489. // },
  490. // ancestors: [
  491. // {
  492. // id: '63f37272c590054efc012d12',
  493. // title: 'دبیر'
  494. // }
  495. // ],
  496. // order: '0',
  497. // type: null,
  498. // number_of_children: 0,
  499. // updated_at: '2023-03-01 15:49:18',
  500. // created_at: '2023-03-01 15:47:57'
  501. // },
  502. // {
  503. // id: '63ff427c66344faf860f0fa0',
  504. // title: 'دبیر 2',
  505. // parent: {
  506. // id: '63f37272c590054efc012d12',
  507. // title: 'دبیر'
  508. // },
  509. // ancestors: [
  510. // {
  511. // id: '63f37272c590054efc012d12',
  512. // title: 'دبیر'
  513. // }
  514. // ],
  515. // order: '2',
  516. // type: null,
  517. // number_of_children: 1,
  518. // updated_at: '2023-03-01 15:49:18',
  519. // created_at: '2023-03-01 15:48:04'
  520. // }
  521. // ]