Skip to content

Commit

Permalink
Merge pull request #16 from medienhaus/legacydata-interpreter
Browse files Browse the repository at this point in the history
Legacydata interpreter
  • Loading branch information
robertschnuell authored Aug 23, 2023
2 parents acbda51 + dab6b1d commit 8d2e64c
Show file tree
Hide file tree
Showing 2 changed files with 550 additions and 8 deletions.
26 changes: 18 additions & 8 deletions src/item.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import fs from 'fs'
import { join } from 'path'
import moment from 'moment'
import { isNull, template } from 'lodash'
import { LegacyInterpreter } from './legacy-interpreter.service'

@Injectable()
@Dependencies(ConfigService, HttpService)
Expand Down Expand Up @@ -42,9 +43,11 @@ export class ItemService {

this.getAllSpaces = this.getAllSpaces.bind(this)
this.getBatch = this.getBatch.bind(this)

this.legacyInterpreter = new LegacyInterpreter(this.configService, this.httpService, this.matrixClient)
}

@Interval(30 * 60 * 1000) // Call this every 30 minutes
@Interval(120 * 60 * 1000) // Call this every 120 minutes
async fetch () {
if (!this.configService.get('fetch.autoFetch') && this.initiallyFetched) {
return
Expand Down Expand Up @@ -73,6 +76,7 @@ export class ItemService {
this.configService.get('matrix.root_context_space_id'),
{}
)
this.legacyInterpreter.clear()
const structure = {}
this.graphQlCache = {}
structure[generatedStrucute.room_id] = generatedStrucute
Expand Down Expand Up @@ -279,6 +283,10 @@ export class ItemService {
const tagEvent = _.find(stateEvents, { type: 'dev.medienhaus.tags' })
const joinRulesEvent = _.find(stateEvents, { type: 'm.room.join_rules' })

if (!['item', 'context', 'content'].some((f) => f === metaEvent?.content?.type)) { // check if legacy from old CMS
return this.legacyInterpreter.convertLegacySpace(stateEvents, spaceId, rawSpaces)
}

const parent = {}
let parents = []
if (idsToApplyFullStaeUpdate) {
Expand Down Expand Up @@ -385,6 +393,7 @@ export class ItemService {
type: 'de.udk-berlin.rundgang'
})?.content?.hideAuthors
if (udkCustomHideAuthors) authors = []

let descriptions = languageSpaces?.map((lang) => {
return { id: lang?.room_id, name: lang?.name, topic: lang?.topic }
})
Expand Down Expand Up @@ -816,6 +825,10 @@ export class ItemService {
return null
}

if (this.legacyInterpreter.isLegacy(id)) {
return this.legacyInterpreter.get(id, language)
}

if (this.items[id].content) return this.items[id]
const { content, formattedContent } = await this.getContent(id, language)
return { ...this.items[id], content, formatted_content: formattedContent }
Expand Down Expand Up @@ -980,6 +993,9 @@ export class ItemService {
return lastMessage.content.formatted_body
// For all other types we render the HTML using the corresponding Handlebars template in /views/contentBlocks
default:
if (!this.configService.get('attributable.spaceTypes.content').some((f) => f === template)) {
return ''
}
return Handlebars.compile(
fs.readFileSync(
join(
Expand Down Expand Up @@ -1455,13 +1471,6 @@ export class ItemService {
languages[language?.toUpperCase()] = await this.getContent(id, language)
}

const matrixClient = createMatrixClient({
baseUrl: this.configService.get('matrix.homeserver_base_url'),
accessToken: this.configService.get('matrix.access_token'),
userId: this.configService.get('matrix.user_id'),
useAuthorizationHeader: true
})

return {
abstract: {
name: abstract?.name,
Expand Down Expand Up @@ -1642,6 +1651,7 @@ export class ItemService {
}

_transformAbstractToGraphQl (space) {
if (!space) return
const ret = JSON.parse(JSON.stringify(space))
ret.parents = space?.parents?.map((parent) => {
return { id: parent }
Expand Down
Loading

0 comments on commit 8d2e64c

Please sign in to comment.