File size: 4,064 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/**
 * This file is shared between server and client.
 * 鈿狅笍 DON'T PUT ANY NODE.JS OR BROWSER-SPECIFIC CODE IN HERE 鈿狅笍
 *
 * Note: This uses Flow comment syntax so this whole file is actually valid JS without any transpilation
 * The reason I did that is because create-react-app doesn't transpile files outside the source folder,
 * so it chokes on the Flow syntax.
 * More info: https://flow.org/en/docs/types/comments/
 */
var truncate = require('./truncate');
var striptags = require('striptags');
var toPlainText = require('./clients/draft-js/utils/plaintext').toPlainText;

var DEFAULT_META = {
  title: 'Spectrum',
  description: 'Where communities live.',
};

var HIDE_FROM_CRAWLERS = '<meta name="robots" content="noindex, nofollow">';

/*::
type MaybeMeta = {
  title?: string,
  description?: string,
  extra?: string,
};

type Meta = {
  title: string,
  description: string,
  extra: string,
};

type OtherInput = {
  type?: string,
  data?: void,
};
type ThreadInput = {
  type: 'thread',
  data?: { title: string, body?: ?string, communityName?: string, privateChannel?: ?boolean type?: ?string },
};
type UserInput = {
  type: 'user',
  data?: { name: string, username: string, description?: string },
};
type ChannelInput = {
  type: 'channel',
  data?: { name: string, description?: string, communityName?: string, private?: ?boolean },
};
type CommunityInput = {
  type: 'community',
  data?: { name: string, description?: string },
};
type DirectMessageInput = {
  type: 'directMessage',
  data?: { title: string, description?: string },
};
type Input =
  | ThreadInput
  | UserInput
  | ChannelInput
  | CommunityInput
  | OtherInput;
*/

function setDefault(input /*: MaybeMeta */) /*: Meta */ {
  var title = input.title || DEFAULT_META.title;
  var description = input.description || DEFAULT_META.description;
  // If theres a custom title but no custom description
  // prefix "On spectrum" to the description
  // Otherwise you end up with "SpecFM | Where communities live"
  if (input.title && !input.description) {
    description = 'on Spectrum, ' + DEFAULT_META.description.toLowerCase();
  }
  return {
    title: title,
    description: cleanDescription(description),
    extra: input.extra || '',
  };
}

function cleanDescription(input /*: string */) /*: string */ {
  return truncate(striptags(input), 160);
}

function generateMetaInfo(input /*: Input */) /*: Meta */ {
  var exists = input || {};
  var type = exists.type;
  var data = exists.data;
  switch (type) {
    case 'explore': {
      return {
        title: 'Explore',
        description: 'Explore some of the communities on Spectrum',
      };
    }
    case 'thread': {
      if (data.privateChannel)
        return setDefault({
          extra: HIDE_FROM_CRAWLERS,
        });

      var body =
        data &&
        data.body &&
        (data.type === 'DRAFTJS'
          ? toPlainText(JSON.parse(data.body))
          : data.body);
      return setDefault({
        title: data && data.title + ' 路 ' + data.communityName,
        description: body,
      });
    }
    case 'user': {
      return setDefault({
        title: data && data.name + ' 路 @' + data.username,
        description: data && data.description,
      });
    }
    case 'channel': {
      if (data.private)
        return setDefault({
          extra: HIDE_FROM_CRAWLERS,
        });
      return setDefault({
        title: data && data.communityName + ' 路 ' + data.name,
        description: data && data.description,
      });
    }
    case 'community': {
      return setDefault({
        title: data && data.name,
        description: data && data.description,
      });
    }
    case 'directMessage': {
      return setDefault({
        title: data && data.title,
        description: data && data.description,
      });
    }
    case 'notifications': {
      return setDefault({
        title: 'Notifications',
        description: 'Notifications on Spectrum',
      });
    }
    default: {
      return DEFAULT_META;
    }
  }
}

module.exports = generateMetaInfo;