-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstream.js
124 lines (120 loc) · 3.08 KB
/
stream.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
'use strict';
const { Op } = require("sequelize");
const {
Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
class Stream extends Model {
/**
* Helper method for defining associations.
* This method is not a part of Sequelize lifecycle.
* The `models/index` file will call this method automatically.
*/
static associate(models) {
// define association here
}
// Assign self.city and self.location "automatically."
// Does nothing if either city or region are already set
// Assumes that the last entered location for a streamer is correct.
// Does not automatically save to the database
// Does nothing if no past streams exist that match self.source or self.link
async inferLocation() {
if (this.city || this.region) {
return
}
// Assumes that the pastStream.source is "unique-enough";
// However, we know that multiple streamers sometimes
// use the same name to stream, e.g., Bear Gang, Concrete Reporting, Unicorn Riot, Boop Troop, etc.
const matchingFilter = [
{ link: this.link },
]
if (this.source) {
matchingFilter.push({ source: this.source })
}
const pastStream = await Stream.findOne({
where: {
[Op.and]: [
{
[Op.or]: matchingFilter,
},
{
[Op.or]: [
{
city: {
[Op.not]: null,
[Op.not]: ''
}
},
{
region: {
[Op.not]: null,
[Op.not]: ''
}
},
]
}
]
},
order: [['createdAt', 'DESC']]
})
this.city = (pastStream && pastStream.city) || ''
this.region = (pastStream && pastStream.region) || ''
}
}
Stream.init({
source: {
type: DataTypes.TEXT,
},
platform: {
type: DataTypes.TEXT,
},
link: {
type: DataTypes.TEXT,
allowNull: false,
validate: {
isUrl: true,
},
},
status: {
type: DataTypes.TEXT,
defaultValue: 'Unknown',
},
title: {
type: DataTypes.TEXT,
},
isExpired: {
type: DataTypes.BOOLEAN,
defaultValue: false,
},
checkedAt: DataTypes.DATE,
liveAt: DataTypes.DATE,
embedLink: {
type: DataTypes.TEXT,
validate: {
isUrl: true,
}
},
postedBy: DataTypes.TEXT,
isPinned: DataTypes.BOOLEAN,
city: DataTypes.TEXT,
region: DataTypes.TEXT,
state: {
type: DataTypes.VIRTUAL,
get() {
return this.region
},
set(_value) {
throw new Error('Stream.state is deprecated and read-only. Use Stream.region instead.')
}
}
}, {
hooks: {
beforeSave: async (stream, options) => {
await stream.inferLocation()
},
},
sequelize,
modelName: 'Stream',
});
return Stream;
};