Class: Hipmost::Cmds::Room

Inherits:
Object
  • Object
show all
Defined in:
lib/hipmost/cmds/room.rb

Instance Method Summary collapse

Constructor Details

#initialize(path:, verbose: false) ⇒ Room



4
5
6
7
# File 'lib/hipmost/cmds/room.rb', line 4

def initialize(path:, verbose: false)
  $path    = Pathname.new(path).expand_path
  @verbose = verbose
end

Instance Method Details

#import(args) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/hipmost/cmds/room.rb', line 31

def import(args)
  rooms      = args
  rooms_size = rooms.size

  if rooms_size.zero? || (rooms_size % 2).nonzero?
    puts "Need a pair of rooms to migrate"
    exit 1
  end

  @data = rooms

  parse_rooms_arg
  save
  true
end

#list(_args) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/hipmost/cmds/room.rb', line 22

def list(_args)
  puts "Listing rooms..." if @verbose
  Hipchat.rooms.each do |_,room|
    puts room.display_name
  end

  true
end

#parse_rooms_argObject



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
# File 'lib/hipmost/cmds/room.rb', line 93

def parse_rooms_arg
  @rooms    = []
  @channels = []
  @teams    = []

  @data.each_slice(2).each do |hipchat_room, mattermost_room|
    if @verbose
      puts "Parsing rooms for Hipchat and Mattermost..."
      puts "Hipchat room is:               #{hipchat_room}"
      puts "Mattermost team & channel are: #{mattermost_room}"
    end

    team, channel_name = mattermost_room.split(":")
    team               = Mattermost::Team.new(team)
    room               = Hipchat.rooms.find_by_name(hipchat_room)
    channel            = Mattermost::Channel.from_hipchat(room, name: channel_name, team: team)

    room.team    = team
    room.channel = channel

    @rooms    << room
    @teams    << team
    @channels << channel

    puts "Successfully parsed rooms\n\n" if @verbose
  end

  @teams.uniq!
  @rooms.uniq!
  @channels.uniq!
end

#run(args) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/hipmost/cmds/room.rb', line 9

def run(args)
  subcommand = args.shift
  @outpath = $path.join("..", "#{args[0]}.jsonl").expand_path

  if ["list", "import"].include?(subcommand)
  else
    puts "Command invalid for `public`; must be `import` or `list`"
    exit 1
  end

  send(subcommand, args)
end

#saveObject



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
# File 'lib/hipmost/cmds/room.rb', line 47

def save
  puts "Opening #{@outpath} for writing..." if @verbose

  File.open(@outpath, "w") do |jsonl|
    puts "Writing version header..." if @verbose
    jsonl.puts %[{ "type": "version", "version": 1 }]

    puts "Writing team info..." if @verbose
    @teams.each {|t| jsonl.puts(t.to_jsonl) }
    puts "Writing channel info..." if @verbose
    @channels.each {|r| jsonl.puts(r.to_jsonl) }

    puts "Writing room members..." if @verbose
    @rooms.each do |room|
      room.users.each do |user|
        jsonl.puts(user.to_jsonl)
      end
    end

    puts "Writing room posts (newest to oldest)..." if @verbose
    i = 1 if @verbose
    j = 1 if @verbose

    @rooms.each do |room|
      if @verbose
        puts "On room #{i}"
        i += 1
      end

      Hipchat::PostRepository.new(room).tap(&:load).each do |post|
        if @verbose
          print "On post #{j}\r"
          j += 1
        end

        jsonl.puts(post.to_jsonl)
      end

      if @verbose
        print "\n"
        puts "Successfully wrote public room data\n\n"
      end
    end
  end
end